Home > Web Front-end > JS Tutorial > body text

Let the picture rotate to any angle and introduce the use of JQuery plug-in_jquery

WBOY
Release: 2016-05-16 17:40:04
Original
1021 people have browsed it

Introduce the jquery.rotate.js file below, and then use $("selector").rotate(angle); to rotate to any angle,
for example $("#rotate-image").rotate(45); This sentence is placed in $(document).ready(function(){ });
to rotate the image with the ID rotate-image by 45 degrees.

However, it seems that it is always not displayed in Chrome.
Alas, after searching for two hours, I found out that Chrome is too tricky to get the length and width of the image.
The solution is to put $("#rotate-image").rotate(45); in
$(window).load(function(){ }); because in Chrome the image is When the statement in $(document).ready(function(){ }); is executed, the image is not loaded, which is a scam.

In addition, it is more convenient to rotate 90 degrees to the right and 90 degrees to the left by calling $("selector").rotateRight() and $("selector").rotateLeft().

jquery.rotate.js:

Copy code 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);
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template