SVG Path Drawing
SVG paths that draw themselves on scroll using stroke-dashoffset animated by GSAP ScrollTrigger.
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
- Measure each path’s total length with
path.getTotalLength() - Set
stroke-dasharrayandstroke-dashoffsetboth to that length (path fully hidden) - Animate
stroke-dashoffsetto0— 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