예제
실용적인 예제와 라이브 데모.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
직접 만든 슬라이더입니다. 포인터의 각도가 값을 정하고, 방향키로 한 단계씩 움직이며, pathLength="100"이 값을 선의 길이로 바꿔 줍니다.
import 'nuclo' let value = 40 const set = (next: number) => { value = Math.round(Math.max(0, Math.min(100, next))) update() // refresh everything that reads `value` } // 0..100 maps to -135°..135°, clockwise from 12 o'clock. const polar = (deg: number, r: number) => { const rad = (deg * Math.PI) / 180 return `${100 + r * Math.sin(rad)},${100 - r * Math.cos(rad)}` } const ARC = `M${polar(-135, 76)} A76,76 0 1 1 ${polar(135, 76)}` // The pointer's angle around the center is the value. function turn(e: PointerEvent) { if (!e.buttons) return const box = (e.currentTarget as Element).getBoundingClientRect() const x = e.clientX - box.left - box.width / 2 const y = e.clientY - box.top - box.height / 2 let deg = (Math.atan2(x, -y) * 180) / Math.PI if (Math.abs(deg) > 135) deg = value > 50 ? 135 : -135 // in the gap: stay at the nearer end set((deg + 135) / 2.7) } const KEYS: Record<string, number> = { ArrowUp: 1, ArrowRight: 1, ArrowDown: -1, ArrowLeft: -1, PageUp: 10, PageDown: -10, Home: -100, End: 100, } const caption = { fontSize: '0.8125rem', color: '#8b949e' } // pathLength="100" makes the arc 100 units long, whatever its // real length, so the value is the dash length as it is. export const Knob = () => div( { style: { display: 'flex', flexDirection: 'column', alignItems: 'center', gap: '12px' } }, svgSvg( { viewBox: '0 0 200 200', width: '200', height: '200', tabindex: '0', role: 'slider', 'aria-label': 'Volume', 'aria-valuemin': '0', 'aria-valuemax': '100', 'aria-valuenow': () => String(value), style: { touchAction: 'none', cursor: 'grab', borderRadius: '50%', outline: 'none' }, onPointerDown: (e) => { e.currentTarget.setPointerCapture(e.pointerId) turn(e) }, onPointerMove: turn, onKeyDown: (e) => { if (!(e.key in KEYS)) return e.preventDefault() set(value + KEYS[e.key]) }, }, // 27 ticks light up as the value passes them ...Array.from({ length: 27 }, (_, i) => lineSvg({ x1: 100, y1: 4, x2: 100, y2: 11, 'stroke-width': 2.5, 'stroke-linecap': 'round', transform: `rotate(${-135 + (i * 270) / 26} 100 100)`, style: () => ({ stroke: (i / 26) * 100 <= value ? '#ff3f00' : '#ddd', transition: 'stroke 0.15s ease', }), }), ), pathSvg({ d: ARC, pathLength: '100', fill: 'none', stroke: '#eee', 'stroke-width': 7, 'stroke-linecap': 'round', }), pathSvg({ d: ARC, pathLength: '100', fill: 'none', stroke: '#ff3f00', 'stroke-width': 7, 'stroke-linecap': 'round', 'stroke-dasharray': () => `${value} 100`, style: { transition: 'stroke-dasharray 0.12s ease-out' }, }), circleSvg({ cx: 100, cy: 100, r: 58, fill: '#fff', stroke: '#ddd', style: { filter: 'drop-shadow(0 8px 14px rgba(0,0,0,0.22))' }, }), gSvg( { style: () => ({ transform: `rotate(${-135 + value * 2.7}deg)`, transformOrigin: '100px 100px', transition: 'transform 0.12s ease-out', }), }, lineSvg({ x1: 100, y1: 50, x2: 100, y2: 64, stroke: '#ff3f00', 'stroke-width': 4, 'stroke-linecap': 'round', }), ), textSvg( { x: 100, y: 112, 'text-anchor': 'middle', 'font-size': 30, 'font-weight': 700 }, () => String(value), ), textSvg( { x: 100, y: 130, 'text-anchor': 'middle', 'font-size': 10, fill: '#8b949e' }, 'VOLUME', ), ), div( { style: { display: 'flex', gap: '8px' } }, button('Mute', { onClick: () => set(0) }), button('Half', { onClick: () => set(50) }), button('Max', { onClick: () => set(100) }), ), div({ style: caption }, 'drag it, or focus it and use the arrow keys'), )