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

Calendar Heatmap · How it works & code

GitHub-style calendar heatmap: graded coloring, active days, and longest streaks

Runtime logic

Visiting `/dataviz/cal-heat`, DemoHost lazy-loads `demos/kits/kit-dataviz.js` (registry `mod: 'kit-dataviz'`) and calls `mount(host, { variant: 'cal-heat' })`. mount resolves the variant to family `cal-heat`; `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]; clamp — Clamps a value to a range; TAU — Constant 2π; 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.beginPath() — Starts a new path; ctx.arc() — Adds a circular arc (center, radius, start/end angle); ctx.stroke() — Strokes the current path; ctx.strokeRect() — Strokes a rectangle; ctx.fillRect() — Fills a rectangle; 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

Source code

frontend/src/demos/kits/kit-dataviz.js: FAM.cal-heat (L256-L294)

Code walkthrough

L257-257: Control definitions (colour/speed/size/count/extras): Activity Color, Grid Gap, Show Week Count, Activity Level, Color Grading Count, Fire Color Scale. L258-258: Preset buttons: name → parameter overrides (6). L259-267: Info-panel content: live rows(v), formula, notes, params, steps. L268-293: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `fill`, `beginPath`, `arc`, `stroke`, `strokeRect`, `fillRect`; math via `Math.sin`, `Math.ceil`, `Math.round`, `Math.floor`, `Math.min`, `Math.max`; returns readouts `wk`, `days`, `date`, `cnt`, `tot`, `ad`, `streak` … to the info panel.

Principles

Cell position: Column = Week, Row = Day of Week Colour Level = ⌈ Number of times / Maximum number × Series ⌉ Maximum duration = maximum number of days > 0 GitHub It's the type of green box that submits the record Calendar Hot Tri Primary :每一列是一周,每一行是星期一到星期日,每个格子是一天,颜色越深当天越活跃。 It's very suitable Customs and cycles :周末是不是总空着?有没有连续很多天坚持?颜色不是连续渐变,而是分成几个 Rank , This small difference does not interfere with judgement. The date will move forward over time"Today", There's a radial circle) and a pointer is suspended for any day.

Open the interactive demo · GitHub