前面介绍了使用 canvas 来画一些图形,这里将介绍如何使用 canvas 去制作动态图形。见代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
canvas{
border: 2px solid black;
}
</style>
</head>
<body>
<canvas id="can" width="800" height="600"></canvas>
</body>
<script type="text/javascript">
var can=window.document.getElementById("can");
var ctx=can.getContext("2d");
var x=100;
setInterval(function(){ //setInterval() 方法可按照指定的周期(以毫秒计)来调用函数或计算表达式。
//清屏
ctx.clearRect(0,0,800,600);
//重新绘制
ctx.beginPath();
ctx.arc(x++,100,80,0,2*Math.PI);
ctx.strokeStyle="grey";
ctx.lineWidth=5;
ctx.stroke();
ctx.closePath();
ctx.beginPath();
ctx.arc(x++,250,60,0,2*Math.PI);
ctx.strokeStyle="grey";
ctx.lineWidth=5;
ctx.stroke();
ctx.closePath();
ctx.beginPath();
ctx.arc(x++,360,40,0,2*Math.PI);
ctx.strokeStyle="grey";
ctx.lineWidth