Skip to content
All articles

SVG Line Drawings That Draw In Evenly

5 min read

Every article on this site opens with a line drawing that draws itself in. The technique is the standard one: give each stroke a dash as long as the stroke, offset it by the same amount so the stroke is hidden, then animate the offset to zero.

It worked, in the sense that things appeared. It also looked wrong in a way that took a while to name: long lines crawled in, and every small mark — a tick, an arrowhead, a dot — popped into existence in the last moment of the animation. The cause was one number shared by every stroke.

One dash length for strokes of every length

The rule applied to every path, line and circle in a drawing was effectively this:

.art-scene path {
  stroke-dasharray: 1200;
  animation: draw 1.5s both;
}

@keyframes draw {
  from { stroke-dashoffset: 1200; }
  to   { stroke-dashoffset: 0; }
}

1200 was chosen because it is longer than any stroke in a 1200-wide drawing, so everything starts hidden. But the offset only reveals a stroke once it drops below that stroke's own length. A line 1000 units long starts appearing almost immediately. An arrowhead 30 units long stays invisible until the offset falls below 30 — the last 2.5% of the animation — and then appears all at once.

So the timing of every element was decided by its length rather than by anything anyone chose. The long structural lines, which are the least interesting part of a drawing, got the whole animation. The details got a frame.

pathLength makes every stroke length 1

SVG has an attribute for exactly this. pathLength tells the browser to treat the element's length as whatever number you give it, and scale dash and offset values accordingly. Set it to 1 on every stroke and a dash of 1 always covers the whole stroke, however long it really is:

<path d="..." pathLength="1" />

@keyframes draw {
  from { stroke-dasharray: 1; stroke-dashoffset: 1; }
  to   { stroke-dasharray: 1; stroke-dashoffset: 0; }
}

Now every stroke draws over the full duration, a short tick at the same rate of progress as a long rule. It works on lines, circles, ellipses and rects as well as paths.

Three details that matter

Put the dash in the keyframes, not on the element, and fill backwards. If stroke-dasharray is set on the element permanently, a finished drawing is still a dashed line with one dash and one gap, one rounding error away from a visible seam. Declaring it only inside the keyframes and using animation-fill-mode: backwards means it applies before and during the animation and is gone afterwards. The resting drawing is plain strokes.

Exempt strokes that are meant to be dashed. A deliberately dotted line with strokeDasharray="8 9" would have its pattern rescaled by pathLength too — 8 units of a length-1 path is the whole thing. Those strokes skip pathLength and fade in instead.

Hide the start cap. With round line caps, a stroke at full offset still shows its cap as a dot at the start point. A short opacity fade at the start of each stroke's animation hides it until the stroke actually begins.

Then choose the order on purpose

Once timing no longer comes from length, you can decide it. The drawings here use three stroke roles already — faint context, normal structure, and one accent for the thing the drawing is about — so the delays follow those roles: context first, then structure, then the accent last, where the eye lands.

That is a few lines of CSS keyed on the stroke colour each role already uses. The drawing now builds in the order you would explain it, instead of the order its geometry happens to imply.

One unrelated bug the rewrite found

The old selector listed path, line, polyline, circle, ellipse — no rect. Every rectangle in every drawing had been appearing instantly while the rest animated around it, and because the effect is brief nobody had looked closely enough to see it. Rewriting the rule meant reading the selector, which is how it turned up.

All of it is CSS and one attribute per stroke, gated behind prefers-reduced-motion like the rest of the site's motion. No library, no measuring path lengths in JavaScript, and every stroke in every drawing now draws the same way.