HTML5 label canvas for animation,

Source: Internet
Author: User

HTML5 label canvas for animation,
Abstract:

Canvas allows you to draw images, and naturally you can make animations, because each frame of an animation is an image. We can use the setInterval function of javascript to achieve the animation effect.

The following is an example in which the circle is continuously circled around the center of the red dot.

Code:

1 <canvas id = "myCanvas" width = "300" height = "300"> 2 your browser does not support canvas! 3 </canvas> 4 <script> 5 var canvas = document. getElementById ('mycanvas '); 6 if (canvas. getContext) {7 var context = canvas. getContext ('2d '); 8 var posX = 100, 9 posY =, 10 flag = true; 11 12 setInterval (function () {13 14 context. fillStyle = "# ccc"; 15 context. fillRect (0, 0, canvas. width, canvas. height); 16 context. beginPath (); 17 context. fillStyle = "white"; 18 19 context. arc (posX, posY, 20, 0, Math. PI * 2, true); 20 context. closePath (); 21 context. fill (); 22 23 console. log (posX + "," + posY); 24 if (flag & posX <201) {25 posX + = 1; 26} else {27 posX-= 1; 28 flag = false; 29 if (posX <100) {30 flag = true; 31} 32} 33 if (flag) {34 posY = 150-Math.sqrt (2500-Math.pow (posX-150, 2); 35} else {36 posY = 150 + Math. sqrt (2500-Math.pow (posX-150, 2); 37} 38 39 context. beginPath (); 40 context. arc2 (150,150, 2, 0, Math. PI * 2, true) 41 context. fillStyle = "red"; 42 context. fill (); 43 44}, 50); 45} 46 </script>

 

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.