Self-drawing SVG connection lines

By

Tagged: CSS

Animate SVG connection lines that draw themselves on scroll, with staggered edges, IntersectionObserver, and prefers-reduced-motion fallbacks.

Static lines between boxes look fine. Lines that draw themselves when you scroll into view feel deliberate.

I used this in one of my apps for mini stack diagrams on comparison pages. The markup just has straight hub-and-spoke lines, and CSS plus a tiny script handle the reveal.

Paths between two boxes

For a simple layout, an SVG <line> from center to center works. When boxes sit at different heights and you want a softer curve, use a cubic bezier <path>:

function edgePath(x1, y1, x2, y2) {
  const cx = (x1 + x2) / 2
  return `M ${x1} ${y1} C ${cx} ${y1}, ${cx} ${y2}, ${x2} ${y2}`
}

Compute x1,y1 and x2,y2 from each node’s position plus half its width and height. Store the path string server-side or in a layout helper so Astro can render it once.

The stroke-dashoffset trick

You can’t animate “draw this line” with a single CSS property, so you fake it.

Set both stroke-dasharray and stroke-dashoffset to the path length, then animate the offset down to zero. As the offset shrinks, the visible stroke grows from completely hidden to the full line.

For a <line>, the length is Math.hypot(x2 - x1, y2 - y1). For a <path>, call path.getTotalLength() in the browser or precompute it at build time. Once you have the length, wire it into the stroke setup:

<line
  x1="80" y1="110" x2="200" y2="40"
  style="--edge-len:134;stroke-dasharray:134;stroke-dashoffset:134"
  class="edge-draw-line"
/>

Then animate the offset back to zero:

.edge-draw-run {
  animation: edge-draw-in 0.4s cubic-bezier(0.22, 1, 0.36, 1) both;
}

@keyframes edge-draw-in {
  from { stroke-dashoffset: var(--edge-len) }
  to { stroke-dashoffset: 0 }
}

You’ll see the name cubic bezier again here, except this time it controls the animation easing rather than the path geometry.

Two lines per edge

I render each connection twice:

  1. A draw line uses a solid accent color and runs the draw-in animation once
  2. A base line is dashed gray and stays visible after the draw finishes

When animationend fires on the draw line, hide it and turn the dashed line on. You get a quick “wire being laid” moment, then a calm marching dash forever.

Stagger per edge

Nodes pop in with transition-delay: i * 70ms. Edges should wait until their spoke node appears:

edge.delayMs = nodeDelay + 200 + edgeIndex * 20

Set animation-delay inline when you add the edge-draw-run class, so the hub draws first and the satellites follow in sequence.

Trigger on scroll

Don’t run the animation on page load if the diagram is below the fold. Use IntersectionObserver:

const observer = new IntersectionObserver((entries) => {
  for (const entry of entries) {
    if (entry.isIntersecting) {
      reveal(entry.target)
      observer.unobserve(entry.target)
    }
  }
}, { threshold: 0.25 })

observer.observe(diagramRoot)

reveal() adds a class to the root, activates the nodes, and starts the edge draw-ins. It only runs once per diagram.

prefers-reduced-motion

Some users disable motion in the OS, so respect that setting.

When matchMedia('(prefers-reduced-motion: reduce)') matches:

CSS helps too:

@media (prefers-reduced-motion: reduce) {
  .edge-draw-line { display: none }
  .stack-node { transition: none; opacity: 1 }
}

Readers still see the complete diagram and all its connections, just without the motion.

Want me to talk about your product? You can sponsor this site.

Related posts about css: