It is necessary to rotate images from any angle in some applications. Now there is a JQuery plug-in that can be used. Next, let's introduce how to use it. For more information, see, we hope to help introduce jquery below. rotate. js file, and then use $ ("selector "). rotate (angle); can rotate any angle,
For example, $ ("# rotate-image"). rotate (45); Put this sentence in $ (document). ready (function () {});
It is to rotate the image with id rotate-image 45 degrees.
However, it seems that it is always not displayed in Chrome.
Alas, it took two hours to find that Chrome is too boring to get the length and width of the image.
The solution is to put $ ("# rotate-image"). rotate (45); In
$ (Window ). load (function () {});, because the image in Chrome is executing $ (document ). the statement in ready (function () {}); does not load the image.
In addition, you can call $ ("selector") more conveniently "). rotateRight () and $ ("selector "). rotateLeft () to rotate 90 degrees to the right and 90 degrees to the left respectively.
Jquery. rotate. js:
The Code is as follows:
JQuery. fn. rotate = function (angle, whence ){
Var p = this. get (0 );
// We store the angle inside the image tag for persistence
If (! Whence ){
P. angle = (p. angle = undefined? 0: p. angle) + angle) % 360;
} Else {
P. angle = angle;
}
If (p. angle> = 0 ){
Var rotation = Math. PI * p. angle/180;
} Else {
Var rotation = Math. PI * (360 + p. angle)/180;
}
Var costheta = Math. round (Math. cos (rotation) * 1000)/1000;
Var sintheta = Math. round (Math. sin (rotation) * 1000)/1000;
// Alert (costheta + "," + sintheta );
If (document. all &&! Window. opera ){
Var canvas = document. createElement ('img ');
Canvas. src = p. src;
Canvas. height = p. height;
Canvas. width = p. width;
Canvas. style. filter = "progid: DXImageTransform. microsoft. matrix (M11 = "+ costheta +", M12 = "+ (-sintheta) +", M21 = "+ sintheta +", M22 = "+ costheta + ", sizingMethod = 'Auto expand ')";
} Else {
Var canvas = document. createElement ('canvas ');
If (! P. oImage ){
Canvas. oImage = new Image ();
Canvas. oImage. src = p. src;
} Else {
Canvas. oImage = p. oImage;
}
Canvas. style. width = canvas. width = Math. abs (costheta * canvas. oImage. width) + Math. abs (sintheta * canvas. oImage. height );
Canvas. style. height = canvas. height = Math. abs (costheta * canvas. oImage. height) + Math. abs (sintheta * canvas. oImage. width );
Var context = canvas. getContext ('2d ');
Context. save ();
If (rotation <= Math. PI/2 ){
Context. translate (sintheta * canvas. oImage. height, 0 );
} Else if (rotation <= Math. PI ){
Context. translate (canvas. width,-costheta * canvas. oImage. height );
} Else if (rotation <= 1.5 * Math. PI ){
Context. translate (-costheta * canvas. oImage. width, canvas. height );
} Else {
Context. translate (0,-sintheta * canvas. oImage. width );
}
Context. rotate (rotation );
Context. drawImage (canvas. oImage, 0, 0, canvas. oImage. width, canvas. oImage. height );
Context. restore ();
}
Canvas. id = p. id;
Canvas. angle = p. angle;
P. parentNode. replaceChild (canvas, p );
}
JQuery. fn. rotateRight = function (angle ){
This. rotate (angle = undefined? 90: angle );
}
JQuery. fn. rotateLeft = function (angle ){
This. rotate (angle = undefined? -90:-angle );
}