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 Flow Field to the guided-steps page
matin5970efcf3f24
diff --git a/animations/07-ambient-background/flow-field/README.md b/animations/07-ambient-background/flow-field/README.mdindex 95ccd2f1..5de74391 100644--- a/animations/07-ambient-background/flow-field/README.md+++ b/animations/07-ambient-background/flow-field/README.md@@ -1,7 +1,7 @@# Flow Field## What it is-Thousands of particles crawl across the canvas, each one steering by the angle of an invisible vector field. The field comes from a small noise function, so neighbouring particles curve in unison and the whole surface reveals smooth, river-like currents. Because the frame is dimmed rather than cleared, particles leave fading trails that trace the flow.+A flow field moves thousands of particles across a canvas, each one steering by the direction of an invisible current under it. The directions change smoothly from one spot to the next, so particles near each other curve together, and the whole surface shows gentle, river-like currents. The canvas is dimmed a little each frame instead of cleared, so every particle leaves a fading trail that traces the flow.## When to use it- Generative, organic hero backgrounds where every load looks slightly different@@ -32,20 +32,20 @@ p.x += Math.cos(a) * SPD; p.y += Math.sin(a) * SPD;## Key parameters| Parameter | Default | Effect ||-----------|---------|--------|-| Particle count | 900 | Density of the trails; the main cost driver |-| Noise scale | 34 | Larger = broader, calmer currents; smaller = tight turbulence |-| Speed | 1.0 | Step length per frame; higher smears the trails |-| Trail persistence | 0.06 | Fade alpha — low = long ghostly trails, high = short crisp ones |-| Color | mint | Fixed hue, or `spectrum` for a hue that maps to x-position |+| Swirl size | Medium | How large the currents are: the direction turns over about 20px (small), 34px (medium) or 60px (large); larger looks calm, smaller turbulent |+| Trail length | Medium | How long trails linger: short fades 12% a frame, medium 6% and long 4%; below about 4% trails can leave faint marks that never fade |+| Speed | Normal | How far each particle moves every frame: slow is 0.6px, normal 1px and fast 1.6px |+| Number of particles | Medium | Few is 400, medium 900 and many 1,500; phones show at most 500 |+| Color | Mint | Mint, ember or ice, or rainbow, where the color changes across the stage and over time |## Production notes- **Trail alpha vs. buildup**: because trails rely on incomplete clearing, a very low persistence value can leave permanent residue on some GPUs. Nudge it up (0.04+) if you see ghosting that never fully fades.-- **Mobile cap**: particle count is limited to 500 under 600px. Each particle is a stroked line segment, so fill rate — not math — is the bottleneck on phones.+- **Mobile cap**: particle count is limited to 500 on phone-sized screens (up to 600px wide, or up to 500px tall for a phone held sideways). Each particle is a stroked line segment, so fill rate — not math — is the bottleneck on phones.- **Deterministic noise**: the `hash` uses `sin(x*127.1 + y*311.7)*43758.5453`, a classic GLSL trick. It is not cryptographic and not true Perlin noise, but it is cheap, dependency-free, and smooth enough for a field.-- **Reduced motion**: on `prefers-reduced-motion`, the loop is skipped and ~40 frames are pre-rendered once to leave a static, settled composition.+- **Reduced motion**: the demo starts paused, showing about 40 frames drawn at once as a still, settled picture, until the visitor presses Play. The same 40 frames are drawn whenever the page opens, so the stage never starts empty.- **Library equivalents**: production flow fields usually run on the GPU — [three.js](https://threejs.org) with a fragment/compute shader, or curl-noise in a particle system. [tsParticles](https://github.com/matteobruni/tsparticles) does not do true flow fields, but its path plugins approximate directed motion.## See also-- [Particle Constellation](../particle-constellation/) — particles that link rather than flow-- [Aurora](../aurora/) — flowing colour bands from blur instead of particles-- [Mesh Gradient](../mesh-gradient/) — smooth drifting colour with no discrete particles+- [Particle Constellation](../particle-constellation/) — particles that link up instead of flowing+- [Aurora / Northern Lights](../aurora/) — flowing bands of color made with blur+- [Mesh Gradient Animation](../mesh-gradient/) — smooth drifting color with no particlesdiff --git a/animations/07-ambient-background/flow-field/index.html b/animations/07-ambient-background/flow-field/index.htmlindex 48ad578b..2c9d6170 100644--- a/animations/07-ambient-background/flow-field/index.html+++ b/animations/07-ambient-background/flow-field/index.html@@ -4,7 +4,7 @@<meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Flow Field — Animation Handbook</title>- <meta name="description" content="Particles ride a vector field sampled from inline pseudo-noise, leaving fading trails that map the invisible currents.">+ <meta name="description" content="Particles ride invisible currents, leaving fading trails. Best for art pages."><link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/flow-field/"><link rel="icon" type="image/png" sizes="96x96" href="../../../favicon.png"><link rel="icon" type="image/svg+xml" href="../../../favicon.svg">@@ -12,159 +12,252 @@<meta property="og:type" content="article"><meta property="og:site_name" content="Animation Handbook"><meta property="og:title" content="Flow Field — Animation Handbook">- <meta property="og:description" content="Particles ride a vector field sampled from inline pseudo-noise, leaving fading trails that map the invisible currents.">+ <meta property="og:description" content="Particles ride invisible currents, leaving fading trails. Best for art pages."><meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/flow-field/"><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="Flow Field — Animation Handbook">- <meta name="twitter:description" content="Particles ride a vector field sampled from inline pseudo-noise, leaving fading trails that map the invisible currents.">+ <meta name="twitter:description" content="Particles ride invisible currents, leaving fading trails. Best for art pages."><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": "Flow Field", "name": "Flow Field", "description": "Particles ride a vector field sampled from inline pseudo-noise, leaving fading trails that map the invisible currents.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/flow-field/", "inLanguage": "en", "articleSection": "Ambient & Background", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Ambient & Background, Flow Field"}</script>+ <script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "Flow Field", "name": "Flow Field", "description": "Particles ride invisible currents, leaving fading trails. Best for art pages.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/flow-field/", "inLanguage": "en", "articleSection": "Ambient & Background", "isPartOf": {"@type": "WebSite", "name": "Animation Handbook", "url": "https://matinmonshizadeh.github.io/animation-handbook/"}, "author": {"@type": "Person", "name": "matinmonshizadeh"}, "keywords": "web animation, CSS animation, Ambient & Background, Flow Field"}</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:'PlexMono';src:url('../../../assets/fonts/plex-400.woff2') format('woff2');font-weight:400;font-display:swap}- :root{--bg:#0b0b0d;--ui-bg:#111114;--ui-border:rgba(255,255,255,.09);--ui-accent:#7affc8;--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:#ffce5a}*,*::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;background:#05060a}+ .stage{position:relative;overflow:hidden;background:#05060a}canvas{display:block;width:100%;height:100%}- .fps-badge{position:absolute;top:8px;right:8px;font-size:9px;color:rgba(255,255,255,.4);background:rgba(0,0,0,.5);padding:3px 8px;border-radius:4px;pointer-events:none}- 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:44px;text-align:right;color:var(--ui-text)}- select{background:var(--ui-bg);border:1px solid var(--ui-border);color:var(--ui-text);font-family:monospace;font-size:10px;padding:8px 6px;border-radius:4px;width:100%;min-height:44px}- button.rst{background:transparent;border:1px solid var(--ui-border);color:var(--ui-text);font-family:monospace;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;align-items:stretch}.stage{flex:none;width:100%}aside{width:100%}:root{--stage-h:380px}}- @media(prefers-reduced-motion:reduce){.fps-badge{display:none}}</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="../particle-constellation/" rel="prev" title="Previous">‹ Prev</a><a class="ah-hideSm" href="../synthwave-grid/" 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/07-ambient-background/flow-field/index.html" target="_blank" rel="noopener noreferrer">Source ↗</a></span></div>-<header>- <h1>Flow Field</h1>- <p>Particles ride a vector field sampled from inline pseudo-noise, leaving fading trails that map the invisible currents.</p>-</header>-<div class="layout">- <div class="stage" id="stage">- <canvas id="cvs"></canvas>- <div class="fps-badge" id="fps">— fps</div>- </div>- <aside>- <p class="note">There is no image here — only a math function that returns an angle for every point on screen. Each particle reads the angle under it and steps that way. Trails appear because the frame is dimmed, not cleared.</p>- <div class="lbl">Particle Count</div>- <div class="sr"><input type="range" id="count-sl" min="200" max="2000" value="900" step="100"><span class="sv" id="count-v">900</span></div>- <div class="lbl">Noise Scale</div>- <div class="sr"><input type="range" id="scale-sl" min="10" max="80" value="34" step="2"><span class="sv" id="scale-v">34</span></div>- <div class="lbl">Speed</div>- <div class="sr"><input type="range" id="spd-sl" min="2" max="30" value="10" step="1"><span class="sv" id="spd-v">1.0</span></div>- <div class="lbl">Trail Persistence</div>- <div class="sr"><input type="range" id="trail-sl" min="1" max="14" value="6" step="1"><span class="sv" id="trail-v">0.06</span></div>- <div class="lbl">Color</div>- <select id="col-sel">- <option value="mint" selected>Mint</option>- <option value="ember">Ember</option>- <option value="ice">Ice</option>- <option value="spectrum">Spectrum</option>- </select>- <div class="div"></div>- <div style="font-size:9px;color:var(--ui-muted)">Count caps at 500 on small screens.</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="../particle-constellation/" rel="prev" aria-label="Previous: Particle Constellation"><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>Particle Constellation</span></a><a href="../synthwave-grid/" rel="next" aria-label="Next: Synthwave Grid"><span><span class="hb-dir">Next: </span>Synthwave Grid</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">07.14 · Ambient & Background</p>+ <h1>Flow Field</h1>+ <p class="hb-lede">Particles ride invisible currents, leaving fading trails. Best for art pages.</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">+ <canvas id="cvs" aria-hidden="true"></canvas>+ </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="scale-lbl">Swirl size</p>+ <div class="seg" id="scale-seg" role="group" aria-labelledby="scale-lbl">+ <button type="button" data-px="20" aria-pressed="false">Small</button>+ <button type="button" data-px="34" class="on" aria-pressed="true">Medium</button>+ <button type="button" data-px="60" aria-pressed="false">Large</button>+ </div>+ <p class="hb-hint">Large swirls look calm; small ones look stormy.</p>+ </div>+ <div class="hb-setting">+ <p class="hb-setting-name" id="trail-lbl">Trail length</p>+ <div class="seg" id="trail-seg" role="group" aria-labelledby="trail-lbl">+ <button type="button" data-fade="0.12" aria-pressed="false">Short</button>+ <button type="button" data-fade="0.06" class="on" aria-pressed="true">Medium</button>+ <button type="button" data-fade="0.04" aria-pressed="false">Long</button>+ </div>+ <p class="hb-hint">How long each particle's trail lingers.</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.6" aria-pressed="false">Slow</button>+ <button type="button" data-spd="1" class="on" aria-pressed="true">Normal</button>+ <button type="button" data-spd="1.6" aria-pressed="false">Fast</button>+ </div>+ <p class="hb-hint">How far each particle moves every frame.</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="count-lbl">Number of particles</p>+ <div class="seg" id="count-seg" role="group" aria-labelledby="count-lbl">+ <button type="button" data-n="400" aria-pressed="false">Few</button>+ <button type="button" data-n="900" class="on" aria-pressed="true">Medium</button>+ <button type="button" data-n="1500" aria-pressed="false">Many</button>+ </div>+ <p class="hb-hint">More particles fill the currents; phones show at most 500.</p>+ </div>+ <div class="hb-setting">+ <p class="hb-setting-name" id="color-lbl">Color</p>+ <div class="seg" id="color-seg" role="group" aria-labelledby="color-lbl">+ <button type="button" data-col="mint" class="on" aria-pressed="true">Mint</button>+ <button type="button" data-col="ember" aria-pressed="false">Ember</button>+ <button type="button" data-col="ice" aria-pressed="false">Ice</button>+ <button type="button" data-col="spectrum" aria-pressed="false">Rainbow</button>+ </div>+ <p class="hb-hint">Rainbow changes color across the stage and over time.</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 a flow field background to [the section you want it behind]. Fill a canvas with many particles, give every point a direction that changes smoothly from one spot to the next (smooth random noise does this), and move each particle a small step along the direction under it every frame, so neighbours curve together into currents. Instead of clearing the canvas, cover it with a see-through dark layer each frame, so every particle leaves a trail that slowly fades. Let the directions change slowly over time, and use fewer particles on phones. If the visitor has reduced motion turned on, show one still frame of trails. 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>Art and creative sites</li><li>Hero sections</li><li>Loading screens</li><li>Data art</li></ul>+ <h3>Avoid on</h3>+ <ul class="hb-tags hb-avoid"><li>Busy layouts</li><li>Long reading pages</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 cvs=document.getElementById('cvs'),ctx=cvs.getContext('2d');-const stage=document.getElementById('stage'),fpsEl=document.getElementById('fps');-let W,H,paused=false,raf=null,lastT=0,t=0;-let COUNT=900,SCALE=34,SPD=1.0,TRAIL=0.06,COL='mint';-let parts=[];-const reduce=window.matchMedia('(prefers-reduced-motion:reduce)').matches;+ const cvs=document.getElementById('cvs'),ctx=cvs.getContext('2d');+ const stage=document.querySelector('.stage');+ const slowTog=document.getElementById('slow-tog');+ const PHONE_PARTS=500; // phones and short screens draw at most this many particles+ const phone=matchMedia('(max-width:600px),(max-height:500px)');+ let W=0,H=0,raf=null,lastT=0,paused=false,unseen=false,t=0;+ let COUNT=900,SCALE=34,SPD=1.0,TRAIL=0.06,COL='mint';+ let parts=[];++ // tiny deterministic value-noise: hash a lattice, smooth-interpolate, return an angle.+ function hash(x,y){const s=Math.sin(x*127.1+y*311.7)*43758.5453;return s-Math.floor(s);}+ function smooth(t){return t*t*(3-2*t);}+ function vnoise(x,y){+ const xi=Math.floor(x),yi=Math.floor(y),xf=x-xi,yf=y-yi;+ const tl=hash(xi,yi),tr=hash(xi+1,yi),bl=hash(xi,yi+1),br=hash(xi+1,yi+1);+ const u=smooth(xf),v=smooth(yf);+ return (tl*(1-u)+tr*u)*(1-v)+(bl*(1-u)+br*u)*v;+ }+ function angleAt(x,y,time){+ const n=vnoise(x/SCALE,y/SCALE+time*0.15);+ return n*Math.PI*4; // map [0,1] noise to a couple of full turns+ }-// tiny deterministic value-noise: hash a lattice, smooth-interpolate, return an angle.-function hash(x,y){const s=Math.sin(x*127.1+y*311.7)*43758.5453;return s-Math.floor(s);}-function smooth(t){return t*t*(3-2*t);}-function vnoise(x,y){- const xi=Math.floor(x),yi=Math.floor(y),xf=x-xi,yf=y-yi;- const tl=hash(xi,yi),tr=hash(xi+1,yi),bl=hash(xi,yi+1),br=hash(xi+1,yi+1);- const u=smooth(xf),v=smooth(yf);- return (tl*(1-u)+tr*u)*(1-v)+(bl*(1-u)+br*u)*v;-}-function angleAt(x,y,time){- const n=vnoise(x/SCALE,y/SCALE+time*0.15);- return n*Math.PI*4; // map [0,1] noise to a couple of full turns-}+ function init(){parts=Array.from({length:phone.matches?Math.min(COUNT,PHONE_PARTS):COUNT},()=>({x:Math.random()*W,y:Math.random()*H}));}-function effCount(){return W<600?Math.min(COUNT,500):COUNT;}-function resize(){W=cvs.width=stage.clientWidth;H=cvs.height=stage.clientHeight;init();ctx.fillStyle='#05060a';ctx.fillRect(0,0,W,H);if(reduce)prerender();}-function init(){parts=Array.from({length:effCount()},()=>({x:Math.random()*W,y:Math.random()*H}));}+ // Rainbow takes its color from a table of whole degrees: building a color string for every particle costs about four times more+ const HUES=Array.from({length:360},(_,h)=>`hsla(${h},80%,65%,.5)`);+ function colorFor(p){+ if(COL==='mint')return'rgba(122,255,200,.5)';+ if(COL==='ember')return'rgba(255,150,80,.5)';+ if(COL==='ice')return'rgba(120,190,255,.5)';+ return HUES[Math.floor((p.x/W*120+t*20)%360)];+ }-function colorFor(p){- if(COL==='mint')return'rgba(122,255,200,.5)';- if(COL==='ember')return'rgba(255,150,80,.5)';- if(COL==='ice')return'rgba(120,190,255,.5)';- return`hsla(${(p.x/W*120+t*20)%360},80%,65%,.5)`;-}+ // One frame: dim the canvas a little instead of clearing it, then move every particle one step along the field and+ // draw that step. k is 1, or a third in slow motion: the step and the field's time shrink by it, the fade does not.+ function frame(){+ const k=slowTog.checked?1/3:1;+ ctx.fillStyle=`rgba(5,6,10,${TRAIL})`;ctx.fillRect(0,0,W,H);+ for(const p of parts){+ const a=angleAt(p.x,p.y,t);+ const nx=p.x+Math.cos(a)*SPD*k,ny=p.y+Math.sin(a)*SPD*k;+ ctx.strokeStyle=colorFor(p);ctx.lineWidth=1;+ ctx.beginPath();ctx.moveTo(p.x,p.y);ctx.lineTo(nx,ny);ctx.stroke();+ p.x=nx;p.y=ny;+ if(p.x<0||p.x>W||p.y<0||p.y>H){p.x=Math.random()*W;p.y=Math.random()*H;}+ }+ t+=0.01*k;+ }+ // A settled picture: 40 frames at once. The canvas is covered completely first, because a see-through dim leaves a+ // faint ghost of whatever was there before+ function prerender(){+ ctx.fillStyle='#05060a';ctx.fillRect(0,0,W,H);+ for(let i=0;i<40;i++)frame();+ unseen=false;+ }-function frame(){- // translucent fill instead of clear -> old pixels fade, forming trails- ctx.fillStyle=`rgba(5,6,10,${TRAIL})`;ctx.fillRect(0,0,W,H);- for(const p of parts){- const a=angleAt(p.x,p.y,t);- const nx=p.x+Math.cos(a)*SPD,ny=p.y+Math.sin(a)*SPD;- ctx.strokeStyle=colorFor(p);ctx.lineWidth=1;- ctx.beginPath();ctx.moveTo(p.x,p.y);ctx.lineTo(nx,ny);ctx.stroke();- p.x=nx;p.y=ny;- if(p.x<0||p.x>W||p.y<0||p.y>H){p.x=Math.random()*W;p.y=Math.random()*H;}+ // loop() keeps exactly one frame requested in raf; Pause cancels it and Play starts one loop again. At most one frame per 16 ms.+ function loop(ts){+ raf=requestAnimationFrame(loop);+ if(ts-lastT<16)return;+ lastT=ts;+ frame();unseen=false;}- t+=0.01;-}-// reduced-motion: paint a settled composition once instead of looping-function prerender(){for(let i=0;i<40;i++)frame();}+ function start(){if(raf)return;lastT=0;raf=requestAnimationFrame(loop);}-function loop(ts){- if(paused){raf=null;return;}- raf=requestAnimationFrame(loop);- const dt=ts-lastT;if(dt<16)return;lastT=ts;- fpsEl.textContent=Math.round(1000/(dt||16))+' fps';- frame();-}+ // 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. The picture on screen stays as it is while paused, unless a+ // setting changed just before and no frame has drawn it yet.+ document.addEventListener('hb:pause',e=>{+ paused=e.detail.paused;+ if(paused){cancelAnimationFrame(raf);raf=null;if(unseen)prerender();}+ else start();+ });-resize();-if(reduce)fpsEl.textContent='static';-else raf=requestAnimationFrame(loop);-window.addEventListener('resize',resize);+ // Setting the canvas size wipes it, so a real change of the stage's size starts the particles again with a settled+ // picture at once, playing or paused. A resize that leaves the stage as it was (a phone's address bar sliding in or out) does nothing.+ function resize(){+ const w=stage.clientWidth,h=stage.clientHeight;+ if(w===W&&h===H)return;+ W=cvs.width=w;H=cvs.height=h;+ init();prerender();+ }++ function choices(id,pick){+ const buttons=document.querySelectorAll('#'+id+' button');+ buttons.forEach(b=>b.addEventListener('click',()=>{+ if(b.classList.contains('on'))return; // pressing the choice already made does nothing+ buttons.forEach(x=>{x.classList.toggle('on',x===b);x.setAttribute('aria-pressed',String(x===b))});+ pick(b.dataset);+ }));+ }+ // Playing, the next frames show a changed setting. Paused, the trails need steps to show it, so a fresh settled picture+ // is drawn; if Pause comes before any frame has, the picture is drawn then. Speed changes no picture: it keeps the one on screen.+ function changed(){if(paused)prerender();else unseen=true;}+ choices('scale-seg',d=>{SCALE=+d.px;changed();});+ choices('trail-seg',d=>{TRAIL=+d.fade;changed();});+ choices('speed-seg',d=>{SPD=+d.spd;});+ choices('count-seg',d=>{COUNT=+d.n;init();changed();});+ choices('color-seg',d=>{COL=d.col;changed();});-document.getElementById('count-sl').addEventListener('input',e=>{COUNT=+e.target.value;document.getElementById('count-v').textContent=COUNT;init();});-document.getElementById('scale-sl').addEventListener('input',e=>{SCALE=+e.target.value;document.getElementById('scale-v').textContent=SCALE;});-document.getElementById('spd-sl').addEventListener('input',e=>{SPD=e.target.value/10;document.getElementById('spd-v').textContent=SPD.toFixed(1);});-document.getElementById('trail-sl').addEventListener('input',e=>{TRAIL=e.target.value/100;document.getElementById('trail-v').textContent=TRAIL.toFixed(2);});-document.getElementById('col-sel').addEventListener('change',e=>{COL=e.target.value;});-document.getElementById('pause-btn').addEventListener('click',()=>{- paused=!paused;document.getElementById('pause-btn').textContent=paused?'▶ Resume':'⏸ Pause';- if(!paused){lastT=performance.now();raf=requestAnimationFrame(loop);}-});+ resize();+ window.addEventListener('resize',resize);+ start();+ phone.addEventListener('change',()=>{init();changed();}); // a window that crosses the phone size changes how many particles it shows</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 3f5a7090..166af748 100644--- a/index.html+++ b/index.html@@ -1046,7 +1046,7 @@ const CATS=[['grid-dot-pattern-parallax','Grid / Dot Pattern Parallax','A dot grid shifts gently against your pointer for depth. Best for tech sites.'],['abstract-geometric-motion','Abstract Geometric Motion','Shapes turn, spread and flow in a calm, endless pattern. Best for music players.'],['particle-constellation','Particle Constellation','Drifting dots link up with lines whenever they come close. Best for tech sites.'],- ['flow-field','Flow Field','Particles follow a noise-driven vector field, leaving flowing trails.'],+ ['flow-field','Flow Field','Particles ride invisible currents, leaving fading trails. Best for art pages.'],['synthwave-grid','Synthwave Grid','A perspective grid scrolls toward a glowing sun — the retro synthwave horizon.'],['matrix-rain','Matrix Rain','Columns of glowing characters fall down a dark canvas, leaders bright and trails fading.'],['plasma','Plasma Field','Summed sine functions of x, y, and time make a flowing, organic color field — the demoscene classic.'],