Created
October 29, 2017 07:58
-
-
Save Luftare/fd238b7aac27c4e82c13b4a9526c878f to your computer and use it in GitHub Desktop.
Draw image rotated and scaled on html canvas element
This file contains hidden or 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
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
Oh my god thank you!!!
Only issue was that the scaling was happening from the top-left corner
This scales it from the center: