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

Fire · How it works & code

Heat cellular automaton flame.

Runtime logic

Visiting `/canvas/fire`, DemoHost lazy-loads `demos/kits/kit-canvas.js` (registry `mod: 'kit-canvas'`) and calls `mount(host, { variant: 'fire' })`. mount resolves the variant to family `fire`; `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; makeLayer — Offscreen layer (trails / pixel fields); rng — Seeded deterministic PRNG (same seed, same result); hexRgb — Colour string → [r, g, b]; clamp — Clamps a value to a range; fmt — Number formatting (fixed decimals); F — Formula block HTML; S.get — Reads current control values; S.onFrame — Registers a per-frame callback; its return value feeds the info panel; ctx.drawImage() — Draws an image or offscreen canvas

Source code

frontend/src/demos/kits/kit-canvas.js: FAM.fire (L1069-L1108)

Code walkthrough

L1071-1071: Control definitions (colour/speed/size/count/extras): Flame Color, Fire Source Width, Cooling Rate, Wind, Pointer Hotspot. L1072-1072: Preset buttons: name → parameter overrides (6). L1073-1081: Info-panel content: live rows(v), formula, notes, params, steps. L1082-1107: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `drawImage`; math via `Math.min`, `Math.max`, `Math.round`, `Math.floor`, `Math.ceil`; returns readouts `cols`, `rows`, `src`, `cool`, `hgt`, `wind`, `hp` … to the info panel.

Principles

Heat upload: T(x + w, y - 1) = T(x, y) - c · U(0, 1) w = random + wind, c = cooling rate Color = Palette (T): Black → Primary Color → Yellow → White It's classic“DOOM Fire”算法:底部一排格子一直保持最高温度,每一步每个格子把自己的热量传给 Place above The grid, with a random loss. The lower the amount of heat, the more uneven the flames are formed; and horizontal random deviations and wind shake the flames. The higher the cooling rate, the shorter the flame. Temperature maps to palettes: low temperature dark red, high temperature yellow white. The pointer position is also a hot spot.

Open the interactive demo · GitHub