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

Particle Rain · How it works & code

Particle rain under air resistance reaching terminal velocity.

Runtime logic

Visiting `/canvas/rain`, DemoHost lazy-loads `demos/kits/kit-canvas.js` (registry `mod: 'kit-canvas'`) and calls `mount(host, { variant: 'rain' })`. mount resolves the variant to family `rain`; `withControls` builds the control panel (7 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]; 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.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.beginPath() — Starts a new path; ctx.moveTo() — Moves the pen without drawing; ctx.lineTo() — Adds a straight line to a point; ctx.stroke() — Strokes the current path; ctx.fillRect() — Fills a rectangle; ctx.arc() — Adds a circular arc (center, radius, start/end angle)

Source code

frontend/src/demos/kits/kit-canvas.js: FAM.rain (L360-L405)

Code walkthrough

L362-362: Control definitions (colour/speed/size/count/extras): Raindrop Color, Raindrop Length, Number of Drops N, Wind Speed w, air resistance k. L363-363: Preset buttons: name → parameter overrides (7). L364-372: Info-panel content: live rows(v), formula, notes, params, steps. L373-404: Init function: sets up state and returns the per-frame callback: draws with Canvas 2D `beginPath`, `moveTo`, `lineTo`, `stroke`, `fillRect`, `arc`; math via `Math.round`, `Math.atan2`; returns readouts `n`, `vt`, `w`, `k`, `tilt`, `spl`, `blocked` to the info panel.

Principles

a = g − k · (v - wind), vertical g = 900 px / s² a = 0 at terminal speed v t = g / k Rainline Inclination φ = atan(w / v_t) Two forces per drop: down Gravity And Peace with the relative air speed Resistance 。刚开始越落越快,直到阻力等于重力,速度不再增加,这就是 Terminal speed g/k。 水平方向上,阻力把雨滴拉向风速,所以雨线会倾斜 atan(w/v_t)。 It's a pointer“伞”,The raindrops that hit the umbrella or the ground will splatter the water.

Open the interactive demo · GitHub