Skip to content

Instantly share code, notes, and snippets.

@andion
Last active May 15, 2020 13:45
Show Gist options
  • Save andion/9deaf1a44ca09d9c3b9954264c1c85cd to your computer and use it in GitHub Desktop.
Save andion/9deaf1a44ca09d9c3b9954264c1c85cd to your computer and use it in GitHub Desktop.
const resizeToWidth = 800;
const resizeToHeight = 600;
const canvas = document.getElementById("canvas");
const image = document.getElementById("source");
const ctx = canvas.getContext("2d");
// Get *real* image size with naturalXXX
const {naturalWidth: imageWidth, naturalHeight: imageHeight} = image;
// Set the canvas to the desired size
canvas.width = resizeToWidth;
canvas.height = resizeToHeight;
// Paste our whole image on the the canvas
ctx.drawImage(image, 0, 0, resizeToWidth, resizeToHeight);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment