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: run Floating Elements at the same speed on every screen

matin04915e39a160
diff --git a/animations/07-ambient-background/floating-elements/README.md b/animations/07-ambient-background/floating-elements/README.md
index a1e91aa9..9b4db0bf 100644
--- a/animations/07-ambient-background/floating-elements/README.md
+++ b/animations/07-ambient-background/floating-elements/README.md
@@ -10,7 +10,7 @@ Floating elements are small, see-through shapes, such as circles, squares or rin
- Dashboard hero areas where the background differentiates sections
## How it works
-Each shape has a base position (`bx`, `by`) and sine-wave parameters. Per animation frame, current position is computed from base position plus sinusoidal offsets:
+Each shape has a base position (`bx`, `by`) and sine-wave parameters. On every animation frame, its current position is computed from the base position plus sinusoidal offsets of a running time `t`:
```js
function animate(t) {
@@ -18,8 +18,7 @@ function animate(t) {
const x = el.bx + Math.sin(el.freq * t + el.phase) * el.amplitude;
const y = el.by + Math.cos(el.freq * t * 0.7 + el.phaseY) * el.amplitude * 0.6;
- if (el.rotation) el.rot += el.rotSpeed; // its own small turn each frame
- const rot = el.rotation ? el.rot : 0;
+ const rot = el.rotation ? el.rot : 0; // its own small turn, added up in the loop below
const opacity = el.pulse
? 0.4 + 0.3 * Math.sin(el.pulsePhase + t * 0.5) // stays inside 0.1–0.7
: 0.5;
@@ -30,6 +29,23 @@ function animate(t) {
}
```
+The clock `t` and each shape's turn move by the time that has passed, not by a fixed amount per frame. Every 60th of a second adds `speed * 0.005` to `t`, however many frames that takes, so a 30 Hz phone and a 144 Hz monitor drift at the same speed. The first frame after a start, a pause or a return from a hidden tab adds nothing, and a long gap between frames counts for at most 50 ms:
+
+```js
+const FRAME = 1000 / 60; // what one 60 Hz frame adds up to
+let last = null; // set back to null on Play and when the tab returns
+
+function loop(now) {
+ requestAnimationFrame(loop);
+ const dt = last === null ? 0 : Math.min(now - last, 50); // ms since the last frame
+ last = now;
+ const k = dt / FRAME; // 1 at 60 Hz, 2 at 30 Hz, about 0.42 at 144 Hz
+ t += speed * 0.005 * k;
+ if (rotates) elements.forEach(el => { el.rot += el.rotSpeed * k; });
+ animate(t);
+}
+```
+
**Key construction** — no two shapes share the same parameters:
```js
diff --git a/animations/07-ambient-background/floating-elements/index.html b/animations/07-ambient-background/floating-elements/index.html
index b3ef1693..6630d1e2 100644
--- a/animations/07-ambient-background/floating-elements/index.html
+++ b/animations/07-ambient-background/floating-elements/index.html
@@ -181,7 +181,8 @@
const layer=document.getElementById('float-layer');
const stage=document.querySelector('.stage');
const slowTog=document.getElementById('slow-tog');
- let W=0,H=0,raf=null,paused=false,t=0,mouse={x:-999,y:-999};
+ 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,t=0,mouse={x:-999,y:-999};
let COUNT=8,SPD=0.5,RANGE=50,SHAPE='mixed',PAL='cool',ROT=true,PULSE=true,REACT=false;
let elements=[];
@@ -244,15 +245,19 @@
place();
}
- // One pending frame at a time: Pause cancels it, Play starts one loop again. A frame moves t and each turn one step (a third in slow motion).
- function loop(){
+ // One pending frame at a time: Pause cancels it, Play starts one loop again. A step moves t and each turn (a third of a step in
+ // slow motion). A frame adds as many steps as 60ths of a second have passed since the last one, so the shapes drift at the same
+ // speed on every screen. A long gap (a background tab) counts for at most 50 ms, and the first frame after a start adds none.
+ function loop(ts){
raf=requestAnimationFrame(loop);
- const k=slowTog.checked?1/3:1;
+ const dt=lastT===null?0:Math.min(ts-lastT,50);
+ lastT=ts;
+ const k=dt/FRAME*(slowTog.checked?1/3:1);
t+=SPD*.005*k;
if(ROT)elements.forEach(e=>{e.rot+=e.rotSpd*k});
place();
}
- function start(){if(raf)return;raf=requestAnimationFrame(loop)}
+ function start(){if(raf)return;lastT=null;raf=requestAnimationFrame(loop)}
// Top level, so it is there before the shared script (deferred) pauses the loop on arrival under reduced motion. Paused shapes stay put.
document.addEventListener('hb:pause',e=>{
@@ -260,6 +265,8 @@
if(paused){cancelAnimationFrame(raf);raf=null}
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});
// A real change of the stage's size places the shapes again, playing or paused; the same size (a phone's address bar) does nothing
function resize(){