예제
실용적인 예제와 라이브 데모.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
예제를 선택하면 라이브 미리보기와 소스 코드를 나란히 표시하는 전체 화면이 열립니다.
시곗바늘은 시간에 따라 transform이 계산되는 선입니다. onMount가 타이머를 시작하고 cleanup 함수를 반환합니다.
import 'nuclo' let now = new Date() const hours = () => (now.getHours() % 12) * 30 + now.getMinutes() * 0.5 const minutes = () => now.getMinutes() * 6 + now.getSeconds() * 0.1 const seconds = () => now.getSeconds() * 6 // Each hand's transform is a function of now. onMount starts the // interval and returns its cleanup, which runs when the svg is // removed, so the clock stops when it leaves the page. export const Clock = () => svgSvg( { viewBox: '0 0 200 200', width: '200', height: '200', onMount: () => { const id = setInterval(() => { now = new Date(); update() }, 1000) return () => clearInterval(id) }, }, circleSvg({ cx: 100, cy: 100, r: 92, fill: 'none', stroke: '#eee', 'stroke-width': 4 }), ...Array.from({ length: 12 }, (_, i) => lineSvg({ x1: 100, y1: 12, x2: 100, y2: 24, stroke: '#222', 'stroke-width': 2.5, transform: `rotate(${i * 30} 100 100)`, }), ), lineSvg({ x1: 100, y1: 108, x2: 100, y2: 52, stroke: '#222', 'stroke-width': 6, 'stroke-linecap': 'round', transform: () => `rotate(${hours()} 100 100)`, }), lineSvg({ x1: 100, y1: 110, x2: 100, y2: 30, stroke: '#222', 'stroke-width': 4, 'stroke-linecap': 'round', transform: () => `rotate(${minutes()} 100 100)`, }), lineSvg({ x1: 100, y1: 118, x2: 100, y2: 22, stroke: '#ff3f00', 'stroke-width': 1.5, 'stroke-linecap': 'round', transform: () => `rotate(${seconds()} 100 100)`, }), circleSvg({ cx: 100, cy: 100, r: 4, fill: '#ff3f00' }), )