Heatmap · How it works & code
Grid data from Gaussian hotspot overlay, γ contrast with cool-warm color palette.
Runtime logic
Visiting `/dataviz/heatmap`, DemoHost lazy-loads `demos/kits/kit-dataviz.js` (registry `mod: 'kit-dataviz'`) and calls `mount(host, { variant: 'heatmap' })`. mount resolves the variant to family `heatmap`; `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; hexRgb — Colour string → [r, g, b]; hueOf — Hue of a colour (0–360°); clamp — Clamps a value to a range; 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.fill() — Fills the current path; ctx.fillRect() — Fills a rectangle; ctx.strokeRect() — Strokes a rectangle; ctx.beginPath() — Starts a new path; ctx.moveTo() — Moves the pen without drawing; ctx.arcTo() — Adds an arc using tangent lines; ctx.closePath() — Closes the current sub-path; ctx.fillText() — Draws filled text; ctx.measureText() — Measures text width; ctx.stroke() — Strokes the current path
Source code
frontend/src/demos/kits/kit-dataviz.js: FAM.heatmap (L70-L114)
Code walkthrough
L72-72: Control definitions (colour/speed/size/count/extras): Color for High Values, Grid Gap, Grid Resolution N, Contrast γ, Number of Hotspots, Cool-Warm Dual Color. L73-73: Preset buttons: name → parameter overrides (6). L74-82: Info-panel content: live rows(v), formula, notes, params, steps. L83-113: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `fill`, `fillRect`, `strokeRect`; math via `Math.round`, `Math.cos`, `Math.sin`, `Math.exp`, `Math.min`, `Math.max` …; returns readouts `n`, `cell`, `val`, `raw`, `rmn`, `rmx`, `rm` … to the info panel.
Principles
值 v(x, y) = Σₖ exp(−dₖ² / 2σ²) (One Gauss in each hot spot"Hill") Normalization u = (v-min) / (max − min) Colour = tape (u^ga) Heatmap Draw every number in a two-dimensional table into a small square Dark color 代替数字本身:越亮的格子数值越大。人眼对颜色比对数字敏感得多,一眼就能看出"Hot spot"Where. 这里的数据来自几个慢慢移动的热点,每个热点对周围格子的贡献随距离按高斯曲线衰减。先把所有值缩放到 0~1,Again γ Contrast:γ>1 Only the hottest place is brightγ<1 The median will also be brightened. Mouse/手指移到哪个格子,右侧就显示它的行列、数值以及这一行、这一列的平均值(十字高亮)。