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.
HTML ai-promptsanimationcss-animationsfrontendgithub-pagesjavascriptmicrointeractionsmotion-designno-dependenciesreferencescroll-animationui-animationweb-animationweb-developmentwebgl
feat: move Volumetric Smoke / 3D Noise to the guided-steps page
matin67d85e60a7a1
diff --git a/animations/06-3d-advanced/volumetric-smoke/README.md b/animations/06-3d-advanced/volumetric-smoke/README.mdindex d69e79e9..07385b00 100644--- a/animations/06-3d-advanced/volumetric-smoke/README.md+++ b/animations/06-3d-advanced/volumetric-smoke/README.md@@ -1,7 +1,7 @@# Volumetric Smoke / 3D Noise## What it is-Volumetric rendering treats 3D space as a participating medium — fog, smoke, clouds, or fire — rather than a collection of surfaces. Instead of stopping at the first surface hit (like standard ray marching), each ray accumulates density as it travels through the volume. The result is light scattering, depth-based opacity, and the soft edges that make clouds and smoke look real. This demo samples a 3D fractal Brownian motion (fBm) noise field and renders it as animated smoke rising from a source.+Volumetric smoke is drawn as a real 3D cloud rather than a flat picture. For every pixel, a ray passes through a cloud of smooth noise and adds up how much smoke it meets, shading each part by how much smoke lies between it and the light. That gives soft edges and depth, which is how films draw smoke, fog and clouds.## When to use it- Atmospheric background effects: rising smoke, volumetric fog, cloud formations@@ -50,18 +50,22 @@ float smokeDensity(vec3 p, float t) {float r = length(p.xz);// smoothstep needs edge0 < edge1, so the upper falloff is inverted rather than reversedfloat src = exp(-r * 3.0) * smoothstep(-0.2, 0.6, p.y) * (1.0 - smoothstep(1.0, 1.8, p.y));- float n = fbm(p * 1.4 + vec3(windDrift, t * speed, 0.0));+ float n = fbm(p * 1.4 + vec3(windDrift, rise, 0.0));return max(0.0, n - 0.45) * src * density;}```+`rise` grows every frame by the frame's time × the speed, on the page, so changing the speed never makes the smoke jump.+## Key parameters| Parameter | Default | Effect ||-----------|---------|--------|-| March steps | 32 | 16 = fast/coarse; 64 = smooth/accurate; diminishing returns above 48 |-| Smoke density | 0.7 | Higher = denser, more opaque cloud; lower = wispy, transparent |-| Speed | 0.6 | How fast noise "rises" through the field (animation speed) |-| Step size | `3.0 / steps` | Smaller steps = finer detail but more iterations needed |+| Thickness | Medium | How dense the smoke is: thin is 0.4, medium 0.7 and thick 1.2; thin looks wispy, thick hides what is behind |+| Speed | Normal | How fast the smoke rises: slow is 0.35, normal 0.6 and fast 1 |+| Detail | Medium | How many steps each ray takes: low is 16, medium 32 and high 64 (48 on phones); fewer steps leave visible stripes |+| Smoke color | Gray blue | The color of the lit smoke; the shadowed parts are darker shades of it |+| Wind | on | The whole plume drifts slowly to one side |+| Fine grain | on | Starts each ray at a slightly random point, which turns the stripes that few steps leave into fine grain |## Production notes- **Precision**: the `sin`-based hash multiplies by 43758.5, which overflows the usable range of a 16-bit `mediump` float and collapses the noise to flat blocks on many mobile GPUs. Request `highp` in the fragment shader (guarded by `GL_FRAGMENT_PRECISION_HIGH`) or swap the hash for an integer-free variant with a smaller multiplier.@@ -74,6 +78,6 @@ float smokeDensity(vec3 p, float t) {- **Temporal accumulation**: real-time volumetric engines (Unreal Engine's Volumetric Fog) spread the ray march samples across multiple frames and blend results. This reduces per-frame cost by 4–8× at the cost of minor ghosting artifacts during fast camera motion.## See also-- [Ray Marching / SDF Scene](../ray-marching-sdf/) — ray marching that finds surface hits rather than accumulating volume-- [Noise-Based Motion](../noise-based-motion/) — 2D application of the same noise functions-- [Fluid Simulation](../fluid-simulation/) — SDF-based alternative for liquid-like effects+- [Ray Marching / SDF Scene](../ray-marching-sdf/) — rays that stop at surfaces instead of adding up smoke+- [Noise-Based Motion](../noise-based-motion/) — the same kind of noise, on a flat canvas+- [Fluid / Liquid Simulation](../fluid-simulation/) — a soft, liquid look drawn by a shaderdiff --git a/animations/06-3d-advanced/volumetric-smoke/index.html b/animations/06-3d-advanced/volumetric-smoke/index.htmlindex a61f6abf..3f94675d 100644--- a/animations/06-3d-advanced/volumetric-smoke/index.html+++ b/animations/06-3d-advanced/volumetric-smoke/index.html@@ -4,7 +4,7 @@<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Volumetric Smoke / 3D Noise — Animation Handbook</title>- <meta name="description" content="Ray marching through a 3D noise field — density accumulated along each ray for a true volumetric look.">+ <meta name="description" content="Soft smoke curls upward, drawn as a real 3D cloud. Best for moody backgrounds."><link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/06-3d-advanced/volumetric-smoke/"><link rel="icon" type="image/png" sizes="96x96" href="../../../favicon.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg">@@ -12,97 +12,148 @@<meta property="og:type" content="article"><meta property="og:site_name" content="Animation Handbook"><meta property="og:title" content="Volumetric Smoke / 3D Noise — Animation Handbook">- <meta property="og:description" content="Ray marching through a 3D noise field — density accumulated along each ray for a true volumetric look.">+ <meta property="og:description" content="Soft smoke curls upward, drawn as a real 3D cloud. Best for moody backgrounds."><meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/06-3d-advanced/volumetric-smoke/"><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="Volumetric Smoke / 3D Noise — Animation Handbook">- <meta name="twitter:description" content="Ray marching through a 3D noise field — density accumulated along each ray for a true volumetric look.">+ <meta name="twitter:description" content="Soft smoke curls upward, drawn as a real 3D cloud. Best for moody 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": "Volumetric Smoke / 3D Noise", "name": "Volumetric Smoke / 3D Noise", "description": "Ray marching through a 3D noise field — density accumulated along each ray for a true volumetric look.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/06-3d-advanced/volumetric-smoke/", "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, Volumetric Smoke / 3D Noise"}</script>+ <script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "Volumetric Smoke / 3D Noise", "name": "Volumetric Smoke / 3D Noise", "description": "Soft smoke curls upward, drawn as a real 3D cloud. Best for moody backgrounds.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/06-3d-advanced/volumetric-smoke/", "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, Volumetric Smoke / 3D Noise"}</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;font-size:clamp(11px,1.8vw,15px);letter-spacing:-.01em}- 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}+ .stage{position:relative;background:#030810}canvas{display:block;width:100%;height:100%}- .hud{position:absolute;top:8px;left:8px;right:8px;display:flex;justify-content:space-between;pointer-events:none}- .badge{font-size:9px;color:rgba(255,255,255,.5);background:rgba(0,0,0,.4);padding:3px 8px;border-radius:4px}- .msg{position:absolute;inset:0;display:none;align-items:center;justify-content:center;text-align:center;padding:24px;font-size:11px;color:var(--ui-muted);line-height:1.7;white-space:pre-wrap}+ .msg{position:absolute;inset:0;display:none;align-items:center;justify-content:center;text-align:center;padding:clamp(16px,4vw,32px);font-size:13px;line-height:1.7;color:rgba(255,255,255,.72);white-space:pre-wrap;overflow-y:auto}.msg.on{display:flex}- .src-panel{position:absolute;bottom:0;left:0;right:0;background:rgba(11,11,13,.92);border-top:1px solid var(--ui-border);padding:12px;font-size:8.5px;color:var(--ui-muted);font-family:var(--mono);line-height:1.5;max-height:220px;overflow-y:auto;display:none;white-space:pre-wrap;word-break:break-all}- 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(max-width:600px){.layout{flex-direction:column}.stage{width:100%;flex:none}aside{width:100%}:root{--stage-h:380px}}- /* prefers-reduced-motion: the script renders one static frame and starts paused */</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="../cloth-simulation/" rel="prev" title="Previous">‹ Prev</a><a class="ah-hideSm" href="../morphing-blob/" 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/volumetric-smoke/index.html" target="_blank" rel="noopener noreferrer">Source ↗</a></span></div>-<header>- <h1>Volumetric Smoke / 3D Noise</h1>- <p>Ray marching through a 3D noise field — density accumulated along each ray for a true volumetric look.</p>-</header>-<div class="layout">- <div class="stage" id="stage">- <canvas id="cvs"></canvas>- <div class="hud">- <div class="badge" id="fps">— fps</div>- <div class="badge" id="steps-badge">32 steps</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="../cloth-simulation/" rel="prev" aria-label="Previous: Cloth / Soft-Body Simulation"><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>Cloth / Soft-Body Simulation</span></a><a href="../morphing-blob/" rel="next" aria-label="Next: Morphing Blob"><span><span class="hb-dir">Next: </span>Morphing Blob</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.16 · 3D & Advanced</p>+ <h1>Volumetric Smoke / 3D Noise</h1>+ <p class="hb-lede">Soft smoke curls upward, drawn as a real 3D cloud. Best for moody 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. Pause it to look closely, or turn on slow motion to see each part of the movement.</p></div>+ </div>+ <div class="stage" id="stage">+ <canvas id="cvs" role="img" aria-label="Soft smoke rising and curling against a dark background"></canvas>+ <div class="msg" id="msg" role="status"></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="dens-lbl">Thickness</p>+ <div class="seg" id="dens-seg" role="group" aria-labelledby="dens-lbl">+ <button type="button" data-dens="0.4" aria-pressed="false">Thin</button>+ <button type="button" data-dens="0.7" class="on" aria-pressed="true">Medium</button>+ <button type="button" data-dens="1.2" aria-pressed="false">Thick</button>+ </div>+ <p class="hb-hint">Thin smoke is wispy; thick smoke hides what is behind.</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.35" aria-pressed="false">Slow</button>+ <button type="button" data-spd="0.6" class="on" aria-pressed="true">Normal</button>+ <button type="button" data-spd="1" aria-pressed="false">Fast</button>+ </div>+ <p class="hb-hint">How fast the smoke rises.</p>+ </div>+ <div class="hb-setting">+ <p class="hb-setting-name" id="steps-lbl">Detail</p>+ <div class="seg" id="steps-seg" role="group" aria-labelledby="steps-lbl">+ <button type="button" data-steps="16" aria-pressed="false">Low</button>+ <button type="button" data-steps="32" class="on" aria-pressed="true">Medium</button>+ <button type="button" data-steps="64" aria-pressed="false">High</button>+ </div>+ <p class="hb-hint">More steps look smoother but cost more.</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="smoke-lbl">Smoke color</p>+ <div class="swatches" id="smoke-sw" role="group" aria-labelledby="smoke-lbl">+ <button type="button" class="on" aria-pressed="true" aria-label="Gray blue" data-color="#9ab4d8" style="background:#9ab4d8"></button>+ <button type="button" aria-pressed="false" aria-label="White" data-color="#f4f4f2" style="background:#f4f4f2"></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="Orange" data-color="#ffa657" style="background:#ffa657"></button>+ </div>+ <p class="hb-hint">The color of the lit smoke.</p>+ </div>+ <div class="hb-setting">+ <label class="hb-switch-row"><span>Wind</span><input class="hb-switch" type="checkbox" role="switch" id="wind-tog" checked autocomplete="off"></label>+ <p class="hb-hint">The smoke drifts slowly to one side.</p>+ </div>+ <div class="hb-setting">+ <label class="hb-switch-row"><span>Fine grain</span><input class="hb-switch" type="checkbox" role="switch" id="dither-tog" checked autocomplete="off"></label>+ <p class="hb-hint">Grain hides the stripes that few steps leave.</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 volumetric smoke to [the section you want it in]. In a fragment shader, send a ray through a 3D cloud of smooth noise for every pixel and add up how much smoke it passes through, shading each step by how much smoke lies between it and the light, so the plume looks soft, deep and lit from one side. Move the noise upward over time so the smoke rises and curls, and let it drift sideways when the settings include wind. It is costly, so draw fewer pixels and take fewer steps on phones. If the visitor has reduced motion turned on, show the smoke 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>Moody backgrounds</li><li>Title cards</li><li>Games</li><li>Sci-fi themes</li></ul>+ <h3>Avoid on</h3>+ <ul class="hb-tags hb-avoid"><li>Behind text</li><li>Low-power devices</li></ul></div>- <div class="msg" id="msg"></div>- <div class="src-panel" id="src-panel"></div>- </div>- <aside>- <p class="note">Volumetric rendering accumulates density along a ray through 3D space — unlike surface rendering which stops at the first hit. Films use this for every cloud, fog, and smoke effect.</p>- <div class="lbl">Smoke Density</div>- <div class="sr"><input type="range" id="dens-sl" min="1" max="20" value="7" step="1"><span class="sv" id="dens-v">0.7</span></div>- <div class="lbl">Animation Speed</div>- <div class="sr"><input type="range" id="spd-sl" min="1" max="20" value="6" step="1"><span class="sv" id="spd-v">0.6</span></div>- <div class="lbl">March Steps</div>- <div class="sr"><input type="range" id="steps-sl" min="8" max="96" value="32" step="8"><span class="sv" id="steps-v">32</span></div>- <div class="lbl">Smoke Color</div>- <input type="color" id="smoke-pick" value="#9ab4d8" aria-label="Smoke 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="wind-tog" checked><span>Wind drift</span></label>- <label class="tog"><input type="checkbox" id="dither-tog" checked><span>Noise dither (reduce banding)</span></label>- <label class="tog"><input type="checkbox" id="src-tog"><span>View shader source</span></label>- <div class="div"></div>- <button class="rst" id="pause-btn">⏸ Pause</button>- </aside>-</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 VS=`attribute vec2 a;void main(){gl_Position=vec4(a,0.,1.);}`;const FS=`@@ -111,7 +162,7 @@ precision highp float;#elseprecision mediump float;#endif-uniform vec2 uRes;uniform float uT,uDens,uSpd;uniform int uSteps;+uniform vec2 uRes;uniform float uT,uDens,uRise;uniform int uSteps;uniform bool uWind,uDither;uniform vec3 uSmoke,uBG;// 3D value noise via hash@@ -134,7 +185,7 @@ float density(vec3 p,float t){vec3 q=p+vec3(windX,0.,0.);// Cone source shape: narrower at topfloat r=length(p.xz);float src=exp(-r*3.)*smoothstep(-.2,0.6,p.y)*(1.-smoothstep(1.0,1.8,p.y));- float n=fbm(q*1.4+vec3(0.,t*uSpd*.4,0.));+ float n=fbm(q*1.4+vec3(0.,uRise*.4,0.));return max(0.,n-.45)*src*uDens*2.;}@@ -176,87 +227,107 @@ void main(){}`;const stage=document.getElementById('stage'),cvs=document.getElementById('cvs');-const fpsEl=document.getElementById('fps'),stepsBadge=document.getElementById('steps-badge');-const srcPanel=document.getElementById('src-panel'),msgEl=document.getElementById('msg');-const stepsSl=document.getElementById('steps-sl');-let W,H,gl,prog,raf=null,paused=false,lastT=0,startT=0,pausedAt=0,curT=2;+const msgEl=document.getElementById('msg'),slowTog=document.getElementById('slow-tog');+const UNIFORMS=['uRes','uT','uDens','uRise','uSteps','uWind','uDither','uSmoke','uBG'];+// The clock and the rise start where the old still frame was (2 seconds in, at the default speed), so the first picture is that frame+let W=0,H=0,gl=null,prog=null,U=null,raf=null,paused=false,last=null,curT=2,rise=1.2,wasPhone=false,lost=false;let DENS=0.7,SPD=0.6,STEPS=32,WIND=true,DITHER=true;-let SMOKE=[154/255,180/255,216/255],BG=[3/255,8/255,16/255];+let SMOKE=[154/255,180/255,216/255];+const BG=[3/255,8/255,16/255];+// Phones are screens up to 600px wide, or up to 500px tall when held sideways+const PHONE=matchMedia('(max-width:600px),(max-height:500px)');function h2r(h){return[parseInt(h.slice(1,3),16)/255,parseInt(h.slice(3,5),16)/255,parseInt(h.slice(5,7),16)/255];}-function fail(msg){msgEl.textContent=msg;msgEl.classList.add('on');fpsEl.textContent='—';}+// A canvas that can never draw is also taken out of the page for screen readers (hide); a lost context leaves it under the message.+function fail(msg,hide){msgEl.textContent=msg;msgEl.classList.add('on');if(hide)cvs.hidden=true;}function init(){- gl=cvs.getContext('webgl');if(!gl)return false;+ gl=cvs.getContext('webgl');+ if(!gl){fail('WebGL is unavailable in this browser, so the volume cannot be ray marched.',true);return false;}let glErr='';function sh(t,s){const x=gl.createShader(t);gl.shaderSource(x,s);gl.compileShader(x);if(!gl.getShaderParameter(x,gl.COMPILE_STATUS)){glErr=(gl.getShaderInfoLog(x)||'shader compile failed').replace(/\0/g,'').trim();console.error(glErr);}return x;}prog=gl.createProgram();gl.attachShader(prog,sh(gl.VERTEX_SHADER,VS));gl.attachShader(prog,sh(gl.FRAGMENT_SHADER,FS));gl.linkProgram(prog);if(!gl.getProgramParameter(prog,gl.LINK_STATUS)){const e=(gl.getProgramInfoLog(prog)||'program link failed').replace(/\0/g,'').trim();console.error(e);if(!glErr)glErr=e;}// A failed compile otherwise draws nothing at all, with no visible symptom.- if(glErr){fail('Shader failed to build: '+glErr);return false;}+ if(glErr){fail('Shader failed to build: '+glErr,true);gl=null;return false;}const buf=gl.createBuffer();gl.bindBuffer(gl.ARRAY_BUFFER,buf);gl.bufferData(gl.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),gl.STATIC_DRAW);const a=gl.getAttribLocation(prog,'a');gl.enableVertexAttribArray(a);gl.vertexAttribPointer(a,2,gl.FLOAT,false,0,0);- srcPanel.textContent=FS;return true;+ U={};UNIFORMS.forEach(n=>{U[n]=gl.getUniformLocation(prog,n);});+ return true;}-function setSteps(n){STEPS=n;stepsSl.value=n;document.getElementById('steps-v').textContent=n;stepsBadge.textContent=n+' steps';}-// Ray marching is fill-rate bound, so the backing store never exceeds one CSS-// pixel per fragment. Phones render below that and cap the march steps as well-// — that pair is the reduced-quality path this effect needs on mobile.-function quality(){const mob=innerWidth<=600;return{scale:Math.min(devicePixelRatio||1,mob?.75:1),maxSteps:mob?48:96};}-function resize(){- const q=quality();- W=cvs.width=Math.max(1,Math.round(stage.clientWidth*q.scale));- H=cvs.height=Math.max(1,Math.round(stage.clientHeight*q.scale));- if(+stepsSl.max!==q.maxSteps){stepsSl.max=q.maxSteps;if(STEPS>q.maxSteps)setSteps(q.maxSteps);}- if(gl){gl.viewport(0,0,W,H);refresh();}+// After a lost context nothing draws again until the page is reloaded (as the message says), even if the browser restores the context+const live=()=>!!gl&&!lost&&!gl.isContextLost();++// Ray marching is fill-rate bound, so the backing store never exceeds one CSS pixel per fragment. Phones render below that+// (0.75) and take at most 48 steps, so High is 48 there: that pair is the reduced-quality path this effect needs on mobile.+// The visitor's Detail stays as chosen; the limit is applied when drawing, so it follows the screen both ways.+function steps(){return Math.min(STEPS,PHONE.matches?48:64);}+function layout(force){+ const phone=PHONE.matches,s=Math.min(window.devicePixelRatio||1,phone?.75:1);+ const w=Math.max(1,Math.round(stage.clientWidth*s)),h=Math.max(1,Math.round(stage.clientHeight*s));+ // Setting the size clears the canvas, so nothing is touched when the stage is the same size and the phone limits are the same+ // (a phone's address bar showing or hiding also fires resize).+ if(!force&&w===W&&h===H&&phone===wasPhone)return false;+ W=cvs.width=w;H=cvs.height=h;wasPhone=phone;+ if(gl)gl.viewport(0,0,W,H);+ return true;}-function u(n){return gl.getUniformLocation(prog,n);}-// Resizing clears the canvas and control changes need a frame: redraw while paused-function refresh(){if(paused&&gl)draw(curT);}+// While paused a setting still draws the picture again, so its effect shows without anything moving.+function refresh(){if(paused&&live())draw(curT);}+// The loop keeps exactly one pending frame in raf. The clock and the rise add up the time of each drawn frame (at most 50 ms,+// and a third of it while Slow motion is on); the rise adds the speed as well, so a pause or a new speed never makes the smoke jump.function render(ts){- if(paused){raf=null;return;}raf=requestAnimationFrame(render);- const dt=ts-lastT;if(dt<16)return;- fpsEl.textContent=Math.round(1000/(dt||16))+' fps';lastT=ts;- draw((ts-startT)/1000);+ if(last===null){last=ts;return;} // the first frame after a start only takes the time+ const dt=ts-last;if(dt<16)return;+ last=ts;+ const step=Math.min(dt,50)/1000/(slowTog.checked?3:1);+ curT+=step;rise+=step*SPD;+ draw(curT);}function draw(t){- curT=t;gl.useProgram(prog);- gl.uniform2f(u('uRes'),W,H);gl.uniform1f(u('uT'),t);- gl.uniform1f(u('uDens'),DENS);gl.uniform1f(u('uSpd'),SPD);- gl.uniform1i(u('uSteps'),STEPS);- gl.uniform1i(u('uWind'),WIND?1:0);gl.uniform1i(u('uDither'),DITHER?1:0);- gl.uniform3fv(u('uSmoke'),SMOKE);gl.uniform3fv(u('uBG'),BG);+ gl.uniform2f(U.uRes,W,H);gl.uniform1f(U.uT,t);+ gl.uniform1f(U.uDens,DENS);gl.uniform1f(U.uRise,rise);+ gl.uniform1i(U.uSteps,steps());+ gl.uniform1i(U.uWind,WIND?1:0);gl.uniform1i(U.uDither,DITHER?1:0);+ gl.uniform3fv(U.uSmoke,SMOKE);gl.uniform3fv(U.uBG,BG);gl.drawArrays(gl.TRIANGLE_STRIP,0,4);}-const pauseBtn=document.getElementById('pause-btn');+// Play carries on from the clock and the rise where they stopped; it never stacks a second loop.+function start(){if(raf!==null||!live())return;last=null;raf=requestAnimationFrame(render);}+function stop(){if(raf!==null)cancelAnimationFrame(raf);raf=null;}++// 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.+document.addEventListener('hb:pause',e=>{+ paused=e.detail.paused;+ if(paused)stop();else start();+});+if(init()){- resize();startT=performance.now();lastT=startT;window.addEventListener('resize',resize);- cvs.addEventListener('webglcontextlost',e=>{e.preventDefault();paused=true;cancelAnimationFrame(raf);raf=null;fail('WebGL context lost — reload the page to restart the render.');});- if(matchMedia('(prefers-reduced-motion: reduce)').matches){- paused=true;pausedAt=startT;pauseBtn.textContent='▶ Resume';fpsEl.textContent='static';draw(curT);- }else raf=requestAnimationFrame(render);-}else if(!gl)fail('WebGL is unavailable in this browser, so the volume cannot be ray marched.');+ layout(true);+ draw(curT); // the first picture, so a loop that is paused on arrival never shows an empty stage+ start();+ window.addEventListener('resize',()=>{if(layout()&&live())draw(curT);});+ cvs.addEventListener('webglcontextlost',e=>{e.preventDefault();lost=true;stop();fail('WebGL context lost — reload the page to restart the render.');});+}-document.getElementById('dens-sl').addEventListener('input',e=>{DENS=e.target.value/10;document.getElementById('dens-v').textContent=DENS.toFixed(1);refresh();});-document.getElementById('spd-sl').addEventListener('input',e=>{SPD=e.target.value/10;document.getElementById('spd-v').textContent=SPD.toFixed(1);refresh();});-stepsSl.addEventListener('input',e=>{setSteps(+e.target.value);refresh();});-document.getElementById('smoke-pick').addEventListener('input',e=>{SMOKE=h2r(e.target.value);refresh();});-document.getElementById('bg-pick').addEventListener('input',e=>{BG=h2r(e.target.value);refresh();});+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('dens-seg',d=>{DENS=+d.dens;refresh();});+choices('speed-seg',d=>{SPD=+d.spd;});+choices('steps-seg',d=>{STEPS=+d.steps;refresh();});+choices('smoke-sw',d=>{SMOKE=h2r(d.color);refresh();});document.getElementById('wind-tog').addEventListener('change',e=>{WIND=e.target.checked;refresh();});document.getElementById('dither-tog').addEventListener('change',e=>{DITHER=e.target.checked;refresh();});-document.getElementById('src-tog').addEventListener('change',e=>{srcPanel.style.display=e.target.checked?'block':'none';});-pauseBtn.addEventListener('click',()=>{- paused=!paused;pauseBtn.textContent=paused?'▶ Resume':'⏸ Pause';- cancelAnimationFrame(raf);raf=null; // never leave a second loop queued- const now=performance.now();- if(paused){pausedAt=now;return;}- if(gl){startT+=now-pausedAt;lastT=now;raf=requestAnimationFrame(render);}-});</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.htmlindex c30eaa7c..fd9fde76 100644--- a/index.html+++ b/index.html@@ -1023,7 +1023,7 @@ const CATS=[['gpgpu-particle-system','GPGPU Particle System','Tens of thousands of particles flow on the graphics chip. Best for hero effects.'],['image-distortion-hover','Image Distortion on Hover','Fragment shader displaces UV coordinates around the cursor — ripple, push, liquid, pixelate.'],['cloth-simulation','Cloth Simulation','A cloth sways in the wind, and you can drag it around. Best for playful pages.'],- ['volumetric-smoke','Volumetric Smoke','Ray marching accumulates 3D noise density — light scattering, shadow rays, fBm clouds.'],+ ['volumetric-smoke','Volumetric Smoke','Soft smoke curls upward, drawn as a real 3D cloud. Best for moody backgrounds.'],['morphing-blob','Morphing Blob','Metaball circles fused by an SVG blur-plus-contrast filter melt like liquid — the blob drifts and a droplet chases the pointer.'],['flip-card-3d','3D Flip Card','A card rotates in 3D to reveal its back face using preserve-3d, perspective, and backface-visibility.'],]