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

Floating Bubbles · How it works & code

Stokes' Law: Larger bubbles rise faster (r²).

Runtime logic

Visiting `/physics/bubbles`, DemoHost lazy-loads `demos/kits/kit-physics.js` (registry `mod: 'kit-physics'`) and calls `mount(host, { variant: 'bubbles' })`. mount resolves the variant to family `bubbles`; `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. Pointer down/drag/move updates the interaction (drag handles, hover readouts); when untouched and auto-demo is on, the pointer follows a Lissajous path. 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.onDown — Pointer down; 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.createLinearGradient() — Creates a linear gradient; ctx.fillRect() — Fills a rectangle; ctx.beginPath() — Starts a new path; ctx.arc() — Adds a circular arc (center, radius, start/end angle); ctx.fill() — Fills the current path; ctx.stroke() — Strokes the current path

Source code

frontend/src/demos/kits/kit-physics.js: FAM.bubbles (L394-L429)

Code walkthrough

L396-396: Control definitions (colour/speed/size/count/extras): Bubble Color, Bubble Size, Bubble Count, Liquid Viscosity μ, Side-to-Side Oscillation. L397-397: Preset buttons: name → parameter overrides (6). L398-406: Info-panel content: live rows(v), formula, notes, params, steps. L407-428: Init function: sets up state and returns the per-frame callback: registers pointer handlers `S.onDown`; draws with Canvas 2D `createLinearGradient`, `fillRect`, `beginPath`, `arc`, `fill`, `stroke`; math via `Math.round`, `Math.min`, `Math.max`, `Math.exp`, `Math.sin`; returns readouts `n`, `rmin`, `rmax`, `vbig`, `vsmall`, `ratio`, `mu` … to the info panel.

Principles

Stokes Terminal Speed: v t = 2 (glue - glubber) · g · r² / (9μ) Approaching terminal speed: v ← v + (v t - v) · (1 − e^(−Δt / τ)) ⇒ The radius doubles, the speed of rise changes 4 × A little bubble in the liquid Buoyancy Up, with a positive ratio to speed Sticky resistance 。两者平衡时达到终端速度,由斯托克斯定律给出:v ∝ r²/μ。 So the bubbles rise much faster than the little bubbles 2 × → Speed 4 The more viscosity (like honey) increases slowly. The bubble will break up to the top and be regenerated from the bottom; the pointer will push the bubble away and click to puncture.

Open the interactive demo · GitHub