Similar Triangles · How it works & code
Parallel Transversals Generate Similar Triangles, Ratio Varies with Parameter.
Runtime logic
Visiting `/math/similar-tri`, DemoHost lazy-loads `demos/kits/kit-math.js` (registry `mod: 'kit-math'`) and calls `mount(host, { variant: 'similar-tri' })`. mount resolves the variant to family `similar-tri`; `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.lineTo() — Adds a straight line to a point; ctx.moveTo() — Moves the pen without drawing; ctx.closePath() — Closes the current sub-path; ctx.fill() — Fills the current path; ctx.stroke() — Strokes the current path; ctx.setLineDash() — Sets the dash pattern for strokes; ctx.strokeRect() — Strokes a rectangle
Source code
frontend/src/demos/kits/kit-math.js: FAMILY_CONTROLS['similar-tri'] (L75-L78), mountLegacy() · if (family === 'similar-tri') (L303-L306), hitTargets() · if (family === 'similar-tri') (L463-L468), applyDrag() · if (family === 'similar-tri') (L542-L550), drawFamily() · case 'similar-tri' (L598-L598), MATH_DOCS['similar-tri'] (L23-L31)
Code walkthrough
L75-78: This demo's entry in the `FAMILY_CONTROLS` table (control config). L303-306: Branch for this demo inside `mountLegacy()`: math via `Math.round`, `Math.max`, `Math.min`. L463-468: Branch for this demo inside `hitTargets()`. L542-550: Branch for this demo inside `applyDrag()`: math via `Math.min`, `Math.max`. L598-598: `case` branch in `drawFamily()`: this demo's per-frame drawing. L23-31: This demo's entry in the `MATH_DOCS` table (info-panel text: formula/notes/params/steps).
Principles
△CPQ ∼ △CAB k = PQ AB = CP CA = 1 − t Area ratio = k² A small triangle that crosses a line parallel to one side is similar to the original triangle (AA determines: Public corner C + equal). Corresponds to the ratio of the edge, and the area ratio is the square of a similar ratio.