Comments: This article mainly introduces two code examples of HTML5 Canvas rotation animation to achieve the effect of a rotating Canvas. For friends who want to learn HTML5 Canvas rotation animation, refer
:
Method 1:
The Code is as follows: <! Doctype html>
<Html>
<Body>
<Canvas id = "myCanvas" width = "500" height = "500"> your browser does not support the canvas tag </canvas>
<Script type = "text/javascript">
Var deg = 0;
Var r = 30;
Var rl = 100;
Function drawTaiji (){
Var canvas = document. getElementById ('mycanvas ');
Var context = canvas. getContext ('2d ');
Var colorA = "rgb (0, 0, 0 )";
Var colorB = "red ";
Var px = Math. sin (deg) * r;
Var py = Math. cos (deg) * r;
Context. clearRect (0, 0,300,300 );
Context. beginPath ();
Context. fillStyle = colorA;
Context. arc (rl, rl, 60, 0.5 * Math. PI + deg, 1.5 * Math. PI + deg, true );
Context. closePath ();
Context. fill ();
Context. fillStyle = colorB;
Context. beginPath ();
Context. arc (rl, rl, 60, 1.5 * Math. PI + deg, 0.5 * Math. PI + deg, true );
Context. closePath ();
Context. fill ();
Context. fillStyle = colorB;
Context. beginPath ();
Context. arc (rl + px, rl-py, 30, 0.5 * Math. PI + deg, 1.5 * Math. PI + deg, true );
Context. closePath ();
Context. fill ();
Context. fillStyle = colorA;
Context. beginPath ();
Context. arc (rl-px, rl + py, 30, 1.5 * Math. PI + deg, 0.5 * Math. PI + deg, true );
Context. closePath ();
Context. fill ();
Context. fillStyle = colorA;
Context. beginPath ();
Context. arc (rl + px, rl-py, 8, 0, 2 * Math. PI, true );
Context. closePath ();
Context. fill ();
Context. fillStyle = colorB;
Context. beginPath ();
Context. arc (rl-px, rl + py, 8, 0, 2 * Math. PI, true );
Context. closePath ();
Context. fill ();
Deg + = 0.1;
}
SetInterval (FIG, 100 );
</Script> </p> </body>
</Html>
Method 2:
The Code is as follows:
<! Doctype html>
<Html>
<Body>
<Canvas id = "myCanvas" width = "500" height = "500"> your browser does not support the canvas tag </canvas>
<Script type = "text/javascript">
Var canvas = document. getElementById ('mycanvas ');
Var ctx = canvas. getContext ("2d ");
Var angle = 0;
Var count = 360;
Var clrA = '#000 ';
Var clrB = 'red ';
Function taiji (x, y, radius, angle, wise ){
Angleangle = angle | 0;
Wisewise = wise? 1:-1;
Ctx. save ();
Ctx. translate (x, y );
Ctx. rotate (angle );
Ctx. fillStyle = clrA;
Ctx. beginPath ();
Ctx. arc (0, 0, radius, 0, Math. PI, true );
Ctx. fill ();
Ctx. beginPath ();
Ctx. fillStyle = clrB;
Ctx. arc (0, 0, radius, 0, Math. PI, false );
Ctx. fill ();
Ctx. fillStyle = clrB;
Ctx. beginPath ();
Ctx. arc (wise *-0.5 * radius, 0, radius/2, 0, Math. PI * 2, true );
Ctx. fill ();
Ctx. beginPath ();
Ctx. fillStyle = clrA;
Ctx. arc (wise * + 0.5 * radius, 0, radius/2, 0, Math. PI * 2, false );
Ctx. arc (wise *-0.5 * radius, 0, radius/10, 0, Math. PI * 2, true );
Ctx. fill ();
Ctx. beginPath ();
Ctx. fillStyle = clrB;
Ctx. arc (wise * + 0.5 * radius, 0, radius/10, 0, Math. PI * 2, true );
Ctx. fill ();
Ctx. restore ();
}
Loop = setInterval (function (){
BeginTag = true;
Ctx. clearRect (0, 0, canvas. width, canvas. height );
Taiji (200,200, 50, Math. PI * (angle/count) * 2, true );
// Taiji (350,350, 50, Math. PI * (count-angle)/count) * 2, false );
Angle = (angle + 5) % count;
}, 50 );
</Script> </p> </body>
</Html>