Typography · 32 of 33
Stroke Draw
Text outline draws itself like handwriting (SVG stroke-dasharray)
<svg class="stroke-svg" viewBox="0 0 700 120" width="100%">
<text x="350" y="85" text-anchor="middle" class="stroke-text" id="strokeText">Draw Me</text>
</svg>
.stroke-text {
font-size: 4.5rem;
font-weight: 700;
fill: none;
stroke: #7c5cff;
stroke-width: 1.5;
}
.stroke-text.drawing {
/* Near-linear pen speed — ease-out-expo drew ~90% of the outline in the first
third and then crawled, which reads as a glitch rather than handwriting */
animation: stroke-draw 2.6s cubic-bezier(0.4, 0, 0.5, 1) forwards;
}
@keyframes stroke-draw {
0% { stroke-dashoffset: var(--dash-from, 3300); fill: transparent; }
84% { fill: transparent; }
100% { stroke-dashoffset: var(--dash-to, 1020); fill: #f0f0f5; }
}
var textEl = document.getElementById('strokeText');
// SVG <text> has no getTotalLength(), so the glyph outline perimeter has to be
// estimated. getComputedTextLength() is the *advance width* — not the outline —
// so a small multiplier falls well short of it. When stroke-dasharray is shorter
// than the real outline the pattern REPEATS, so a second dash starts partway
// through and leaves a visible seam mid-glyph.
//
// Model each glyph as its bbox perimeter 2(w + h) times ~1.6 for the inner
// counters that most letters carry: actual ~= 3.2 * (W + N*h)
function measure() {
var W = textEl.getComputedTextLength(); // total advance width
var h = textEl.getBBox().height; // ink height
var N = (textEl.textContent.replace(/\s/g, '') || ' ').length;
var unit = W + N * h;
// `bound` must exceed the true perimeter so the pattern can never repeat.
// `draw` must also exceed it (an undershoot would leave letters unfinished),
// but only slightly, so the reveal spans almost the whole duration.
return { bound: Math.ceil(5.5 * unit), draw: Math.ceil(3.8 * unit) };
}
function play() {
textEl.classList.remove('drawing');
var bound = 3300, draw = 2280; // fallbacks if measurement throws
try {
var m = measure();
bound = m.bound;
draw = m.draw;
} catch (e) {}
textEl.style.strokeDasharray = bound;
// Reveal by walking the offset from `bound` down to `bound - draw` — never to
// 0, which would idle for ~40% of the duration after the outline is complete.
textEl.style.setProperty('--dash-from', bound);
textEl.style.setProperty('--dash-to', bound - draw);
textEl.style.strokeDashoffset = bound;
void textEl.getBoundingClientRect(); // force reflow so the animation restarts
textEl.classList.add('drawing');
}
// Fonts must be loaded before measuring the outline length
if (document.fonts && document.fonts.ready) {
document.fonts.ready.then(play);
} else {
play();
}