jquery plugin makes the picture rotate at any angle and the code is extremely simple

Source: Internet
Author: User

The Jquery.rotate.js file is introduced below and then passed through $ ("selector"). Rotate (angle), can rotate any angle,

For example $ ("#rotate-image"). Rotate (45); Put this sentence in $ (document). Ready (function () {});

is to rotate the picture with ID rotate-image 45 degrees.

However, it does not seem to appear in chrome.

Alas, the search for two hours, only to find that Chrome is too bad to get the image of the length of the width.

The solution is to put $ ("#rotate-image"). Rotate (45);

$ (window). Load (function () {}); in Chrome, because the picture is in the execution of $ (document). Ready (function () {}); The statement in is not finished loading the picture, Pit Daddy.


It can also be more convenient by calling $ ("selector"). Rotateright () and $ ("selector"). Rotateleft () to rotate 90 degrees to the right and 90 degrees to the left.

Jquery.rotate.js:

JQuery.fn.rotate =function(angle,whence) {varp = 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) {              varrotation = Math.PI * p.angle/180; } Else {              varrotation = Math.PI * (360+p.angle)/180; }          varCostheta = Math.Round (Math.Cos (rotation) * 1000)/1000; varSintheta = Math.Round (Math.sin (rotation) * 1000)/1000; //alert (costheta+ "," +sintheta);                if(document.all &&!)Window.opera) {varCanvas = 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 {              varCanvas = document.createelement (' Canvas '); if(!p.oimage) {canvas.oimage=NewImage (); 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); varcontext = 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); }  

jquery plugin makes the picture rotate at any angle and the code is extremely simple

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.