Created
June 1, 2016 22:54
-
-
Save rafaelcanovas/876b6b0e2f645aa3a467f7ec08e7f453 to your computer and use it in GitHub Desktop.
Javascript utility to rotate image.
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
function rotateImage(path, degrees, callback) { | |
var canvas = document.createElement('canvas'), | |
ctx = canvas.getContext('2d'), | |
image = new Image(); | |
image.src = path; | |
image.onload = function () { | |
canvas.width = image.height; | |
canvas.height = image.width; | |
ctx.translate(image.width/2, image.width/2); | |
ctx.rotate(degrees * Math.PI / 180); | |
ctx.translate(-image.width/2, -image.width/2); | |
ctx.drawImage(image, 0, 0); | |
callback(canvas.toDataURL()); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment