Last active
November 3, 2024 14:13
-
-
Save maoosi/5f28f47d7a22e649c31ae24c7e96c688 to your computer and use it in GitHub Desktop.
Image drawing function for Canvas context. Include rotation and resizing.
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
drawImage (context, image, width, height, posX = 0, posY = 0, rotate = 0) { | |
let rotation = rotate * (Math.PI / 180) | |
let translateX = posX + (width / 2) | |
let translateY = posY + (height / 2) | |
context.translate(translateX, translateY) | |
context.rotate(rotation) | |
context.drawImage(image, -(width / 2), -(height / 2), width, height) | |
context.rotate(-rotation) | |
context.translate(-translateX, -translateY) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment