Skip to content

Instantly share code, notes, and snippets.

@NKid
Last active August 19, 2020 10:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NKid/062d260e23f3d2e17c70 to your computer and use it in GitHub Desktop.
Save NKid/062d260e23f3d2e17c70 to your computer and use it in GitHub Desktop.
[旋轉圖片] Rotate Image #bookmarklet
javascript: (function() {
window.addEventListener("click", function(event) {
if(event.target.tagName == 'IMG') {
event.preventDefault();
var image = event.target;
var canvas = document.createElement('canvas');
canvas.id = 'myCanvas' + new Date().getTime();
var ctx = canvas.getContext('2d');
var width = image.width,
height = image.height,
x = 0,
y = 0;
var degree = prompt('Rotate Angle ?\n[90,180,270]', '90');
switch(degree) {
case '90':
width = image.height;
height = image.width;
y = image.height * (-1);
break;
case '180':
x = image.width * (-1);
y = image.height * (-1);
break;
case '270':
width = image.height;
height = image.width;
x = image.width * (-1);
break;
}
canvas.setAttribute('width', width);
canvas.setAttribute('height', height);
ctx.rotate(degree * Math.PI / 180);
ctx.drawImage(image, x, y);
image.parentElement.insertBefore(canvas, image);
image.style.display = "none";
}
}, false);
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment