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

204

u/Iklowto Jun 25 '22

75

u/royemosby Jun 25 '22

Nice! Are there any major restrictions like the svg’s have to have the same number of anchors?

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

→ More replies (9)

5

u/royemosby Jun 25 '22

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

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.

→ More replies (2)
→ More replies (1)

1

u/[deleted] Jun 26 '22

OP you are OP. This is awesome. Good job! I'm curious to see how you did this! Very interesting.

57

u/the--Dude0 Jun 25 '22

My brother was trying to make a animation studio in web and He also made a similar module for dezier curves. How did you handle the cases where the no of vertices are different?

72

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

Very simply by throwing an error, haha.

The library requires that the paths have identical command sequences (but different parameters). Trying to figure out when, where, and how to insert arbitrary SVG commands at a given point in a morph was too complicated and error prone (and slow) for me to try in this library

11

u/[deleted] Jun 25 '22

Ah so that’s how you do it!

24

u/grammatiker Jun 25 '22

How do you do it? That's the neat part - you don't

→ More replies (1)

11

u/MorningPants Jun 25 '22

There’s a Greensock Library that can do that!

15

u/Halkenguard full-stack Jun 25 '22

That honestly sounds like a major pain in the ass to make. Thanks for making a package!

18

u/grumd Jun 25 '22

Actually a pretty simple package. Svgs have to have the same number of elements, and then you take all the parameters of the elements, like position, curve, etc, get them into an array of numbers, and interpolate these numbers, and then draw the same svg shape with the new interpolated numbers.

Interpolating svgs of different structure on the other hand would have been an actual pain in the ass

12

u/Iklowto Jun 25 '22

This is exactly right. The process is relatively simple and requires little computation, as long as the shapes can be drawn from the same commands.

There are some awesome libraries that can morph between arbitrary shapes - I stumbled upon Flubber and Greensock before making this. Unfortunately, I wasn’t able to get intermediate shapes during the morphs, and the morphs required a significant amount of computation (go figure). So sacrificing flexibility for performance as in this library seemed like a reasonable trade-off.

29

u/[deleted] Jun 25 '22

Once it was "there's an app for that", now it's "there's a package for that."

0

u/[deleted] Jun 26 '22

[deleted]

4

u/Soul_and_Syrup Jun 26 '22

What do you mean?

4

u/devdoggie Jun 26 '22

They are after you, buddy

1

u/[deleted] Jul 01 '22

you are doing things immensely wrong if that's happening to you lmao

10

u/mattsowa Jun 25 '22

It's morpin time

19

u/mr-poopy-butthole-_ Jun 25 '22

This looks awesome

6

u/Evla03 Jun 25 '22

I have wanted this for so long!

2

u/lord_070 Jun 25 '22

What will u do of this.... What's the use of it ??

10

u/Evla03 Jun 25 '22

For example you could make a really cool spinner/loading animation

2

u/lord_070 Jun 25 '22

Can you share some website which uses this .... I haven't seen any of them yet 😅

17

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

Much of the inspiration comes from Robb Owen's website:

https://robbowen.digital/

On his page, the face is animated "manually", i.e. he calculates how to reposition, rotate, and transform each path individually depending on the mouse position.

This library allows you do create similar interactive designs, but you only have to draw out the ending positions (for example, a face looking in different directions). The library will then figure out how to interpolate between each position without you having to define the movements manually.

As an example, in the post video, the face starts looking directly forward, but I never drew that version of the face - I only drew the four corner positions.

6

u/Sphism Jun 25 '22

Nice. I miss the old days of flash shape tweening. So it's nice to see an svg version. Great demo too.

3

u/giiiiiiiiiiiiii Jun 25 '22

Thats sick!!! Would love this as a plugin in after effects

2

u/CodoneMastr Jun 25 '22

That's impressive. I will be taking web dev courses next month . I am a beginner, anything that should be aware of

2

u/Avocado_baguette Jun 25 '22

As a designer I can tell you this opens up a world to us.

2

u/geeknintrovert Jun 25 '22

This SVG morphing reminded me of the Metamask wallet 'fox' icon. That was my first experiencing something like this. I was stunned and curious!

2

u/YokaiCode Jun 26 '22

I don't have a use for this, but very creative and cool idea. I'm sure there's someone out there thankful for this niche!

1

u/atocanist Jun 25 '22

Extremely cool.

1

u/ajayadav09 Jun 25 '22

Awesome stuff bro

1

u/omeksioglu Jun 25 '22

Wow wow wow amazing work

1

u/mmmaaaatttt Jun 25 '22

Brilliant!

1

u/SpongeCake11 Jun 25 '22

That's pretty cool.

1

u/MisterAngstrom Jun 25 '22

Ooh that is neat

1

u/json_derulo_ Jun 25 '22

Super cool!

1

u/apianbellYT Jun 25 '22

That would be pretty useful for something like head tracking

1

u/p44v9n Jun 25 '22

Thanks for sharing this! So excited to work out how to implement this with the line drawing portrait I've got on paavandesign.com

1

u/NULL_42 Jun 25 '22

This is so cool! Great work!

1

u/yeso126 Jun 25 '22

Mmmmmm it would be interesting using this to create animations

1

u/Numzane Jun 25 '22

Does each image have to have the same amount of curves / nodes?

1

u/GustaviNovo Jun 25 '22

Nice work! When it's in the middle it looks like MJ

1

u/[deleted] Jun 25 '22

This is particularly awesome.

1

u/revrenlove full-stack Jun 25 '22

That's rad!

1

u/IBN_E_KHAN Jun 25 '22

This is so Cool man!

1

u/Imaginary_Sweet2710 Jun 25 '22

woah!! this is so cool.

1

u/[deleted] Jun 26 '22

Slow down egg head. What did you make the squiggles do?

1

u/No-Yogurtcloset4626 Jun 26 '22

thats bizarre and revolutionary

1

u/[deleted] Jun 26 '22

This is bloody amazing. I could see this being used in 3D spaces

1

u/Grouchy_Molasses7001 Jun 26 '22

Tot the arrow was fly moving on my screen

1

u/nedhaskins Jun 27 '22

This is great! I'm looking to get more expertise in SVGs and will definitely dig into this. Great work.

1

u/slurnomatch Jul 14 '22

Looks great

1

u/teddblue Oct 23 '22

Trippy, I love it!

1

u/studiooriley Jan 25 '23

This is brilliant.

0

u/[deleted] Jun 25 '22

[deleted]

16

u/Iklowto Jun 25 '22

An SVG path is made up of a series of commands, e.g.

<svg>
  <path d="M 10 10 H 90 V 90 H 10 L 10 10"/>
</svg>

... where "M 10 10 H 90 V 90 H 10 L 10 10" is the path string.

The M, H, V, L letters are commands, and the numbers following them are the command parameters:

  1. M 10 10 means "move to pixels X=10, Y=10"

  2. H 90 means "draw a horizontal line from here, 90 pixels to the right"

  3. V 90 means "draw a vertical line from here, 90 pixels down"

  4. L 10 10 means "draw a line from here, moving 10 pixels to the right and 10 pixels down"

The `svg-path-morph` library takes all your SVG paths, extracts their command parameters, and builds an "average" path. It then takes each path and calculates the difference between its parameter values and the average parameter values.

With that, we can easily interpolate between shapes by adding each paths difference to the average, weighted by a value between 0 and 1 determining how close to the target path we want the interpolation to be.

2

u/[deleted] Jun 25 '22

[removed] — view removed comment

2

u/Iklowto Jun 25 '22

As long as your polygons are drawn by the same sequence of commands, yes. So if your morphing between a triangle and a square, your triangle path would need two vertices to overlap.

1

u/grumd Jun 25 '22

Does it only work with <path>? What about <rect>? Does it interpolate colors and other stuff that are not the path commands?

2

u/Iklowto Jun 25 '22

Strictly, input and output is the d attribute of an SVG subelement - so only command parameters are processed. I don’t know if <rect> events have d attributes, but if they do, it should work!

1

u/brocococonut Jun 26 '22

Hmmm, it should be easy enough to add them by interpolating other attributes. I can have a go at a PR for them when I'm free if you'd like 😊

2

u/Iklowto Jun 26 '22

That would be awesome!