two.js icon indicating copy to clipboard operation
two.js copied to clipboard

Use group as a mask

Open xinranwang opened this issue 7 years ago • 4 comments

Is there a way to use a group to mask another group? I tried it and it didn't work.

It looks like snap.svg is supporting it .

xinranwang avatar Mar 06 '18 19:03 xinranwang

It's commented out because of this Chromium Issue. You can re-enable it by uncommenting these lines.

In the meantime I can see what snap.svg does and see if it makes sense to incorporate into Two.js. snap.svg has the benefit of only rendering to SVG.., so it may not be possible for it to work the same way for canvas / webgl.

jonobr1 avatar Mar 06 '18 21:03 jonobr1

Thanks for the information!

I tried uncommenting out the lines but it still doesn't seem to work. Am i missing something?

var mask_group = two.makeGroup();
two.makeCircle(0, 0, 60).addTo(mask_group);
two.makeRectangle(40, 40, 90, 90).addTo(mask_group);
mask_group.center();

var bounding_box = mask_group.getBoundingClientRect();
var bg = two.makeGroup(two.makeRectangle(0, 0, bounding_box.width, bounding_box.height));
bg.mask = mask_group;

var group = two.makeGroup(mask_group, bg);
group.translation.set(two.width / 2, two.height / 2);

xinranwang avatar Mar 06 '18 21:03 xinranwang

Ah, sorry about that. I think the group.mask property expects a Two.Path not another group... I'll have to look into the details further on how snap does group to group masking. Sorry!

jonobr1 avatar Mar 06 '18 21:03 jonobr1

no worries. thanks for your help!

xinranwang avatar Mar 06 '18 21:03 xinranwang