让canvas画的正方形动起来

点击(此处)折叠或打开

  1. <!DOCTYPE html >
  2. <html>
  3.  <head>
  4.     <title>cars</title>
  5.     <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
  6.  </head>
  7.  <body>
  8.    <canvas id ="canvas" width="200" height="200"></canvas>
  9.    <script>
  10.      var canvas = document.getElementById("canvas");
  11.      var ctx = canvas.getContext("2d");
  12.      var position = 0;
  13.      setInterval(function(){
  14.      ctx.clearRect(0,0,200,200);
  15.      ctx.fillRect(position,0,20,20);
  16.      position++;
  17.      if(position > 200){
  18.      position = 0;
  19.      }
  20.      },30)
  21.    </script>
  22.  </body>
  23. </html>

11-10 20:14