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: 3D Model Orbit turns by elapsed time, so it spins at the same speed on every screen

matin76e3bbdf6076
diff --git a/animations/06-3d-advanced/3d-model-orbit/README.md b/animations/06-3d-advanced/3d-model-orbit/README.md
index 5f7e841d..1687668b 100644
--- a/animations/06-3d-advanced/3d-model-orbit/README.md
+++ b/animations/06-3d-advanced/3d-model-orbit/README.md
@@ -51,12 +51,21 @@ const M = multiply(multiply(T, rotateY(ry)), rotateX(rx));
const MVP = multiply(P, M);
```
+**Turning** — the angles grow by the time that has passed, not by a fixed amount per frame, so the shape turns at the same speed on a 30, 60 or 144 Hz screen:
+
+```js
+const FRAME = 1000 / 60; // one frame on a 60 Hz screen, in milliseconds
+const step = Math.min(elapsed, 50) / FRAME; // 1 at 60 Hz; a long gap counts for at most 50 ms
+ry += SPEED_Y * 0.01 * step; // the spin
+rx += SPEED_X * 0.01 * step * (Math.sin(clock * 0.0003) * 0.5 + 0.5); // the forward roll, at a speed that rises and falls
+```
+
## Key parameters
| Parameter | Default | Effect |
|-----------|---------|--------|
| Shape | Cube | The object that is drawn: a cube, a sphere, a ring or a four-sided pyramid |
| Movement | Spins | Spins turns it by itself; Follows the pointer turns it to face your pointer or finger |
-| Speed | Normal | How fast it spins: slow is about 17°, normal 27° and fast 45° a second |
+| Speed | Normal | How fast it spins: slow is about 17°, normal 27° and fast 45° a second, on any screen |
| Tumbles as it spins | on | Also rolls it forward, at a speed that rises and falls, so the top and bottom come into view |
| Distance | Medium | How far away the camera is: close looks bigger, far looks smaller; the lens stays the same |
| Color | Blue | The object's color; the lighting adds the shading and the highlight |
@@ -64,6 +73,7 @@ const MVP = multiply(P, M);
## Production notes
- **Three.js in production**: Three.js's `PerspectiveCamera`, `MeshPhongMaterial`, and `OrbitControls` replace everything in this demo in ~20 lines. Use Three.js for anything more complex than a single object — raw WebGL becomes unmanageable fast.
+- **Time, not frames**: a fixed turn per frame would spin the shape twice as fast on a 120 Hz screen and half as fast on a 30 Hz phone. Scale each turn by the time since the last frame, cap that time (this demo uses 50 ms) so a tab that comes back does not lurch, and let a returning tab start from its next frame.
- **Normal matrix**: strictly, normals should be transformed by the inverse-transpose of the model matrix, not the model matrix itself. For uniform scaling (no non-uniform squash/stretch), the model matrix works fine. For non-uniform scale, use `mat3(transpose(inverse(uM)))`.
- **`prefers-reduced-motion`**: stop the animation loop and show the object in its default orientation. The object should remain visible and interactive for camera control.
- **Touch/pointer interaction**: use `pointerdown` / `pointermove` / `pointerup` for drag-to-rotate, not `mousedown` — this covers touch without separate event listeners.
diff --git a/animations/06-3d-advanced/3d-model-orbit/index.html b/animations/06-3d-advanced/3d-model-orbit/index.html
index 8405c05c..3b5ff866 100644
--- a/animations/06-3d-advanced/3d-model-orbit/index.html
+++ b/animations/06-3d-advanced/3d-model-orbit/index.html
@@ -338,21 +338,26 @@ function draw(){
else{gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER,idxBuf);gl.drawElements(gl.TRIANGLES,idxCount,gl.UNSIGNED_SHORT,0);}
}
-// The loop keeps exactly one pending frame in raf. The page's own clock adds up the time of each drawn frame
-// (at most 50 ms, and a third of it while Slow motion is on, so the roll's speed wave slows down with the spin),
-// so the forward roll carries on smoothly after a pause instead of jumping.
+// One step is 1/60 s: the shape turns SY x 0.01 radians in that time, whatever the screen's refresh rate.
+const FRAME=1000/60;
+// The loop keeps exactly one pending frame in raf. Each drawn frame turns the shape by the time since the last drawn one
+// (a gap counts for at most 50 ms), counted in steps of 1/60 s, and by a third of that while Slow motion is on. The page's own
+// clock adds up the same time, so the roll's speed wave slows down with the spin and the forward roll carries on smoothly
+// after a pause instead of jumping. At most one picture is drawn per 16 ms. A tab that comes back to the front starts again
+// from its next frame, so the time away adds no step.
function render(ts){
raf=requestAnimationFrame(render);
if(last===null){last=ts;return;} // the first frame after a start only takes the time
const dt=ts-last;if(dt<16)return;
- const k=slowTog.checked?1/3:1;
- last=ts;clock+=Math.min(dt,50)*k;
+ const s=Math.min(dt,50)/FRAME*(slowTog.checked?1/3:1);
+ last=ts;clock+=s*FRAME;
if(MODE==='orbit'){
- ry+=SY*.01*k;rx+=SX*.01*k*(Math.sin(clock*.0003)*.5+.5);
+ ry+=SY*.01*s;rx+=SX*.01*s*(Math.sin(clock*.0003)*.5+.5);
}else{rx=mouseNY*1.2;ry=mouseNX*1.5;}
draw();
}
function start(){if(raf||!gl)return;last=null;raf=requestAnimationFrame(render);}
+document.addEventListener('visibilitychange',()=>{last=null;});
// While paused a setting or a resize draws the picture again, so the change shows without anything moving.
function redraw(){if(gl&&paused)draw();}