This article describes how to use HTML5CanvasAPI to perform image rotation, including rotating the image's central Coordinate Position and rotating the image around a certain point, as a web developer, I have been dealing with HTML5 canvas elements. Rendering an image is a big branch that is very important and commonly used. Therefore, today's tutorial is about canvas Image Display and how to rotate the image. It may be something you really want now.
In general, there are two ways to rotate the canvas: center Rotation and Reference point rotation. Skillful application of the rotation function will be of great help to your development work.
About the center rotation of an object
For the first type of rotation, we need to see that it is to rotate an object to its center. The canvas element is the simplest rotation type. We tested the image of a gorilla as material.
The basic idea is to rotate the canvas according to a central point, rotate the canvas, and then return the canvas to its original position. If you have some experience with graphics engines, it should sound familiar. The code is like this: (click to see the effect)
Copy the content to the clipboard using JavaScript Code
- Function onload (){
- Var canvas = document. getElementById ('c1 ');
- Var ctx1 = canvas. getContext ('2d ');
- Var image1 = new Image ();
- Image1.onload = function (){
- // Regular rotation about center
- Var xpos = canvas. width/2;
- Var ypos = canvas. height/2;
- Ctx1.drawImage (image1, xpos-image1.width/2, ypos-image1.height/2 );
- Ctx1.save ();
- Ctx1.translate (xpos, ypos );
- Ctx1.rotate (47 * Math. PI/180); // 47 degrees of Rotation
- Ctx1.translate (-xpos,-ypos );
- Ctx1.drawImage (image1, xpos-image1.width/2, ypos-image1.height/2 );
- Ctx1.restore ();
- }
- Image1.src = 'image.png ';
- }
Annotations are already very detailed, but I still want to mention:. save () and. restore (). Their role is to save the canvas before rotation and restore it after rotation. It is critical to effectively avoid conflicts with other rendering services. Many of our friends have not been able to rotate smoothly, most of which is the cause.
Rotate around a point
The second type is image rotation around a certain point in space, which will become more complicated. But why? In many cases, you need to refer to another object for rotation. A single center-centered rotation cannot meet your needs. In addition, the latter will be more commonly used. For example, rotation is often used in web games.
Copy the content to the clipboard using JavaScript Code
- Function onload (){
- Var canvas2 = document. getElementById ('c2 ');
- Var ctx2 = canvas2.getContext ('2d ');
- // Regular rotation about point
- Var image2 = new Image ();
- Image2.onload = function (){
- // Regular rotation about a point
- Var angle = 120 * Math. PI/180; // angle of rotation in radians
- Var rx = 300, ry = 200; // the rotation x and y
- Var px = 300, py = 50; // the objects center x and y
- Var radius = ry-py; // the difference in y positions or the radius
- Var dx = rx + radius * Math. sin (angle); // the draw x
- Var dy = ry-radius * Math. cos (angle); // the draw y
- Ctx2.drawImage (image2, 300-image2.width/2, 50-image2.height/2 );
- Ctx2.beginPath ();
- Ctx2.arc (300,200, Math. PI * 2, false );
- Ctx2.closePath ();
- Ctx2.fillStyle = 'rgba (0,255, 0, 0.25 )';
- Ctx2.fill ();
-
- Ctx2.save ();
- Ctx2.translate (dx, dy );
- Ctx2.rotate (angle );
- Ctx2.translate (-dx,-dy );
- Ctx2.drawImage (image2, dx-image2.width/2, dy-image2.height/2 );
- Ctx2.restore ();
- }
- Image2.src = 'smallimage.png ';
- }
The code is concise, and the function is to rotate an image 120 degrees by point, which is more vivid.
Draw a magic Logo
This is a logo seen on DU Niang, cleverly using the Rotation Transformation, using a very simple rectangle, through the Rotation Transformation, into a very beautiful logo. Is this logo a magic? Shoes are moving their brains and try to implement it. The code for implementing it is provided below.
Copy the content to the clipboard using JavaScript Code
-
-
-
-
- Draw a magic Logo
-
-
-
-
- Does your browser support Canvas ?! Just change one !!
-
-
-
- Script
- Window. onload = function (){
- Var canvas = document. getElementById ("canvas ");
- Canvas. width = 800;
- Canvas. height = 600;
- Var context = canvas. getContext ("2d ");
- Context. fillStyle = "# FFF ";
- Context. fillRect (0, 0, 800,600 );
-
- For (var I = 1; I <= 10; I ++ ){
- Context. save ();
- Context. translate (400,300 );
- Context. rotate (36x I * Math. PI/180 );
- Context. fillStyle = "rgba (255,0, 0, 0.25 )";
- Context. fillRect (0,-200,200,200 );
- Context. restore ();
- }
- };
- Script
-
-
Running result:
Is it really cool? It is quite simple to analyze this image, that is, to rotate a positive shape with the center of the center at the midpoint of the screen (that is, the vertex in the lower left corner of the initial square.
Is Art wonderful? You must have realized the wonders of Canvas. A few simple lines of code can achieve endless results. As long as the brain hole is large enough, nothing can be done. So raise the banner of our artists, speed up the pace, and move on!