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 draw polylines. list() renders one per stroke, and the stroke in progress grows on every update().
import 'nuclo' type Stroke = { color: string; size: number; points: number[][] } const W = 320 const H = 200 const COLORS = [ ['Orange', '#ff3f00'], ['Blue', '#2563eb'], ['Amber', '#ff8a00'], ['Ink', '#222'], ] const SIZES = [2, 5, 10] let strokes: Stroke[] = [] let color = COLORS[0][1] let size = SIZES[1] let current: Stroke | null = null // The pointer in viewBox units, kept inside the pad: the pointer // is captured, so a stroke can leave the box. const at = (e: PointerEvent) => { const box = (e.currentTarget as Element).getBoundingClientRect() const x = ((e.clientX - box.left) / box.width) * W const y = ((e.clientY - box.top) / box.height) * H return [ Math.round(Math.max(4, Math.min(W - 4, x))), Math.round(Math.max(4, Math.min(H - 4, y))), ] } // A round tool button; the pressed one gets an outline. const tool = (name: string, pressed: () => boolean, onClick: () => void, dot: number, background: string) => button( { 'aria-label': name, 'aria-pressed': () => String(pressed()), style: () => ({ display: 'flex', alignItems: 'center', justifyContent: 'center', width: '36px', height: '36px', padding: '0', borderRadius: '50%', outline: pressed() ? '2px solid currentColor' : 'none', outlineOffset: '2px', }), onClick: () => { onClick(); update() }, }, span({ style: { width: dot + 'px', height: dot + 'px', borderRadius: '50%', background } }), ) // list() renders one polyline per stroke. Its points are a // function, so the stroke being drawn grows on every update(). // Undo is strokes.pop(). export const DrawingPad = () => div( svgSvg( { viewBox: `0 0 ${W} ${H}`, width: '320', height: '200', style: { touchAction: 'none', cursor: 'crosshair' }, onPointerDown: (e) => { e.currentTarget.setPointerCapture(e.pointerId) const p = at(e) current = { color, size, points: [p, p] } // a tap leaves a dot strokes.push(current) update() }, onPointerMove: (e) => { if (!current) return const p = at(e) const last = current.points[current.points.length - 1] if (Math.hypot(p[0] - last[0], p[1] - last[1]) < 1.5) return current.points.push(p) update() }, onPointerUp: () => { current = null }, onPointerCancel: () => { current = null }, }, rectSvg({ x: 0.5, y: 0.5, width: W - 1, height: H - 1, rx: 12, fill: 'none', stroke: '#ddd', }), textSvg( { x: W / 2, y: H / 2 + 4, 'text-anchor': 'middle', 'font-size': 11, fill: '#999', opacity: () => (strokes.length ? 0 : 1), }, 'draw here', ), list(() => strokes, (stroke) => polylineSvg({ points: () => stroke.points.join(' '), fill: 'none', stroke: stroke.color, 'stroke-width': stroke.size, 'stroke-linecap': 'round', 'stroke-linejoin': 'round', }), ), ), div( { style: { display: 'flex', gap: '8px', justifyContent: 'center' } }, ...COLORS.map(([name, c]) => tool(name, () => color === c, () => { color = c }, 20, c)), ...SIZES.map((s) => tool('Size ' + s, () => size === s, () => { size = s }, s + 4, 'currentColor')), ), div( { style: { display: 'flex', gap: '8px', justifyContent: 'center' } }, button('Undo', { onClick: () => { strokes.pop(); update() } }), button('Clear', { onClick: () => { strokes = []; update() } }), ), div(() => `${strokes.length} ${strokes.length === 1 ? 'stroke' : 'strokes'}`), )