Coordinate Geometry · How it works & code
Dynamic illustration of distance, midpoint, and slope between two points in a plane.
Runtime logic
Visiting `/math/coord-geo`, DemoHost lazy-loads `demos/kits/kit-math.js` (registry `mod: 'kit-math'`) and calls `mount(host, { variant: 'coord-geo' })`. mount resolves the variant to family `coord-geo`; `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. Pointer down/drag/move updates the interaction (drag handles, hover readouts); when untouched and auto-demo is on, the pointer follows a Lissajous path. 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; MATH_DOCS — Formula/notes/params text table for math 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.lineTo() — Adds a straight line to a point; ctx.stroke() — Strokes the current path; ctx.closePath() — Closes the current sub-path; ctx.fill() — Fills the current path; ctx.arc() — Adds a circular arc (center, radius, start/end angle); ctx.setLineDash() — Sets the dash pattern for strokes
Source code
frontend/src/demos/kits/kit-math.js: FAMILY_CONTROLS['coord-geo'] (L81-L81), hitTargets() · if (family === 'coord-geo') (L488-L494), applyDrag() · if (family === 'coord-geo') (L572-L579), drawFamily() · case 'coord-geo' (L601-L601), MATH_DOCS['coord-geo'] (L51-L60)
Code walkthrough
L81-81: This demo's entry in the `FAMILY_CONTROLS` table (control config). L488-494: Branch for this demo inside `hitTargets()`. L572-579: Branch for this demo inside `applyDrag()`. L601-601: `case` branch in `drawFamily()`: this demo's per-frame drawing. L51-60: This demo's entry in the `MATH_DOCS` table (info-panel text: formula/notes/params/steps).
Principles
|AB| = √ (x₂−x₁)² + (y₂−y₁)² k = y₂ − y₁ x₂ − x₁ M = ( x₁+x₂ 2 , y₁+y₂ 2 ) 两点距离来自勾股定理(虚线即直角边);斜率是纵向变化比横向变化;直线点斜式 y − y₁ = k(x − x₁)。