Distance & Length · How it works & code
logarithmic comparison of lengths across orders of magnitude.
Runtime logic
Visiting `/convert/length`, DemoHost lazy-loads `demos/kits/kit-convert.js` (registry `mod: 'kit-convert'`) and calls `mount(host, { variant: 'length' })`. mount resolves the variant to family `length`; `withControls` builds the control panel (6 presets + sliders). `createInfoPanel` splits the stage into canvas + info panel; `makeCanvas` creates a HiDPI canvas. Every `requestAnimationFrame`: read controls, `drawFamily` runs this demo’s branch, and the info panel readout refreshes. Changing parameters: colour/speed/size apply live; structural ones remount; leaving the page cancels rAF, removes listeners and frees resources.
Environment
Canvas 2D: 2D canvas drawing; requestAnimationFrame: Display-synced animation loop; ResizeObserver: Canvas follows container size; Pointer Events: Unified mouse/touch/pen events; devicePixelRatio: Crisp HiDPI rendering (capped at 2×) · react 18.3.1, react-router-dom 6.30.6, vite 5.4.21
Dependencies
react 18.3.1
Imported APIs
makeCanvas — Creates an auto-sized canvas; loop — requestAnimationFrame loop (stoppable); bar — Demo control bar; paramsFromSpec — Turns the spec (colour/speed/size/count/extras) into control-panel parameter definitions; readLiveControls — Reads live control values (CSS-variable fallback); localPoint — Event → canvas-local coordinates; withControls — Wraps the demo in the shared control panel (presets, sliders, reset); hot-updates or remounts on change; createInfoPanel — Side/bottom info panel: live readout + formula/notes/params/steps; alignDocParams — Aligns the documented parameters with the actual controls; CONVERT_DOCS — Text table for unit-conversion demos; ctx.fillRect() — Fills a rectangle; ctx.fillText() — Draws filled text; ctx.beginPath() — Starts a new path; ctx.moveTo() — Moves the pen without drawing; ctx.arcTo() — Adds an arc using tangent lines; ctx.closePath() — Closes the current sub-path; ctx.fill() — Fills the current path; ctx.createLinearGradient() — Creates a linear gradient; ctx.arc() — Adds a circular arc (center, radius, start/end angle)
Source code
frontend/src/demos/kits/kit-convert.js: FAMILY_CONTROLS['length'] (L33-L33), drawFamily() · case 'length' (L235-L235), CONVERT_DOCS['length'] (L37-L45)
Code walkthrough
L33-33: This demo's entry in the `FAMILY_CONTROLS` table (control config). L235-235: `case` branch in `drawFamily()`: this demo's per-frame drawing. L37-45: This demo's entry in the `CONVERT_DOCS` table (info-panel text: formula/notes/params/steps).
Principles
1 mile = 1609.344 m,1 ft = 0.3048 m,1 in = 2.54 cm 1 nmi = 1852 m, 1 mile = 500 m The slider is a number of scales: from an agreement 1 m About 500 km, Every move 1/6 Length Zoom In 10 Multiply, easily cross-numeric.