87,991
社区成员
发帖
与我相关
我的任务
分享
<canvas id="draw" width="500px" height="200px"></canvas>
<button id ="start">开始</button>
var start = document.getElementById("start");
var draw = document.getElementById("draw");
var context = draw.getContext("2d");
var y = 50;
var i = 1;
function ball() {
context.clearRect(0,0,500,200);
context.fillStyle = 'red';
context.beginPath();
context.arc(100,y,20,0,Math.PI*2,false);
context.fill();
y = y + i;
if(y==20||y==180) {
i=i*-1;
}
}
start.onclick = setInterval(ball,5);
start.onclick = function(){setInterval(ball,5)}