Billiards · How it works & code
Wait for mass elastic collisions and rolling frictions to stop distance v²/2μg。
Runtime logic
Visiting `/physics/billiard`, DemoHost lazy-loads `demos/kits/kit-physics.js` (registry `mod: 'kit-physics'`) and calls `mount(host, { variant: 'billiard' })`. mount resolves the variant to family `billiard`; `withControls` builds the control panel (6 presets + sliders). `createCanvasStage` creates the `<canvas>` (HiDPI ≤ 2×, ResizeObserver) and the info panel. The family’s `build(S)` initialises state and returns the per-frame callback. Every `requestAnimationFrame`: read controls, advance a speed-scaled clock (dt ≤ 50 ms), run the frame callback; returned readouts go to the info panel. 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
withControls — Wraps the demo in the shared control panel (presets, sliders, reset); hot-updates or remounts on change; paramsFromSpec — Turns the spec (colour/speed/size/count/extras) into control-panel parameter definitions; createCanvasStage — Shared Canvas 2D stage: canvas + info panel, HiDPI, one rAF loop, pointer state, idle auto-pointer; rng — Seeded deterministic PRNG (same seed, same result); hexRgb — Colour string → [r, g, b]; rgba — Builds an rgba() colour string; clamp — Clamps a value to a range; TAU — Constant 2π; fmt — Number formatting (fixed decimals); F — Formula block HTML; S.onDown — Pointer down; S.onUp — Pointer up; S.get — Reads current control values; S.clear — Clears with the background colour (translucent = trails); S.onFrame — Registers a per-frame callback; its return value feeds the info panel; ctx.fill() — Fills the current path; ctx.fillRect() — Fills a rectangle; ctx.beginPath() — Starts a new path; ctx.arc() — Adds a circular arc (center, radius, start/end angle); ctx.moveTo() — Moves the pen without drawing; ctx.lineTo() — Adds a straight line to a point; ctx.stroke() — Strokes the current path; ctx.setLineDash() — Sets the dash pattern for strokes; ctx.arcTo() — Adds an arc using tangent lines
Source code
frontend/src/demos/kits/kit-physics.js: FAM.billiard (L602-L648)
Code walkthrough
L604-604: Control definitions (colour/speed/size/count/extras): Table color, ball radius, Ball count, Rolling friction μ, Stroke force, Auto stroke when unattended. L605-605: Preset buttons: name → parameter overrides (6). L606-614: Info-panel content: live rows(v), formula, notes, params, steps. L615-647: Init function: sets up state and returns the per-frame callback: registers pointer handlers `S.onDown`, `S.onUp`; draws with Canvas 2D `fill`, `fillRect`, `beginPath`, `arc`, `moveTo`, `lineTo` …; math via `Math.round`, `Math.min`, `Math.max`, `Math.hypot`, `Math.atan2`, `Math.cos` …; returns readouts `mv`, `n`, `cv`, `sd`, `K`, `mu`, `aim` … to the info panel.
Principles
Mass elastic collisions: exchange speed weights along the method line v₁ₙ ↔ v₂ₙ (e = 0.95) advises Reconstruction: a = μ·g (♪ In the opposite direction ♪ Stop Distance s = v² / (2μg); Back to the cork e = 0.8 Two same-heavy billiards bump into each other Switch speed :打出去的球停下,被撞的球以原速飞走;斜着撞时只交换连心线方向的速度分量,两球大约成 90° Separate. The friction on the table keeps every ball down at constant speed μg 慢下来,所以停止距离与速度的平方成正比:速度翻倍,滚得远 4 Double. Press from the white ball Move back Aim, loose-handed hit; self-activated hit demonstration if not operated. The ball in the bag will be rewinded.