Examples
Practical examples. Live demos.
Pick an example to open it full screen, with the live preview and its source side by side.
Pick an example to open it full screen, with the live preview and its source side by side.
Type a badge. textPath bends the text around a circle and textLength fits it to exactly one lap.
import 'nuclo' let text = 'Nuclo • SVG • Builders' let radius = 66 let spinning = true let guide = false // A circle as a path: from the top, clockwise, so the letters // stand on its outside. const ring = () => `M100,${100 - radius} a${radius},${radius} 0 1,1 0,${radius * 2} ` + `a${radius},${radius} 0 1,1 0,${-radius * 2}` // Repeat the text until it is long enough, size the font to it, // and textLength spreads it over exactly one lap. const length = () => 2 * Math.PI * radius const words = () => { const unit = `${text.trim() || 'Nuclo'} • ` return unit.repeat(Math.max(1, Math.floor(length() / (unit.length * 11)))) } const spin = keyframes({ to: { transform: 'rotate(360deg)' } }) export const CircularText = () => div( svgSvg( { viewBox: '0 0 200 200', width: '200', height: '200', role: 'img', 'aria-label': () => `Badge: ${text}`, }, defsSvg(pathSvg({ id: 'ring', d: ring })), circleSvg({ cx: 100, cy: 100, r: () => radius - 24, fill: '#ff3f00' }), textSvg( { x: 100, y: 100, fill: 'white', 'font-weight': 800, 'text-anchor': 'middle', 'dominant-baseline': 'central', 'font-size': () => Math.round(radius * 0.8), }, () => (text.trim()[0] ?? 'N').toUpperCase(), ), gSvg( { style: () => ({ transformOrigin: '100px 100px', animation: `${spin} 18s linear infinite`, animationPlayState: spinning ? 'running' : 'paused', }), }, // the path itself, shown on request pathSvg({ d: ring, fill: 'none', stroke: '#ccc', 'stroke-dasharray': '3 4', opacity: () => (guide ? 1 : 0), }), textSvg( { 'font-weight': 700, 'font-size': () => Math.min(20, length() / (words().length * 0.64)), }, textPathSvg( { href: '#ring', textLength: () => length() - 4, lengthAdjust: 'spacing' }, words, ), ), ), ), input({ value: text, maxLength: 40, 'aria-label': 'Badge text', onInput: (e) => { text = e.currentTarget.value; update() }, }), label( span('Radius'), input({ type: 'range', min: '48', max: '80', step: '1', value: () => String(radius), onInput: (e) => { radius = Number(e.currentTarget.value); update() }, }), span(() => String(radius)), ), div( { style: { display: 'flex', gap: '8px' } }, button(() => (spinning ? 'Pause' : 'Spin'), { onClick: () => { spinning = !spinning; update() }, }), button(() => (guide ? 'Hide path' : 'Show path'), { 'aria-pressed': () => String(guide), onClick: () => { guide = !guide; update() }, }), ), )