Guide · 8 min read
How SVG handwriting animation works, with code.
The effect where a signature writes itself onto a web page is a few lines of CSS acting on an SVG path. This guide explains the trick, shows a working example you can paste into a file, and then covers the parts tutorials usually skip: several strokes in order, lines that vary in width like real ink, and the pitfalls. It is written from building Everstroke’s SVG exporter.
By Atif M. · Updated
The trick: one very long dash
An SVG path can be stroked with a dashed line. The stroke-dasharray property sets the length of the dashes and gaps, and stroke-dashoffset slides the whole dash pattern along the path.
Make the dash exactly as long as the path, and the gap just as long. Now the path is one dash followed by one gap. Offset the pattern by the full length and the gap covers the path: nothing shows. Animate the offset back to zero and the dash slides in from the path’s starting point, which looks exactly like the line being drawn.
A working example
Save this as a .svg file and open it in a browser. It draws a wavy stroke, then an underline.
signature.svg
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 300 100" width="300"
role="img" aria-label="A signature writing itself">
<style>
.stroke {
fill: none;
stroke: #111;
stroke-width: 3;
stroke-linecap: round;
stroke-linejoin: round;
stroke-dasharray: 1; /* one dash, one gap, each the whole path */
stroke-dashoffset: 1; /* start with the gap over the path */
opacity: 0; /* hides the round cap before it starts */
animation: write 1.2s ease-in-out forwards;
}
.second { animation-delay: 1.35s; animation-duration: 0.5s; }
@keyframes write {
from { opacity: 1; }
to { opacity: 1; stroke-dashoffset: 0; }
}
@media (prefers-reduced-motion: reduce) {
.stroke { animation: none; opacity: 1; stroke-dashoffset: 0; }
}
</style>
<path class="stroke" pathLength="1"
d="M20 70 C60 10 90 90 130 40 S200 20 240 60" />
<path class="stroke second" pathLength="1" d="M60 86 L250 80" />
</svg>pathLength saves you from measuring
Older tutorials measure each path with getTotalLength() in JavaScript and write that number into the dash properties. The pathLength attribute makes that unnecessary. Setting pathLength="1" tells the browser to treat the path as one unit long for dashing, whatever its real length, so a dash of 1 and an offset of 1 work for every path.
With no JavaScript, the animation is entirely inside the file. That matters: an SVG shown through an image tag or as a CSS background is not allowed to run scripts, but its own CSS animations still play.
Several strokes, in the order you wrote them
A signature is several strokes, and it only looks handwritten if they play in writing order, each starting where the pen went down. Give every stroke its own path, drawn in the direction it was written, and stagger them with animation-delay: each stroke’s delay is the sum of the durations before it, plus a short pause for the pen lifting.
The forwards fill mode keeps a stroke visible after its animation ends. Before its delay runs out, a stroke would normally sit in its starting state, and with round line caps that state can show a dot at the start of the path. The example hides strokes with opacity until their animation begins.
Tip: Durations should follow length. If every stroke takes the same time, a short crossbar crawls and a long loop races. Make each stroke’s duration proportional to its length, then slow the tight curves a little.
Where the paths come from
This is the hard part. The technique needs centerline paths: one line down the middle of each stroke. A scanned signature that has been auto-traced, or text converted to outlines, gives you the outline of the ink instead: a closed shape around each letter. Animate a dash along that and the edge of the letters gets traced, not the writing.
- Draw the centerlines by hand with the pen tool in Figma, Illustrator or Inkscape, over a photo of the signature. Keep fill off, and draw each stroke in writing direction.
- Use a single-line font if you’re animating text. Ordinary fonts are outlines.
- Record the strokes as they are drawn. A drawing pad captures the pen’s path, direction, order and timing directly, which is what Everstroke does: the paths in its SVG are the strokes you made, in the order and rhythm you made them.
Real ink changes width; a stroke doesn’t
An SVG stroke has one width along its whole length. Handwriting doesn’t: it swells on downstrokes and tapers to a point where the pen leaves the paper. A single uniform stroke looks like a marker. There are two ways around it.
- A mask. Draw the signature as a filled shape with its true varying width, then animate a much thicker dashed centerline as a mask over it. The mask reveals the shape as it travels. The shape stays perfect, but where strokes cross, the fat mask uncovers parts of the neighbouring stroke early.
- Chunks. Split each stroke into short pieces, give each piece its own stroke-width, and animate the pieces one after another with their timing lined up end to end. There is no early reveal at crossings, at the cost of more elements. Everstroke’s exporter does this, starting a new piece every short distance and wherever the width has changed enough to be seen.
Pitfalls worth knowing
- Scaling. Use a viewBox and let the SVG scale; because pathLength is relative, the animation is unaffected by size.
- Looping. For a loop, animate to the drawn state, hold, fade the whole signature out, and restart, all inside one keyframe timeline per stroke that shares a total duration. Separate delays don’t repeat with animation-iteration-count.
- Reduced motion. Some people turn animation off in their system settings. The prefers-reduced-motion query in the example shows the finished signature to them straight away.
- Accessibility. Give the SVG role="img" and a label such as “Signed, Your Name”, or alt text on the image tag.
- Glow. An SVG blur filter gives a soft halo, but filters are costly on large areas; give the filter a region that just covers the signature.
- Email. None of this works in email, where SVG isn’t displayed. Use a GIF there.
Putting it on a page
An image tag is enough, and keeps the SVG cacheable and out of your markup. Set a width; the height follows the viewBox.
HTML
<img src="/signature.svg" alt="Signed, Your Name" width="320">The animation starts when the image loads. To start it when it scrolls into view instead, inline the SVG in the page and add the animation class from an IntersectionObserver, or load the image lazily with loading="lazy" so it begins closer to the moment it is seen.
Questions
Frequently asked questions
How do I animate an SVG path being drawn?
Set stroke-dasharray to the path’s length so the stroke is one long dash, set stroke-dashoffset to the same length to hide it, and animate stroke-dashoffset to 0. With pathLength="1" on the path, both values are simply 1.
Does an animated SVG work in an img tag?
Yes, if the animation is CSS or SMIL inside the SVG file. Scripts inside an SVG don’t run in an image tag, so animations that rely on JavaScript need the SVG inlined in the page.
Why does my SVG animation trace the outline of the letters?
The paths are outlines of the ink, from converted text or an auto-traced scan, rather than centerlines. Redraw each stroke as a single open path down its middle, or use a mask over the filled shape.
Can I do this without writing code?
Yes. Draw your signature and download the animated SVG; the file contains the paths and the CSS described here, with the line width following your pen. It is free.
How do I make the SVG animation loop?
Put the draw, the hold and a fade-out in one keyframe timeline that every stroke shares, and set animation-iteration-count to infinite. Per-stroke delays only apply to the first run.
Skip the keyframes.
Draw your signature and watch it write itself, free