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: hang the Cloth before a still picture, and correct its README's performance figures and phone rule

matindd3676891066
diff --git a/animations/06-3d-advanced/cloth-simulation/README.md b/animations/06-3d-advanced/cloth-simulation/README.md
index 199e201e..0425d7aa 100644
--- a/animations/06-3d-advanced/cloth-simulation/README.md
+++ b/animations/06-3d-advanced/cloth-simulation/README.md
@@ -75,7 +75,7 @@ for (let r = 0; r < ROWS; r++) {
- **GPU cloth simulation**: AAA games simulate cloth on the GPU using compute shaders (DirectX/Vulkan/Metal) or GPGPU passes (same ping-pong texture technique as the GPGPU particle demo). WebGPU enables this in browsers as of Chrome 113+.
- **Cannon.js / Rapier.js**: JavaScript physics engines that include cloth/soft-body simulation with more accuracy and more constraint types. Rapier (Rust/WASM) is the fastest modern option.
- **Self-collision**: this demo does not prevent cloth from passing through itself (self-intersection). Self-collision detection is O(n²) and requires spatial hashing or BVH acceleration structures.
-- **Performance**: at 40×30 resolution (1200 vertices, ~3600 constraints, 4 iterations = 14400 constraint solves per frame), expect ~45fps on mid-range mobile. The demo starts at 16×12 on screens 600px wide or less; its Detail setting still lets you pick more.
+- **Performance**: at 40×30 (1,271 points, 2,470 links, 4 passes = 9,880 link corrections a frame) a phone-class CPU manages about 17 frames a second, and 24×18 about 47. The demo starts at 16×12 on phones (screens 600px wide or less, or 500px tall or less), where it holds 60; its Detail setting still lets you pick more.
- **Canvas resolution**: the backing store is sized to `devicePixelRatio` (capped at 2) and the context is scaled to match, so the mesh stays crisp on phones without paying for a 3× framebuffer.
## See also
diff --git a/animations/06-3d-advanced/cloth-simulation/index.html b/animations/06-3d-advanced/cloth-simulation/index.html
index f0f0eb56..05494ef9 100644
--- a/animations/06-3d-advanced/cloth-simulation/index.html
+++ b/animations/06-3d-advanced/cloth-simulation/index.html
@@ -183,8 +183,9 @@ function resize(){
ctx.setTransform(dpr,0,0,dpr,0,0);
}
-// Hangs a fresh cloth and draws it, so a cloth that is paused (or not yet moving) is never an empty stage.
-function initCloth(){
+// Hangs a fresh cloth and draws it, so a cloth that is paused (or not yet moving) is never an empty stage. With settle, 120 silent
+// physics steps run first, so a cloth shown still (at load, or while paused) already hangs.
+function initCloth(settle){
const w=W*.8,h=H*.65;
const ox=W*.1,oy=H*.1;
const sw=w/COLS,sh=h/ROWS;
@@ -207,6 +208,7 @@ function initCloth(){
if(c<COLS){const rest=Math.hypot(pts[n(r,c+1)][0]-pts[n(r,c)][0],pts[n(r,c+1)][1]-pts[n(r,c)][1]);constraints.push([n(r,c),n(r,c+1),rest]);}
if(r<ROWS){const rest=Math.hypot(pts[n(r+1,c)][0]-pts[n(r,c)][0],pts[n(r+1,c)][1]-pts[n(r,c)][1]);constraints.push([n(r,c),n(r+1,c),rest]);}
}}
+ if(settle)for(let i=0;i<120;i++){windT+=16;update();}
draw();
}
@@ -275,7 +277,7 @@ function loop(ts){
update();draw();
}
// Hangs a fresh cloth to the size of the stage (Detail, a resize) ...
-function rebuild(){resize();initCloth();}
+function rebuild(settle){resize();initCloth(settle);}
// ... and Play carries on with the cloth as it is; it never stacks a second loop.
function resume(){if(raf!==null)return;lastT=performance.now();raf=requestAnimationFrame(loop);}
@@ -287,7 +289,7 @@ document.addEventListener('hb:pause',e=>{
else resume();
});
-rebuild();resume();
+rebuild(true);resume();
// Drag interaction: Pointer Events cover mouse, touch and pen; capture keeps the drag alive outside the stage.
function getClosestVertex(x,y,maxDist=30){
@@ -308,7 +310,7 @@ const endDrag=e=>{if(e.isPrimary)dragIdx=-1;};
['pointerup','pointercancel','lostpointercapture'].forEach(type=>stage.addEventListener(type,endDrag));
// A phone's address bar showing or hiding changes only the window's height, not the stage's, so nothing is hung again
-window.addEventListener('resize',()=>{if(stage.clientWidth===W&&stage.clientHeight===H)return;rebuild();});
+window.addEventListener('resize',()=>{if(stage.clientWidth===W&&stage.clientHeight===H)return;rebuild(paused);});
function choices(id,pick){
const buttons=document.querySelectorAll('#'+id+' button');
@@ -318,17 +320,17 @@ function choices(id,pick){
}));
}
// Choosing what is already chosen must not hang a fresh cloth again
-choices('pin-seg',d=>{if(d.pin===PIN_MODE)return;PIN_MODE=d.pin;initCloth();});
+choices('pin-seg',d=>{if(d.pin===PIN_MODE)return;PIN_MODE=d.pin;initCloth(paused);});
choices('wind-seg',d=>{WIND=+d.wind;});
choices('stiff-seg',d=>{ITERS=+d.iters;});
choices('grav-seg',d=>{GRAVITY=+d.grav;});
choices('detail-seg',d=>{
const n=+d.cols;
if(n===COLS)return;
- COLS=n;ROWS=Math.round(COLS*.75);rebuild();
+ COLS=n;ROWS=Math.round(COLS*.75);rebuild(paused);
});
document.getElementById('verts-tog').addEventListener('change',e=>{SHOW_VERTS=e.target.checked;if(paused)draw();});
-document.getElementById('btn-reset').addEventListener('click',()=>initCloth());
+document.getElementById('btn-reset').addEventListener('click',()=>initCloth(paused));
</script>
</body>
</html>
diff --git a/docs/superpowers/plans/2026-09-29-3d-content.md b/docs/superpowers/plans/2026-09-29-3d-content.md
index 8629c784..0d55d228 100644
--- a/docs/superpowers/plans/2026-09-29-3d-content.md
+++ b/docs/superpowers/plans/2026-09-29-3d-content.md
@@ -1259,17 +1259,17 @@ None: leave out the `details.hb-options` block.
- **Description:** A cloth sways in the wind, and you can drag it around. Best for playful pages.
- **Watch it help line:** It moves by itself. Drag the cloth to pull it, or pause it to look closely.
- **Player bar:** Pause (page) · Reset. No Slow motion: each frame is one physics step, and slowing the steps would change how stiff the cloth is.
-- **Reset:** hangs a fresh cloth (`initCloth()`, today's Reset cloth). While paused it shows the fresh cloth still.
+- **Reset:** hangs a fresh cloth (`initCloth(paused)`, today's Reset cloth). While paused it settles the fresh cloth first (see the first picture below), so it shows the cloth already hanging, still; while playing it hangs the cloth flat and the loop lets it fall.
- **Sequence:** the canvas loop as in the preamble (`loop(ts)`, 16ms gate). Each drawn frame runs `update()`, then `draw()`, as today.
- - **Two functions in place of today's `start()`,** which both rebuilt the cloth and started the loop: `rebuild()` is `resize(); initCloth();`, for Detail and for a resize; `resume()` is `if(raf)return; lastT=performance.now(); raf=requestAnimationFrame(loop);`, for Play (`hb:pause` with `paused` false). At load the page runs `rebuild()`, then `resume()`.
- - **The first picture:** `initCloth()` draws the cloth (today it draws only while paused), so `rebuild()` at load shows it.
- - **While paused,** Hangs from and Detail show a fresh cloth, Shows the points redraws, and Wind, Gravity and Stiffness show at the next Play (they change how it moves).
+ - **Two functions in place of today's `start()`,** which both rebuilt the cloth and started the loop: `rebuild(settle)` is `resize(); initCloth(settle);`, for Detail and for a resize (both pass `paused` on as `settle`); `resume()` is `if(raf)return; lastT=performance.now(); raf=requestAnimationFrame(loop);`, for Play (`hb:pause` with `paused` false). At load the page runs `rebuild(true)`, then `resume()`.
+ - **The first picture:** `initCloth(settle)` draws the cloth (today it draws only while paused). With `settle` it first runs 120 silent physics steps (`windT+=16; update();` each, two seconds of a 60 fps loop), so a cloth shown still (at load, on a paused arrival or under reduced motion) already hangs and sways, not the flat coloured rectangle it starts as; `rebuild(true)` at load does that. It costs 25 · 47 · 71 ms at Low · Medium · High on a laptop; on the throttled phone 84 ms at Low (the phone default, paid once at load), 195 ms at Medium and 470 ms at High (paid when the visitor presses Detail while paused).
+ - **While paused,** Hangs from, Detail, Reset and a resize show a fresh cloth that has been settled first (`initCloth(paused)`, `rebuild(paused)`); while playing they hang it flat and the loop lets it fall. Shows the points redraws, and Wind, Gravity and Stiffness show at the next Play (they change how it moves).
- **Dragging does nothing while paused:** the `if(paused){update();draw();}` line in the `pointermove` handler goes, so a paused cloth stays still.
- The resize handler keeps its guard (it returns when the stage's size has not changed) and calls `rebuild()`.
- **Reduced motion:** the page's `paused=matchMedia(…).matches` start goes, and so does the rule `*,*::before,*::after{animation:none!important;transition:none!important}` (see the preamble).
- **Stage font:** no text on the stage.
- **Stage:** only the canvas. The fps badge goes. `touch-action:none` on the stage and the canvas stays (dragging needs it). `hb-dots`: no. Default height.
-- **Phone fallback:** today's stays: on screens 600px wide or less the page starts at Low detail (16 × 12 points). At load it gives Low the `on` class and `aria-pressed="true"` (and takes them from Medium) itself, before the shared script reads the settings. Measured on the throttled phone: Low takes 5.2ms a frame, Medium 8.1ms and High 29ms.
+- **Phone fallback:** today's stays: on phones (screens 600px wide or less, or 500px tall or less) the page starts at Low detail (16 × 12 points). At load it gives Low the `on` class and `aria-pressed="true"` (and takes them from Medium) itself, before the shared script reads the settings. Measured on the throttled phone: Low takes 5.2ms a frame, Medium 8.1ms and High 29ms; frames drawn in 3 seconds (180 is a full 60 fps): Low 180, Medium 141, High 52 (about 17 a second).
- **Touch:** as today: Pointer Events with pointer capture; a finger drags the nearest point within 30px.
**Main settings**
@@ -1322,7 +1322,7 @@ None: leave out the `details.hb-options` block.
- [Noise-Based Motion](../noise-based-motion/) — natural movement from noise instead of physics
- [Canvas Particle Effect](../canvas-particle-effect/) — simpler physics for loose particles
- **README How it works:** unchanged
-- **README Production notes:** the Performance bullet's last sentence becomes "The demo starts at 16×12 on screens 600px wide or less; its Detail setting still lets you pick more." The rest is unchanged.
+- **README Production notes:** the Performance bullet becomes "**Performance**: at 40×30 (1,271 points, 2,470 links, 4 passes = 9,880 link corrections a frame) a phone-class CPU manages about 17 frames a second, and 24×18 about 47. The demo starts at 16×12 on phones (screens 600px wide or less, or 500px tall or less), where it holds 60; its Detail setting still lets you pick more." The rest is unchanged.
- **Category line:** `06.15 · 3D &amp; Advanced`
- **Pager:** Previous: Image Distortion on Hover (`../image-distortion-hover/`) · Next: Volumetric Smoke / 3D Noise (`../volumetric-smoke/`)