(一)在页面上移动
<html>
<head>
<title>Canvas Animation</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
<body>
<canvas id="canvas" width="200" height="200"></canvas>
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var position = 0;
//定时器函数第一个参数是要执行的函数
//第二个参数是每隔多少分钟执行一次函数
setInterval(function(){
//清除出一个矩阵
ctx.clearRect(0,0,200,200);
//填充矩阵
ctx.fillRect(position,0,20,20);
position++;
if(position>200){
position=0;
}
},30);
</script>
</body>
</html>
(二)对方块的大小实现动画
<html>
<head>
<title>Canvas Animation</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
<body>
<canvas id="canvas" width="200" height&