Skip to content

176 web animation techniques, each a live demo with plain settings and a ready-made prompt for your AI assistant. No dependencies, no build step.

feat: move Fluid / Liquid Simulation to the guided-steps page

matina084e271a3aa
diff --git a/animations/06-3d-advanced/fluid-simulation/README.md b/animations/06-3d-advanced/fluid-simulation/README.md
index 07fb9e80..54107422 100644
--- a/animations/06-3d-advanced/fluid-simulation/README.md
+++ b/animations/06-3d-advanced/fluid-simulation/README.md
@@ -1,7 +1,7 @@
# Fluid / Liquid Simulation
## What it is
-The fluid simulation demo renders an organic blob effect — metaballs that merge softly when they come close and separate cleanly when far apart. It is not a true Navier-Stokes fluid simulation but a convincing approximation: signed distance field (SDF) spheres combined with a smooth-minimum function, rendered per-pixel in a WebGL fragment shader. The result looks like lava lamp blobs or liquid mercury.
+This fluid effect looks like liquid but is not a real fluid simulation. A WebGL shader works out, for every pixel, how far it is from a few moving circles, and blends those distances so circles that come close melt into one smooth shape, like drops of mercury or a lava lamp. Circles that drift apart separate cleanly again.
## When to use it
- Liquid-aesthetic hero backgrounds on health, wellness, and creative product sites
@@ -53,10 +53,13 @@ vec2 ballPosition(int i, float t) {
## Key parameters
| Parameter | Default | Effect |
|-----------|---------|--------|
-| Metaball count | 5 | More = busier, more connections; 8+ at high res drops FPS |
-| Smoothness (k) | 0.08 | 0 = sharp, hard edges (no blending); 0.2 = very soft merge radius |
-| Ball radius | 0.14 | Larger = bigger blobs; overlaps more = more frequent merges |
-| Speed | 0.8 | Faster = frantic; 0.3 = meditative |
+| Melting | Medium | How far apart two blobs start to melt together: a little is 0.03, medium 0.08 and a lot 0.15 of the stage height; a lot gives thick, soft necks |
+| Number of blobs | Medium | Few is 3, medium 5 and many 8 blobs; each one adds work for every pixel |
+| Speed | Normal | How fast the blobs move on their paths: slow is 0.5, normal 0.8 and fast 1.3 |
+| Blob size | Medium | Each blob's radius: small is 9%, medium 14% and large 20% of the stage height |
+| Color | Blue | The color of the liquid |
+| Glow | on | Adds a soft halo that fades out around the liquid |
+| One blob follows the pointer | on | One blob leaves its path and sits under the pointer or a finger |
## Production notes
- **Real fluid simulation**: Navier-Stokes-based fluid (velocity fields, pressure, diffusion) requires full-screen texture updates per frame. Pavel DoGreat's WebGL Fluid Simulation (open source) is the go-to — it renders truly interactive fluid at 60fps using a series of physics passes.
@@ -68,6 +71,6 @@ vec2 ballPosition(int i, float t) {
- **`prefers-reduced-motion`**: pause the animation. The blobs should remain visible in their default positions.
## See also
-- [WebGL Shader Animation](../webgl-shader-animation/) — same fullscreen-quad approach, different shader patterns
-- [Canvas Particle Effect](../canvas-particle-effect/) — 2D canvas alternative for organic movement
-- [Noise-Based Motion](../noise-based-motion/) — Perlin noise for similar organic feel with less WebGL complexity
+- [WebGL Shader Animation](../webgl-shader-animation/) — the same one-surface shader setup, painting patterns
+- [Morphing Blob](../morphing-blob/) — the same melting look from blurred circles, without WebGL
+- [Noise-Based Motion](../noise-based-motion/) — smooth noise moves dots and a blob instead
diff --git a/animations/06-3d-advanced/fluid-simulation/index.html b/animations/06-3d-advanced/fluid-simulation/index.html
index 74282af8..b669482e 100644
--- a/animations/06-3d-advanced/fluid-simulation/index.html
+++ b/animations/06-3d-advanced/fluid-simulation/index.html
@@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fluid / Liquid Simulation — Animation Handbook</title>
- <meta name="description" content="WebGL metaballs — SDF spheres that merge smoothly via smooth-minimum in a GLSL fragment shader.">
+ <meta name="description" content="Blobs drift and melt into each other like liquid. Best for hero backgrounds.">
<link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/06-3d-advanced/fluid-simulation/">
<link rel="icon" type="image/png" sizes="96x96" href="../../../favicon.png">
<link rel="icon" type="image/svg+xml" href="../../../favicon.svg">
@@ -12,100 +12,170 @@
<meta property="og:type" content="article">
<meta property="og:site_name" content="Animation Handbook">
<meta property="og:title" content="Fluid / Liquid Simulation — Animation Handbook">
- <meta property="og:description" content="WebGL metaballs — SDF spheres that merge smoothly via smooth-minimum in a GLSL fragment shader.">
+ <meta property="og:description" content="Blobs drift and melt into each other like liquid. Best for hero backgrounds.">
<meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/06-3d-advanced/fluid-simulation/">
<meta property="og:image" content="https://matinmonshizadeh.github.io/animation-handbook/og-image.png">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Fluid / Liquid Simulation — Animation Handbook">
- <meta name="twitter:description" content="WebGL metaballs — SDF spheres that merge smoothly via smooth-minimum in a GLSL fragment shader.">
+ <meta name="twitter:description" content="Blobs drift and melt into each other like liquid. Best for hero backgrounds.">
<meta name="twitter:image" content="https://matinmonshizadeh.github.io/animation-handbook/og-image.png">
- <script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "Fluid / Liquid Simulation", "name": "Fluid / Liquid Simulation", "description": "WebGL metaballs — SDF spheres that merge smoothly via smooth-minimum in a GLSL fragment shader.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/06-3d-advanced/fluid-simulation/", "inLanguage": "en", "articleSection": "3D & Advanced", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, 3D & Advanced, Fluid / Liquid Simulation"}</script>
+ <script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "Fluid / Liquid Simulation", "name": "Fluid / Liquid Simulation", "description": "Blobs drift and melt into each other like liquid. Best for hero backgrounds.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/06-3d-advanced/fluid-simulation/", "inLanguage": "en", "articleSection": "3D & Advanced", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, 3D & Advanced, Fluid / Liquid Simulation"}</script>
<style>
- @font-face{font-family:'Bricolage';src:url('../../../assets/fonts/brico-700.woff2') format('woff2');font-weight:700;font-display:swap}
- @font-face{font-family:'Bricolage';src:url('../../../assets/fonts/brico-800.woff2') format('woff2');font-weight:800;font-display:swap}
- @font-face{font-family:'PlexMono';src:url('../../../assets/fonts/plex-400.woff2') format('woff2');font-weight:400;font-display:swap}
- @font-face{font-family:'PlexMono';src:url('../../../assets/fonts/plex-500.woff2') format('woff2');font-weight:500;font-display:swap}
- :root{--bg:#0b0b0d;--ui-bg:#111114;--ui-border:rgba(255,255,255,.09);--ui-accent:#3fd6c4;--ui-text:#f4f4f2;--ui-muted:#77777e;--disp:'Bricolage','Arial Narrow',sans-serif;--mono:'PlexMono',ui-monospace,Menlo,Consolas,monospace;--stage-h:620px}
+ :root{--ui-accent:#3fd6c4}
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
- body{background:var(--bg);color:var(--ui-text);font-family:var(--mono);-webkit-font-smoothing:antialiased;padding:clamp(12px,3vw,24px)}
- header{margin-bottom:20px}
- header h1{font-family:var(--disp);font-weight:700;letter-spacing:-.01em;font-size:clamp(11px,1.8vw,15px)}
- header p{font-size:clamp(10px,1.2vw,12px);color:var(--ui-muted);margin-top:4px}
- .layout{display:flex;gap:clamp(12px,2vw,24px);align-items:flex-start}
- .stage{flex:1;min-width:0;height:var(--stage-h);border:1px solid var(--ui-border);border-radius:8px;overflow:hidden;position:relative;touch-action:pan-y}
+ .stage{position:relative;background:#030810;touch-action:pan-y}
canvas{display:block;width:100%;height:100%}
- .fps-badge{position:absolute;top:8px;right:8px;font-size:9px;color:rgba(255,255,255,.5);background:rgba(0,0,0,.4);padding:3px 8px;border-radius:4px;pointer-events:none}
- .no-webgl{position:absolute;inset:0;display:none;align-items:center;justify-content:center;color:var(--ui-muted);font-size:12px;text-align:center;padding:24px}
- aside{width:clamp(200px,22vw,240px);flex-shrink:0;background:var(--ui-bg);border:1px solid var(--ui-border);border-radius:8px;padding:16px;display:flex;flex-direction:column;gap:10px}
- .note{font-size:10px;color:var(--ui-muted);line-height:1.6;border-left:2px solid var(--ui-accent);padding-left:8px}
- .lbl{font-size:10px;text-transform:uppercase;letter-spacing:1px;color:var(--ui-accent)}
- .div{height:1px;background:var(--ui-border)}
- .sr{display:flex;align-items:center;gap:6px;font-size:10px}
- .sr input[type=range]{flex:1;accent-color:var(--ui-accent);min-height:44px}
- .sv{width:40px;text-align:right;color:var(--ui-text)}
- .tog{display:flex;align-items:center;gap:8px;font-size:10px;cursor:pointer;min-height:44px}
- .tog input{accent-color:var(--ui-accent);width:16px;height:16px;flex-shrink:0}
- input[type=color]{border:none;background:none;width:44px;height:44px;cursor:pointer;border-radius:4px;padding:0}
- button.rst{background:transparent;border:1px solid var(--ui-border);color:var(--ui-text);font-family:var(--mono);font-size:10px;padding:14px 8px;border-radius:4px;cursor:pointer;width:100%}
- @media(hover:hover){button.rst:hover{border-color:var(--ui-accent);color:var(--ui-accent)}}
- @media(prefers-reduced-motion:reduce){*{transition:none!important;animation:none!important}}
- @media(max-width:600px){.layout{flex-direction:column}.stage{flex:none;width:100%}aside{width:100%}:root{--stage-h:380px}}
+ .no-webgl{position:absolute;inset:0;display:none;align-items:center;justify-content:center;text-align:center;padding:24px;font-size:13px;line-height:1.7;color:rgba(255,255,255,.72)}
</style>
-<style id="ah-inject">
-:root{--ah-bg:rgba(17,17,20,.82);--ah-line:rgba(255,255,255,.1);--ah-ink:#f4f4f2;--ah-muted:#9a9aa2;--ah-accent:#6ea8ff}
-body{padding-top:46px}
-.ah-bar{position:fixed;top:0;left:0;right:0;height:46px;z-index:2147483000;display:flex;align-items:center;justify-content:space-between;gap:10px;padding:0 clamp(10px,3vw,18px);background:var(--ah-bg);backdrop-filter:blur(10px);-webkit-backdrop-filter:blur(10px);border-bottom:1px solid var(--ah-line);font-family:ui-monospace,Menlo,Consolas,monospace;font-size:12px;box-sizing:border-box}
-.ah-bar a,.ah-bar button{color:var(--ah-muted);text-decoration:none;background:none;border:0;font:inherit;cursor:pointer;display:inline-flex;align-items:center;gap:6px;min-height:34px;padding:0 7px;border-radius:6px;transition:color .18s,background .18s;white-space:nowrap}
-.ah-bar a:hover,.ah-bar button:hover{color:var(--ah-ink)}
-.ah-home{color:var(--ah-ink);font-weight:600}
-.ah-actions{display:flex;align-items:center;gap:2px}
-.ah-sep{width:1px;height:16px;background:var(--ah-line);margin:0 4px}
-.ah-copied{color:var(--ah-accent)!important}
-@media(max-width:600px){.ah-bar .ah-hideSm{display:none}}
-@media print{.ah-bar{display:none}body{padding-top:0}}
-</style>
+ <link rel="stylesheet" href="../../../assets/css/demo-page.css?v=3">
+ <script src="../../../assets/js/demo-page.js?v=3" defer></script>
</head>
-<body>
-<div class="ah-bar" role="navigation" aria-label="Animation Handbook"><a class="ah-home" href="../../../">← Animation Handbook</a><span class="ah-actions"><a class="ah-hideSm" href="../canvas-particle-effect/" rel="prev" title="Previous">‹ Prev</a><a class="ah-hideSm" href="../glassmorphism-animated/" rel="next" title="Next">Next ›</a><span class="ah-sep ah-hideSm"></span><button type="button" id="ah-copy" class="ah-hideSm">Copy source</button><a class="ah-hideSm" href="https://github.com/matinmonshizadeh/animation-handbook/blob/main/animations/06-3d-advanced/fluid-simulation/index.html" target="_blank" rel="noopener noreferrer">Source ↗</a></span></div>
-<header>
- <h1>Fluid / Liquid Simulation</h1>
- <p>WebGL metaballs — SDF spheres that merge smoothly via smooth-minimum in a GLSL fragment shader.</p>
-</header>
-<div class="layout">
- <div class="stage" id="stage">
- <canvas id="cvs"></canvas>
- <div class="fps-badge" id="fps">— fps</div>
- <div class="no-webgl" id="no-webgl">WebGL not available in this browser.</div>
- </div>
- <aside>
- <p class="note">Real fluid simulation solves Navier-Stokes equations. This is a shortcut: SDF metaballs rendered per-pixel in a fragment shader. They look fluid but are just smoothly merged SDFs.</p>
- <div class="lbl">Metaballs</div>
- <div class="sr"><input type="range" id="balls-sl" min="2" max="8" value="5" step="1"><span class="sv" id="balls-v">5</span></div>
- <div class="lbl">Smoothness (smin k)</div>
- <div class="sr"><input type="range" id="smooth-sl" min="1" max="20" value="8" step="1"><span class="sv" id="smooth-v">0.08</span></div>
- <div class="lbl">Ball Radius</div>
- <div class="sr"><input type="range" id="rad-sl" min="5" max="25" value="14" step="1"><span class="sv" id="rad-v">0.14</span></div>
- <div class="lbl">Speed</div>
- <div class="sr"><input type="range" id="spd-sl" min="2" max="20" value="8" step="1"><span class="sv" id="spd-v">0.8</span></div>
- <div class="lbl">Fluid Color</div>
- <input type="color" id="color-pick" value="#58a6ff" aria-label="Fluid color">
- <div class="lbl">Background Color</div>
- <input type="color" id="bg-pick" value="#030810" aria-label="Background color">
- <label class="tog"><input type="checkbox" id="mouse-tog" checked><span>Mouse moves a ball</span></label>
- <label class="tog"><input type="checkbox" id="glow-tog" checked><span>Glow effect</span></label>
- <div class="div"></div>
- <button class="rst" id="pause-btn">⏸ Pause</button>
- </aside>
-</div>
+<body class="hb" data-hb-kind="loop">
+<nav class="hb-bar" aria-label="Animation Handbook">
+ <a class="hb-home" href="../../../"><svg class="hb-mark" viewBox="0 0 32 32" aria-hidden="true"><g class="g"><circle cx="11.16" cy="20.84" r="6.1"/><circle cx="15.12" cy="16.88" r="6.1"/><circle cx="18.51" cy="13.49" r="6.1"/></g><circle class="b" cx="21.2" cy="10.8" r="6.6"/></svg><span>Animation Handbook</span></a>
+ <span class="hb-pager"><a href="../canvas-particle-effect/" rel="prev" aria-label="Previous: Canvas Particle Effect"><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m15 18-6-6 6-6"/></svg><span><span class="hb-dir">Previous: </span>Canvas Particle Effect</span></a><a href="../glassmorphism-animated/" rel="next" aria-label="Next: Glassmorphism Animated"><span><span class="hb-dir">Next: </span>Glassmorphism Animated</span><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m9 18 6-6-6-6"/></svg></a></span>
+</nav>
+<main class="hb-page">
+ <header class="hb-head">
+ <p class="hb-cat">06.05 · 3D &amp; Advanced</p>
+ <h1>Fluid / Liquid Simulation</h1>
+ <p class="hb-lede">Blobs drift and melt into each other like liquid. Best for hero backgrounds.</p>
+ </header>
+
+ <section class="hb-step hb-watch" aria-labelledby="hb-watch-title">
+ <div class="hb-step-head">
+ <span class="hb-num" aria-hidden="true">1</span>
+ <div><h2 id="hb-watch-title">Watch it</h2><p>It moves by itself, and one blob follows your pointer or finger. Pause it to look closely, or turn on slow motion.</p></div>
+ </div>
+ <div class="stage" id="stage">
+ <canvas id="cvs" role="img" aria-label="Soft blobs drifting and melting into each other on a dark background"></canvas>
+ <div class="no-webgl" id="no-webgl">WebGL not available in this browser.</div>
+ </div>
+ <div class="hb-player">
+ <button class="hb-play" type="button" id="btn-pause" data-hb-pause data-state="playing"><svg class="hb-ic hb-i-pause" viewBox="0 0 24 24" aria-hidden="true"><rect x="6" y="4" width="4" height="16" rx="1"/><rect x="14" y="4" width="4" height="16" rx="1"/></svg><svg class="hb-ic hb-i-play" viewBox="0 0 24 24" aria-hidden="true"><path d="M6 3l14 9-14 9z"/></svg><span class="hb-pause-label">Pause</span></button>
+ <label class="hb-toggle"><input class="hb-switch" type="checkbox" role="switch" id="slow-tog" data-hb-slowmo autocomplete="off"><span>Slow motion</span></label>
+ </div>
+ </section>
+
+ <section class="hb-step hb-try" aria-labelledby="hb-try-title">
+ <div class="hb-step-head">
+ <span class="hb-num" aria-hidden="true">2</span>
+ <div><h2 id="hb-try-title">Try it</h2><p>Change a setting and see the difference as it moves.</p></div>
+ </div>
+ <div class="hb-settings">
+ <div class="hb-setting">
+ <p class="hb-setting-name" id="melt-lbl">Melting</p>
+ <div class="seg" id="melt-seg" role="group" aria-labelledby="melt-lbl">
+ <button type="button" data-smooth="0.03" aria-pressed="false">A little</button>
+ <button type="button" data-smooth="0.08" class="on" aria-pressed="true">Medium</button>
+ <button type="button" data-smooth="0.15" aria-pressed="false">A lot</button>
+ </div>
+ <p class="hb-hint">How far apart two blobs start to melt together.</p>
+ </div>
+ <div class="hb-setting">
+ <p class="hb-setting-name" id="count-lbl">Number of blobs</p>
+ <div class="seg" id="count-seg" role="group" aria-labelledby="count-lbl">
+ <button type="button" data-balls="3" aria-pressed="false">Few</button>
+ <button type="button" data-balls="5" class="on" aria-pressed="true">Medium</button>
+ <button type="button" data-balls="8" aria-pressed="false">Many</button>
+ </div>
+ <p class="hb-hint">More blobs meet and merge more often.</p>
+ </div>
+ <div class="hb-setting">
+ <p class="hb-setting-name" id="speed-lbl">Speed</p>
+ <div class="seg" id="speed-seg" role="group" aria-labelledby="speed-lbl">
+ <button type="button" data-spd="0.5" aria-pressed="false">Slow</button>
+ <button type="button" data-spd="0.8" class="on" aria-pressed="true">Normal</button>
+ <button type="button" data-spd="1.3" aria-pressed="false">Fast</button>
+ </div>
+ <p class="hb-hint">Slow feels calm, like a lava lamp.</p>
+ </div>
+ </div>
+ <details class="hb-options">
+ <summary><span class="hb-more-label">More options</span><span class="hb-less">Fewer options</span><svg class="hb-ic" viewBox="0 0 24 24" aria-hidden="true"><path d="m6 9 6 6 6-6"/></svg></summary>
+ <div class="hb-settings">
+ <div class="hb-setting">
+ <p class="hb-setting-name" id="size-lbl">Blob size</p>
+ <div class="seg" id="size-seg" role="group" aria-labelledby="size-lbl">
+ <button type="button" data-rad="0.09" aria-pressed="false">Small</button>
+ <button type="button" data-rad="0.14" class="on" aria-pressed="true">Medium</button>
+ <button type="button" data-rad="0.2" aria-pressed="false">Large</button>
+ </div>
+ <p class="hb-hint">Bigger blobs touch and merge more.</p>
+ </div>
+ <div class="hb-setting">
+ <p class="hb-setting-name" id="color-lbl">Color</p>
+ <div class="swatches" id="color-sw" role="group" aria-labelledby="color-lbl">
+ <button type="button" aria-pressed="false" aria-label="Pink" data-color="#ff6f8b" style="background:#ff6f8b"></button>
+ <button type="button" aria-pressed="false" aria-label="White" data-color="#f4f4f2" style="background:#f4f4f2"></button>
+ <button type="button" class="on" aria-pressed="true" aria-label="Blue" data-color="#58a6ff" style="background:#58a6ff"></button>
+ <button type="button" aria-pressed="false" aria-label="Purple" data-color="#d2a8ff" style="background:#d2a8ff"></button>
+ <button type="button" aria-pressed="false" aria-label="Green" data-color="#56d364" style="background:#56d364"></button>
+ <button type="button" aria-pressed="false" aria-label="Orange" data-color="#ffa657" style="background:#ffa657"></button>
+ </div>
+ <p class="hb-hint">The color of the liquid.</p>
+ </div>
+ <div class="hb-setting">
+ <label class="hb-switch-row"><span>Glow</span><input class="hb-switch" type="checkbox" role="switch" id="glow-tog" checked autocomplete="off"></label>
+ <p class="hb-hint">A soft halo around the liquid.</p>
+ </div>
+ <div class="hb-setting">
+ <label class="hb-switch-row"><span>One blob follows the pointer</span><input class="hb-switch" type="checkbox" role="switch" id="mouse-tog" checked autocomplete="off"></label>
+ <p class="hb-hint">Moves one blob to your pointer or finger.</p>
+ </div>
+ </div>
+ </details>
+ </section>
+
+ <section class="hb-step hb-prompt-step" aria-labelledby="hb-prompt-title">
+ <div class="hb-step-head">
+ <span class="hb-num" aria-hidden="true">3</span>
+ <div><h2 id="hb-prompt-title">Copy the prompt</h2><p>Paste it into an AI assistant such as ChatGPT, Claude or Cursor to build this in your own project.</p></div>
+ </div>
+ <div class="hb-card">
+ <p class="hb-prompt">Add liquid blobs to [the section you want them in]. In a WebGL fragment shader, work out for every pixel its distance to each of a few moving circles, and blend those distances with a smooth minimum so circles that come close melt into one shape with soft necks between them, like drops of mercury. Move the circles on looping paths so they keep merging and separating; one can follow the pointer or a finger. Every pixel is checked against every circle, so draw fewer pixels on phones. If the visitor has reduced motion turned on, show the blobs still. Match the settings listed below.</p>
+ <div class="hb-chips-box" hidden>
+ <p class="hb-chips-title">Your settings</p>
+ <ul class="hb-chips"></ul>
+ </div>
+ <div class="hb-copy-row">
+ <button class="hb-copy" type="button"><svg class="hb-ic hb-i-copy" viewBox="0 0 24 24" aria-hidden="true"><rect x="9" y="9" width="13" height="13" rx="2"/><path d="M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"/></svg><svg class="hb-ic hb-i-done" viewBox="0 0 24 24" aria-hidden="true"><path d="M20 6 9 17l-5-5"/></svg><span class="hb-copy-label" aria-live="polite">Copy prompt</span></button>
+ <p class="hb-hint">Replace the words in brackets with your own element. Your settings are added at the end.</p>
+ </div>
+ </div>
+ </section>
+
+ <section class="hb-about" aria-labelledby="hb-what-title">
+ <div class="hb-what">
+ <h2 id="hb-what-title">What it is</h2>
+ <div class="hb-what-text"><p>The full explanation is in <a href="README.md">README.md</a>.</p></div>
+ </div>
+ <div class="hb-fit">
+ <h2>Good for</h2>
+ <ul class="hb-tags hb-good"><li>Hero backgrounds</li><li>Wellness brands</li><li>Creative studios</li><li>Splash screens</li></ul>
+ <h3>Avoid on</h3>
+ <ul class="hb-tags hb-avoid"><li>Behind text</li><li>Small elements</li></ul>
+ </div>
+ </section>
+
+ <section class="hb-related" aria-labelledby="hb-related-title" hidden>
+ <h2 id="hb-related-title">Similar animations</h2>
+ <div class="hb-rel-list"></div>
+ </section>
+</main>
+<footer class="hb-foot"><span>Animation Handbook</span><a href="#top">Back to top ↑</a></footer>
<script>
const stage=document.getElementById('stage');
const cvs=document.getElementById('cvs');
-const fpsEl=document.getElementById('fps');
const noWgl=document.getElementById('no-webgl');
-let W,H,gl,prog,U={},raf=null,paused=false,lastT=0,startT=0,simT=0;
+const slowTog=document.getElementById('slow-tog');
+let W,H,gl,prog,U={},raf=null,paused=false,lastT=0,simT=0;
let BALLS=5,SMOOTH=.08,RAD=.14,SPD=.8,MOUSE_BALL=true,GLOW=true;
-let COLOR=[88/255,166/255,255/255],BG=[3/255,8/255,16/255];
-let mouseX=.5,mouseY=.5;
+let COLOR=[88/255,166/255,255/255];
+const BG=[3/255,8/255,16/255];
+// The pointer is recorded at once, but the picture takes it only when a frame moves on,
+// so a repaint while paused shows a new setting and moves nothing.
+let mouseX=.5,mouseY=.5,shownX=.5,shownY=.5;
const VS=`attribute vec2 aPos;void main(){gl_Position=vec4(aPos,0.,1.);}`;
const FS=`
@@ -143,7 +213,7 @@ void main(){
}`;
// A failed shader is otherwise a silent black canvas — say so instead.
-function fail(msg){gl=null;cvs.style.display='none';fpsEl.style.display='none';
+function fail(msg){gl=null;cvs.style.display='none';
noWgl.textContent=msg;noWgl.style.display='flex';}
function init(){
@@ -181,14 +251,15 @@ function resize(){
if(!gl)return;
gl.viewport(0,0,W,H);
// Resizing wipes the drawing buffer; while paused nothing would redraw it.
- if(paused)render(lastT+16);
+ redraw();
}
+// The loop keeps exactly one pending frame in raf. Slow motion adds a third of the frame's time to the clock.
function render(ts){
raf=paused?null:requestAnimationFrame(render);
const dt=ts-lastT;if(dt<16)return;
// Accumulate sim time so speed changes and pauses never make the blobs jump.
- if(!paused){fpsEl.textContent=Math.round(1000/dt)+' fps';simT+=Math.min(dt,100)/1000*SPD;}
+ if(!paused){simT+=Math.min(dt,100)/1000*SPD/(slowTog.checked?3:1);shownX=mouseX;shownY=mouseY;}
lastT=ts;
gl.useProgram(prog);
gl.uniform2f(U.uRes,W,H);
@@ -196,25 +267,39 @@ function render(ts){
gl.uniform1i(U.uBalls,BALLS);
gl.uniform1f(U.uSmooth,SMOOTH);
gl.uniform1f(U.uRad,RAD);
- gl.uniform2f(U.uMouse,mouseX,mouseY);
+ gl.uniform2f(U.uMouse,shownX,shownY);
gl.uniform1i(U.uGlow,GLOW?1:0);
gl.uniform1i(U.uMouseBall,MOUSE_BALL?1:0);
gl.uniform3fv(U.uColor,COLOR);
gl.uniform3fv(U.uBG,BG);
gl.drawArrays(gl.TRIANGLE_STRIP,0,4);
}
+function start(){if(raf||!gl)return;lastT=performance.now();raf=requestAnimationFrame(render);}
+// While paused a setting or a resize draws the picture again, so the change shows without anything moving.
+// It hands render a time 17 ms on, not 16: adding 16 to a large time can round to just under 16 and the frame gate would skip the repaint.
+function redraw(){if(gl&&paused)render(lastT+17);}
+
+// Registered at the top level, before the shared script (deferred) can run: under reduced motion it pauses the
+// loop on arrival, which this listener must catch. Pause cancels the pending frame and the picture stays;
+// Play calls start(), which carries on from the current clock.
+document.addEventListener('hb:pause',e=>{
+ paused=e.detail.paused;
+ if(paused){cancelAnimationFrame(raf);raf=null;}
+ else start();
+});
function hexToRgb(hex){return[parseInt(hex.slice(1,3),16)/255,parseInt(hex.slice(3,5),16)/255,parseInt(hex.slice(5,7),16)/255];}
+// Shader uv.y is flipped to top-down, so the pointer y must be top-down too.
+// A finger counts as the pointer: a tap moves the blob there and a sideways drag moves it along.
+function aim(e){const r=cvs.getBoundingClientRect();mouseX=(e.clientX-r.left)/r.width;mouseY=(e.clientY-r.top)/r.height;}
+
if(init()){
- resize();startT=performance.now();lastT=startT;
- if(matchMedia('(prefers-reduced-motion:reduce)').matches){
- // Draw one still frame, then hold: the blobs stay visible but do not move.
- paused=true;render(startT+16);fpsEl.textContent='paused';
- document.getElementById('pause-btn').textContent='▶ Resume';
- }else raf=requestAnimationFrame(render);
- // Shader uv.y is flipped to top-down, so the pointer y must be top-down too.
- stage.addEventListener('pointermove',e=>{const r=cvs.getBoundingClientRect();mouseX=(e.clientX-r.left)/r.width;mouseY=(e.clientY-r.top)/r.height;});
+ resize();lastT=performance.now();
+ // The first picture: draws a frame at once and starts the loop.
+ render(lastT+17);
+ stage.addEventListener('pointermove',aim);
+ stage.addEventListener('pointerdown',aim);
window.addEventListener('resize',resize);
// Without these a GPU reset or tab restore leaves the stage black forever.
cvs.addEventListener('webglcontextlost',e=>{e.preventDefault();cancelAnimationFrame(raf);raf=null;});
@@ -225,24 +310,22 @@ if(init()){
});
}
-document.getElementById('balls-sl').addEventListener('input',e=>{BALLS=+e.target.value;document.getElementById('balls-v').textContent=BALLS;});
-document.getElementById('smooth-sl').addEventListener('input',e=>{SMOOTH=e.target.value/100;document.getElementById('smooth-v').textContent=SMOOTH.toFixed(2);});
-document.getElementById('rad-sl').addEventListener('input',e=>{RAD=e.target.value/100;document.getElementById('rad-v').textContent=RAD.toFixed(2);});
-document.getElementById('spd-sl').addEventListener('input',e=>{SPD=e.target.value/10;document.getElementById('spd-v').textContent=SPD.toFixed(1);});
-document.getElementById('color-pick').addEventListener('input',e=>{COLOR=hexToRgb(e.target.value);});
-document.getElementById('bg-pick').addEventListener('input',e=>{BG=hexToRgb(e.target.value);});
-document.getElementById('mouse-tog').addEventListener('change',e=>{MOUSE_BALL=e.target.checked;});
-document.getElementById('glow-tog').addEventListener('change',e=>{GLOW=e.target.checked;});
-// While paused, a control change still repaints one frame so the result is visible.
-document.querySelector('aside').addEventListener('input',()=>{if(paused&&gl)render(lastT+16);});
-document.getElementById('pause-btn').addEventListener('click',()=>{
- paused=!paused;document.getElementById('pause-btn').textContent=paused?'▶ Resume':'⏸ Pause';
- if(!gl)return;
- cancelAnimationFrame(raf);raf=null;
- if(paused)fpsEl.textContent='paused';
- else{lastT=performance.now();raf=requestAnimationFrame(render);}
-});
+function choices(id,pick){
+ const buttons=document.querySelectorAll('#'+id+' button');
+ buttons.forEach(b=>b.addEventListener('click',()=>{
+ buttons.forEach(x=>{x.classList.toggle('on',x===b);x.setAttribute('aria-pressed',String(x===b))});
+ pick(b.dataset);
+ }));
+}
+choices('melt-seg',d=>{SMOOTH=+d.smooth});
+choices('count-seg',d=>{BALLS=+d.balls});
+choices('speed-seg',d=>{SPD=+d.spd});
+choices('size-seg',d=>{RAD=+d.rad});
+choices('color-sw',d=>{COLOR=hexToRgb(d.color)});
+document.getElementById('glow-tog').addEventListener('change',e=>{GLOW=e.target.checked});
+document.getElementById('mouse-tog').addEventListener('change',e=>{MOUSE_BALL=e.target.checked});
+// While paused, any click or change in Try it still repaints one frame so the result is visible.
+['click','change'].forEach(type=>document.querySelector('.hb-try').addEventListener(type,redraw));
</script>
-<script>(function(){var b=document.getElementById("ah-copy");if(!b)return;b.addEventListener("click",function(){fetch(location.href).then(function(r){return r.text();}).catch(function(){return document.documentElement.outerHTML;}).then(function(t){return navigator.clipboard.writeText(t);}).then(function(){b.textContent="Copied!";b.classList.add("ah-copied");setTimeout(function(){b.textContent="Copy source";b.classList.remove("ah-copied");},1500);}).catch(function(){b.textContent="Copy failed";setTimeout(function(){b.textContent="Copy source";},1500);});});})();</script>
</body>
</html>
diff --git a/index.html b/index.html
index e106a2d3..936558b4 100644
--- a/index.html
+++ b/index.html
@@ -1012,7 +1012,7 @@ const CATS=[
['scroll-driven-3d-rotation','Scroll-Driven 3D Rotation','CSS cube rotates through four choreographed stages as you scroll — the Apple product-page pattern.'],
['parallax-3d-tilt','Parallax 3D Tilt','CSS perspective + rotateX/Y follows mouse — a shine highlight tracks the virtual light source.'],
['canvas-particle-effect','Canvas Particle Effect','Dots drift, link up when close, and dodge your pointer. Best for tech sites.'],
- ['fluid-simulation','Fluid Simulation','WebGL metaballs — SDF spheres merged via smooth-minimum in a GLSL fragment shader.'],
+ ['fluid-simulation','Fluid Simulation','Blobs drift and melt into each other like liquid. Best for hero backgrounds.'],
['glassmorphism-animated','Glassmorphism Animated','backdrop-filter: blur() over animated gradient blobs — three frost variants.'],
['webgl-shader-animation','WebGL Shader Animation','Four GLSL fragment shader presets: plasma, wave distortion, Voronoi, kaleidoscope.'],
['noise-based-motion','Noise-Based Motion','Simplex noise drives a wind field and a morphing blob — smooth random, not chaotic random.'],