Created
November 29, 2019 03:54
-
-
Save vmoratog/cb5f8972aca7f6a7fd7502aa1774c553 to your computer and use it in GitHub Desktop.
Rotate images in Canvas
This file contains 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
const imageData = canvas.toDataURL() | |
const degrees = 90 | |
const image = new Image(); | |
image.onload = () => { | |
ctx.clearRect(0, 0, canvas.width, canvas.height); | |
ctx.save(); | |
ctx.translate(canvas.width / 2, canvas.height / 2); | |
ctx.rotate(degrees * Math.PI / 180); | |
ctx.drawImage(image, -image.width / 2, -image.width / 2); | |
ctx.restore(); | |
// NEW IMAGE HERE! | |
// canvas.toDataURL() | |
}; | |
image.src = imageData |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment