用定时器实现矩形的移动
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style>
body{ background:black;}
#c1{ background:white;}
span{ color:white;}
</style>
<script>
window.onload=function(){
var oC=document.getElementById('c1');
var oGC=oC.getContext('2d');
var num=0;
oGC.fillRect(0,0,100,100);//画矩形
setInterval(function(){
num++;
oGC.clearRect(0,0,oC.width,oC.height);//清除矩形
oGC.fillRect(num,num,100,100)//画新的矩形
},30);
}
</script>
</head>
<body>
<canvas id="c1" width="400" height="400">
<span>不支持canvas浏览器</span>
</canvas> <!--默认:宽300 高150-->
</body>
</html>