Home Web Front-end JS Tutorial 3 Solutions to Rotate Pictures_The Basics

3 Solutions to Rotate Pictures_The Basics

May 16, 2016 pm 05:13 PM
Picture rotation

Research on the Image Rotation Effect
Recently, I needed to do the image rotation function in my project, so I did some research. Let’s summarize the support of image rotation in various browsers

1. Image rotation plan

1) CSS3 implements image rotation function: supported browsers are chrome and firefox; opera is not supported.

The specific code is: -moz-transfrom:rotate(90deg);-webkit-ransfrom:rotate(90deg);
The above code means to rotate the image 90 degrees clockwise. In fact, it can be rotated to any degree. However, the only browsers that support CSS3 are chrome, firefox3.6, safari, and ie browsers do not support it. So how to deal with it in IE
? So we have the following solution

2) Rotate through filters in IE

The specific code is: filter:progid:DXImageTransform.Microsoft.BasicImage(rotation=1);
The rotation parameter here can be 0,1,2,3, which means that these numbers are multiplied by 90 Rotation angle. If you want to rotate to any angle under IE, you need to use a matrix transformation filter. In our daily use,
most of us use transformations that are multiples of 90. We won’t discuss arbitrary angles here. But there is still a problem. If the browser is not Internet Explorer and does not support CSS3, then if you realize the rotation of the image, the canvas can also rotate the image.

3) Use canvas to rotate images

Canvas is supported in chrome, firefox, opera and other browsers. It has a drawing API based on JavaScript. First, let’s take a look at how to use canvas JavaScript to rotate images

The code is as follows:

Copy the codeThe code is as follows:

var test = function(){
var canvas = document.getElementById("result");
var oImg = document.getElementById("Img");
canvas.height = 300;
canvas.width = 200;
var context = canvas.getContext("2d");
context.save();
context.translate(200,0);
context.rotate (Math.PI/3);
context.drawImage(oImg, 0, 0, 300, 200);
context.restore();
oImg.style.display = "none";
};

The above code first gets a canvas object, then sets its height and starts drawing. This changes the center point and rotation angle of the canvas, then draws the image into the canvas, stores it, and then
hides the previous image. This method is relatively smooth to implement.

2. Comparison of various solutions

The implementation of css3 will not change the size of the space occupied by the original image, but the filter under IE will change the space occupied by the image. The size of the space.
In fact, IE can also support canvas. You only need to quote a canvas script. This one is provided by Google. But this script is a bit big, more than 20k before compression
I prefer to use filters under IE, and other browsers to use the canvas tag.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1671
14
PHP Tutorial
1276
29
C# Tutorial
1256
24
How to use JavaScript to achieve image rotation effect? How to use JavaScript to achieve image rotation effect? Oct 20, 2023 pm 07:09 PM

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

Use uniapp to implement image rotation function Use uniapp to implement image rotation function Nov 21, 2023 am 11:58 AM

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

How to realize image rotation using PHP and GD library How to realize image rotation using PHP and GD library Jul 12, 2023 am 11:52 AM

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

Implement image rotation effect in WeChat applet Implement image rotation effect in WeChat applet Nov 21, 2023 am 08:26 AM

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 and flipping in Golang How to implement image rotation and flipping in Golang Aug 27, 2023 pm 01:03 PM

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

How to solve the problem of mobile image rotation in Vue development How to solve the problem of mobile image rotation in Vue development Jun 29, 2023 pm 09:22 PM

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.

Methods to optimize image rotation and scaling performance in Java development Methods to optimize image rotation and scaling performance in Java development Jun 30, 2023 pm 04:34 PM

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

Best practices for image rotation using PHP and GD library Best practices for image rotation using PHP and GD library Jul 12, 2023 am 09:53 AM

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

See all articles