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.
Hands are lines whose transform is a function of the time. onMount starts the ticking and returns the 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' }), )