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

Add Boolean Operations as WASM Modules to Two.js

Open jonobr1 opened this issue 3 years ago • 6 comments

Is your feature request related to a problem? Please describe. Boolean operations are a common, but computationally expensive and mathematically complicated operations in vector drawing suites.

Describe the solution you'd like For the types of projects Two.js aspires to help make possible, it's important to have these features:

  • Union: Combine two paths into one.
  • Subtraction: Remove one path's form from another's.
  • Intersection: Get the overlapping area of two paths as a new path.
  • Exclusion: Get the non-overlapping area of two paths as a new path.

Essentially these (taken from Adobe Illustrator): Screen Shot 2022-01-12 at 5 53 25 PM

Additional context Paper.js has great examples of these running in JavaScript: http://paperjs.org/examples/boolean-operations/ • It would be prudent to port these to Rust as a separate library and compile them to WASM into Two.js as extras.

jonobr1 avatar Jan 13 '22 01:01 jonobr1

With Boolean Operations implemented it would be possible to resolve issue https://github.com/jonobr1/two.js/issues/585

jonobr1 avatar Jan 28 '22 01:01 jonobr1

This issue would be the first explorations of larger scale WASM use described in issue https://github.com/jonobr1/two.js/issues/552

jonobr1 avatar Jan 28 '22 01:01 jonobr1

This grant seems like a good opportunity to get some resources to achieve this feature: https://developer.chrome.com/blog/advanced-web-apps-fund/

jonobr1 avatar May 09 '22 18:05 jonobr1

Hello. Just found your library. Regarding boolean operations, you should look at lib2geom: https://gitlab.com/inkscape/lib2geom/ . It's written in c++ and is used by Inkspace. Wasm port will be great I suppose.

I think is one of the few libraries that support bezier curves. Most libraries convert them in tiny line segments then apply the boolean operations. Hope this helps you.

From their description:

Exact boolean ops (elliptic arcs remain elliptic arcs)

catapop84 avatar May 27 '22 18:05 catapop84

This is epic, thanks for sharing. I should explore Two.js simply being the rendering output for all this functionality. Would trim down and speed up quite a few operations.

jonobr1 avatar May 29 '22 03:05 jonobr1

@jonobr1 That's the idea. This library just does the geometry processing, leaving the user to do the rendering part. Building this as wasm and keeping separate from twojs is the way to go.

If you're only interested in boolean operations: https://gitlab.com/inkscape/lib2geom/-/blob/7a0d8698d6eb4f33a93c4f7fa895771d34170d54/src/2geom/intersection-graph.cpp

PathVector p1= parse_svg_path("M 0,0 L 5,0 5,8 0,8 Z");
PathVector p2= ...
auto graph = PathIntersectionGraph(p1, p2);

PathVector union = graph.getUnion(); 
PathVector intersection = graph.getIntersection(); 
PathVector aMinB= graph.getAminusB(); 
PathVector bMinA= graph.getBminusA(); 
PathVector xor = graph.getXOR(); 

catapop84 avatar May 29 '22 17:05 catapop84