How it works
A real Navier–Stokes simulation runs on a 228×128 velocity grid. A 1820×1024 dye field rides on it and is drawn as a thick liquid over the page. The trick is that your pointer is not a paintbrush. It only partly grips the fluid, so the dye is left behind and keeps flowing as a wake, while a clear window around the pointer keeps whatever you are about to click visible.
Pipeline (per frame)
pointer segment: last sim step → this one fixed 60 Hz step (0–2 per frame)
│
├─▶ SPLAT vel vel RG16F 228×128 mix(v, v_pointer, 1−(1−w·grip)^(Δt·60)); w = capsule Gaussian
├─▶ SPLAT dye dye R16F 1820×1024 d += w · min(1, density·travel/σ) · ½(1 + depth·sin(q·2π/λ))
│ q = signed distance across the stroke; the direction turns
│ from the last capsule's to this one's (≤ 0.6 rad per capsule)
│ depth = .7, → 0 at the brush rim, on sharp turns and in
│ the leading cap, so stroke fronts get no stripe teeth
├─▶ CURL curl R16F 228×128 ω = ∂v/∂x − ∂u/∂y
├─▶ VORTICITY vel v += ε·(N × ω)·Δt, N = ∇|ω| / |∇|ω|| Fedkiw 2001
├─▶ COPY v₀ RG16F 228×128 park v₀ for the implicit solve
├─▶ VISCOUS ×12 vel Jacobi on (I − ν·Δt·∇²) v = v₀ Stam 1999
├─▶ DIVERGENCE div R16F 228×128 ∇·v; edges: solid (mirror v) or open (copy v)
├─▶ KEEP pres R16F 228×128 p ← 0.8·p (warm start)
├─▶ PRESSURE ×20 pres Jacobi on ∇²p = ∇·v (open edges: p = 0 outside)
├─▶ GRADIENT vel v ← v − ∇p
├─▶ ADVECT vel vel semi-Lagrangian self-advection, ÷(1 + 0.55·Δt)
└─▶ ADVECT dye dye semi-Lagrangian, ÷(1 + fade·Δt); open edges: 0 when the
backtrace leaves the screen (no edge streaks flow in)
[MacCormack: forward → hat R16F 1820×1024, then
corrected + clamped to the 4 source texels]
┌──────────────────────────────── display, every frame ───────────────────────────────┐
│ SOFT ¼-res → 2× (H+V) 9-tap Gaussian RG16F 455×256 │
│ .r bright-pass (halo) · .g coverage (glossy) or thickness (luminous) │
│ DISPLAY → canvas 2560×1440 RGBA8, premultiplied │
│ glossy: h = smoothstep(0,1.6,min(d,.8)) + .94·ln(1+max(d−.8,0)) → normals │
│ → Blinn-Phong · ramp(thin→mid→deep), over a cast shadow: SOFT.g │
│ sampled toward the light, tinted from the deep colour │
│ luminous: ramp emitted · Hessian crest strands · tube normal = fine √d slope │
│ (fades out on thick cores) + slope of SOFT.g (ribbon scale) → rim + │
│ 2-light specular · hue-preserving shoulder, + halo from SOFT.r │
│ α = max(.18·a, max(rgb)): dim body is see-through glow │
│ both: a = smoothstep(cut, cut+soft, d) · clear zone (real pointer only) │
│ · idle fade, ±1 LSB dither │
└─────────────────────────────────────────────────────────────────────────────────────┘
43 draws per moving frame: +1 with MacCormack, +5 for SOFT (luminous, or a visible shadow)
The ideas that matter
- A wake, not a brush. The velocity splat blends the fluid toward the pointer’s velocity with a capped grip (0.45) instead of adding force. The liquid can never outrun your hand, and most of the dye is left behind to drift. Impulse splats are what make most WebGL fluids read as smoke. The blend is made frame-rate independent with
1 − (1 − w·grip)Δt·60. One consequence shaped the intro stroke: a slow, straight stroke gives the fluid under the brush time to reach the pointer’s full speed, so the dye rides along as one blob. A quick stroke with broad turns leaves it behind. The ghost therefore draws one fast S (about 900–1300 px/s) in 1.4s instead of a slow 2.6s wander. - Capsule splats, deposit per distance. Each step splats a soft capsule from the previous pointer position to the current one, so fast flicks stay continuous instead of turning into beads. The dye laid down is proportional to the distance travelled per brush width, so slow and fast strokes come out equally thick.
- Implicit viscosity is the syrup. Twelve Jacobi steps of
(I − νΔt∇²)v = v₀diffuse momentum without the time-step limit an explicit Laplacian would impose. That is what turns fine turbulence into big, lazy S-curves. Vorticity confinement (Fedkiw, Stam & Jensen) then puts back the curl that numerical damping eats, so the wake still rolls into eddies. - Warm-started pressure. Twenty Jacobi iterations would leave most of the divergence in place from a cold start. Starting from 0.8× last frame’s pressure gets the solver most of the way for free. Faithful to the reference, divergence uses the ½ central-difference factor and the gradient subtract does not. The research notes show this mismatch happens to help at 20 Jacobi iterations, but past about 30 it leaves more divergence, not less, so the iterations slider stops at 30. A converged solver would need the gradient
0.5·(R−L, T−B). - Open edges for an overlay. The reference’s walls are free-slip: the divergence mirrors the normal velocity and pressure is clamped. The wake then piles up along the screen edges and frames the page like a box. With Open edges on, an out-of-range neighbour copies the cell’s own velocity (zero-gradient outflow) and pressure is 0 outside the grid (Dirichlet), so the wake flows off the screen. Dye advection then also returns 0 when a backtrace leaves the screen. Otherwise
CLAMP_TO_EDGEhands back the edge texel, and wherever the flow runs inward that edge colour is extruded across the page as straight streaks. Every preset opens the edges; turn the toggle off for the reference’s walls. - Density is a height field. On light pages the dye’s thickness drives everything. Its slope gives the normals for Blinn-Phong gloss, and the colour ramps from bright thin edges to deep pools. The reference’s
smoothstep(0, 1.6, d)height goes flat above 1.6, so every turnaround and slow stretch became a matte maroon stain with no gloss. The height keeps that curve up to its steepest point (d = 0.8), then continues with a log tail of the same slope that never saturates. Thin and mid dye look exactly as before, and thick pools stay glossy. A shadow tinted from the deep colour lifts the liquid off the page. It is drawn in the shader, from a quarter-resolution blurred coverage map sampled toward the light, rather than with a CSS filter, so its cost shows up in the GPU timer. Bloom does nothing on light backgrounds; surface shading does. - Cut faint dye. Dye is cut with
smoothstep(cut, cut + softness, d), so old dye never spreads into a page-wide film. The reference’s soft .06–.40 ramp let a decaying trail fade through translucency, and two seconds after a stroke the page sat under a pale pink fog. Every preset now cuts tighter (Crimson .10–.26, Emerald .12–.24), so ribbons erode crisply instead. A tighter cut also shortens the visible life, t = ln(d₀/cut)/fade, so Crimson’s fade drops from 0.9 to 0.8/s, which gives back the reference’s ~3s for d₀ ≈ 1. The trail also eases out over the 1.6s before the 7s idle-stop, so a long-lived preset never freezes on screen. - Stripes that survive curves. The deposit is striped across the stroke with the reference’s
sinof the signed distance across the stroke direction. On its own, that phase jumps wherever the direction changes from one capsule to the next. Here each capsule turns its direction from the previous capsule’s end direction to its own, by its projection parameter, so neighbouring capsules agree where they meet, end caps included. A capsule turns by at most 0.6 rad, and its stripes fade out as the turn grows, so a sharp corner catches up over a few capsules instead of fanning its streaks out inside one. Slow, pixel-quantised moves are smoothed in proportion to travel, so the streaks don’t jitter. A new stroke starts with a zero-length segment, so the “new stroke” flag is held until a capsule is actually drawn. Spending it on that empty segment would turn the first real capsule from the previous stroke’s stale direction. (Measuring the phase as the distance to the segment instead draws concentric rings around every capsule end, which show up as fringes wherever the pointer slows or turns.) Stripes finer than ~20px smear under semi-Lagrangian advection. The clamped MacCormack option (Selle et al. 2008) keeps 12px stripes at 56% contrast after 3s, against 2% (numbers from the research notes). - Stripes without teeth. The reference’s stripe profile,
.5 + .32·sin + .18·sin, reaches zero. Under a tight alpha cut every trough becomes a gap, and every stripe end sticks out of a stroke’s front or a turnaround as a tooth, a crown of spikes or a sawtooth fringe. The profile keeps the same mean and harmonics, but its depth is 0.7, so a trough keeps 18% of a crest. The depth also falls to zero at the brush rim and in the leading cap, the part of each capsule past its end point. That works because a capsule is much shorter than the brush is wide. Most of any pixel’s dye comes from the caps of nearby capsules, not the one whose segment it lies on. The trail keeps its stripes from the trailing caps, while the front of a stroke, which only leading caps ever cover, stays smooth. - Luminous mode puts the light on thin structures. Brightness spread over the dye body reads as smoke, so the light goes on structure instead. Crests become bright strands. The detector is the most negative eigenvalue of the dye’s Hessian a fifth of a stripe out, which is rotation invariant, so streaks the flow has turned or sheared still read as crests. A round tube profile,
h = √d, is steep at every ribbon edge. It gives a rim light and a two-light specular, so ribbons keep edges and highlights even with stripes off. Its slope is divided by the probe distance in dye texels, so Rim light means the same at every stripe width. On thick cores the swirl compresses the stripes below the probe distance, and this fine normal flips sign texel to texel, which reads as crumpled foil. So it fades out between d = 0.9 and 1.6, and the slope of the blurred ¼-resolution thickness takes over. That gives broad, smooth tube shading at the scale of the whole ribbon, and the strands keep the streaks. The dim body glows in the saturated thin/mid hue. A light deep colour emitted dimly turns muddy (gold goes olive, cyan goes teal), so the deep colour appears only where it is bright: on crests and on thick hot cores. A hue-preserving1 − e−xshoulder replaces clipping, so the cores stay saturated instead of washing out to beige. Coverage follows the light,α = max(.18·a, max(rgb)). Dim body is then see-through glow over the headline instead of a dark smoked-glass film (an earlier .55 floor made every dye pixel at least 55% opaque). The halo comes from a ¼-resolution bright-pass that only sees visible ribbons. Output stays valid premultiplied colour (rgb ≤ α), which composites the same in every browser. - The clear zone follows only a real pointer. The ghost stroke never opens a window, so a visitor who hasn’t moved yet never sees a hole frozen where the ghost stopped. The first real move fades the zone in over about 0.1s.
- Display-independent. The sim takes fixed 60 Hz steps. More, smaller semi-Lagrangian steps blur more, so a variable step would make the trail about 3× softer on a 120 Hz display. Brush, stripes and clear zone are measured in the screen’s short side (screen heights on landscape, exactly as in the reference), so a portrait phone gets a proportionate brush instead of a flood.
Parameters
Presets
Performance & compatibility
- Measured in Chrome (ANGLE-Metal, Apple-silicon Mac, 1280×720 at DPR 2, so a 2560×1440 backing store) during the harness’s 1.5s pointer sweep, with each preset loaded by
?preset=. Other agents were rendering on the same GPU all session, so there was no idle machine. Each preset got 16 serial sweeps, and the quietest quarter of them is the uncontended estimate, GPU mean / p95: Crimson syrup 3.6–4.7 / 7.0–8.3ms, Emerald 3.2–3.9 / 4.8–8.9ms, ServiceTitan ribbon 2.8–4.8 / 5.6–7.0ms, Neon 3.1–3.8 / 3.7–5.9ms. Medians across all 16, contention included: 5.4 / 8.2, 4.8 / 7.3, 5.6 / 10.2 and 4.6 / 7.8ms. The harness’s own sweep of the default (Crimson) gave 3.7–3.9ms mean / 5.4–6.1ms p95 in the two quietest of seven runs (4.5 / 6.3 and 5.2 / 10.5ms in the last two full runs), JS 0.07–0.19ms per frame and a steady 60 fps in all of them. Contended runs reached 8–9ms mean and 15–21ms p95, so read any single p95 as an upper bound. The cast shadow’s quarter-resolution chain is skipped when the shadow can’t show (strength × opacity ≤ 0.01). Neon runs 12 viscous iterations, not 16, for headroom. - What scales the cost. Pass count dominates: 43 draws per moving frame (+1 with MacCormack, +5 for the soft chain), 32 of them Jacobi iterations on a 29k-cell grid. Each draw carries a fixed per-pass overhead on tile-based GPUs. After that comes dye resolution (dye 512 saves ~1ms) and the full-resolution display pass. Memory: about 7.5MB of dye ping-pong at 1820×1024 R16F, plus 3.7MB for MacCormack. The sim grid and the soft chain are under 2MB.
- Requirements. WebGL2 with
EXT_color_buffer_float, for rendering to half-float targets. Without it, or without WebGL2, the live hero stays visible and clickable, and a small message card in the corner explains why the liquid is missing. - Mobile. Drag to play. The canvas is capped at 1.5× DPR on coarse pointers (585px wide on a 390px phone), and the dye texture is capped at the canvas’s short side. Sizes follow the short side, so the ribbon keeps its proportions in portrait. The harness measured a 53–56 fps touch drag at 390px; it emulates the phone on the desktop GPU, so this measures the pipeline at phone size, not phone hardware.
- Idle and visibility. The loop stops 7s after the last movement (measured: idle at 7.05–7.09s, zero frames after), and only that idle-stop, after the trail has faded, clears the fields. It also pauses off-screen and in hidden tabs, but those stops keep the fields, so scrolling down to this text and straight back resumes the trail where it was. If the loop only restarts after the idle window, the fluid starts clean. The gallery embed runs the ghost stroke forever at DPR 1, in ServiceTitan ribbon, the flagship dark look.
- Reduced motion. The 1.4s intro stroke and 1.4s of drift are simulated off-screen and shown as one still frame, with a “Play anyway” button. The still is re-rendered after a resize, since resizing re-allocates the fields.
References & credits
- Jos Stam, “Stable Fluids”, SIGGRAPH 1999: semi-Lagrangian advection and implicit diffusion. dgp.toronto.edu/…/ns.pdf (paper).
- Mark J. Harris, “Fast Fluid Dynamics Simulation on the GPU”, GPU Gems ch. 38, 2004: the render-to-texture formulation, Jacobi solves and boundaries. developer.nvidia.com/gpugems (book text, free online).
- Ronald Fedkiw, Jos Stam, Henrik Wann Jensen, “Visual Simulation of Smoke”, SIGGRAPH 2001: vorticity confinement. web.stanford.edu/…/smoke.pdf (paper).
- Andrew Selle, Ronald Fedkiw, ByungMoon Kim, Yingjie Liu, Jarek Rossignac, “An Unconditionally Stable MacCormack Method”, J. Sci. Comput. 2008: the clamped MacCormack option. yingjie.math.gatech.edu/…/SFKLR.pdf (paper). GPU form after Crane, Llamas & Tariq, GPU Gems 3 ch. 30 (developer.nvidia.com/gpugems3).
- Pavel Dobryakov, WebGL-Fluid-Simulation. github.com/PavelDoGreat/WebGL-Fluid-Simulation, MIT License, © 2017 Pavel Dobryakov. The simulation shaders here (curl, vorticity confinement with its ±1000 clamp, advection, divergence, Jacobi pressure, gradient subtract, the warm-start scale) and their pass order are adapted from it, by way of the reference trail, which copied them. The full MIT notice is in the effect code’s header comment. The grip and capsule splats, implicit viscosity, open edges, MacCormack option, fixed step and both display modes are not in that code.
- Robert Bridson, Fluid Simulation course notes: boundary conditions and why collocated grids behave as they do. cs.ubc.ca/~rbridson/… (notes).
- Dither hash: Dave Hoskins, “Hash without Sine”. shadertoy.com/view/4djSRW, MIT.
- Engine and tuning lessons: the stable-fluids cursor trail in the Russ Hype Machine page (
index.html, the reference implementation this page ports). Research:research/eulerian-fluids.mdin this library, which covers the projection-factor analysis, the MacCormack sharpness measurements and the 120 Hz finding.