Skip to content

Instantly share code, notes, and snippets.

@maoosi
Last active September 28, 2018 05:07
Show Gist options
  • Save maoosi/5f28f47d7a22e649c31ae24c7e96c688 to your computer and use it in GitHub Desktop.
Save maoosi/5f28f47d7a22e649c31ae24c7e96c688 to your computer and use it in GitHub Desktop.
Image drawing function for Canvas context. Include rotation and resizing.
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