Two code examples of HTML5 Canvas rotation animation (effects of a rotating render map)

Source: Internet
Author: User

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>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.