flow-field-worklet
v0.2.0
Published
Animated generative flow fields and magnetic streamlines.
Maintainers
Readme
flow-field-worklet
Animated generative flow fields: tapered, luminous streamlines with bloom and depth of field.
Zero runtime dependencies. One self-contained paint module, a minified alternative, and an optional typed loader.
npm install flow-field-workletLoad
For Vite:
import { register, attachView } from "flow-field-worklet/register";
import workletUrl from "flow-field-worklet/worklet.js?url";
await register(workletUrl);
const cleanup = attachView(document.querySelector(".art"));
// Call cleanup() when removing the element.For other build systems, copy worklet.js to a public asset and call CSS.paintWorklet.addModule('/worklets/flow-field.js') after checking globalThis.CSS?.paintWorklet. Keep the native cursor and a static background fallback. register(url?) returns Promise<boolean>: false when unsupported or during SSR, true after loading, and a rejection for load failures. Repeated calls with the same URL are deduplicated; failures can be retried. A default URL works when the package directory structure is served intact; bundled apps should pass their asset URL explicitly.
Paint and animate
@property --worklet-time {
syntax: "<number>";
inherits: false;
initial-value: 0;
}
.art {
min-height: 24rem;
background-color: #080a10;
background-image: linear-gradient(135deg, #182243, #33213d);
--worklet-hue: 220;
}
@supports (background-image: paint(flow-field)) {
.art {
background-image: paint(flow-field);
animation: worklet-loop 24s linear infinite;
}
}
@keyframes worklet-loop {
from {
--worklet-time: 0;
}
to {
--worklet-time: 1;
}
}
@media (prefers-reduced-motion: reduce) {
.art {
animation: none;
}
}Register --worklet-time once in your document. Without an animation, the effect is a stable still image. The worklet does not own a timer or access the DOM. Pause animations when the surface is hidden.
Optional attachView(element) eases pointer lighting or parallax through --worklet-pointer-x and --worklet-pointer-y (0–1, centered at 0.5). It stops when settled, honors reduced motion, leaves touch scrolling intact, and returns cleanup that restores previous inline values. These properties can also be driven directly by your application.
CSS inputs
| Property | Default | Meaning |
| --------------------- | ------- | -------------------------------------------- |
| --worklet-time | 0 | One loop is 0–1; animate a registered number |
| --worklet-seed | 42 | Repeatable variation |
| --worklet-hue | 225 | Base hue in degrees, clamped to -3600–3600 |
| --worklet-intensity | 1 | 0–2 light multiplier |
| --flow-density | 550 | 10–1000 streamlines |
| --flow-turbulence | 2.4 | 0.1–8 |
| --flow-length | 44 | 4–80 integration steps |
| --flow-vortex | 1 | 0–3 |
Numeric values are finite and bounded; missing or malformed numeric values use defaults. Counts are integer-truncated unless stated otherwise. CSS colors should be valid explicit colors; Paint API currentColor does not resolve to the element's text color. Invalid colors follow the browser's Canvas style-assignment behavior.
Browser support
Requires CSS Paint and a secure context (HTTPS or localhost); test CSS.paintWorklet. Chrome and Edge support it; provide a static fallback elsewhere. See CSS Painting API. Rendering cost depends on dimensions, pixel ratio, and settings. Keep large animated surfaces and density modest on mobile.
The root entry retains the worklet.js URL convention. For a smaller asset use flow-field-worklet/worklet.min.js. Loading multiple different URLs or versions that register the same paint name in one document is unsupported; use a single module URL per effect.
MIT © Emilio Domínguez.
