Introduction to rotating images at any angle and JQuery plug-in

Source: Internet
Author: User

Introduce the jquery. rotate. js file below, and then use $ ("selector"). rotate (angle); to 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:Copy codeThe 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 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.