GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - chasebuilds/cursor-fluid: A tiny WebGL2 fluid simulation that follows the cursor or finger. One ES module, no dependencies, 7 KB gzipped.

BranchesTags
Open more actions menu

Latest commit

 

History

3 Commits
3 Commits

Folders and files

NameName
Last commit message
Last commit date
.github/workflows
.github/workflows
 
 
demo
demo
 
 
dist
dist
 
 
docs
docs
 
 
scripts
scripts
 
 
src
src
 
 
test
test
 
 
.gitignore
.gitignore
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
eslint.config.js
eslint.config.js
 
 
index.html
index.html
 
 
package-lock.json
package-lock.json
 
 
package.json
package.json
 
 

Repository files navigation

cursor-fluid

ci

A soft, flowing-color background for a website that reacts to the visitor's mouse or finger, like ink dropped in water. It sits behind your real page content, keeps text readable, slows itself down on weak phones, and shows a calm still picture for visitors who ask their device for less motion.

Live demo: https://chasebuilds.github.io/cursor-fluid/

Recorded from the live demo: a scripted cursor path stirring the fluid

Real screenshot of the live demo, taken in headless Chrome after sweeping a cursor across the page.

What you get

  • One small file. 16.8 KB minified, 7.0 KB gzipped. No libraries, no build step needed on your side.
  • One line to use it. createFluid(canvas, options).
  • Four looks. brand, ink, smoke, aurora, or your own brand colors.
  • Text stays readable. A contrast setting fades the colors under your headline until it passes the accessibility target (measured below).
  • Fast on phones. It watches its own frame rate and lowers or raises its detail to keep up.
  • Polite. Stops drawing when scrolled offscreen or when the tab is in the background. Respects the "reduce motion" setting.
  • Never breaks the page. If the visitor's device has no WebGL2, they get a still gradient instead of an error.

cursor-fluid playground: colored fluid swirling behind a settings panel

Install

  1. Download cursor-fluid.min.js next to your page.
  2. Add a canvas behind your content: <canvas id="bg" style="position:fixed;inset:0;width:100%;height:100%;z-index:-1"></canvas>
  3. Start it: <script type="module">import { createFluid } from './cursor-fluid.min.js'; createFluid(document.getElementById('bg'), { theme: 'brand', contrast: 0.5 });</script>

The canvas needs a CSS size (the example fills the window). Your page content goes on top as normal; the canvas listens to the whole window, so it keeps working even when text and buttons sit in front of it.

Options

All options are optional.

Option Default What it does
theme 'brand' 'brand', 'ink', 'smoke' or 'aurora'
colors theme's Your own colors, 1 to 8 of them: ['#2f6bff', '#19d3c5']
background theme's Your own background color
contrast 0 0 to 1. Higher fades the colors so text on top stays readable
quality 'auto' 'auto', 'low', 'medium' or 'high'
swirl 18 0 to 60. How much small curls the flow keeps
fade 0.9 0 to 5. How fast color fades out
radius 0.09 0.01 to 0.5. Brush size as a share of the canvas height
force 22 0 to 100. How hard the cursor pushes the fluid
ambient true A slow ghost cursor keeps the background alive when nobody is touching it
pointer true Follow the mouse or finger
reducedMotion 'auto' 'auto' follows the visitor's device setting, or force 'always' / 'never'
fps 50 The frame rate quality: 'auto' tries to hold

Bad values fail loudly (unknown option "colour", option "swirl" must be a number). Numbers outside their range are clamped.

API

import { createFluid, readableTextColor } from './cursor-fluid.min.js';

const fluid = createFluid(canvas, { theme: 'aurora' });

fluid.setOptions({ theme: 'ink', contrast: 0.4 }); // change anything live
fluid.splat(0.5, 0.5, 0.05, 0, '#ff5d73');          // x, y (0 to 1, from top-left), push dx, dy, optional color
fluid.pause();                                       // and fluid.resume()
fluid.destroy();                                     // remove listeners, free the graphics memory
fluid.info();                                        // { mode, running, level, quality, fps, theme }

readableTextColor({ theme: 'ink' });                 // '#111827', the text color that reads best on that background

info().mode is 'webgl2' (live), 'static' (reduced motion) or 'static-2d' (no WebGL2).

How it behaves

  • Quality scaling. There are five detail levels. Phones start at level 1 and stop at level 2; desktops start at 2 or 3 and can reach 4. If the average frame time stays over budget it steps down (two steps if it is far over). If it stays fast for 5 seconds it tries one step up. If an upgrade fails soon after, the wait before the next try doubles, and after three failed tries it stops trying. Tab switches and long stalls are ignored. The logic is a pure function in src/quality.js with its own tests.
  • Pausing. An observer pauses the loop when the canvas is scrolled out of view. The loop also stops when the tab is hidden. pause() and resume() are separate from both.
  • Reduced motion. When the device asks for less motion, the simulation never starts. The visitor sees one still, pre-simulated swirl of your colors. It updates if the window is resized or the setting changes.
  • Contrast. The contrast option caps how strong the colors can get over the background. It does not touch your text, so use it together with readableTextColor or your own dark/light text choice.

Measured

Everything here comes from npm run measure, which writes docs/MEASURED.md.

Result
Bundle 16,818 bytes minified, 6,984 bytes gzipped
White text on the brand theme, contrast: 0 worst spot 2.81:1, 13.5% of pixels under 4.5:1
White text on the brand theme, contrast: 0.6 worst spot 7.02:1, 0.0% of pixels under 4.5:1
White text on the brand theme, contrast: 1 worst spot 13.73:1

The contrast numbers shift a few points from run to run. They are the worst case after a heavy brush stroke across a 400 by 300 canvas, measured on real screenshot pixels in headless Chrome with software rendering. 4.5:1 is the WCAG minimum for body text. Frame rate is not claimed here: the test machine renders in software, which says nothing about a real phone. Try the demo on your own device.

How it works

It is a standard grid fluid solver, written from scratch for WebGL2. Each frame it adds the cursor's push and a dab of color, curls the flow back in a little so it does not go flat (vorticity confinement), measures how much the flow is bunching up, removes that with a pressure solve (Jacobi iterations), and then carries both the flow and the color along the flow. The color is drawn onto your background color or added to it, depending on the theme. The shader code is about 70 lines.

Velocity is stored in canvas heights per second, so changing the detail level mid-run just resamples the grid and the motion carries on.

Run it locally

npm install
npm run serve       # then open the address it prints
npm test            # lint is separate: npm run lint
npm run build       # rebuilds dist/cursor-fluid.min.js

Tests: unit tests for the options and the quality scaler (Node's built-in runner), and a real-browser run (Playwright) that checks the API, offscreen and hidden-tab pausing, reduced motion, the no-WebGL2 fallback, touch devices and the contrast numbers.

Limits

  • Needs WebGL2 with renderable half-float textures, which current Chrome, Edge, Firefox and Safari provide on normal hardware. Tests here run in Chromium only. Anything without it gets the still gradient.
  • One page can run several instances, but each is its own simulation, so keep it to one or two.
  • The canvas must have a CSS size. Without one the browser uses 300 by 150.
  • Colors blend in plain sRGB, so very different colors can mix to a muddy tone. Pick palettes that sit near each other.

Credits

The idea comes from PavelDoGreat's WebGL Fluid Simulation and Jos Stam's paper Stable Fluids. The code here is written fresh: different structure, different shaders, different API.

License

MIT, see LICENSE. Made by Chase Builds.

More from Chase Builds

  • r3f-showcase: 3D product configurator, scroll hero and gallery wall for a business site. (live demo)
  • motion-design-gallery: Eleven copy-paste web animations in plain HTML, CSS and JavaScript, safe for visitors who turn motion off. (live demo)
  • liquid-logo-studio: Turns any logo into animated liquid chrome or gold, ready to export or embed. (live demo)

All projects, sorted by industry · chasebuilds.github.io

About

A tiny WebGL2 fluid simulation that follows the cursor or finger. One ES module, no dependencies, 7 KB gzipped.

chasebuilds.github.io/cursor-fluid/

Topics

es-modulefluid-simulationhero-backgroundjavascriptweb-graphicswebgl2

Resources

Readme
MIT license

Contributing

Contributing

Security policy

Security policy
Activity

Stars

0 stars

Watchers

0 watching

Forks

0 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.