svg-path-morphing
SVG Path Morphing
When to use this
- You need to animate one SVG shape smoothly transforming into another (icon morph, logo reveal, blob transition).
- The two paths have different point counts, segment types, or winding directions and naive interpolation produces spaghetti.
- You want to morph between a circle and an arbitrary silhouette, or between two complex outlines.
- You need to control morph timing, direction matching, and intermediate shape aesthetics.
- Do NOT use this when the shapes are simple enough that CSS
d:property interpolation works (same point count, same segment types) -- direct CSS transition is simpler.
Mental model
SVG paths are strings of drawing commands: M (move), L (line), C (cubic bezier), Q (quad bezier), A (arc), Z (close). To interpolate between two paths, you need to interpolate the numeric parameters of each command.
The problem: this only works if both paths have the exact same sequence of command types and the same number of points. A circle (M...A...A...Z) and a star (M...L...L...L...Z) have incompatible command sequences. Naively interpolating their d strings produces garbage -- points jump between unrelated segments, shapes collapse to lines or explode outward.
The solution is point-count normalization: convert both paths to the same number of points, using the same command type (usually all cubic beziers, since any SVG command can be approximated by cubics). Then interpolate point-by-point.
The second problem is correspondence: which point on shape A should map to which point on shape B? Poor correspondence means the shape turns inside out during the morph. Good correspondence preserves visual features -- a corner maps to a corner, a bump maps to a bump.
The third problem is winding direction: if shape A goes clockwise and shape B goes counterclockwise, the morph will twist the shape like wringing a towel. Both paths must wind the same direction.