简体   繁体   English

画布-清除矩形不起作用

[英]Canvas - Clear rect is not working

I want to move a rect from the top to the bottom of the canvas. 我想将矩形从画布的顶部移动到底部。 But somehow the canvas does not get cleared. 但是不知何故画布没有被清除。 What is wrong? 怎么了?

js fiddle js小提琴

JS JS

(function animloop(){
  requestAnimFrame(animloop);
  redraw();
})();


    function redraw() {

        ctx.clearRect(0,0,canvasWidth, canvasHeight);
        ctx.rect(20,y,50,50);
        ctx.fillStyle="red";
        ctx.fill(); 
        y += 2;

    }

It is cleared, but you do not start new path, thus the old keeps being re-painted. 它已清除,但是您没有开始新的路径,因此旧的图像一直被重新绘制。

Add: 加:

ctx.beginPath();

in the redraw() function. redraw()函数中。

You might also want to look at 您可能还想看看

and/or similar. 和/或类似。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM