How can I clear an arc or circle in HTML5 canvas?

后端 未结 6 1978
谎友^
谎友^ 2021-02-04 02:06

I found that there\'s a clearRect() method, but can\'t find any to clear an arc (or a full circle).

Is there any way to clear an arc in canvas?

6条回答
  •  日久生厌
    2021-02-04 02:22

    You can use the clearRect() method to erase a portion of the canvas (including your arc), but when you're using clearRect() with arcs or anything else that you used beginPath() and closePath() for while drawing, you'll need to handle the paths while erasing, too. Otherwise, you may end up with a faded version of your arc still appearing.

    //draw an arc (in this case, a circle)
    context.moveTo(x, y);
    context.beginPath();
    context.arc(x,y,radius,0,Math.PI*2,false);
    context.closePath();
    context.strokeStyle = "#ccc";
    context.stroke();
    
    //now, erase the arc by clearing a rectangle that's slightly larger than the arc
    context.beginPath();
    context.clearRect(x - radius - 1, y - radius - 1, radius * 2 + 2, radius * 2 + 2);
    context.closePath();
    

提交回复
热议问题