예제
실용적인 예제와 라이브 데모.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
stroke-dashoffset으로 차오르는 호와 CSS transform으로 돌아가는 바늘입니다. 둘 다 숫자 하나에서 계산됩니다.
import 'nuclo' let value = 62 const ARC = Math.PI * 70 // length of the half circle const set = (next: number) => { value = Math.max(0, Math.min(100, next)) update() } // The arc is a dash as long as the half circle; the offset hides // the part past the value. The needle is a group whose CSS // transform rotates it around the pivot. export const Gauge = () => div( svgSvg( { viewBox: '0 0 200 150', width: '200', height: '150' }, pathSvg({ d: 'M30,110 A70,70 0 0 1 170,110', fill: 'none', stroke: '#eee', 'stroke-width': 14, 'stroke-linecap': 'round', }), pathSvg({ d: 'M30,110 A70,70 0 0 1 170,110', fill: 'none', stroke: '#ff3f00', 'stroke-width': 14, 'stroke-linecap': 'round', 'stroke-dasharray': `${ARC} ${ARC}`, style: { transition: 'stroke-dashoffset 0.5s' }, 'stroke-dashoffset': () => ARC * (1 - value / 100), }), gSvg( { style: () => ({ transition: 'transform 0.5s', transformOrigin: '100px 110px', transform: `rotate(${-90 + value * 1.8}deg)`, }), }, lineSvg({ x1: 100, y1: 110, x2: 100, y2: 54, stroke: '#222', 'stroke-width': 3, 'stroke-linecap': 'round', }), circleSvg({ cx: 100, cy: 110, r: 6, fill: '#fff', stroke: '#ff3f00', 'stroke-width': 3, }), ), textSvg( { x: 100, y: 144, 'text-anchor': 'middle', 'font-size': 22 }, () => `${value}%`, ), ), button('−10', { onClick: () => set(value - 10) }), button('Random', { onClick: () => set(Math.round(Math.random() * 100)) }), button('+10', { onClick: () => set(value + 10) }), )