Skip to content

Instantly share code, notes, and snippets.

@rafaelcanovas
Created June 1, 2016 22:54
Show Gist options
  • Save rafaelcanovas/876b6b0e2f645aa3a467f7ec08e7f453 to your computer and use it in GitHub Desktop.
Save rafaelcanovas/876b6b0e2f645aa3a467f7ec08e7f453 to your computer and use it in GitHub Desktop.
Javascript utility to rotate image.
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