加载应用…
云澜 Clyune Clyune Component Gallery · 中文

Modular Arithmetic · How it works & code

Residue classes of integers modulo m.

Runtime logic

Visiting `/math/modular`, DemoHost lazy-loads `demos/kits/kit-math.js` (registry `mod: 'kit-math'`) and calls `mount(host, { variant: 'modular' })`. mount resolves the variant to family `modular`; `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.beginPath() — Starts a new path; ctx.arc() — Adds a circular arc (center, radius, start/end angle); ctx.fill() — Fills the current path; ctx.fillText() — Draws filled text; ctx.stroke() — Strokes the current path

Source code

frontend/src/demos/kits/kit-math.js: FAMILY_CONTROLS['modular'] (L105-L105), onPointerDown() · if (family === 'modular') (L216-L238), hitTargets() · if (family === 'modular') (L504-L513), applyDrag() · if (family === 'modular') (L533-L533), drawFamily() · case 'modular' (L625-L625), MATH_DOCS['modular'] (L295-L303)

Code walkthrough

L105-105: This demo's entry in the `FAMILY_CONTROLS` table (control config). L216-238: Branch for this demo inside `onPointerDown()`: math via `Math.hypot`, `Math.max`, `Math.min`, `Math.round`. L504-513: Branch for this demo inside `hitTargets()`: math via `Math.min`, `Math.max`, `Math.round`, `Math.cos`, `Math.sin`. L533-533: Branch for this demo inside `applyDrag()`. L625-625: `case` branch in `drawFamily()`: this demo's per-frame drawing. L295-303: This demo's entry in the `MATH_DOCS` table (info-panel text: formula/notes/params/steps).

Principles

a = q·m + r,0 ≤ r a ≡ r (mod m) Wraps the whole number around the clock of the m and takes a step to the r grid. The number of multiples with the difference of m is the same.

Open the interactive demo · GitHub