Skip to documentation content

Fluid

Stir colored dye through a real-time fluid simulation with the pointer, or let it drift on its own.

fluid-demo

Overview

Fluid runs a stable-fluids simulation on the GPU. Each frame it adds splats of dye and force where the pointer moved, keeps small eddies alive with vorticity, solves for pressure so the flow stays incompressible, and carries both the velocity and the dye along the flow. The result reacts to every pointer movement and settles back to calm when left alone.

Use it for an expressive hero, an empty state, or a playful moment where interaction is the point. Its cost is higher than a static gradient, so measure on representative devices and lower simResolution, dyeResolution, or pressureIterations if frames drop.

Installation

npm install honestui

Usage

import { Fluid } from "honestui/shaders"

<div className="relative h-96 overflow-hidden rounded-xl bg-neutral-950">
  <Fluid className="absolute inset-0" colors={["#4f8bff", "#ff5fb4"]} />
  <div className="relative">{/* content */}</div>
</div>

By default the simulation listens to pointer movement over its own container. When content is layered above the simulation, set listenOnWindow so pointer movement over that content still stirs the fluid beneath it. Only movement within the container's bounds splats. Leave colors empty for a slowly turning rainbow, and set transparent to draw dye over whatever is behind the canvas.

Accessibility

Fluid is decorative and hidden from assistive technology. Nothing it shows should carry meaning, and content placed over it needs its own background or enough contrast against every color the dye can reach.

The simulation pauses while it is offscreen or the tab is hidden. When the user prefers reduced motion, or paused is set, it renders a still image and ignores the pointer. Without WebGL2 or float render targets it shows a static gradient in the first two colors.

API Reference

PropTypeDefaultDescription
colorsstring[][]Hex dye colors used in turn. Empty gives a turning rainbow.
colorIntensitynumber0.15Brightness of each splat's dye.
densityDissipationnumber1How quickly the dye fades. 0 keeps it forever.
velocityDissipationnumber0.2How quickly motion settles.
pressurenumber0.8How much pressure carries over between frames, from 0 to 1.
pressureIterationsnumber20Pressure solver passes per frame.
curlnumber28Strength of the swirl that keeps small eddies alive.
splatRadiusnumber0.22Splat radius, from 0.01 to 1.
splatForcenumber6000How hard pointer movement pushes the fluid.
simResolutionnumber128Velocity grid size along the shorter side.
dyeResolutionnumber1024Dye grid size along the shorter side.
shadingbooleantrueLights the dye from its gradient so it reads as a surface.
transparentbooleanfalseDraws dye over a transparent canvas.
backgroundColorstring"#0b0d12"Canvas color when not transparent.
autoSplatsbooleantrueStirs the fluid on its own while nobody is interacting.
listenOnWindowbooleanfalseListens for pointer movement on the whole window.
pausedbooleanfalseFreezes the simulation on its current frame.
classNamestring–Class for sizing and positioning the container.