Skip to content

Instantly share code, notes, and snippets.

@andion
Last active May 17, 2020 07:55
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 andion/54c1d501b988b896eace2fcabe00441b to your computer and use it in GitHub Desktop.
Save andion/54c1d501b988b896eace2fcabe00441b to your computer and use it in GitHub Desktop.
const {
sx,
sy,
sWidth,
sHeight,
dx,
dy,
dWidth,
dHeight,
canvasWidth,
canvasHeight,
} = getAspectRatioParams(originalWidth, originalHeight, transformedRatio, true);
canvas.width = canvasWidth;
canvas.height = canvasHeight;
// Create a rectangle with the desired background color the size of the canvas.
ctx.fillStyle = "#FE27FF";
ctx.fillRect(0, 0, canvasWidth, canvasHeight);
// Now we can "paste" our cropped image on top
ctx.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment