Animations/SVG Path Draw

SVG Path Draw

Un trait SVG se dessine progressivement quand la section entre dans le viewport.

scrollSVGrevealgraph
Trigger: Scroll (IntersectionObserver)
CSS requis: Non
Difficulte: easy

Preview live

Code

"use client";
import { useEffect, useRef, useState } from "react";

export default function PathDraw({ path, viewBox }: { path: string; viewBox: string }) {
  const ref = useRef<HTMLDivElement>(null);
  const [visible, setVisible] = useState(false);

  useEffect(() => {
    const obs = new IntersectionObserver(
      ([e]) => { if (e.isIntersecting) { setVisible(true); obs.disconnect(); } },
      { threshold: 0.3 }
    );
    if (ref.current) obs.observe(ref.current);
    return () => obs.disconnect();
  }, []);

  return (
    <div ref={ref}>
      <svg viewBox={viewBox} fill="none">
        <path d={path} stroke="currentColor" strokeWidth="2"
          pathLength={1} strokeDasharray="1 1"
          strokeDashoffset={visible ? "0" : "1"}
          style={{ transition: "stroke-dashoffset 2s ease-out" }} />
      </svg>
    </div>
  );
}

Instructions

Passez votre path SVG et viewBox en props. Le trait se dessine automatiquement quand la section entre dans le viewport (IntersectionObserver, threshold 0.3). Pas de CSS externe requis.