How to fill the 'opposite' shape on canvas?

前端 未结 4 510
小蘑菇
小蘑菇 2020-12-08 04:45

Say I have a circle (an arc) on HTML5 canvas. I can just fill it like this:

ctx.arc(100, 100, 50, 0, 2 * Math.PI);
ctx.fill();

It works a t

4条回答
  •  广开言路
    2020-12-08 05:25

    Using the XOR function on a canvas mask only works with opaque fills (i.e. not compatible with globalAlpha). One way around this is to use the .clip() function and filling the clipped region with what you had in the background. Then, you can overlay the clipped region on top of the original canvas, on which you performed whatever fill you needed to.

提交回复
热议问题