-
-
Save NKid/062d260e23f3d2e17c70 to your computer and use it in GitHub Desktop.
[旋轉圖片] Rotate Image #bookmarklet
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
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