如何在 HTML5 画布中为球变小制作动画
我正在尝试使用 HTML5 画布使球变得越来越小。我已经能够让它变得更大,所以我认为反过来会很简单。我在这里做错了什么? Console.log 显示从 11 到 0 的值递减 1。当 x 小于 0 时,它会停止。但球的形状并没有改变,我怀疑是因为它在彼此之上绘制了更小的形状,也许?我认为clearRect 可以做到这一点?
function draw2()
{
console.log(x);
context2D.clearRect(0, 0, canvas.width, canvas.height);
context2D.arc(10, 10, x, 0, Math.PI * 2, true);
context2D.fill();
x -= 1;
if (x < 0) {
clearInterval(s);
}
}
演示位于:http://www.chronicled.org/dev/test.html
谢谢!
I'm trying to make a ball become increasingly smaller using HTML5 canvas. I've been able to make it grow larger, so I figured the reverse would be simple. What am I doing wrong here? Console.log shows me values from 11 to 0 decreasing by 1. When x is less than 0, it stops. But the ball doesn't change shape, and I suspect its because it's drawing smaller shapes on top of each other, perhaps? I thought clearRect would work for that?
function draw2()
{
console.log(x);
context2D.clearRect(0, 0, canvas.width, canvas.height);
context2D.arc(10, 10, x, 0, Math.PI * 2, true);
context2D.fill();
x -= 1;
if (x < 0) {
clearInterval(s);
}
}
A demo is available at: http://www.chronicled.org/dev/test.html
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加
context2D.beginPath();
到draw2的开头(将它放在draw中也不会造成伤害).fill正在填充包括旧弧线的整个路径
add
context2D.beginPath();
to the beginning of draw2 (it also wouldn't hurt to have it in draw)the .fill is filling the whole path which includes the old arcs
fill()
调用再次填充旧矩形。试试这个:The
fill()
call is filling the old rect again. Try this instead: