30 / blog / deep diveJune 20266 min read

Scroll-driven SVG path morphing

The morphTo option lets you interpolate a SVG path's d attribute from its original shape to a target shape — all driven by scroll position. No GSAP MorphSVGPlugin, no extra import, zero extra dependencies.

  • Deep Dive
  • morphTo
  • SVG paths
  • no MorphSVG
Extra dependencies
0
Vs GSAP MorphSVG
built in
Works with
<path>
Triggers on
scroll

t = 0Source d

t = 0.5morphPath()

t = 1morphTo

sourceM1010L9010L9090L1090Z
t = 0.5M3010L9030L7090L1070Z
morphToM5010L9050L5090L1050Z

Numbers pair up by positionLetters come from the source only

Fig. 1 — what morphTo does to this post's square → diamond pair: each number moves in a straight line to its partner, and the middle frame is the engine's morphPath() at t = 0.5
01The concept

What is SVG path morphing?

Every SVG <path> has a d attribute — a string of commands that defines its shape. Path morphing means smoothly interpolating between two d strings at a given progress value (0–1).

At progress 0, the path looks like the original shape. At progress 1, it looks like morphTo. Anywhere in between, each numeric coordinate is linearly interpolated — producing a smooth shape transition.

Combined with scroll-driven progress, you get shape changes that respond directly to how far the user has scrolled — without a single line of animation logic.

shape.js8 lines
// A circle → square morph driven by scroll
scrollDraw('#shape', {
  morphTo: 'M10 10 L90 10 L90 90 L10 90 Z',  // target: a square
  easing: 'ease-in-out',
  trigger: { start: 'top 70%', end: 'top 20%' },
});
// The path starts as its original shape (e.g. a circle)
// and morphs into the square as the user scrolls down
02Under the hood

How svg-scroll-draw morphs paths

The engine extracts all numeric tokens from both the original and target d strings, then linearly interpolates each token pair at the current scroll alpha. The non-numeric parts (command letters like M, L, C) are taken from the original path — they define the shape structure, which must be compatible between the two paths.

morphing (simplified)13 lines
function morphPath(from: string, to: string, t: number): string {
  const toNums = to.match(/[-+]?[\d.]+/g).map(Number);
  let idx = 0;
  return from.replace(/[-+]?[\d.]+/g, (match) => {
    const fromNum = parseFloat(match);
    const toNum   = toNums[idx++] ?? fromNum;
    return String(fromNum + (toNum - fromNum) * t);
  });
}

// At t=0:   returns original path d
// At t=0.5: returns midpoint between original and target
// At t=1:   returns morphTo path d

This runs every animation frame while the element is in view, updating the d attribute directly on the <path> element. The stroke-dashoffset draw animation runs simultaneously — path draws and morphs at the same time.

03The golden rule

Paths must be numerically compatible

The morph works by pairing numeric tokens from the original and target path strings one-for-one. If they have different counts, the extra target tokens are ignored and any missing ones hold their start value, so the morph never quite reaches the target shape. For a perfect morph, both paths should have the same number of numeric tokens. A development build warns in the console when the command letters or the number count differ.

Compatible

snippet4 lines
<!-- Both have 8 numeric tokens -->
<path d="M10 10 L90 10 L90 90 L10 90" />

morphTo: "M50 10 L90 50 L50 90 L10 50"

Square → Diamond: same structure, same token count. Morphs cleanly.

Incompatible

snippet5 lines
<!-- Triangle: 6 tokens -->
<path d="M50 10 L90 90 L10 90 Z" />

<!-- Star: 20 tokens -->
morphTo: "M50 5 L61 35 L95 35 L68 57 ..."

Different command counts — morph will be approximate or look wrong.

Or skip the counting: the SVG morph generator matches two shapes point for point and writes the morphTo call, and the SVG path editor shows each command of a path so you can line them up by hand.

04Use cases

When to reach for morphTo

Icon transitions

Morph a play button into a pause button, a hamburger into an X, or a circle into a checkmark as the user scrolls past a milestone.

icon.js5 lines
scrollDraw('#icon', {
  morphTo: pauseIcon,
  easing: 'spring',
  trigger: { start: 'top 60%', end: 'top 40%' },
});

Data visualisation

Morph a bar chart shape into a line chart shape, or transform a circle chart into its expanded state as the section scrolls into view.

chart.js5 lines
scrollDraw('#chart-shape', {
  morphTo: lineChartPath,
  easing: 'ease-in-out',
  once: true,
});

Blob / organic shapes

Animate background blobs or decorative shapes between two organic forms. Combine with fade for a smooth entrance.

blob.js5 lines
scrollDraw('#blob', {
  morphTo: blobVariant2,
  fade: true,
  easing: 'ease-out',
});

Logo storytelling

Draw the logo as a simple shape, then morph it into its final form as the hero section exits the viewport.

logo.js5 lines
scrollDraw('#logo-path', {
  morphTo: finalLogoPath,
  easing: 'spring',
  trigger: { start: 'top 90%', end: 'top 10%' },
});
05Combining effects

Draw and morph simultaneously

morphTo runs alongside the stroke-dashoffset draw animation — both update on the same scroll alpha. The path traces itself in and transforms its shape at the same time. Combined with fade and strokeColor, this produces effects that feel far more complex than the code suggests.

combined.js11 lines
import { scrollDraw } from 'svg-scroll-draw';

// Path draws in, morphs shape, changes colour, and fades — all at once
scrollDraw('#hero-shape', {
  morphTo:     finalShape,
  strokeColor: ['#ff90e8', '#5865F2'],  // pink → indigo as it draws
  fade:        true,                     // opacity 0 → 1
  easing:      'ease-in-out',
  once:        true,
  trigger:     { start: 'top 80%', end: 'top 20%' },
});

React version

HeroShape.tsx18 lines
import { ScrollDraw } from 'svg-scroll-draw/react';

function HeroShape() {
  return (
    <ScrollDraw
      morphTo={finalShape}
      strokeColor={['#ff90e8', '#5865F2']}
      fade
      easing="ease-in-out"
      once
      trigger={{ start: 'top 80%', end: 'top 20%' }}
    >
      <svg viewBox="0 0 100 100">
        <path d={originalShape} stroke="currentColor" strokeWidth="2" fill="none" />
      </svg>
    </ScrollDraw>
  );
}
06Limitations

What morphTo can't do

  1. 01<path> only morphTo no-ops on <rect>, <circle>, <line>, <polyline>, and other SVG shape elements (a development build warns in the console). Convert them to <path> equivalents first.
  2. 02Non-SVG elements This is SVG path interpolation — it does not work on HTML elements, CSS clip-paths, or canvas. For HTML morphing you still need GSAP or the Web Animations API.
  3. 03Complex structure changes If your paths have fundamentally different command structures (e.g. one uses cubic beziers, the other straight lines), the interpolation will look wrong. Restructure to match.
  4. 04Activates native CSS opt-out morphTo requires JS per-frame updates, so it automatically bypasses the native CSS animation-timeline path. The JS engine draws it frame by frame instead.
07Quick reference

Full API

OptionTypeNotes
morphTostringTarget path d attribute. Must be numerically compatible with the source path.
easingEasingName | fnControls the morph curve, not just the draw. spring and bounce give great shape transitions.
fadebooleanFade in simultaneously with draw + morph.
oncebooleanStay morphed at the target shape after first completion.
trigger{ start, end }Viewport anchors for when the morph starts and ends.
strokeColorstring | [string, string]Interpolate stroke color as the shape morphs.
onComplete() => voidFires when morph reaches 100%. Swap to a static version here if needed.

— / next

Try it in the Playground

Paste any two compatible SVG paths and set morphTo to see it live

npm i svg-scroll-drawPlayground →

Star on GitHubFound this useful? A star helps others find it.

→Parts list

Keep reading

More from the blog