Rotate image

4.6
5 votes
Your rating: None

This stroke can rotate images, or more specifically, <img> and <canvas> elements under cursor.

By default, the image will be rotated clockwise by 90 degrees, you can modify the degree variable at the first line to customize it.

Runs in:

the target frame

Script:

/* Rotate by 90 degrees clock wise. Minus degrees are allowed */
var degree = 90;

var elem = document.getElementById(MS.initevt.target.id);
var canvas, context, radian, width, height;

function rotate() {
	canvas = document.createElement('canvas');
	context = canvas.getContext('2d');

	canvas.setAttribute('width', width);
	canvas.setAttribute('height', height);
	context.translate(width/2, height/2);
	context.rotate(radian);
	context.drawImage(elem, -elem.width/2, -elem.height/2);

	elem.parentNode.insertBefore(canvas, elem);
	elem.parentNode.removeChild(elem);
}

if (elem) {
	degree = ((degree % 360) + 360) % 360;
	radian = degree * Math.PI / 180;
	width = elem.width * Math.abs(Math.cos(radian)) + elem.height * Math.abs(Math.sin(radian));
	height = elem.width * Math.abs(Math.sin(radian)) + elem.height * Math.abs(Math.cos(radian));

	if (/^(img|canvas)$/i.test(elem.nodeName)) {
		rotate();
	} else {
		throw new TypeError('Not an image or canvas, cannot rotate');
	}
}

Comments

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <strike> <cite> <code> <pre> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is to prevent automated spam submissions, you will not see this again after registration.