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.

fix: audit and repair all 17 Ambient & Background demos (section 7)

matin031693ec96c6
diff --git a/animations/07-ambient-background/abstract-geometric-motion/README.md b/animations/07-ambient-background/abstract-geometric-motion/README.md
index 2be2c39b..3991197a 100644
--- a/animations/07-ambient-background/abstract-geometric-motion/README.md
+++ b/animations/07-ambient-background/abstract-geometric-motion/README.md
@@ -36,8 +36,8 @@ function drawPolygons(t) {
**Concentric expanding rings** — rings spawn from center, expand outward, and fade:
```js
-// Spawn a new ring periodically
-if (Math.random() < 0.3) rings.push({ r: 0, color: randomColor() });
+// Spawn a new ring on a fixed interval of t, so spacing stays even at any speed
+if (t - lastSpawn >= 0.5) { lastSpawn = t; rings.push({ r: 0, color: randomColor() }); }
// Update and draw each ring
rings.forEach(ring => {
diff --git a/animations/07-ambient-background/abstract-geometric-motion/index.html b/animations/07-ambient-background/abstract-geometric-motion/index.html
index caed900f..3ef023f1 100644
--- a/animations/07-ambient-background/abstract-geometric-motion/index.html
+++ b/animations/07-ambient-background/abstract-geometric-motion/index.html
@@ -47,8 +47,7 @@
.tog input{accent-color:var(--ui-accent);width:16px;height:16px;flex-shrink: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}aside{width:100%}:root{--stage-h:420px}}
- @media(prefers-reduced-motion:reduce){canvas{display:none}}
+ @media(max-width:600px){.layout{flex-direction:column;align-items:stretch}.stage{flex:none;width:100%}.ptab{min-height:44px}aside{width:100%}:root{--stage-h:420px}}
</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}
@@ -109,9 +108,16 @@ const cvs=document.getElementById('cvs'),ctx=cvs.getContext('2d');
const stage=document.getElementById('stage');
let W,H,paused=false,raf=null,lastT=0,t=0;
let PRESET=0,SPD=0.6,CX=6,PAL='cool',BG='#020408',CONTRAST=false;
+const RM=matchMedia('(prefers-reduced-motion:reduce)');
-function resize(){W=cvs.width=stage.clientWidth;H=cvs.height=stage.clientHeight;}
-resize();window.addEventListener('resize',resize);
+function resize(){
+ const dpr=Math.min(window.devicePixelRatio||1,2);
+ W=stage.clientWidth;H=stage.clientHeight;
+ cvs.width=Math.round(W*dpr);cvs.height=Math.round(H*dpr);
+ ctx.setTransform(dpr,0,0,dpr,0,0);
+ if(!raf)repaint(); // keep a frame on screen while paused / reduced-motion
+}
+window.addEventListener('resize',resize);
const PALETTES={
cool:c=>`hsla(${200+c*10},70%,65%,`,
@@ -138,9 +144,9 @@ function drawPolygons(){
}
// PRESET 1: Concentric expanding rings
-let rings2=[];
+let rings2=[],ringT=-1e9;
function updateRings(){
- if(t-Math.floor(t)<.02&&Math.floor(t)%1===0&&Math.random()<.3)rings2.push({r:0,born:t,c:Math.floor(Math.random()*CX)});
+ if(t-ringT>=.5){ringT=t;rings2.push({r:0,born:t,c:Math.floor(Math.random()*CX)});}
rings2=rings2.filter(r=>r.r<Math.max(W,H));
rings2.forEach(r=>{r.r+=SPD*2;});
}
@@ -191,11 +197,7 @@ function drawLines(){
}
}
-function loop(ts){
- if(paused){raf=null;return;}
- raf=requestAnimationFrame(loop);
- const dt=ts-lastT;if(dt<16)return;lastT=ts;
- t+=SPD*.01;
+function render(){
ctx.fillStyle=BG;ctx.fillRect(0,0,W,H);
if(PRESET===0)drawPolygons();
else if(PRESET===1){updateRings();drawRings();}
@@ -203,20 +205,38 @@ function loop(ts){
else drawLines();
}
+// Reduced motion: one representative still frame instead of the loop.
+function still(){
+ t=1.2;
+ if(PRESET===1){const m=Math.max(W,H);ringT=t;rings2=[];for(let i=0;i<6;i++)rings2.push({r:m*(i+1)/7,born:t,c:i%CX});}
+ render();
+}
+function repaint(){RM.matches?still():render();}
+
+function loop(ts){
+ if(paused){raf=null;return;}
+ if(RM.matches){raf=null;still();return;}
+ raf=requestAnimationFrame(loop);
+ const dt=ts-lastT;if(dt<16)return;lastT=ts;
+ t+=SPD*.01;
+ render();
+}
+
+resize();
raf=requestAnimationFrame(loop);
document.querySelectorAll('.ptab').forEach(b=>{
b.addEventListener('click',()=>{
PRESET=+b.dataset.idx;
document.querySelectorAll('.ptab').forEach(x=>x.classList.remove('active'));
- b.classList.add('active');rings2=[];
+ b.classList.add('active');rings2=[];ringT=-1e9;if(!raf)repaint();
});
});
-document.getElementById('spd-sl').addEventListener('input',e=>{SPD=e.target.value/10;document.getElementById('spd-v').textContent=SPD.toFixed(1);});
-document.getElementById('cx-sl').addEventListener('input',e=>{CX=+e.target.value;document.getElementById('cx-v').textContent=CX;});
-document.getElementById('pal-sel').addEventListener('change',e=>{PAL=e.target.value;});
-document.getElementById('bg-sel').addEventListener('change',e=>{BG=e.target.value;});
-document.getElementById('contrast-tog').addEventListener('change',e=>{CONTRAST=e.target.checked;});
+document.getElementById('spd-sl').addEventListener('input',e=>{SPD=e.target.value/10;document.getElementById('spd-v').textContent=SPD.toFixed(1);if(!raf)repaint();});
+document.getElementById('cx-sl').addEventListener('input',e=>{CX=+e.target.value;document.getElementById('cx-v').textContent=CX;if(!raf)repaint();});
+document.getElementById('pal-sel').addEventListener('change',e=>{PAL=e.target.value;if(!raf)repaint();});
+document.getElementById('bg-sel').addEventListener('change',e=>{BG=e.target.value;if(!raf)repaint();});
+document.getElementById('contrast-tog').addEventListener('change',e=>{CONTRAST=e.target.checked;if(!raf)repaint();});
document.getElementById('pause-btn').addEventListener('click',()=>{
paused=!paused;document.getElementById('pause-btn').textContent=paused?'▶ Resume':'⏸ Pause';
if(!paused){lastT=performance.now();raf=requestAnimationFrame(loop);}
diff --git a/animations/07-ambient-background/ambient-ripple/README.md b/animations/07-ambient-background/ambient-ripple/README.md
index 810ac098..6477a703 100644
--- a/animations/07-ambient-background/ambient-ripple/README.md
+++ b/animations/07-ambient-background/ambient-ripple/README.md
@@ -14,12 +14,12 @@ Each ripple source has a timer that spawns new `Ring` objects at a configurable
```js
class Ring {
- constructor(x, y) {
+ constructor(x, y, now) {
this.x = x;
this.y = y;
this.r = 0;
- this.born = performance.now();
- this.duration = RING_LIFE_MS;
+ this.born = now; // the rAF frame timestamp — mixing it with performance.now()
+ this.duration = RING_LIFE_MS; // yields a negative radius on the birth frame
}
update(now) {
diff --git a/animations/07-ambient-background/ambient-ripple/index.html b/animations/07-ambient-background/ambient-ripple/index.html
index 3f1f453a..e3eadc1c 100644
--- a/animations/07-ambient-background/ambient-ripple/index.html
+++ b/animations/07-ambient-background/ambient-ripple/index.html
@@ -48,8 +48,8 @@
input[type=color]{border:none;background:none;width:44px;height:30px;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;align-items:stretch}aside{width:100%}:root{--stage-h:420px}.stage{flex:none}}
- @media(prefers-reduced-motion:reduce){canvas{display:none}}
+ @media(max-width:600px){.layout{flex-direction:column;align-items:stretch}aside{width:100%}:root{--stage-h:420px}.stage{flex:none}input[type=color]{height:44px}}
+ /* Reduced motion is handled in the script: one static frame, no loop. */
</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}
@@ -104,11 +104,17 @@ body{padding-top:46px}
const cvs=document.getElementById('cvs'),ctx=cvs.getContext('2d');
const stage=document.getElementById('stage');
let W,H,paused=false,raf=null,lastT=0;
+const REDUCED=matchMedia('(prefers-reduced-motion:reduce)').matches;
let SRCS=3,INTERVAL=3,MAX_R=160,LIFE=2.5,THICK=1.5,IRR=true,DRIFT=false;
let COLOR=[88,166,255];
let sources=[],rings=[];
-function resize(){W=cvs.width=stage.clientWidth;H=cvs.height=stage.clientHeight;initSources();}
+function resize(){
+ const dpr=Math.min(window.devicePixelRatio||1,2);
+ W=stage.clientWidth;H=stage.clientHeight;
+ cvs.width=Math.round(W*dpr);cvs.height=Math.round(H*dpr);ctx.setTransform(dpr,0,0,dpr,0,0);
+ initSources();
+}
function initSources(){
sources=Array.from({length:SRCS},(_,i)=>({
@@ -119,7 +125,7 @@ function initSources(){
}
class Ring{
- constructor(x,y){this.x=x;this.y=y;this.r=0;this.born=performance.now();this.dur=LIFE*1000;}
+ constructor(x,y,now){this.x=x;this.y=y;this.r=0;this.born=now;this.dur=LIFE*1000;}
update(now){this.r=MAX_R*(now-this.born)/this.dur;}
isDead(now){return now-this.born>this.dur;}
draw(now){
@@ -130,8 +136,23 @@ class Ring{
}
}
+// Reduced motion still needs to show the technique, so freeze a single frame
+// with each source mid-emission rather than an empty stage.
+function paintStatic(){
+ ctx.clearRect(0,0,W,H);
+ const now=performance.now();
+ sources.slice(0,SRCS).forEach(s=>{
+ ctx.fillStyle=`rgba(${COLOR[0]},${COLOR[1]},${COLOR[2]},.4)`;
+ ctx.beginPath();ctx.arc(s.x,s.y,3,0,Math.PI*2);ctx.fill();
+ [.3,.6,.9].forEach(f=>{
+ const r=new Ring(s.x,s.y,now-f*LIFE*1000);
+ r.update(now);r.draw(now);
+ });
+ });
+}
+
function loop(ts){
- if(paused){raf=null;return;}
+ if(paused||REDUCED){raf=null;return;}
raf=requestAnimationFrame(loop);
const dt=ts-lastT;if(dt<16)return;lastT=ts;
ctx.clearRect(0,0,W,H);
@@ -143,7 +164,7 @@ function loop(ts){
if(s.y<H*.1||s.y>H*.9)s.vy*=-1;
}
if(ts>=s.nextEmit){
- rings.push(new Ring(s.x,s.y));
+ rings.push(new Ring(s.x,s.y,ts));
const jitter=IRR?(Math.random()-.5)*INTERVAL*1000*.8:0;
s.nextEmit=ts+INTERVAL*1000+jitter;
}
@@ -156,7 +177,12 @@ function loop(ts){
rings.forEach(r=>{r.update(ts);r.draw(ts);});
}
-resize();raf=requestAnimationFrame(loop);window.addEventListener('resize',()=>{resize();});
+resize();
+if(REDUCED)paintStatic();else raf=requestAnimationFrame(loop);
+window.addEventListener('resize',()=>{resize();if(REDUCED||paused)paintStatic();});
+// Controls must still repaint the frozen frame when the loop is not running.
+document.addEventListener('input',()=>{if(REDUCED||paused)paintStatic();});
+document.addEventListener('change',()=>{if(REDUCED||paused)paintStatic();});
document.getElementById('src-sl').addEventListener('input',e=>{SRCS=+e.target.value;document.getElementById('src-v').textContent=SRCS;initSources();});
document.getElementById('int-sl').addEventListener('input',e=>{INTERVAL=e.target.value/10;document.getElementById('int-v').textContent=INTERVAL.toFixed(1)+'s';});
diff --git a/animations/07-ambient-background/animated-gradient-background/index.html b/animations/07-ambient-background/animated-gradient-background/index.html
index 029db6ed..1c165cd6 100644
--- a/animations/07-ambient-background/animated-gradient-background/index.html
+++ b/animations/07-ambient-background/animated-gradient-background/index.html
@@ -37,14 +37,18 @@
.var-hue{background:linear-gradient(135deg,#58a6ff,#d2a8ff,#56d364,#ffa657);animation:grad-hue var(--dur) linear infinite}
.var-multi{background:linear-gradient(135deg,#0d1a2e 0%,#58a6ff 25%,#d2a8ff 50%,#56d364 75%,#0d1a2e 100%);background-size:300% 300%;animation:grad-pos calc(var(--dur)*1.5) ease infinite}
@keyframes grad-pos{0%{background-position:0% 50%}50%{background-position:100% 50%}100%{background-position:0% 50%}}
- @keyframes grad-hue{to{filter:hue-rotate(360deg)}}
+ @keyframes grad-hue{from{filter:saturate(var(--sat,1)) hue-rotate(0deg)}to{filter:saturate(var(--sat,1)) hue-rotate(360deg)}}
+ .grad-bg.subtle{--sat:.3;filter:saturate(.3)}
/* Palette overrides via JS class */
.pal-sunset .var-pos{background:linear-gradient(135deg,#1a0d05,#2e1505,#1a0518,#2e0505,#1a0d05);background-size:400% 400%}
.pal-sunset .var-hue{background:linear-gradient(135deg,#ffa657,#f85149,#d2a8ff,#ffa657)}
+ .pal-sunset .var-multi{background:linear-gradient(135deg,#1a0d05 0%,#ffa657 25%,#f85149 50%,#d2a8ff 75%,#1a0d05 100%);background-size:300% 300%}
.pal-ocean .var-pos{background:linear-gradient(135deg,#05101a,#051a1a,#05101a,#0a0525,#05101a);background-size:400% 400%}
.pal-ocean .var-hue{background:linear-gradient(135deg,#58a6ff,#56d364,#0d3040,#58a6ff)}
+ .pal-ocean .var-multi{background:linear-gradient(135deg,#05101a 0%,#58a6ff 25%,#56d364 50%,#0d3040 75%,#05101a 100%);background-size:300% 300%}
.pal-mono .var-pos{background:linear-gradient(135deg,#050608,#0d1117,#1a1f28,#0d1117,#050608);background-size:400% 400%}
.pal-mono .var-hue{background:linear-gradient(135deg,#3a4050,#c9d1d9,#3a4050)}
+ .pal-mono .var-multi{background:linear-gradient(135deg,#050608 0%,#3a4050 25%,#c9d1d9 50%,#3a4050 75%,#050608 100%);background-size:300% 300%}
/* Foreground card */
.fg-card{position:absolute;bottom:40px;left:50%;transform:translateX(-50%);width:clamp(200px,60%,320px);background:rgba(6,10,16,.75);backdrop-filter:blur(10px);-webkit-backdrop-filter:blur(10px);border:1px solid rgba(255,255,255,.12);border-radius:10px;padding:20px 22px;z-index:10}
.fg-eyebrow{font-size:9px;letter-spacing:2px;text-transform:uppercase;color:var(--ui-accent);margin-bottom:8px}
@@ -135,7 +139,7 @@ body{padding-top:46px}
curPal=e.target.value;if(curPal)stage.classList.add(curPal);
});
document.getElementById('subtle-tog').addEventListener('change',e=>{
- bg.style.filter=e.target.checked?'saturate(0.3)':'';
+ bg.classList.toggle('subtle',e.target.checked);
});
document.getElementById('pause-btn').addEventListener('click',()=>{
paused=!paused;
diff --git a/animations/07-ambient-background/aurora/index.html b/animations/07-ambient-background/aurora/index.html
index 5ea45d8b..278c90d1 100644
--- a/animations/07-ambient-background/aurora/index.html
+++ b/animations/07-ambient-background/aurora/index.html
@@ -76,7 +76,7 @@
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:420px}.stage{flex:none}}
- @media(prefers-reduced-motion:reduce){.band{animation-duration:300s!important}}
+ @media(prefers-reduced-motion:reduce){.band{animation-duration:300s!important}.star{animation:none!important}}
</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}
@@ -150,16 +150,18 @@ body{padding-top:46px}
}
}
+ // Each palette is the band order it prefers (0=green 1=blue 2=purple 3=pink 4=red);
+ // the count slider takes the first N off that list.
const PALETTES={
- classic:[true,true,false,false,false],
- vivid:[false,true,true,true,false],
- rare:[false,false,false,false,true]
+ classic:[0,1,2,3,4],
+ vivid:[2,3,1,4,0],
+ rare:[4,3,2,1,0]
};
function applyPalette(name){
- const vis=PALETTES[name];
const count=+document.getElementById('bands-sl').value;
- bands.forEach((b,i)=>{b.style.display=(vis[i]&&i<count)?'block':'none';});
+ const on=new Set(PALETTES[name].slice(0,count));
+ bands.forEach((b,i)=>{b.style.display=on.has(i)?'block':'none';});
}
document.getElementById('bands-sl').addEventListener('input',e=>{
diff --git a/animations/07-ambient-background/breathing-glow/README.md b/animations/07-ambient-background/breathing-glow/README.md
index 65bca5cd..5faa1504 100644
--- a/animations/07-ambient-background/breathing-glow/README.md
+++ b/animations/07-ambient-background/breathing-glow/README.md
@@ -15,13 +15,13 @@ A radial gradient element is animated with a CSS `@keyframes` that varies its `t
```css
:root {
--glow-dur: 5s;
- --glow-min: 0.7;
- --glow-max: 1.4;
+ --glow-min: 0.6;
+ --glow-max: 1.5;
--glow-color: #58a6ff;
}
.glow {
- width: 280px; height: 280px;
+ width: 260px; height: 260px;
border-radius: 50%;
background: radial-gradient(circle, var(--glow-color) 0%, transparent 70%);
filter: blur(40px);
@@ -45,7 +45,7 @@ A radial gradient element is animated with a CSS `@keyframes` that varies its `t
```css
.glow-outer {
- width: 420px; height: 420px;
+ width: 390px; height: 390px;
opacity: 0.3;
filter: blur(60px);
animation: breathe calc(var(--glow-dur) * 1.3) ease-in-out infinite reverse;
@@ -57,7 +57,7 @@ The `reverse` direction and 1.3× duration ensures the outer glow is never in ph
**Paired element breathing** — the center element also scales slightly with its own animation:
```css
-.center-icon {
+.center-el {
animation: el-breathe var(--glow-dur) ease-in-out infinite;
}
@keyframes el-breathe {
@@ -72,16 +72,16 @@ The 2% scale on the center element is intentionally subtle — the eye should no
| Parameter | Default | Effect |
|-----------|---------|--------|
| Cycle duration | 5s | 2s = anxious; 4–6s = relaxed breath; 8s+ = barely perceptible |
-| Min scale | 0.7 | Too low = the glow nearly disappears at minimum |
-| Max scale | 1.4 | Too high = the expansion looks alarming rather than calm |
-| Glow size | 280px | Base radius of the gradient — larger fills more of the background |
+| Min scale | 0.6 | Too low = the glow nearly disappears at minimum |
+| Max scale | 1.5 | Too high = the expansion looks alarming rather than calm |
+| Glow size | 260px | Base radius of the gradient — larger fills more of the background |
## Production notes
- **Why 4–6 seconds?** This range matches the respiratory rate of a relaxed adult (10–15 breaths per minute). Apple's Siri orb uses approximately this range. The match is not accidental — the timing creates a subconscious biofeedback of calm.
- **`filter: blur()` vs `border-radius: 50%`**: the combination creates a round soft glow without needing an image. `filter: blur()` is GPU-accelerated and smooth at all scale values.
- **Easing**: `ease-in-out` gives a slow start, gentle acceleration to the midpoint, and slow deceleration at the peak — matching how breathing actually feels (the inhale begins and ends slowly).
- **`will-change: transform`**: adding this to the glow element hints the browser to allocate a compositing layer. Useful when multiple glows are stacked; unnecessary for a single glow.
-- **Framer Motion**: `<motion.div animate={{ scale: [0.7, 1.4, 0.7] }} transition={{ duration: 5, repeat: Infinity, ease: "easeInOut" }} />` — identical result with React.
+- **Framer Motion**: `<motion.div animate={{ scale: [0.6, 1.5, 0.6] }} transition={{ duration: 5, repeat: Infinity, ease: "easeInOut" }} />` — identical result with React.
## See also
- [Ambient Ripple](../ambient-ripple/) — pulsing outward rings rather than scaling inward glow
diff --git a/animations/07-ambient-background/breathing-glow/index.html b/animations/07-ambient-background/breathing-glow/index.html
index 0a2d14d7..60480bbc 100644
--- a/animations/07-ambient-background/breathing-glow/index.html
+++ b/animations/07-ambient-background/breathing-glow/index.html
@@ -72,7 +72,7 @@
input[type=color]{border:none;background:none;width:44px;height:30px;cursor:pointer;border-radius:4px;padding:0}
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}aside{width:100%}:root{--stage-h:420px}}
+ @media(max-width:600px){.layout{flex-direction:column}aside{width:100%}:root{--stage-h:420px}input[type=color]{height:44px}}
@media(prefers-reduced-motion:reduce){.glow,.glow2,.center-el{animation-duration:60s!important}}
</style>
<style id="ah-inject">
@@ -151,7 +151,9 @@ body{padding-top:46px}
document.getElementById('glow2').style.display=e.target.checked?'block':'none';
});
document.getElementById('el-tog').addEventListener('change',e=>{
- document.getElementById('center-el').style.animation=e.target.checked?'':'none';
+ const el=document.getElementById('center-el');
+ el.style.animation=e.target.checked?'':'none';
+ el.style.animationPlayState=paused?'paused':'running';
});
document.getElementById('pause-btn').addEventListener('click',()=>{
paused=!paused;
diff --git a/animations/07-ambient-background/floating-elements/README.md b/animations/07-ambient-background/floating-elements/README.md
index 02c955a9..6fb6eb66 100644
--- a/animations/07-ambient-background/floating-elements/README.md
+++ b/animations/07-ambient-background/floating-elements/README.md
@@ -20,7 +20,7 @@ function animate(t) {
const rot = el.rotation ? el.rotSpeed * t : 0;
const opacity = el.pulse
- ? 0.3 + 0.4 * Math.sin(el.pulsePhase + t * 0.5)
+ ? 0.4 + 0.3 * Math.sin(el.pulsePhase + t * 0.5) // stays inside 0.1–0.7
: 0.5;
el.dom.style.transform = `translate(${x}px, ${y}px) rotate(${rot}deg)`;
diff --git a/animations/07-ambient-background/floating-elements/index.html b/animations/07-ambient-background/floating-elements/index.html
index 73a80771..7e2888fe 100644
--- a/animations/07-ambient-background/floating-elements/index.html
+++ b/animations/07-ambient-background/floating-elements/index.html
@@ -53,10 +53,10 @@
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}
.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}
- 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%}
+ 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%;min-height:44px}
@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:420px}.stage{flex:none}}
- @media(prefers-reduced-motion:reduce){.el{animation:none!important;transform:none!important}}
+ @media(prefers-reduced-motion:reduce){.el{animation:none!important;transition:none!important}}
</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}
@@ -110,7 +110,7 @@ body{padding-top:46px}
<option value="neon">Neon</option>
</select>
<label class="tog"><input type="checkbox" id="rot-tog" checked><span>Subtle rotation</span></label>
- <label class="tog"><input type="checkbox" id="pulse-tog"><span>Opacity pulse</span></label>
+ <label class="tog"><input type="checkbox" id="pulse-tog" checked><span>Opacity pulse</span></label>
<label class="tog"><input type="checkbox" id="react-tog"><span>Mouse repulsion</span></label>
<div class="div"></div>
<button class="rst" id="rebuild-btn">↺ Rebuild</button>
@@ -123,6 +123,7 @@ const stage=document.getElementById('stage');
let W=stage.clientWidth,H=stage.clientHeight;
let COUNT=8,SPD=0.5,RANGE=50,SHAPE='mixed',PAL='cool',ROT=true,PULSE=true,REACT=false;
let elements=[],raf=null,paused=false,t=0,mouse={x:-999,y:-999};
+const REDUCE=matchMedia('(prefers-reduced-motion:reduce)').matches;
const PALETTES={
cool:['rgba(88,166,255,.3)','rgba(100,80,220,.3)','rgba(56,200,180,.25)'],
@@ -131,7 +132,9 @@ const PALETTES={
neon:['rgba(0,255,180,.4)','rgba(255,0,200,.35)','rgba(0,200,255,.4)']
};
-const SHAPES=['circles','squares','rings'];
+const SHAPES=['circle','square','ring'];
+// Swap the alpha channel of an rgba() string, e.g. rgba(88,166,255,.3) -> rgba(88,166,255,0.6)
+const withAlpha=(c,a)=>c.replace(/[\d.]+\)$/,a+')');
function mkEl(){
const shapePick=SHAPE==='mixed'?SHAPES[Math.floor(Math.random()*SHAPES.length)]:SHAPE.replace(/s$/,'');
@@ -147,7 +150,7 @@ function mkEl(){
const pulsePhs=Math.random()*Math.PI*2;
if(shapePick==='ring'){
- el.style.width=sz+'px';el.style.height=sz+'px';el.style.borderRadius='50%';el.style.border='2px solid '+col.replace(',.',',0.6,').replace('rgba(','rgba(').replace(/,[\d.]+\)$/,',0.6)');
+ el.style.width=sz+'px';el.style.height=sz+'px';el.style.borderRadius='50%';el.style.border='2px solid '+withAlpha(col,0.6);
el.style.background='none';
}else if(shapePick==='square'){
el.style.width=sz+'px';el.style.height=sz+'px';el.style.background=col;
@@ -167,18 +170,18 @@ function rebuild(){
function loop(ts){
if(paused){raf=null;return;}
raf=requestAnimationFrame(loop);
- t+=SPD*.005;
+ if(!REDUCE)t+=SPD*.005;
elements.forEach(e=>{
const W=stage.clientWidth,H=stage.clientHeight;
let x=e.bx/100*W+Math.sin(e.freq*t+e.phase)*e.amp;
let y=e.by/100*H+Math.cos(e.freq*t*0.7+e.phaseY)*e.amp*.6;
// Mouse repulsion
- if(REACT&&mouse.x>0){
+ if(REACT&&!REDUCE&&mouse.x>0){
const dx=x-mouse.x,dy=y-mouse.y,d=Math.sqrt(dx*dx+dy*dy);
if(d<120){const f=120-d;x+=dx/d*f*.08;y+=dy/d*f*.08;}
}
- let rot=0;if(ROT){e.rot+=e.rotSpd;rot=e.rot;}
- let op=0.5;if(PULSE)op=0.3+0.4*Math.sin(e.pulsePhs+t*.5);
+ let rot=0;if(ROT&&!REDUCE){e.rot+=e.rotSpd;rot=e.rot;}
+ let op=0.5;if(PULSE&&!REDUCE)op=0.4+0.3*Math.sin(e.pulsePhs+t*.5);
e.el.style.transform=`translate(${x.toFixed(1)}px,${y.toFixed(1)}px) rotate(${rot.toFixed(1)}deg)`;
e.el.style.opacity=op;
});
@@ -186,8 +189,9 @@ function loop(ts){
rebuild();raf=requestAnimationFrame(loop);
-stage.addEventListener('mousemove',e=>{const r=stage.getBoundingClientRect();mouse.x=e.clientX-r.left;mouse.y=e.clientY-r.top;});
-stage.addEventListener('mouseleave',()=>{mouse.x=-999;});
+stage.addEventListener('pointermove',e=>{const r=stage.getBoundingClientRect();mouse.x=e.clientX-r.left;mouse.y=e.clientY-r.top;});
+stage.addEventListener('pointerleave',()=>{mouse.x=-999;});
+stage.addEventListener('pointercancel',()=>{mouse.x=-999;});
window.addEventListener('resize',()=>{W=stage.clientWidth;H=stage.clientHeight;});
document.getElementById('count-sl').addEventListener('input',e=>{COUNT=+e.target.value;document.getElementById('count-v').textContent=COUNT;rebuild();});
diff --git a/animations/07-ambient-background/flow-field/index.html b/animations/07-ambient-background/flow-field/index.html
index dcad8f0b..d1080afa 100644
--- a/animations/07-ambient-background/flow-field/index.html
+++ b/animations/07-ambient-background/flow-field/index.html
@@ -41,7 +41,7 @@
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}aside{width:100%}:root{--stage-h:380px}}
+ @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">
@@ -114,7 +114,7 @@ function angleAt(x,y,time){
}
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);}
+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}));}
function colorFor(p){
@@ -137,6 +137,8 @@ function frame(){
}
t+=0.01;
}
+// reduced-motion: paint a settled composition once instead of looping
+function prerender(){for(let i=0;i<40;i++)frame();}
function loop(ts){
if(paused){raf=null;return;}
@@ -147,7 +149,7 @@ function loop(ts){
}
resize();
-if(reduce){for(let i=0;i<40;i++)frame();fpsEl.textContent='static';}
+if(reduce)fpsEl.textContent='static';
else raf=requestAnimationFrame(loop);
window.addEventListener('resize',resize);
diff --git a/animations/07-ambient-background/grain-overlay/index.html b/animations/07-ambient-background/grain-overlay/index.html
index a4c02f58..265b8d7d 100644
--- a/animations/07-ambient-background/grain-overlay/index.html
+++ b/animations/07-ambient-background/grain-overlay/index.html
@@ -59,7 +59,7 @@
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:auto}.stage{flex:none;height:auto;min-height:400px}}
- @media(prefers-reduced-motion:reduce){canvas{display:none}}
+ /* Reduced motion: grain stays visible but is rendered once, never re-generated (see script). */
</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}
@@ -140,8 +140,13 @@ let IMPL='canvas',SIZE=2,RATE=60,COLOR_NOISE=false,INTENSITY=0.08;
let frameInterval=1000/60;
let svgTurb=document.getElementById('grain-turb');
let seed=0;
+const reduceMQ=window.matchMedia('(prefers-reduced-motion: reduce)');
-function resize(){W=cvs.width=stage.clientWidth;H=cvs.height=stage.clientHeight;}
+function resize(){
+ W=cvs.width=stage.clientWidth;H=cvs.height=stage.clientHeight;
+ // width/height assignment wipes the canvas — repaint if the loop won't
+ if((paused||reduceMQ.matches)&&IMPL==='canvas')drawCanvasGrain();
+}
resize();window.addEventListener('resize',resize);
function drawCanvasGrain(){
@@ -159,8 +164,16 @@ function drawCanvasGrain(){
ctx.putImageData(img,0,0);
}
+// One frame of grain, then stop — the texture is still there, it just never moves.
+function renderStatic(){
+ if(IMPL==='canvas')drawCanvasGrain();
+ else svgTurb.setAttribute('seed',seed);
+ fpsEl.textContent='static';fps2El.textContent='0';
+}
+
function loop(ts){
if(paused){raf=null;return;}
+ if(reduceMQ.matches){raf=null;renderStatic();return;}
raf=requestAnimationFrame(loop);
const dt=ts-lastT;if(dt<10)return;
const f=Math.round(1000/(dt||16));fpsEl.textContent=f+' fps';fps2El.textContent=f;lastT=ts;
@@ -177,19 +190,24 @@ document.getElementById('impl-sel').addEventListener('change',e=>{
IMPL=e.target.value;
document.getElementById('grain-canvas-wrap').style.display=IMPL==='canvas'?'block':'none';
document.getElementById('grain-filter-wrap').style.display=IMPL==='svgfilter'?'block':'none';
+ if(reduceMQ.matches)renderStatic();
});
document.getElementById('int-sl').addEventListener('input',e=>{
INTENSITY=e.target.value/100;document.getElementById('int-v').textContent=e.target.value+'%';
document.documentElement.style.setProperty('--grain-op',INTENSITY);
});
-document.getElementById('size-sel').addEventListener('change',e=>{SIZE=+e.target.value;});
+document.getElementById('size-sel').addEventListener('change',e=>{SIZE=+e.target.value;if(reduceMQ.matches)renderStatic();});
document.getElementById('rate-sel').addEventListener('change',e=>{frameInterval=1000/+e.target.value;});
document.getElementById('blend-sel').addEventListener('change',e=>{document.documentElement.style.setProperty('--grain-blend',e.target.value);});
-document.getElementById('color-tog').addEventListener('change',e=>{COLOR_NOISE=e.target.checked;});
+document.getElementById('color-tog').addEventListener('change',e=>{COLOR_NOISE=e.target.checked;if(reduceMQ.matches)renderStatic();});
document.getElementById('pause-btn').addEventListener('click',()=>{
paused=!paused;document.getElementById('pause-btn').textContent=paused?'▶ Resume':'⏸ Pause';
if(!paused){lastT=performance.now();raf=requestAnimationFrame(loop);}
});
+reduceMQ.addEventListener('change',()=>{
+ if(reduceMQ.matches)renderStatic();
+ else if(!paused&&!raf){lastT=performance.now();raf=requestAnimationFrame(loop);}
+});
</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>
diff --git a/animations/07-ambient-background/grid-dot-pattern-parallax/index.html b/animations/07-ambient-background/grid-dot-pattern-parallax/index.html
index 27bcd004..0f7f8329 100644
--- a/animations/07-ambient-background/grid-dot-pattern-parallax/index.html
+++ b/animations/07-ambient-background/grid-dot-pattern-parallax/index.html
@@ -79,8 +79,8 @@
.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:30px;cursor:pointer;border-radius:4px;padding:0;flex-shrink:0}
- .color-row{display:flex;gap:8px;align-items:center;font-size:10px;color:var(--ui-muted)}
- @media(max-width:600px){.layout{flex-direction:column;align-items:stretch}aside{width:100%}:root{--stage-h:420px}.stage{flex:none}}
+ .color-row{display:flex;flex-wrap:wrap;gap:8px;align-items:center;font-size:10px;color:var(--ui-muted)}
+ @media(max-width:600px){.layout{flex-direction:column;align-items:stretch}aside{width:100%}:root{--stage-h:420px}.stage{flex:none}input[type=color]{height:44px}}
@media(prefers-reduced-motion:reduce){.pattern-layer{transition:none!important}.auto-drift{animation-duration:300s!important}}
</style>
<style id="ah-inject">
@@ -146,6 +146,7 @@ body{padding-top:46px}
const cursor=document.getElementById('cursor');
const stage=document.getElementById('stage');
let W=0,H=0,str=5,spotOn=true,hasMouse=false,raf=null,autoT=0;
+ const reduceMotion=matchMedia('(prefers-reduced-motion: reduce)');
function resize(){W=stage.clientWidth;H=stage.clientHeight;}
resize();window.addEventListener('resize',resize);
@@ -163,6 +164,9 @@ body{padding-top:46px}
cursor.style.left=x+'px';cursor.style.top=y+'px';
if(spotOn){spotlight.style.left=x+'px';spotlight.style.top=y+'px';spotlight.style.opacity='1';}
});
+ stage.addEventListener('touchstart',()=>{if(raf){cancelAnimationFrame(raf);raf=null;}},{passive:true});
+ stage.addEventListener('touchend',startAuto,{passive:true});
+ stage.addEventListener('touchcancel',startAuto,{passive:true});
stage.addEventListener('touchmove',e=>{
const r=stage.getBoundingClientRect();
const x=e.touches[0].clientX-r.left,y=e.touches[0].clientY-r.top;
@@ -171,6 +175,8 @@ body{padding-top:46px}
},{passive:true});
function startAuto(){
+ if(raf!==null)return;
+ if(reduceMotion.matches){layer1.style.transform='translate(0,0)';return;}
function loop(t){raf=requestAnimationFrame(loop);autoT=t*.0002;
const dx=(-Math.sin(autoT)*str*0.01*W*.4).toFixed(1);
const dy=(-Math.cos(autoT*.7)*str*0.01*H*.3).toFixed(1);
diff --git a/animations/07-ambient-background/light-leak/index.html b/animations/07-ambient-background/light-leak/index.html
index dcce5834..dfa36999 100644
--- a/animations/07-ambient-background/light-leak/index.html
+++ b/animations/07-ambient-background/light-leak/index.html
@@ -36,14 +36,10 @@
.photo-mock{width:100%;max-width:300px;height:200px;border-radius:8px;background:linear-gradient(135deg,#0d1a2e,#1a2e3a);border:1px solid rgba(255,255,255,.08);display:flex;align-items:flex-end;padding:16px}
.photo-cap{font-size:11px;color:rgba(255,255,255,.5)}
/* Leak overlay */
- .leak{position:absolute;inset:0;pointer-events:none;z-index:5;opacity:0;transition:opacity .1s}
- .leak.active{opacity:var(--leak-op)}
- /* Direction variants */
- .dir-tl{background:radial-gradient(ellipse at -10% -10%,var(--lc).9) 0%,var(--lc).5) 20%,var(--lc).1) 50%,transparent 70%)}
- .dir-tr{background:radial-gradient(ellipse at 110% -10%,var(--lc).9) 0%,var(--lc).5) 20%,var(--lc).1) 50%,transparent 70%)}
- .dir-bl{background:radial-gradient(ellipse at -10% 110%,var(--lc).9) 0%,var(--lc).5) 20%,var(--lc).1) 50%,transparent 70%)}
- .dir-br{background:radial-gradient(ellipse at 110% 110%,var(--lc).9) 0%,var(--lc).5) 20%,var(--lc).1) 50%,transparent 70%)}
- .dir-h{background:linear-gradient(90deg,transparent,var(--lc).6) 40%,var(--lc).8) 50%,var(--lc).6) 60%,transparent)}
+ .leak{position:absolute;inset:0;pointer-events:none;z-index:5;opacity:0;transition:opacity var(--leak-dur) ease-in-out}
+ .leak.active{opacity:var(--leak-peak,var(--leak-op))}
+ /* Direction variants: the gradient itself is painted by applyColor() in JS,
+ since the leak colour is live-editable. The dir-* class is the state flag. */
/* Bokeh blobs */
.bokeh{position:absolute;border-radius:50%;pointer-events:none;z-index:4;animation:bokeh-float linear infinite;filter:blur(8px);display:none}
@keyframes bokeh-float{from{transform:translateY(100%) scale(0);opacity:0}20%{opacity:.8}80%{opacity:.5}to{transform:translateY(-120%) scale(1.5);opacity:0}}
@@ -67,8 +63,10 @@
input[type=color]{border:none;background:none;width:44px;height:30px;cursor:pointer;border-radius:4px;padding:0}
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}aside{width:100%}:root{--stage-h:420px}}
- @media(prefers-reduced-motion:reduce){.leak{transition:none!important}.bokeh{animation-duration:60s!important}}
+ @media(max-width:600px){.layout{flex-direction:column}.stage{flex:none;width:100%}aside{width:100%}:root{--stage-h:420px}
+ input[type=color]{height:44px}.tog input{width:44px;height:44px}}
+ /* Reduced motion: no repeated flash — the leak sits steady and dim instead. */
+ @media(prefers-reduced-motion:reduce){.leak,.leak.active{transition:none!important;opacity:calc(var(--leak-op) * .5)}.bokeh{animation-duration:60s!important}}
</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}
@@ -132,14 +130,9 @@ body{padding-top:46px}
const nextV=document.getElementById('next-v');
let paused=false,leakTimer=null,countTimer=null,minDelay=2,maxDelay=10,intensity=.55;
let bokehOn=false;
- const COLORS={gold:'rgba(255,165,50,',cyan:'rgba(100,200,255,',rose:'rgba(255,100,150,',white:'rgba(255,255,255,'};
let curColor='rgba(255,165,50,';
function applyColor(){
- // Update CSS custom property for gradient in all direction classes
- document.querySelectorAll('.leak,.dir-tl,.dir-tr,.dir-bl,.dir-br,.dir-h').forEach(el=>{
- // Re-apply the background based on current dir class
- });
const lc=curColor;
const dir=[...leak.classList].find(c=>c.startsWith('dir-'));
if(dir==='dir-tl')leak.style.background=`radial-gradient(ellipse at -10% -10%,${lc}.9) 0%,${lc}.5) 20%,${lc}.1) 50%,transparent 70%)`;
@@ -151,8 +144,10 @@ body{padding-top:46px}
function flash(){
if(paused)return;
- leak.classList.add('active');
const dur=+document.getElementById('dur-sl').value*100;
+ // Real leaks vary in strength — jitter the peak so it never reads as a loop
+ leak.style.setProperty('--leak-peak',(intensity*(.75+Math.random()*.25)).toFixed(3));
+ leak.classList.add('active');
setTimeout(()=>leak.classList.remove('active'),dur+200);
if(bokehOn)spawnBokeh();
scheduleNext();
@@ -162,9 +157,13 @@ body{padding-top:46px}
const delay=(minDelay+Math.random()*(maxDelay-minDelay))*1000;
let countdown=Math.round(delay/1000);
nextV.textContent=countdown+'s';
+ clearInterval(countTimer);
leakTimer=setTimeout(flash,delay);
- countTimer=setInterval(()=>{countdown--;nextV.textContent=Math.max(0,countdown)+'s';},1000);
- setTimeout(()=>clearInterval(countTimer),delay);
+ // Hold the id locally: the stop timer must clear *this* interval, not
+ // whichever one the next cycle has already installed.
+ const tick=setInterval(()=>{countdown--;nextV.textContent=Math.max(0,countdown)+'s';},1000);
+ countTimer=tick;
+ setTimeout(()=>clearInterval(tick),delay);
}
function spawnBokeh(){
@@ -182,10 +181,13 @@ body{padding-top:46px}
leak.classList.remove('dir-tl','dir-tr','dir-bl','dir-br','dir-h');
leak.classList.add(e.target.value);applyColor();
});
- document.getElementById('dur-sl').addEventListener('input',e=>{document.getElementById('dur-v').textContent=(e.target.value/10).toFixed(1)+'s';});
+ document.getElementById('dur-sl').addEventListener('input',e=>{
+ document.getElementById('dur-v').textContent=(e.target.value/10).toFixed(1)+'s';
+ document.documentElement.style.setProperty('--leak-dur',e.target.value*100+'ms');
+ });
document.getElementById('mindelay-sl').addEventListener('input',e=>{minDelay=+e.target.value;document.getElementById('mindelay-v').textContent=minDelay+'s';});
document.getElementById('maxdelay-sl').addEventListener('input',e=>{maxDelay=+e.target.value;document.getElementById('maxdelay-v').textContent=maxDelay+'s';});
- document.getElementById('int-sl').addEventListener('input',e=>{intensity=e.target.value/100;document.getElementById('int-v').textContent=intensity.toFixed(2);document.documentElement.style.setProperty('--leak-op',intensity);});
+ document.getElementById('int-sl').addEventListener('input',e=>{intensity=e.target.value/100;document.getElementById('int-v').textContent=intensity.toFixed(2);document.documentElement.style.setProperty('--leak-op',intensity);leak.style.setProperty('--leak-peak',intensity);});
document.getElementById('color-pick').addEventListener('input',e=>{
const h=e.target.value;curColor=`rgba(${parseInt(h.slice(1,3),16)},${parseInt(h.slice(3,5),16)},${parseInt(h.slice(5,7),16)},`;applyColor();
});
diff --git a/animations/07-ambient-background/matrix-rain/index.html b/animations/07-ambient-background/matrix-rain/index.html
index c133c4f1..d2b20fdd 100644
--- a/animations/07-ambient-background/matrix-rain/index.html
+++ b/animations/07-ambient-background/matrix-rain/index.html
@@ -46,7 +46,8 @@
.tog input{accent-color:var(--ui-accent);width:16px;height:16px;flex-shrink:0}
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}aside{width:100%}:root{--stage-h:380px}}
+ @media(max-width:600px){.layout{flex-direction:column}aside{width:100%}:root{--stage-h:380px}
+ .tog input{width:44px;height:44px}}
</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}
@@ -143,6 +144,10 @@ function step(){
}
}
+// Every column starts above the top edge, so one step paints an empty canvas.
+// Run enough steps for each column to enter view and grow its trail.
+function paintStatic(){const n=Math.ceil(H/fontSize)+20;for(let i=0;i<n;i++)step();}
+
function loop(ts){
if(paused){raf=null;return;}
raf=requestAnimationFrame(loop);
@@ -156,13 +161,16 @@ function loop(ts){
function start(){
resize();
- if(reduced){step();fpsEl.textContent='static';fps2.textContent='—';return;}
+ if(reduced){paintStatic();fpsEl.textContent='static';fps2.textContent='—';return;}
lastT=performance.now();raf=requestAnimationFrame(loop);
}
let rzT;
-window.addEventListener('resize',()=>{clearTimeout(rzT);rzT=setTimeout(()=>{resize();if(reduced)step();},150);});
-if('ResizeObserver'in window)new ResizeObserver(()=>{}).observe(stage);
+// Debounced refit. Repaint one frame when the loop is not running, otherwise
+// resize() leaves the canvas cleared to black until playback resumes.
+function refit(){clearTimeout(rzT);rzT=setTimeout(()=>{resize();if(reduced||paused)paintStatic();},150);}
+window.addEventListener('resize',refit);
+if('ResizeObserver'in window)new ResizeObserver(refit).observe(stage);
document.addEventListener('visibilitychange',()=>{
if(reduced)return;
@@ -171,7 +179,7 @@ document.addEventListener('visibilitychange',()=>{
});
document.getElementById('spd-sl').addEventListener('input',e=>{SPD=+e.target.value;document.getElementById('spd-v').textContent=SPD;});
-document.getElementById('fs-sl').addEventListener('input',e=>{fontSize=+e.target.value;document.getElementById('fs-v').textContent=fontSize;resize();if(reduced)step();});
+document.getElementById('fs-sl').addEventListener('input',e=>{fontSize=+e.target.value;document.getElementById('fs-v').textContent=fontSize;resize();if(reduced||paused)paintStatic();});
document.getElementById('fade-sl').addEventListener('input',e=>{FADE=e.target.value/100;document.getElementById('fade-v').textContent=FADE.toFixed(2).replace('0.','.');});
document.getElementById('set-sel').addEventListener('change',e=>{SET=e.target.value;});
document.getElementById('glow-tog').addEventListener('change',e=>{GLOW=e.target.checked;});
diff --git a/animations/07-ambient-background/mesh-gradient/index.html b/animations/07-ambient-background/mesh-gradient/index.html
index 4a485b42..0a47f3fc 100644
--- a/animations/07-ambient-background/mesh-gradient/index.html
+++ b/animations/07-ambient-background/mesh-gradient/index.html
@@ -123,23 +123,23 @@ body{padding-top:46px}
</div>
<script>
const PALETTES={
- cool:['#1a3060','#2e0a50','#0a2e1a','#2e1a05'],
- warm:['#3a1010','#3a2a10','#3a1025','#2a1035'],
- sunset:['#3a1510','#2a1040','#1a0540','#3a0520'],
- synthwave:['#0a0a3a','#3a0050','#003a3a','#1a003a'],
- earth:['#1a1505','#0a1a0a','#1a0a05','#0a1015']
+ cool:['#1a3060','#2e0a50','#0a2e1a','#2e1a05','#1a0a2e'],
+ warm:['#3a1010','#3a2a10','#3a1025','#2a1035','#3a2015'],
+ sunset:['#3a1510','#2a1040','#1a0540','#3a0520','#3a2510'],
+ synthwave:['#0a0a3a','#3a0050','#003a3a','#1a003a','#3a0030'],
+ earth:['#1a1505','#0a1a0a','#1a0a05','#0a1015','#151a15']
};
const blobs=[...document.querySelectorAll('.blob')];
let paused=false;
function setPalette(name){
const cols=PALETTES[name];
- blobs.forEach((b,i)=>{if(i<cols.length)b.style.background=cols[i%cols.length];});
+ blobs.forEach((b,i)=>{b.style.background=cols[i%cols.length];});
}
document.getElementById('count-sl').addEventListener('input',e=>{
const n=+e.target.value;document.getElementById('count-v').textContent=n;
- blobs.forEach((b,i)=>b.style.display=i<n?'':'none');
+ blobs.forEach((b,i)=>b.style.display=i<n?'block':'none');
});
document.getElementById('blur-sl').addEventListener('input',e=>{
document.getElementById('blur-v').textContent=e.target.value+'px';
diff --git a/animations/07-ambient-background/particle-constellation/index.html b/animations/07-ambient-background/particle-constellation/index.html
index ca6d542a..6f6b2d96 100644
--- a/animations/07-ambient-background/particle-constellation/index.html
+++ b/animations/07-ambient-background/particle-constellation/index.html
@@ -43,8 +43,8 @@
.tog input{accent-color:var(--ui-accent);width:16px;height:16px;flex-shrink:0}
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}aside{width:100%}:root{--stage-h:380px}}
- @media(prefers-reduced-motion:reduce){.fps-badge{display:none}}
+ @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,#pause-btn{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}
@@ -102,7 +102,7 @@ const PAL={cyan:[90,209,255],violet:[168,130,255],mint:[122,255,200],mono:[220,2
const reduce=window.matchMedia('(prefers-reduced-motion:reduce)').matches;
function effCount(){return W<600?Math.min(COUNT,60):COUNT;}
-function resize(){W=cvs.width=stage.clientWidth;H=cvs.height=stage.clientHeight;init();}
+function resize(){W=cvs.width=stage.clientWidth;H=cvs.height=stage.clientHeight;init();if(reduce||paused)draw();}
function init(){
const n=effCount();
@@ -169,7 +169,7 @@ document.getElementById('col-sel').addEventListener('change',e=>{COL=e.target.va
document.getElementById('mouse-tog').addEventListener('change',e=>{MOUSE=e.target.checked;});
document.getElementById('pause-btn').addEventListener('click',()=>{
paused=!paused;document.getElementById('pause-btn').textContent=paused?'▶ Resume':'⏸ Pause';
- if(!paused){lastT=performance.now();raf=requestAnimationFrame(loop);}
+ if(!paused&&!reduce){lastT=performance.now();raf=requestAnimationFrame(loop);}
});
</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>
diff --git a/animations/07-ambient-background/plasma/index.html b/animations/07-ambient-background/plasma/index.html
index 739f8ec5..8d04ce4e 100644
--- a/animations/07-ambient-background/plasma/index.html
+++ b/animations/07-ambient-background/plasma/index.html
@@ -93,7 +93,6 @@ body{padding-top:46px}
</aside>
</div>
<script>
- const stage=document.getElementById('stage');
const view=document.getElementById('c');
const vctx=view.getContext('2d');
const reduce=matchMedia('(prefers-reduced-motion: reduce)').matches;
@@ -124,7 +123,7 @@ body{padding-top:46px}
let speed=1,scale=1,quality=140,paused=false;
function resize(){
- const r=stage.getBoundingClientRect();
+ const r=view.getBoundingClientRect(); // the canvas' own box, not the bordered stage
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));
diff --git a/animations/07-ambient-background/scanline/README.md b/animations/07-ambient-background/scanline/README.md
index b93a0955..a5f8d1b8 100644
--- a/animations/07-ambient-background/scanline/README.md
+++ b/animations/07-ambient-background/scanline/README.md
@@ -35,23 +35,29 @@ The `line-gap` CSS custom property in the demo allows the density to be adjusted
.beam {
position: absolute;
left: 0; right: 0;
- height: 120px;
- top: -120px; /* starts above the visible area */
+ top: -120px; /* starts above the visible area */
+ height: calc(100% + 120px); /* 120px taller than the stage */
background: linear-gradient(
to bottom,
transparent 0%,
rgba(255, 255, 255, 0.08) 50%,
transparent 100%
);
+ background-size: 100% 120px; /* the band sits at the element's top edge */
+ background-repeat: no-repeat;
animation: sweep 4s linear infinite;
}
@keyframes sweep {
- from { top: -120px; }
- to { top: 100%; }
+ from { transform: translateY(0); }
+ to { transform: translateY(100%); } /* 100% = stage height + 120px */
}
```
+Animating `transform` rather than `top` keeps the sweep on the compositor —
+the oversized element exists purely so a percentage translate covers the full
+travel without JavaScript measuring the stage.
+
**CRT curvature** — a radial vignette overlay darkens the corners, simulating the slight convex curvature of a CRT screen:
```css
diff --git a/animations/07-ambient-background/scanline/index.html b/animations/07-ambient-background/scanline/index.html
index de88efa5..238f062e 100644
--- a/animations/07-ambient-background/scanline/index.html
+++ b/animations/07-ambient-background/scanline/index.html
@@ -54,14 +54,18 @@
}
/* Sweep beam */
.beam{
- position:absolute;left:0;right:0;height:120px;top:-120px;z-index:6;pointer-events:none;
+ /* Element is 120px taller than the stage; the 120px band sits at its top
+ edge, so translateY(0 → 100%) sweeps the band across and off the stage. */
+ position:absolute;left:0;right:0;top:-120px;height:calc(100% + 120px);z-index:6;pointer-events:none;
background:linear-gradient(to bottom,transparent 0%,rgba(255,255,255,var(--beam-bright)) 50%,transparent 100%);
+ background-size:100% 120px;background-repeat:no-repeat;background-position:0 0;
animation:beam-sweep var(--beam-dur) linear infinite;
+ will-change:transform;
display:none
}
- @keyframes beam-sweep{from{top:-120px}to{top:100%}}
- /* CRT curvature (barrel distortion via border-radius on content) */
- .crt-wrap{position:absolute;inset:0;border-radius:40%;overflow:hidden;pointer-events:none;display:none}
+ @keyframes beam-sweep{from{transform:translateY(0)}to{transform:translateY(100%)}}
+ /* CRT curvature — radial vignette darkening the corners */
+ .crt-wrap{position:absolute;inset:0;pointer-events:none;display:none}
.crt-inner{position:absolute;inset:-5%;background:radial-gradient(ellipse at center,transparent 60%,rgba(0,0,0,.5) 100%)}
/* Phosphor glow on text (via CSS on terminal) */
.phosphor .term-line{text-shadow:0 0 8px #58a6ff,0 0 20px rgba(88,166,255,.3)}
@@ -166,7 +170,9 @@ body{padding-top:46px}
});
document.getElementById('pause-btn').addEventListener('click',()=>{
paused=!paused;
- document.getElementById('beam').style.animationPlayState=paused?'paused':'running';
+ const state=paused?'paused':'running';
+ document.getElementById('beam').style.animationPlayState=state;
+ document.querySelector('.term-cursor').style.animationPlayState=state;
document.getElementById('pause-btn').textContent=paused?'▶ Resume':'⏸ Pause';
});
</script>
diff --git a/animations/07-ambient-background/starfield/index.html b/animations/07-ambient-background/starfield/index.html
index b97ecfca..7e458a18 100644
--- a/animations/07-ambient-background/starfield/index.html
+++ b/animations/07-ambient-background/starfield/index.html
@@ -49,8 +49,7 @@
.tog input{accent-color:var(--ui-accent);width:16px;height:16px;flex-shrink:0}
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}aside{width:100%}:root{--stage-h:380px}}
- @media(prefers-reduced-motion:reduce){canvas{display:none}}
+ @media(max-width:600px){.layout{flex-direction:column}.stage{flex:none;width:100%}aside{width:100%}:root{--stage-h:380px}}
</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}
@@ -112,8 +111,14 @@ const stage=document.getElementById('stage');
const fpsEl=document.getElementById('fps'),fps2=document.getElementById('fps2');
let W,H,paused=false,raf=null,lastT=0,MODE='radial',COUNT=300,SPD=0.4,TWINKLE=true,NEBULA=false,COL='white';
let stars=[];
+const REDUCED=matchMedia('(prefers-reduced-motion:reduce)').matches;
-function resize(){W=cvs.width=stage.clientWidth;H=cvs.height=stage.clientHeight;initStars();}
+function resize(){
+ const dpr=Math.min(window.devicePixelRatio||1,2);
+ W=stage.clientWidth;H=stage.clientHeight;
+ cvs.width=W*dpr;cvs.height=H*dpr;ctx.setTransform(dpr,0,0,dpr,0,0);
+ initStars();
+}
class Star{
constructor(init){
@@ -145,7 +150,6 @@ class Star{
}
draw(){
const ratio=MODE==='radial'?this.dist/this.maxDist:this.depth;
- const r=parseInt('ff',16),g=parseInt('ff',16),b=parseInt('ff',16);
let col='rgba(255,255,255,';
if(COL==='warm')col=`rgba(255,240,200,`;
if(COL==='color')col=`hsla(${this.hue},80%,90%,`;
@@ -164,7 +168,7 @@ class Star{
}
}
-function initStars(){stars=Array.from({length:COUNT},(_,i)=>new Star(true));}
+function initStars(){stars=Array.from({length:COUNT},(_,i)=>new Star(true));redraw();}
function drawNebula(){
const g=ctx.createRadialGradient(W*.3,H*.4,0,W*.3,H*.4,W*.5);
@@ -177,23 +181,31 @@ function drawNebula(){
function loop(ts){
if(paused){raf=null;return;}
- raf=requestAnimationFrame(loop);
- const dt=ts-lastT;if(dt<16)return;
- const f=Math.round(1000/(dt||16));fpsEl.textContent=f+' fps';fps2.textContent=f;lastT=ts;
- ctx.fillStyle='rgba(0,0,0,.85)';ctx.fillRect(0,0,W,H);
+ if(REDUCED){raf=null;}
+ else{
+ raf=requestAnimationFrame(loop);
+ const dt=ts-lastT;if(dt<16)return;
+ const f=Math.round(1000/(dt||16));fpsEl.textContent=f+' fps';fps2.textContent=f;lastT=ts;
+ }
+ ctx.fillStyle=REDUCED?'#000':'rgba(0,0,0,.85)';ctx.fillRect(0,0,W,H);
if(NEBULA)drawNebula();
- stars.forEach(s=>{s.update();s.draw();});
+ stars.forEach(s=>{if(!REDUCED)s.update();s.draw();});
}
-resize();raf=requestAnimationFrame(loop);
+// reduced motion draws one static frame, so control changes have to ask for a repaint
+function redraw(){if(REDUCED&&!paused&&raf===null)raf=requestAnimationFrame(loop);}
+
+resize();
+if(REDUCED){fpsEl.textContent='static';fps2.textContent='static';}
+raf=requestAnimationFrame(loop);
window.addEventListener('resize',resize);
document.getElementById('mode-sel').addEventListener('change',e=>{MODE=e.target.value;initStars();});
document.getElementById('count-sl').addEventListener('input',e=>{COUNT=+e.target.value;document.getElementById('count-v').textContent=COUNT;initStars();});
-document.getElementById('spd-sl').addEventListener('input',e=>{SPD=e.target.value/10;document.getElementById('spd-v').textContent=SPD.toFixed(1);});
-document.getElementById('col-sel').addEventListener('change',e=>{COL=e.target.value;});
-document.getElementById('twinkle-tog').addEventListener('change',e=>{TWINKLE=e.target.checked;});
-document.getElementById('nebula-tog').addEventListener('change',e=>{NEBULA=e.target.checked;});
+document.getElementById('spd-sl').addEventListener('input',e=>{SPD=e.target.value/10;document.getElementById('spd-v').textContent=SPD.toFixed(1);redraw();});
+document.getElementById('col-sel').addEventListener('change',e=>{COL=e.target.value;redraw();});
+document.getElementById('twinkle-tog').addEventListener('change',e=>{TWINKLE=e.target.checked;redraw();});
+document.getElementById('nebula-tog').addEventListener('change',e=>{NEBULA=e.target.checked;redraw();});
document.getElementById('pause-btn').addEventListener('click',()=>{
paused=!paused;document.getElementById('pause-btn').textContent=paused?'▶ Resume':'⏸ Pause';
if(!paused){lastT=performance.now();raf=requestAnimationFrame(loop);}
diff --git a/animations/07-ambient-background/synthwave-grid/index.html b/animations/07-ambient-background/synthwave-grid/index.html
index 062fb971..4b5d8dff 100644
--- a/animations/07-ambient-background/synthwave-grid/index.html
+++ b/animations/07-ambient-background/synthwave-grid/index.html
@@ -42,7 +42,7 @@
.row{display:flex;align-items:center;gap:8px;font-size:10px}
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}aside{width:100%}:root{--stage-h:380px}}
+ @media(max-width:600px){.layout{flex-direction:column;align-items:stretch}.stage{flex:none;height:var(--stage-h)}aside{width:100%}:root{--stage-h:380px}}
@media(prefers-reduced-motion:reduce){.fps-badge{display:none}}
</style>
<style id="ah-inject">
@@ -145,14 +145,14 @@ function loop(ts){
resize();
if(reduce){frame();fpsEl.textContent='static';}
else raf=requestAnimationFrame(loop);
-window.addEventListener('resize',()=>{resize();if(reduce)frame();});
+window.addEventListener('resize',()=>{resize();if(reduce||paused)frame();});
document.getElementById('spd-sl').addEventListener('input',e=>{SPD=e.target.value/10;document.getElementById('spd-v').textContent=SPD.toFixed(1);});
document.getElementById('dens-sl').addEventListener('input',e=>{DENS=+e.target.value;document.getElementById('dens-v').textContent=DENS;if(reduce)frame();});
document.getElementById('glow-col').addEventListener('input',e=>{GLOW=e.target.value;document.getElementById('glow-hex').textContent=e.target.value;if(reduce)frame();});
document.getElementById('pause-btn').addEventListener('click',()=>{
paused=!paused;document.getElementById('pause-btn').textContent=paused?'▶ Resume':'⏸ Pause';
- if(!paused){lastT=performance.now();raf=requestAnimationFrame(loop);}
+ if(!paused&&raf===null){lastT=performance.now();raf=requestAnimationFrame(loop);}
});
</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>