Phaser line of sight polygon and the "negative" invisible polygon

I’m looking at this Line of sight code and managed to get it to work with Phaser3. It works similar to the demo, so when you move a sprite with the mouse it will draw a yellow polygon for the visible area, see screenshot below.

However, I would like to get the negative or the opposite polygon, so the blue area in the example screenshot. Then I could make the polygon a black fill color, so that it will block any sprites that are outside of the visible area.

What is the best way to change this polygon?
Or is there a better way to do line-of-sight in Phaser?
Like is there a line-of-sight plug-in maybe?

1 Like

I know I’m two years too late, but in case anyone else has the same question and runs across this thread like I did: polygon-tools has a subtract function that does exactly what you want.

Make a polygon for the outer boundaries and subtract the yellow polygon. The result is an array of polygons representing the blue area.