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

97

u/Iklowto Jun 25 '22

Yeah, unfortunately the library can only morph between variations of the same path. So the sequence of commands must be identical, though command parameters can be whatever you want.

Morning between entirely different paths is possible but extremely difficult, and I remember seeing a couple of large libraries out there that can figure out the math for doing so.

61

u/pookage tired front-end veteren šŸ™ƒ Jun 25 '22

Ah yeah - I came here to see if you'd solved this problem, too, haha. I had this one a few years back where I was wanting to <animate> an SVG with a different number of points in its path for a nice wobble effect with no joy - I ended up doing what you did here and moving existing points around.

Still - great work, regardless!

10

u/Iklowto Jun 25 '22

That’s a super cool effect actually, nice!

7

u/d_k97 Jun 25 '22

Damn looks awesome

0

u/[deleted] Jun 25 '22

[deleted]

4

u/pookage tired front-end veteren šŸ™ƒ Jun 25 '22

haha, faaanx but it's wildly out of date now šŸ¤¦ā€ā™€ļø always the way with portfolio sites, hey...

3

u/[deleted] Jun 25 '22

that looks really cool, custom and professional! Is the wobble on the curvy frame for the ladies portrait? I'm on my phone so maybe it isn't as prominent. Again, looks amazing!

2

u/pookage tired front-end veteren šŸ™ƒ Jun 25 '22

Yeah, exactly - there's a background-image that's using clip-path() with an animated SVG to give it a subtle portal-like appearance. Didn't need to be much - just had to be a little bit unsettling given the subject matter.

2

u/Pamander Jun 25 '22

I love that movie! Beautiful work in the demo!

2

u/HorrorMakesUsHappy Jun 26 '22

lol what are the odds.

I cohost a podcast interviewing people in the horror biz, and came to this sub thinking of asking for help testing an unrelated website I've created, happened to click here and end up checking out your link without having any idea what I was about to find lol

1

u/pookage tired front-end veteren šŸ™ƒ Jun 26 '22

✨it's all connected ✨

2

u/NULL_42 Jun 25 '22

Wow! That’s great work. You’re super talented

4

u/pookage tired front-end veteren šŸ™ƒ Jun 25 '22

haha, cheeeers - honestly, it's 90% about working with a talented designer and treating the design with respect. This one was with the ineffable Flo Pollet and was a whiiile back now...

5

u/royemosby Jun 25 '22

I thought so but I figured I’d ask anyways. It’s nice results though!

1

u/CivilBear5 Jun 25 '22

I don’t know anything about programming, so I can’t appreciate it fully, but this is really cool and impressive!

1

u/RGB755 Jun 25 '22

Maybe I’m thinking too simply here, but couldn’t you normalize the length of both paths before picking the anchor points and then do some kind of weighted interpolation between them? Basically interpolate between two anchor points if the number of anchors are different, and then interpolate between the interpolated anchors.

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).