What is the Best way to Subtract Multiple Times from Existing Path Item.

12 views
Skip to first unread message

shishir raven

unread,
Mar 4, 2021, 2:07:11 PMMar 4
to Paper.js
Problem.

A = Path (Existing)
B, C are Paths which I want to subtract from A. 

When I subtract A-B it results in the Path but it gets inserted into the Canvas. 
Then I have to call remove A and insert the resulting Path Again. 
Something Like as shown Below. 

var newCounter = counter.subtract(drawCutouts.children[0]);
counter.remove();
counter.addChild(newCounter);

But I have to actually subtract Multiple Items. 
And I don't want Loop this Remove and Add Cycle. 

Is there any elegant way to do this. Such that I can subtract from the  Current Path. 
And also I don't have to add and remove multiple times. 

Thanks 
Shishir. 

Samuel ASENSI

unread,
Mar 5, 2021, 1:16:52 AMMar 5
to pap...@googlegroups.com
Hi,

You can set the boolean operation option `insert` to false to avoid the result from being inserted in the scene graph.
Then, you can chain your calls like shown in this sketch.

const circle1 = new Path.Circle({
    center: view.center,
    radius: 50,
    strokeColor: 'orange'
});
const circle2 = circle1.clone().translate(10, 50);
const circle3 = circle1.clone().translate(50, 10);

const subtraction = circle1
    .subtract(circle2, { insert: false })
    .subtract(circle3, { insert: false });
subtraction.addTo(project.activeLayer);
subtraction.selected = true;

project.activeLayer.fitBounds(view.bounds.scale(0.8));

--
You received this message because you are subscribed to the Google Groups "Paper.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to paperjs+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/paperjs/6008ba25-c7a7-4077-a47a-08dc2fbb9141n%40googlegroups.com.


--
    Samuel ASENSI

shishir raven

unread,
Mar 8, 2021, 10:29:29 AMMar 8
to pap...@googlegroups.com
Thanks Samuel for pointing me in the right direction. 

Really appreciate that you took time to help me out.👍👍

One small issue was that I cannot chain as the paths I want to subtract are in an Array. 

And so how to chain it is kind of difficult as Array has dynamic number of elements. 

Was hoping if there was something that would subtract and mutate the element itself rather than returning the result. 

But none the less, I learned how to insert back the item when insert = false.

Thanks once again for your help,
Shishir







Reply all
Reply to author
Forward
0 new messages