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: fade Starfield's trails by repeating today's cover once per 60th of a second

matinc41362d59e4a
diff --git a/animations/07-ambient-background/starfield/README.md b/animations/07-ambient-background/starfield/README.md
index 51b32820..2747fe92 100644
--- a/animations/07-ambient-background/starfield/README.md
+++ b/animations/07-ambient-background/starfield/README.md
@@ -71,7 +71,17 @@ last = now;
const k = dt / (1000 / 60); // 1 at 60 Hz, 2 at 30 Hz
```
-The short trails come from covering the canvas with a see-through black layer on every frame instead of clearing it. The layer is made stronger for a longer frame, `1 - 0.15 ** k`, which is the usual 0.85 when `k` is 1, so the trails last as long in seconds on every screen. Slow motion does not slow them.
+The short trails come from covering the canvas with a see-through black layer instead of clearing it. The layer is always the 0.85 one of a 60 Hz frame, and it goes on once for every 60th of a second that has passed (twice on a 30 Hz frame), so the trails last as long in seconds on every screen. Slow motion does not slow them. A running fraction carries the rest to the next frame, and rounding rather than "at least one" keeps a 60 Hz screen at exactly one cover per frame even when its frame times are uneven:
+
+```js
+owed += k; const covers = Math.round(owed); owed -= covers;
+for (let i = 0; i < covers; i++) {
+ ctx.fillStyle = 'rgba(0,0,0,0.85)'; ctx.fillRect(0, 0, W, H);
+ if (haze) drawHaze(); // the nebula haze goes on after each cover, as it does at 60 Hz
+}
+```
+
+One stronger cover, `1 - 0.15 ** k`, would fade the same amount in theory, but an 8-bit canvas rounds every cover, so the faint leftovers, such as the haze, would settle to different colors on different screens. Repeating the same cover makes them settle to the same colors everywhere.
## Key parameters
| Parameter | Default | Effect |
diff --git a/animations/07-ambient-background/starfield/index.html b/animations/07-ambient-background/starfield/index.html
index c507142a..be5aa38c 100644
--- a/animations/07-ambient-background/starfield/index.html
+++ b/animations/07-ambient-background/starfield/index.html
@@ -172,7 +172,7 @@
const PHONE_STARS=300; // phones and short screens draw at most this many stars
const phone=matchMedia('(max-width:600px),(max-height:500px)');
const FRAME=1000/60; // one step is what a 60 Hz frame adds, so a faster screen or a slow frame does not change the speed
- let W=0,H=0,raf=null,lastT=null,paused=false;
+ let W=0,H=0,raf=null,lastT=null,fa=0,paused=false; // fa: the part of a cover not yet applied
let MODE='radial',COUNT=300,SPD=0.4,TWINKLE=true,NEBULA=false,COL='white';
let stars=[];
@@ -240,14 +240,16 @@
ctx.fillStyle=g2;ctx.fillRect(0,0,W,H);
}
- // One picture. paint(true,n) is a frame of the loop that stands for n 60 Hz frames (1 at 60 Hz, 2 at 30 Hz): a see-through
- // cover leaves short trails, stronger for a longer frame so they last as long in seconds on any screen, then the stars
- // move by n (a third of it in slow motion, while the cover does not slow) and are drawn. paint(false) draws the picture
- // where it stands, for the first frame, after a resize and after a setting changes while paused; nothing moved, so it
- // covers the old picture completely.
- function paint(move,n){
- ctx.fillStyle=move?`rgba(0,0,0,${1-Math.pow(.15,n)})`:'#000';ctx.fillRect(0,0,W,H);
- if(NEBULA)drawNebula();
+ // One picture. paint(true,n,nf) is a frame of the loop for n 60 Hz frames (1 at 60 Hz, 2 at 30 Hz): a see-through cover
+ // leaves short trails, put on nf times (see loop()) with the haze redrawn after each, so trails last as long and faint
+ // leftovers settle to the same colors on any screen; then the stars move by n (a third of it in slow motion, while the
+ // cover does not slow) and are drawn. paint(false) draws the picture where it stands, for the first frame, after a resize
+ // and after a setting changes while paused; nothing moved, so it covers the old picture completely.
+ function paint(move,n,nf){
+ for(let i=0,c=move?nf:1;i<c;i++){
+ ctx.fillStyle=move?'rgba(0,0,0,.85)':'#000';ctx.fillRect(0,0,W,H);
+ if(NEBULA)drawNebula();
+ }
const k=move?n*(slowTog.checked?1/3:1):0;
stars.forEach(s=>{if(move)s.update(k);s.draw();});
}
@@ -255,16 +257,21 @@
function still(){if(paused)paint(false);}
// loop() keeps exactly one frame requested in raf; Pause cancels it and Play starts one loop again. At most one frame per
- // 16 ms, and each stands for the time since the last one in 60ths of a second, never more than 50 ms (a background tab
- // does not leap). The first frame after a start has nothing to measure from, so it leaves the picture as it is.
+ // 16 ms, each for the time since the last one in 60ths of a second, never more than 50 ms. The cover goes on once per
+ // whole 60th: fa carries the fraction over, and rounding (not "at least 1") keeps a 60 Hz screen at one cover per frame
+ // even with uneven frame times. The first frame after a start has nothing to measure from and leaves the picture alone.
function loop(ts){
raf=requestAnimationFrame(loop);
if(lastT!==null&&ts-lastT<16)return;
const dt=lastT===null?0:Math.min(ts-lastT,50);
lastT=ts;
- if(dt)paint(true,dt/FRAME);
+ if(dt){
+ const n=dt/FRAME;
+ fa+=n;const nf=Math.round(fa);fa-=nf;
+ paint(true,n,nf);
+ }
}
- function start(){if(raf)return;lastT=null;raf=requestAnimationFrame(loop);}
+ function start(){if(raf)return;lastT=null;fa=0;raf=requestAnimationFrame(loop);}
// 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 stars on screen stay as they are while paused.
@@ -274,7 +281,7 @@
else start();
});
// A tab that comes back after being hidden has no earlier frame to measure from, so its first frame adds no step
- document.addEventListener('visibilitychange',()=>{lastT=null;});
+ document.addEventListener('visibilitychange',()=>{lastT=null;fa=0;});
// Setting the canvas size wipes it, so a real change of the stage's size builds the stars and draws them 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.