SVG Line Drawing Animation

A path that draws itself from stroke-dashoffset, then reveals a terminal dot. Replay restarts the line.


SVG Line Drawing Animation


An SVG line drawing animation reveals a stroke from its start to its end, the way a pen would. Logos, signatures, route maps, and “how it connects” diagrams all use it. The mechanism is always the same: set stroke-dasharray to the length of the path, set stroke-dashoffset to that same length so the dash sits entirely in the gap, then transition the offset to zero.

The demo draws a single coral curve and fades a dot in at the end of the stroke. “Draw again” resets the offset without a library. For a path that also changes shape, see the SVG shape morphing tutorial. For icons that swap, see morphing SVG icons.

Measure the path

Hard-coding the length breaks the moment you edit the d attribute. Read it from the element.

const path = document.querySelector("#mark");
const length = path.getTotalLength();
path.style.strokeDasharray = String(length);
path.style.strokeDashoffset = String(length);

Force a reflow (path.getBoundingClientRect()) before you set the offset back to zero and turn the transition on. If you set the start value and the end value in the same frame, the browser may skip the transition and the line appears already drawn.

path.style.transition = "none";
path.style.strokeDashoffset = String(length);
path.getBoundingClientRect();
path.style.transition = "stroke-dashoffset 1.6s cubic-bezier(.2,.7,.2,1)";
path.style.strokeDashoffset = "0";

The stroke itself

.draw {
  fill: none;
  stroke: #fb7185;
  stroke-width: 6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

fill: none matters. A filled path that also draws its outline looks like a blob being stroked, which is rarely what a line-drawing search is asking for. Round caps hide the flat end of the dash while it travels. On a path with sharp corners, round joins keep the corner from sticking out as a mitre when the dash arrives.

A dot at the end

The dot is a separate circle at the end coordinate, opacity 0 until the dashoffset transition is nearly done. Timing it with a timeout that matches the 1.6s duration is simple and obvious. If the duration changes, change both numbers, or listen for transitionend on the path and fade the dot there. transitionend is the version that will not drift.

Reduced motion and a failed script

If the user prefers reduced motion, set the offset to 0 immediately and show the dot. The mark should exist as a complete graphic. If JavaScript never runs, the path has no dash array and the full stroke is visible, which is the right failure: you see the logo, you miss the draw.

Multiple paths

Stagger them. Draw the primary mark, then secondary strokes 150–200ms later. Drawing twelve paths at once reads as a scribble. Give each path the same dashoffset treatment and delay the moment you set offset to 0.

Questions about SVG line drawing

How do I animate an SVG line drawing itself?

Set stroke-dasharray and stroke-dashoffset to path.getTotalLength(), then transition stroke-dashoffset to 0. Keep the fill empty and the line cap round.

Why does my SVG appear fully drawn with no animation?

The start and end offsets were applied in one frame, so there was nothing to transition. Clear the transition, set the full offset, read a layout property to flush, then enable the transition and set the offset to 0.

Can I draw a filled logo the same way?

Draw the outline first with a fill of none. If the final mark is filled, fade the fill in after transitionend. Filling during the dash animation paints a half shape that rarely looks intentional.