Magnetic Points · How it works & code
Magnet Array Displays Superimposed Fields and Field Lines of Multiple Poles
Runtime logic
Visiting `/physics/magnet`, DemoHost lazy-loads `demos/kits/kit-physics.js` (registry `mod: 'kit-physics'`) and calls `mount(host, { variant: 'magnet' })`. mount resolves the variant to family `magnet`; `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. 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.clear — Clears with the background colour (translucent = trails); S.get — Reads current control values; S.onFrame — Registers a per-frame callback; its return value feeds the info panel; 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.fillRect() — Fills a rectangle; ctx.arc() — Adds a circular arc (center, radius, start/end angle); ctx.fill() — Fills the current path; ctx.fillText() — Draws filled text
Source code
frontend/src/demos/kits/kit-physics.js: FAM.magnet (L521-L556)
Code walkthrough
L523-523: Control definitions (colour/speed/size/count/extras): Magnet Needle Color, Magnet Needle Length, Pole Count, Magnetic Field Strength, Needle Same Pole (Repulsion), Show Tracing Particles. L524-524: Preset buttons: name → parameter overrides (6). L525-533: Info-panel content: live rows(v), formula, notes, params, steps. L534-555: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `beginPath`, `moveTo`, `lineTo`, `stroke`, `fillRect`, `arc` …; math via `Math.round`, `Math.cos`, `Math.sin`, `Math.hypot`, `Math.max`, `Math.atan2`; returns readouts `k`, `B`, `ang`, `Bmax`, `str`, `rep`, `np` to the info panel.
Principles
Two-dimensional magnetic charge: B(p) = Σi qi · (p − cᵢ) / |p − cᵢ|² Magnetic pin orientation = B̂ = B / |B|, Length/Brightness ∝ log(1 + |B|) Trace particles: p ← p + v₀·B̂·Δt (Follow the line) 小磁针(指南针)总会转到和当地磁场平行的方向。画面里布满磁针,就能“See”Invisible magnetic field. Each magnetic pole produces a magnetic field by distance decay N Extreme (red) outward, S The extreme (blue) closes in, multiple magnetic poles directly Vector Plus . The trace particles follow the magnetic field Magnetic Thread 。 指针本身也是一个磁极,移动它看磁针集体转向;“The same”The switch turns the pointer into a match N Proximity.