简体   繁体   中英

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

(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.

You might also want to look at

and/or similar.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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