Lava · How it works & code
Several blobs float up and down; each blob contributes a 'field' of r²/d². Areas where the total field exceeds a threshold connect into a single region. Blobs merge when they get close.
Runtime logic
Visiting `/webgl/lava`, DemoHost lazy-loads `demos/kits/kit-webgl.js` (registry `mod: 'kit-webgl'`) and calls `mount(host, { variant: 'lava' })`. mount resolves the variant to family `lava`; `withControls` builds the control panel (6 presets + sliders). `createCanvasStage` creates the `<canvas>` (HiDPI ≤ 2×, ResizeObserver) and the info panel. `makeGL` compiles this demo’s GLSL fragment shader on an offscreen WebGL canvas; each frame sets uniforms (u_t, u_m, u_col…), `drawArrays` renders a full-screen quad, then `drawImage` copies it onto the 2D stage. 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; WebGL 1: Offscreen WebGL canvas running a GLSL fragment shader; 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]; 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.onFrame — Registers a per-frame callback; its return value feeds the info panel; ctx.fillText() — Draws filled text; 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.fill() — Fills the current path; ctx.stroke() — Strokes the current path; ctx.drawImage() — Draws an image or offscreen canvas; ctx.fillRect() — Fills a rectangle; gl.createShader() — Creates a vertex/fragment shader; gl.shaderSource() — Sets shader source; gl.compileShader() — Compiles a GLSL shader; gl.getShaderParameter() — Queries shader compile status; gl.getShaderInfoLog() — Reads the shader compile log; gl.createProgram() — Creates a shader program
Source code
frontend/src/demos/kits/kit-webgl.js: FAM.lava (L198-L203)
Code walkthrough
L198-203: Calls the factory `glFam()` to produce the full definition (spec + doc + build) from the options below. L198-198: Title shown in the info and control panels: “Lava light”. L198-198: Range/default of the size slider: Blob size. L198-198: Range/default of the count slider: Number of blobs. L199-199: Extra demo-specific slider/toggle: Emission intensity. L199-199: Preset buttons: name → parameter overrides (4). L200-200: Body of the “Notes” section. L200-200: Main formula of the “Formula” section. L200-200: Second formula line. L201-203: GLSL fragment shader source (runs once per pixel on the GPU); uses uniforms `u_n`, `u_s`, `u_col`, `u_x`.
Principles
f = Σ rᵢ² / |p − cᵢ|² lava = smoothstep (0.9, 1.1, f) Melvin lamps: several clusters floating up and down, each contributing r²/d² 的「场」,场之和超过阈值的区域连成一片,团块靠近时会融合。 Every pixel is here GPU Nowadays run the same snigger independently (fragment shader), Enter pixel coordinates uv And time t, Output the color of this pixel.