예제
실용적인 예제와 라이브 데모.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
배지에 들어갈 글자를 입력해 보세요. textPath가 글자를 원 둘레로 휘게 하고, textLength가 정확히 한 바퀴에 맞춰 줍니다.
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() }, }), ), )