Skip to content
Advertisement

Canvas 2DPath Clip overlap pr

I’m trying to create one compound shape in canvas that masks all the content below it – i will be animating these paths so they will eventually intersect – the problem is, when they intersect, they are doing a reverse mask when the drawing overlaps. I just want one solid mask.

JavaScript

Here is an example ( as you can see there is a thin line that overlaps that isn’t clipping correctly – however the OTHER line DOES clip correctly ):

https://codepen.io/pdub888/pen/MWjrdwe

Advertisement

Answer

What you should do is draw it in a separate – hidden – buffer canvas, just fill all the shapes – there’s no need for a Path2D constructor unless you will reuse it, but even then you should not draw your entire shape like this (because of the winding algorithm, certain bits are not part of the fill, but thats not entirely relevant for masking images here – more info and examples here: https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/fill-rule)

Then use that buffer canvas to cut mask another canvas using a globalCompositeOperation. Below I have modified your snippet and removed the constructor, and acted directly on the buffer canvas.

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement