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 Plasma Field to the guided-steps page

matinda09d0ed5592
diff --git a/animations/07-ambient-background/plasma/README.md b/animations/07-ambient-background/plasma/README.md
index ea8b5874..6e3db624 100644
--- a/animations/07-ambient-background/plasma/README.md
+++ b/animations/07-ambient-background/plasma/README.md
@@ -2,10 +2,11 @@
## What it is
-Plasma is the classic demoscene background effect: a smoothly shifting field of
-colour with no hard edges, generated entirely from maths rather than an image or
-video. Every pixel's colour comes from summing a few sine waves of its x and y
-position plus a time offset, so the whole field breathes and flows in a loop.
+Plasma is a classic effect from the demoscene, the 1990s community that made art
+with home computers: a field of color with no hard edges that flows and shifts
+like liquid. It is made from math alone. Each point's color comes from adding up
+a few waves, based on its position and on time, so the field keeps moving
+without any image or video.
## When to use it
@@ -42,19 +43,21 @@ maths.
## Key parameters
-| Parameter | What it controls | Notes |
-|---|---|---|
-| `speed` | How fast the time offset `t` advances | 0 freezes the field; ~1× is a calm drift |
-| `scale` / `f` | Radians of wave per pixel | Higher = tighter, busier pattern; lower = broad soft blobs |
-| `palette` | The colour lookup table | Neon / sunset / ocean / mono, each a cosine gradient |
-| `quality` | Buffer width in pixels | 96 (cheap) to 200 (crisper); height follows the stage aspect |
-| `dpr cap` | Backing-store resolution of the view canvas | Capped at 1.5 so upscaling stays cheap |
+| Parameter | Default | Effect |
+|-----------|---------|--------|
+| Speed | Normal | How fast the field flows: slow is 0.6 times the base speed, normal 1 and fast 1.6 |
+| Pattern size | Medium | How big the color shapes are: small packs more waves into the field, large makes broad, soft blobs |
+| Colors | Neon | The palette: neon, sunset, ocean or rose |
+| Detail | Medium | How wide the small picture is before it is scaled up: low is 96 pixels, medium 140 and high 200; phones use at most 140 |
## Production notes
- The low-res buffer is the whole trick. A per-pixel `putImageData` over the full
canvas at device resolution will drop frames on mobile; a ~140px buffer upscaled
with `drawImage` keeps the work at a few thousand pixels per frame.
+- On phone-sized screens (up to 600px wide, or up to 500px tall for a phone held
+ sideways) the demo caps the buffer at 140 pixels wide; a phone's stage is small
+ enough that it looks the same.
- Precompute what you can: the radial distance of each pixel and the per-row and
per-column sines only need recomputing when size or zoom changes, leaving one
sine per pixel in the inner loop.
@@ -70,6 +73,6 @@ maths.
## See also
-- [Animated Gradient Background](../animated-gradient-background/)
-- [Mesh Gradient](../mesh-gradient/)
-- [Aurora](../aurora/)
+- [Animated Gradient Background](../animated-gradient-background/) — one gradient that slowly shifts, with no script
+- [Mesh Gradient Animation](../mesh-gradient/) — soft blobs of color drift and blend
+- [Aurora / Northern Lights](../aurora/) — bands of color sway like the northern lights
diff --git a/animations/07-ambient-background/plasma/index.html b/animations/07-ambient-background/plasma/index.html
index 95bc103e..67fffa88 100644
--- a/animations/07-ambient-background/plasma/index.html
+++ b/animations/07-ambient-background/plasma/index.html
@@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Plasma Field — Animation Handbook</title>
- <meta name="description" content="The demoscene plasma: color fields generated by summing sine waves of x, y, and time on a canvas.">
+ <meta name="description" content="Smooth waves of color flow endlessly, made from math. Best for creative sites.">
<link rel="canonical" href="https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/plasma/">
<link rel="icon" type="image/png" sizes="96x96" href="../../../favicon.png">
<link rel="icon" type="image/svg+xml" href="../../../favicon.svg">
@@ -12,92 +12,142 @@
<meta property="og:type" content="article">
<meta property="og:site_name" content="Animation Handbook">
<meta property="og:title" content="Plasma Field — Animation Handbook">
- <meta property="og:description" content="The demoscene plasma: color fields generated by summing sine waves of x, y, and time on a canvas.">
+ <meta property="og:description" content="Smooth waves of color flow endlessly, made from math. Best for creative sites.">
<meta property="og:url" content="https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/plasma/">
<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="Plasma Field — Animation Handbook">
- <meta name="twitter:description" content="The demoscene plasma: color fields generated by summing sine waves of x, y, and time on a canvas.">
+ <meta name="twitter:description" content="Smooth waves of color flow endlessly, made from math. Best for creative sites.">
<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": "Plasma Field", "name": "Plasma Field", "description": "The demoscene plasma: color fields generated by summing sine waves of x, y, and time on a canvas.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/plasma/", "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, Plasma Field"}</script>
+ <script type="application/ld+json">{"@context": "https://schema.org", "@type": "TechArticle", "headline": "Plasma Field", "name": "Plasma Field", "description": "Smooth waves of color flow endlessly, made from math. Best for creative sites.", "url": "https://matinmonshizadeh.github.io/animation-handbook/animations/07-ambient-background/plasma/", "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, Plasma 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:'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:#ff6f8b;--ui-text:#f4f4f2;--ui-muted:#77777e;--disp:'Bricolage','Arial Narrow',sans-serif;--mono:'PlexMono',ui-monospace,Menlo,Consolas,monospace;--stage-h:min(64vh,600px)}
+ :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-wrap{flex:1;min-width:0}
- .stage{width:100%;height:var(--stage-h);border:1px solid var(--ui-border);border-radius:8px;overflow:hidden;position:relative;background:#000}
+ .stage{position:relative;overflow:hidden;background:#000}
.stage canvas{position:absolute;inset:0;width:100%;height:100%;display:block}
- 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:46px;text-align:right;color:var(--ui-text)}
- select{background:var(--ui-bg);border:1px solid var(--ui-border);color:var(--ui-text);font-family:var(--mono);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: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;align-items:stretch}aside{width:100%}:root{--stage-h:52vh}}
</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="../matrix-rain/" rel="prev" title="Previous">‹ Prev</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/plasma/index.html" target="_blank" rel="noopener noreferrer">Source ↗</a></span></div>
-<header>
- <h1>Plasma Field</h1>
- <p>The demoscene plasma: color fields generated by summing sine waves of x, y, and time on a canvas.</p>
-</header>
-<div class="layout">
- <div class="stage-wrap">
- <div class="stage" id="stage"><canvas id="c" aria-hidden="true"></canvas></div>
- </div>
- <aside>
- <p class="note">A low-resolution buffer is computed pixel by pixel from a sum of sines, then scaled up smoothly so the whole field stays cheap enough to run on a phone.</p>
- <div class="lbl">Speed</div>
- <div class="sr"><input type="range" id="spd" min="0" max="200" value="100" step="5"><span class="sv" id="spd-v">1.0×</span></div>
- <div class="lbl">Scale / Zoom</div>
- <div class="sr"><input type="range" id="scl" min="30" max="200" value="100" step="5"><span class="sv" id="scl-v">1.0×</span></div>
- <div class="lbl">Palette</div>
- <select id="pal">
- <option value="neon" selected>Neon</option>
- <option value="sunset">Sunset</option>
- <option value="ocean">Ocean</option>
- <option value="mono">Mono</option>
- </select>
- <div class="lbl">Quality</div>
- <select id="qual">
- <option value="96">Low (96px)</option>
- <option value="140" selected>Medium (140px)</option>
- <option value="200">High (200px)</option>
- </select>
- <div class="div"></div>
- <button class="rst" id="pause">⏸ 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="../matrix-rain/" rel="prev" aria-label="Previous: Matrix Rain"><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>Matrix Rain</span></a></span>
+</nav>
+<main class="hb-page">
+ <header class="hb-head">
+ <p class="hb-cat">07.17 · Ambient &amp; Background</p>
+ <h1>Plasma Field</h1>
+ <p class="hb-lede">Smooth waves of color flow endlessly, made from math. Best for creative sites.</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="c" 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="speed-lbl">Speed</p>
+ <div class="seg" id="speed-seg" role="group" aria-labelledby="speed-lbl">
+ <button type="button" data-speed="0.6" aria-pressed="false">Slow</button>
+ <button type="button" data-speed="1" class="on" aria-pressed="true">Normal</button>
+ <button type="button" data-speed="1.6" aria-pressed="false">Fast</button>
+ </div>
+ <p class="hb-hint">Slow drifts calmly; fast churns.</p>
+ </div>
+ <div class="hb-setting">
+ <p class="hb-setting-name" id="size-lbl">Pattern size</p>
+ <div class="seg" id="size-seg" role="group" aria-labelledby="size-lbl">
+ <button type="button" data-scale="1.6" aria-pressed="false">Small</button>
+ <button type="button" data-scale="1" class="on" aria-pressed="true">Medium</button>
+ <button type="button" data-scale="0.6" aria-pressed="false">Large</button>
+ </div>
+ <p class="hb-hint">Large shapes feel soft; small ones feel busy.</p>
+ </div>
+ <div class="hb-setting">
+ <p class="hb-setting-name" id="color-lbl">Colors</p>
+ <div class="seg" id="color-seg" role="group" aria-labelledby="color-lbl">
+ <button type="button" data-pal="neon" class="on" aria-pressed="true">Neon</button>
+ <button type="button" data-pal="sunset" aria-pressed="false">Sunset</button>
+ <button type="button" data-pal="ocean" aria-pressed="false">Ocean</button>
+ <button type="button" data-pal="mono" aria-pressed="false">Rose</button>
+ </div>
+ <p class="hb-hint">The set of colors the field flows through.</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="detail-lbl">Detail</p>
+ <div class="seg" id="detail-seg" role="group" aria-labelledby="detail-lbl">
+ <button type="button" data-px="96" aria-pressed="false">Low</button>
+ <button type="button" data-px="140" class="on" aria-pressed="true">Medium</button>
+ <button type="button" data-px="200" aria-pressed="false">High</button>
+ </div>
+ <p class="hb-hint">Sharper detail costs more; phones use at most Medium.</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 plasma background to [the section you want it behind]. For every point, add up a few waves based on its position and on time (across, down, diagonally and outward from the center) and use the sum to pick a color from a smooth palette, so the whole field flows like liquid color. To keep it fast, draw the field into a small image and scale it up smoothly to fill the section, using a smaller image on phones. If the visitor has reduced motion turned on, show one still frame. 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>Music and creative sites</li><li>Hero sections</li><li>Loading screens</li><li>Retro themes</li></ul>
+ <h3>Avoid on</h3>
+ <ul class="hb-tags hb-avoid"><li>Serious content</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 view=document.getElementById('c');
- const vctx=view.getContext('2d');
- const reduce=matchMedia('(prefers-reduced-motion: reduce)').matches;
+ const view=document.getElementById('c'),vctx=view.getContext('2d');
+ const stage=document.querySelector('.stage');
+ const slowTog=document.getElementById('slow-tog');
+ const phone=matchMedia('(max-width:600px),(max-height:500px)'); // phones and short screens get a smaller picture
// Inigo Quilez cosine palettes: color = a + b*cos(2π(c*t + d))
const PALETTES={
@@ -117,20 +167,26 @@ body{padding-top:46px}
return lut;
}
- // Low-res offscreen buffer, upscaled onto the view canvas.
- const buf=document.createElement('canvas');
- const bctx=buf.getContext('2d');
- let bw=0,bh=0,img=null,dist=null,sinX=null,sinY=null,sinC=null;
+ // The field is computed as a small picture (Detail wide, the stage's shape) and scaled up onto the view canvas.
+ const buf=document.createElement('canvas'),bctx=buf.getContext('2d');
+ let W=0,H=0,bw=0,bh=0,img=null,dist=null,sinX=null,sinY=null,sinC=null;
let lut=buildLUT(PALETTES.neon);
- let speed=1,scale=1,quality=140,paused=false;
+ let speed=1,scale=1,quality=140;
+ let raf=null,last=null,paused=false;
+ let clock=0; // the page's own time in seconds: it moves only while the loop runs, so Play carries on with no jump
+ // The small picture's width: Detail, at most 140 on phones and short screens, where High costs twice as much
+ function pictureWidth(){return phone.matches?Math.min(quality,140):quality}
+
+ // Sizes the view canvas to the stage, builds the small picture and what is precomputed for it, then draws the field
+ // at once, playing or paused: at load, after a real resize, on a Detail change and when the phone cap changes.
function resize(){
- const r=view.getBoundingClientRect(); // the canvas' own box, not the bordered stage
+ W=stage.clientWidth;H=stage.clientHeight;
const dpr=Math.min(window.devicePixelRatio||1,1.5);
- view.width=Math.max(1,Math.round(r.width*dpr));
- view.height=Math.max(1,Math.round(r.height*dpr));
- bw=quality;
- bh=Math.max(1,Math.round(quality*(r.height/r.width)));
+ view.width=Math.max(1,Math.round(W*dpr));
+ view.height=Math.max(1,Math.round(H*dpr));
+ bw=pictureWidth();
+ bh=Math.max(1,Math.round(bw*H/Math.max(W,1)));
buf.width=bw;buf.height=bh;
img=bctx.createImageData(bw,bh);
dist=new Float32Array(bw*bh);
@@ -140,9 +196,11 @@ body{padding-top:46px}
}
sinX=new Float32Array(bw);sinY=new Float32Array(bh);sinC=new Float32Array(bw+bh);
vctx.imageSmoothingEnabled=true;
- if(reduce)draw(0);
+ draw(clock);
}
+ // Every pixel of the small picture is written, and the picture covers the whole view, so nothing of an earlier
+ // frame stays behind.
function draw(t){
const f=0.09*scale; // radians per pixel — the zoom
const data=img.data;
@@ -163,33 +221,56 @@ body{padding-top:46px}
vctx.drawImage(buf,0,0,bw,bh,0,0,view.width,view.height);
}
- let last=performance.now(),clock=0,raf=0;
+ // loop() keeps exactly one frame requested in raf; Pause cancels it and Play starts one loop again. The first frame
+ // after a start adds no time, a long gap (a background tab) counts for at most 50 ms, and slow motion adds a third.
function loop(now){
raf=requestAnimationFrame(loop);
- const dt=Math.min((now-last)/1000,0.05);last=now;
- if(!paused)clock+=dt*speed;
+ const dt=last===null?0:Math.min(now-last,50);
+ last=now;
+ clock+=dt/1000*speed*(slowTog.checked?1/3:1);
draw(clock);
}
- function start(){if(reduce)return;cancelAnimationFrame(raf);last=performance.now();raf=requestAnimationFrame(loop);}
- function stop(){cancelAnimationFrame(raf);raf=0;}
+ function start(){if(raf)return;last=null;raf=requestAnimationFrame(loop);}
- // Controls
- const $=id=>document.getElementById(id);
- $('spd').addEventListener('input',e=>{speed=e.target.value/100;$('spd-v').textContent=speed.toFixed(1)+'×';});
- $('scl').addEventListener('input',e=>{scale=e.target.value/100;$('scl-v').textContent=scale.toFixed(1)+'×';if(reduce)draw(clock);});
- $('pal').addEventListener('change',e=>{lut=buildLUT(PALETTES[e.target.value]);if(reduce)draw(clock);});
- $('qual').addEventListener('change',e=>{quality=+e.target.value;resize();});
- $('pause').addEventListener('click',()=>{
- paused=!paused;$('pause').textContent=paused?'▶ Resume':'⏸ Pause';
+ // 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 field on screen stays as it is while paused.
+ document.addEventListener('hb:pause',e=>{
+ paused=e.detail.paused;
+ if(paused){cancelAnimationFrame(raf);raf=null;}
+ else start();
+ });
+ // A hidden tab does no work; a visible one carries on, unless the visitor paused the loop
+ document.addEventListener('visibilitychange',()=>{
+ if(document.hidden){cancelAnimationFrame(raf);raf=null;}
+ else if(!paused)start();
});
- // Pause the loop when the tab is hidden
- document.addEventListener('visibilitychange',()=>{document.hidden?stop():start();});
- addEventListener('resize',resize);
+ // Sizing the view canvas wipes it, so the window's resize acts only when the stage's size really changed (a phone's
+ // address bar sliding in or out fires resize too) and then draws the field again at once.
+ function refit(){if(stage.clientWidth!==W||stage.clientHeight!==H)resize();}
+ // A window that crosses the phone size changes the cap on the small picture, even when the stage keeps its size
+ function onPhone(){if(pictureWidth()!==bw)resize();}
+
+ 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);
+ }));
+ }
+ // Pattern size, Colors and Detail draw the field again at once, so a change shows while paused without starting the
+ // loop. Speed draws nothing: the field stays where it is and only its pace changes, from the next frame.
+ choices('speed-seg',d=>{speed=+d.speed;});
+ choices('size-seg',d=>{scale=+d.scale;draw(clock);});
+ choices('color-seg',d=>{lut=buildLUT(PALETTES[d.pal]);draw(clock);});
+ choices('detail-seg',d=>{quality=+d.px;resize();});
resize();
+ window.addEventListener('resize',refit);
+ if(phone.addEventListener)phone.addEventListener('change',onPhone);
+ else if(phone.addListener)phone.addListener(onPhone);
start();
</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 05fea1ca..89ee842d 100644
--- a/index.html
+++ b/index.html
@@ -1049,7 +1049,7 @@ const CATS=[
['flow-field','Flow Field','Particles ride invisible currents, leaving fading trails. Best for art pages.'],
['synthwave-grid','Synthwave Grid','A glowing grid rolls toward you under a striped sun. Best for music and games.'],
['matrix-rain','Matrix Rain','Columns of glowing characters rain down a dark screen. Best for tech themes.'],
- ['plasma','Plasma Field','Summed sine functions of x, y, and time make a flowing, organic color field — the demoscene classic.'],
+ ['plasma','Plasma Field','Smooth waves of color flow endlessly, made from math. Best for creative sites.'],
]
}
];