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.
An arc that fills with stroke-dashoffset and a needle that turns with a CSS transform. Both are functions of one number.
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) }), )