Last active
December 20, 2021 12:30
-
-
Save krabs-github/e5317ed255152f304e555b276df0c29a to your computer and use it in GitHub Desktop.
[JavaScript - Crop an Image With Canvas] JavaScript - Crop an Image With Canvas #JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const image = new Image(), | |
canvas = document.getElementById('canvas'), | |
ctx = canvas.getContext('2d'); | |
image.src = 'https://i.stack.imgur.com/I4jXc.png'; | |
image.addEventListener('load', () => { | |
ctx.drawImage(image, | |
70, 20, // Start at 70/20 pixels from the left and the top of the image (crop), | |
50, 50, // "Get" a `50 * 50` (w * h) area from the source image (crop), | |
0, 0, // Place the result at 0, 0 in the canvas, | |
100, 100); // With as width / height: 100 * 100 (scale) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment