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.
Pointer events on SVG elements. onPointerEnter picks the active point and a tooltip group slides to it.
import 'nuclo' const random = () => ({ x: Math.round(Math.random() * 100), y: Math.round(Math.random() * 100), }) let points = Array.from({ length: 24 }, random) let active: number | null = null const sx = (v: number) => 16 + (v / 100) * 288 const sy = (v: number) => 164 - (v / 100) * 148 // onPointerEnter on a circle sets the active index; the svg's // onPointerLeave clears it. The tooltip is a group whose opacity // and transform read the active point, so it slides between them. export const Scatter = () => div( svgSvg( { viewBox: '0 0 320 180', width: '320', height: '180', onPointerLeave: () => { active = null; update() }, }, lineSvg({ x1: 16, y1: 164, x2: 304, y2: 164, stroke: '#ddd' }), lineSvg({ x1: 16, y1: 16, x2: 16, y2: 164, stroke: '#ddd' }), ...points.map((_, i) => circleSvg({ fill: '#ff3f00', style: { cursor: 'pointer', transition: 'r 0.15s, fill-opacity 0.15s', }, cx: () => sx(points[i].x), cy: () => sy(points[i].y), r: () => (active === i ? 7 : 4.5), 'fill-opacity': () => (active === null || active === i ? 0.9 : 0.35), onPointerEnter: () => { active = i; update() }, }), ), gSvg( { 'pointer-events': 'none', opacity: () => (active === null ? 0 : 1), style: () => { const p = points[active ?? 0] return { transition: 'opacity 0.15s, transform 0.15s', transform: `translate(${sx(p.x)}px, ${sy(p.y) - 16}px)`, } }, }, rectSvg({ x: -30, y: -20, width: 60, height: 20, rx: 5, fill: '#222' }), textSvg( { x: 0, y: -6, 'text-anchor': 'middle', fill: '#fff', 'font-size': 11 }, () => (active === null ? '' : `${points[active].x}, ${points[active].y}`), ), ), ), div( { style: { display: 'flex', gap: '8px', justifyContent: 'center' } }, button('New data', { onClick: () => { points = points.map(random); update() }, }), ), )