HTML 5 Canvas seems to redraw removed parts

前端 未结 1 997
时光说笑
时光说笑 2021-01-24 23:39

I have created the following code on jsfiddle. The goal is to remove a box from the canvas after it has been clicked. What actually happens is that the grid is cleared and compl

相关标签:
1条回答
  • 2021-01-25 00:04

    Working Fiddle

    Change createBox to the following.

      this.createBox    = function( xpos, ypos )
        {
            this.context.beginPath();
            this.context.rect( xpos, ypos, this.boxWidth, this.boxHeight );
            this.context.fillStyle    = '#444';
            this.context.fill();
            this.context.closePath();
        };
    

    Your not properly starting/ending paths, so the previous path isnt cleared when you redraw thus filling them all in again. Another way around it is to just use fillRect instead.

    The first step to create a path is calling the beginPath method. Internally, paths are stored as a list of sub-paths (lines, arcs, etc) which together form a shape. Every time this method is called, the list is reset and we can start drawing new shapes.

    Further Reading

    0 讨论(0)
提交回复
热议问题