StealThis .dev

SVG Path Drawing

SVG paths that draw themselves on scroll using stroke-dashoffset animated by GSAP ScrollTrigger.

Open in Lab
gsap scrolltrigger svg
Targets: JS HTML

Code

SVG Path Drawing

SVG paths that draw themselves as the user scrolls, using the classic stroke-dashoffset technique — driven by GSAP ScrollTrigger scrubbing.

How it works

  1. Measure each path’s total length with path.getTotalLength()
  2. Set stroke-dasharray and stroke-dashoffset both to that length (path fully hidden)
  3. Animate stroke-dashoffset to 0 — path appears to draw itself
const len = path.getTotalLength();
gsap.set(path, { strokeDasharray: len, strokeDashoffset: len });
gsap.to(path, {
  strokeDashoffset: 0,
  ease: "none",
  scrollTrigger: { trigger: svg, scrub: 1 }
});

Why scrub: 1

Ties the animation progress directly to scroll position — the path draws forward and backward as the user scrolls up and down. The 1 adds a 1-second lag for a smoothing feel.

When to use it

  • Decorative connecting lines between sections
  • Illustrated process/flow diagrams that reveal on scroll
  • Logo draw-on animations