HTML5 drawing animation

draw animation

While the canvas API doesn't directly provide a way to support animations, implementing animations in canvas itself is simple: just keep updating and redrawing the canvas. This continuous update and redraw is called the animation loop, and it is the core logic of all animations.

To implement animation in canvas, you first need to initialize the objects on the canvas. Then, start an animation loop to update the canvas, clear the canvas, repaint the canvas, and request the next new animation frame. The basic principle of Canvas animation is shown in Figure 4‑36:

Basic principles of canvas animation Figure 4-36 Basic principles of canvas animation

Next, let's take a look at the implementation process of canvas animation through a simple example. This instance implements a rotating gossip diagram in an animated way. code show as below:

 
 
  1. function clear() {  
  2.    context.clearRect(0, 0, canvas.width, canvas.height);
  3. }
  4. function rotate() {
  5.    context.rotate(Math.PI/30);  // 每分钟旋转一周
  6. }
  7. function draw () {
  8.     // 绘制白色半圆
  9.    context.beginPath();
  10.    context.arc(0, 0, 80, 1.5*Math.PI, Math.PI/2, false);
  11.    context.fillStyle = "white";
  12.    context.closePath();
  13.    context.fill();
  14.            
  15.    // 绘制黑色半圆
  16.    context.beginPath();
  17.    context.arc(0, 0, 80, Math.PI/2, 1.5*Math.PI, false);
  18.    context.fillStyle = "black";
  19.    context.closePath();
  20.    context.fill();
  21.            
  22.     // 绘制黑色小圆
  23.     context.beginPath();
  24.     context.arc(0, 40, 40, 0, Math.PI*2, true);
  25.     context.fillStyle = "black";
  26.     context.closePath();
  27.     context.fill();
  28.            
  29.     // 绘制白色小圆
  30.     context.beginPath();
  31.     context.arc(0, -40, 40, 0, Math.PI*2, true);
  32.     context.fillStyle = "white";
  33.     context.closePath();
  34.     context.fill();
  35.            
  36.     // 绘制白色小圆心
  37.     context.beginPath();
  38.     context.arc(0, -40, 5, 0, Math.PI*2, true);
  39.     context.fillStyle = "black";
  40.     context.closePath();
  41.     context.fill();
  42.            
  43.     // 绘制黑色小圆心
  44.     context.beginPath();
  45.     context.arc(0, 40, 5, 0, Math.PI*2, true);
  46.     context.fillStyle = "white";
  47.     context.closePath();
  48.     context.fill();
  49. }
  50. function drawStage() {
  51.      rotate();  // 更新
  52.      clear();   // 清除
  53.      draw();    // 重绘
  54. }
  55. window.onload = function(){
  56.     canvas = document.getElementById('canvas');
  57.     context = canvas.getContext('2d');
  58.           
  59.     context.translate(canvas.width/2, canvas.height/2);
  60.           
  61.     setInterval(drawStage, 100);
  62. };

The above code, when the page is loaded, first initializes, and then calls the setInterval(drawStage, 100) method to start the animation loop. In the animation loop, the drawStage () function is called every 100ms to update the canvas, clear the canvas, Repaint the action of the canvas for animation effects. The running result is shown in Figure 4-37:

Rotating Bagua Diagram Figure 4-37 Rotated Bagua Diagram

Of course, this is just to demonstrate the principle of implementing animation, so the example is relatively simple. In fact, animations in Canvas can be simple or complex. Simple or complex, the basic principles are exactly the same.

About the author

Mr. Crooked neck , more than 15 years of software development experience, loves web development, proficient in HTML, CSS, JavaScript, jQuery, JSON, Python, Less, Bootstrap, etc., author of " HTML Collection ", " Demystifying CSS ", " Less Concise Tutorial " ", " JSON Tutorial ", " Bootstrap2 User Guide ", and all open source on GitHub .

Copyright statement : This article is from  the " HTML Collection " of Waibowang . You are welcome to read it online and give your valuable opinions. 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324886893&siteId=291194637