Typography · 04 of 33
Text on Path
Text flows along an SVG curve with textPath, then slides along it as the offset animates. Great for badges, seals and circular labels.
<svg viewBox="0 0 700 200" width="100%">
<path id="textPath" d="M 50 120 Q 350 84 650 120" fill="none" stroke="none"/>
<!-- text-anchor="middle" + startOffset="50%" centres the run on the curve;
left-aligned at 0% it hangs off the start of the path -->
<text class="path-text" text-anchor="middle">
<textPath href="#textPath" startOffset="50%">Text follows the path</textPath>
</text>
</svg>
.path-text {
font-size: 2rem;
font-weight: 600;
fill: #f0f0f5;
}
var pathEl = document.getElementById('textPath');
// viewBox is 0 0 700 200 at 1:1 scale, .path-text is 2rem (32px).
// Glyph ink runs from baseline-capHeight to baseline+descender, so the ink's
// centre sits above the baseline — measured at 11.3px for this face/size.
var BASELINE = 111.3;
var MAX_AMP = 60;
// On a curved path the glyphs rotate with the tangent, which inflates the ink
// box upward in proportion to the arc depth. Measured at 0.146px per unit amp.
var TILT_COMP = 0.146;
// For a quadratic Bezier the mean y over t is (y0 + y1 + y2)/3. Pinning that
// mean (rather than the endpoints) is what keeps the text vertically centred at
// every curve value: raising the control point by `amp` drops the two ends by
// amp/2, leaving the mean unchanged.
// mean = (2*(m + amp/2) + (m - amp))/3 = m
function buildPath(curveVal) {
var amp = (curveVal / 100) * MAX_AMP;
var mean = BASELINE + TILT_COMP * amp;
var cpY = (mean - amp).toFixed(1);
var endY = (mean + amp / 2).toFixed(1);
pathEl.setAttribute('d', 'M 50 ' + endY + ' Q 350 ' + cpY + ' 650 ' + endY);
}
buildPath(40); // curveVal runs -100..100
// Optional: animate the curve by sweeping curveVal
var phase = 0;
function tick() {
phase += 0.02;
buildPath(Math.sin(phase) * 100);
requestAnimationFrame(tick);
}
requestAnimationFrame(tick);