


Let the picture rotate to any angle and introduce the use of JQuery plug-in_jquery
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:
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);
}

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use JavaScript to achieve image rotation effect? In web development, we often encounter scenarios where image rotation effects need to be achieved, such as displaying 360° rotation images of products, achieving image carousel effects, etc. JavaScript is a powerful scripting language that can easily achieve this image rotation effect. The following will introduce a method to achieve image rotation effects based on JavaScript and provide specific code examples. First, we create a simple HTML structure

Using uniapp to implement image rotation function In mobile application development, we often encounter scenarios where images need to be rotated. For example, the angle needs to be adjusted after taking a photo, or an effect similar to the rotation of a camera after taking a photo is achieved. This article will introduce how to use the uniapp framework to implement the image rotation function and provide specific code examples. uniapp is a cross-platform development framework based on Vue.js, which can simultaneously develop and publish applications for iOS, Android, H5 and other platforms. Implemented in uniapp

To implement the picture rotation effect in WeChat Mini Program, specific code examples are required. WeChat Mini Program is a lightweight application that provides users with rich functions and a good user experience. In mini programs, developers can use various components and APIs to achieve various effects. Among them, the picture rotation effect is a common animation effect that can add interest and visual effects to the mini program. To achieve image rotation effects in WeChat mini programs, you need to use the animation API provided by the mini program. The following is a specific code example that shows how to

How to implement image rotation using PHP and GD libraries Image rotation is a common image processing requirement. By rotating images, you can achieve some special effects or meet user needs. In PHP, you can use the GD library to implement the image rotation function. This article will introduce how to use PHP and the GD library to implement image rotation, with code examples. First, make sure your PHP environment has the GD library extension installed. Enter php-m on the command line to check if there is a gd module. If not, you need to install it first. Here is a simple

With the rapid development of the mobile Internet, more and more websites and applications are beginning to use Vue.js for mobile development. However, during mobile development, we often encounter the problem of image rotation. Image rotation means that when a user takes a photo on a mobile device, due to the change in device orientation, the angle of the photo displayed on the page is inconsistent with the actual shooting angle. To solve the problem of image rotation, you first need to understand the reason why the image is rotated. When a user takes a photo on a mobile device, the device automatically adds some metadata to the photo.

Golang's method of rotating and flipping images. In image processing, it is often necessary to rotate and flip images. This article will introduce how to use Golang to rotate and flip images, and provide corresponding code examples. First, we need to import the image and image/draw packages: import("image" "image/draw") Next, we define a function

With the development of the Internet, image processing has become more and more common. In Java development, it is often necessary to deal with the rotation and scaling of images. How to optimize the performance of these operations has become a concern for developers. This article will discuss how to optimize the performance of image rotation and scaling in Java development. First, let's understand the principles of image rotation and scaling. Image rotation refers to rotating the image by a certain angle counterclockwise or clockwise according to the rotation center. Image scaling refers to reducing or enlarging the image by changing its size. Open in Java

Overview of best practices for image rotation using PHP and GD libraries: Image rotation is one of the common requirements in web development. By rotating images, you can change the direction of the image or adjust the angle of the image. In PHP, the image rotation function can be implemented through the GD library. This article will introduce how to use PHP and the GD library to implement image rotation, and share some best practices and code examples. 1. Install the GD library. Before starting, we need to ensure that the GD library has been installed on the server. If it is not installed, you can follow the following steps
