r/webdev • • Jun 25 '22

Showoff Saturday Wrote a tiny NPM package for interpolating between multiple SVG paths

5.7k Upvotes

94 comments sorted by

View all comments

Show parent comments

2

u/Iklowto Jun 25 '22 edited Jun 25 '22

I think the issue is how you would come up with the normalised/shared form for both shapes. I haven’t tried, but I cannot imagine determining a shared formulation for 5 sequential Bezier curves and 3 straight lines to be simple.

2

u/RGB755 Jun 25 '22

We did something similar for a Uni project where we moved an object along a piece wise function of multiple Bézier curve (segments)across a single interpolation value between 0 and 1. It was a little tricky to determine which particular curve of the piece wise we were in, but I wouldn’t be surprised if it’s a similar problem. If you can calculate the magnitude / length of the full path, I think you can probably normalize it just fine. Then you could just iterate through whichever path has more anchors, and interpolate the equivalent “anchor” on the path with less segments, basically mapping the more segmented path onto the less segmented one.

Who knows though, I didn’t dive into the code, just some random thoughts. :-)

1

u/kythyri Jun 26 '22

Chop them into pieces so that each one has 15 segments. Everything in SVG paths can be turned to cubic beziers with error too small to see (none for lines or quadratic beziers, negligible for arc if you cut it into at least 4 pieces per complete circle).

The hard part is coming up with a rule for where to cut that produces sensible results. Some special cases are more obviously solvable than others (eg, if most segments of one path appear in the other to within error, avoid cutting those).