Skip to content

Instantly share code, notes, and snippets.

@sergiolopes
Created January 29, 2014 16:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergiolopes/8692059 to your computer and use it in GitHub Desktop.
Save sergiolopes/8692059 to your computer and use it in GitHub Desktop.
<canvas width=300 height=300 style="display:none"></canvas>
<h4>Original</h4>
<img class="original" src="https://dl.dropboxusercontent.com/u/139992952/stackoverflow/house-icon.png">
<h4>90 graus</h4>
<img class="rotate90">
<h4>180 graus</h4>
<img class="rotate180">
var imgOriginal = document.querySelector('.original').src;
var canvas=document.querySelector("canvas");
var ctx=canvas.getContext("2d");
var image=document.createElement("img");
image.src=imgOriginal;
function drawRotated(degrees){
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();
return canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
}
document.querySelector('.rotate90').src = drawRotated(90);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment