[英]Why isn't my canvas clearRect working?
I set up an interval: 我设置了一个间隔:
tick = setInterval(function(){
render();
}, 1000 / 25)
Then each render()
I clear rect and expect to see a dot travel across the screen. 然后,我清除rect的每个
render()
并希望看到一个点在屏幕上移动。
function render() {
ctx.clearRect(0, 0, 1000, 1000);
circle(sprite.x, sprite.y, sprite.r);
for (var i = 0; i < animations.length; i++) {
var s = animations[i];
if( s.target ){
Engine.Animate(s);
}
}
}
But for some reason, I'm ending up drawing every single position of the dot, rather than just one. 但是由于某种原因,我最终要绘制点的每个位置,而不仅仅是绘制一个。
Example: https://jsfiddle.net/yvmbsjj1/1/ 示例: https : //jsfiddle.net/yvmbsjj1/1/
You need to tell the canvas context that you are starting a path, and then your clearRect will work. 您需要告诉canvas上下文您正在开始路径,然后您的clearRect将起作用。 Use the beginPath() method to do this.
使用beginPath()方法执行此操作。
function render() {
ctx.beginPath(); // This is needed
ctx.clearRect(0, 0, 1000, 1000);
circle(sprite.x, sprite.y, sprite.r);
for (var i = 0; i < animations.length; i++) {
var s = animations[i];
if( s.target ){
Engine.Animate(s);
}
}
}
This is needed when drawing lines, arcs, circles, rectangles, etc... And should be called every time you render, to initialize a new path. 绘制直线,圆弧,圆形,矩形等时,需要此方法。每次渲染时都应调用它,以初始化新路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.