<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<canvas id="qiu100" width="500" height="470"></canvas>
<script type="text/javascript">
var canvas = document.getElementById("qiu100");
var q = canvas.getContext("2d");
var dir = 100;
var height = 450;
var width = 500;
var exp = 10;//像素移动的位置
function fff()
{
q.clearRect(0,0,height,width);//清除上一次的痕迹
q.fillStyle = "black";
q.beginPath();
q.arc(50, dir, 35, 0, Math.PI * 2, 2);//球的大小
q.closePath();
q.fill();
dir = dir + exp;
if(dir==0||dir==height)
{
exp = exp * -1;
}
}
</script>
<div id="ds"></div>
<button onclick="tt=setInterval(fff,20);">开始</button>
<button onclick="clearInterval(tt);" >停止</button>
</body>