Skip to content

Instantly share code, notes, and snippets.

@Luftare
Created October 29, 2017 07:58
Show Gist options
  • Save Luftare/fd238b7aac27c4e82c13b4a9526c878f to your computer and use it in GitHub Desktop.
Save Luftare/fd238b7aac27c4e82c13b4a9526c878f to your computer and use it in GitHub Desktop.
Draw image rotated and scaled on html canvas element
function drawImage(ctx, img, x, y, angle = 0, scale = 1){
ctx.save();
ctx.translate(x + img.width * scale / 2, y + img.height * scale / 2);
ctx.rotate(angle);
ctx.translate(- x - img.width * scale / 2, - y - img.height * scale / 2);
ctx.drawImage(img, x, y, img.width * scale, img.height * scale);
ctx.restore();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment