Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 13, 2023 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/f0b0c3d0cf98284678fee8df416632cf to your computer and use it in GitHub Desktop.
Save code-boxx/f0b0c3d0cf98284678fee8df416632cf to your computer and use it in GitHub Desktop.

JAVASCRIPT CROP IMAGES

https://code-boxx.com/crop-images-javascript/

IMAGE

easter

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>JS Canvas Crop</title>
</head>
<body>
<!-- (A) HTML CANVAS -->
<canvas id="demo" width="300" height="300"></canvas>
<script>
// (B) IMAGE OBJECT
var img = new Image();
// (C) CROP ON LOAD
img.onload = () => {
// (C1) GET CANVAS + 2D CONTEXT
let canvas = document.getElementById("demo"),
ctx = canvas.getContext("2d");
// (C2) CROP BY COPYING PART OF SOURCE IMAGE TO CANVAS
ctx.drawImage(img,
// SOURCE X, Y, WIDTH, HEIGHT
50, 100, 300, 300,
// DESTINATION X, Y, WIDTH, HEIGHT
0, 0, 300, 300
);
};
// (D) GO!
img.src = "easter.png";
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>JS Canvas Crop</title>
</head>
<body>
<!-- (A) FILE PICKER -->
<input type="file" id="picker" accept="image/jpeg,image/png,image/webp">
<script>
// (B) AUTO START ON SELECT FILE
let picker = document.getElementById("picker");
picker.onchange = () => {
// (C) CREATE CANVAS
let canvas = document.createElement("canvas"),
ctx = canvas.getContext("2d");
canvas.width = 300;
canvas.height = 300;
// (D) CREATE ANCHOR + SELECTED IMAGE URL
let anchor = document.createElement("a"),
surl = URL.createObjectURL(picker.files[0]);
// (E) CREATE IMAGE + CROP
let img = new Image();
img.onload = () => {
// (E1) DO YOUR OWN CROP CALCULATIONS...
ctx.drawImage(img, 50, 100, 300, 300, 0, 0, 300, 300);
// (E2) "FORCE DOWNLOAD"
anchor.href = canvas.toDataURL("image/png");
anchor.download = "cropped.png";
anchor.click(); // MAY NOT WORK - BETTER TO LET USER CLICK
// (E3) CLEAN UP
anchor.remove();
canvas.remove();
img.remove();
URL.revokeObjectURL(surl);
};
// (F) GO!
img.src = surl;
};
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment