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: move Parallax 3D Tilt's shadow and shine with transform only

matin29275168b881
diff --git a/animations/06-3d-advanced/parallax-3d-tilt/README.md b/animations/06-3d-advanced/parallax-3d-tilt/README.md
index d346b293..d9f4ffcc 100644
--- a/animations/06-3d-advanced/parallax-3d-tilt/README.md
+++ b/animations/06-3d-advanced/parallax-3d-tilt/README.md
@@ -32,14 +32,21 @@ card.addEventListener('pointerleave', () => {
});
```
-**Shine highlight** — a radial gradient repositioned to the cursor:
+**Shine highlight** — one fixed radial gradient on a layer of its own, moved to the cursor with a transform. Rewriting a gradient on every move repaints the whole card; moving a layer that is already painted does not, and the card's `overflow: hidden` clips it:
+
+```css
+.shine { /* --shine-d: twice the card's diagonal, set once by the script */
+ position: absolute; left: 0; top: 0;
+ width: var(--shine-d); height: var(--shine-d);
+ margin: calc(var(--shine-d) / -2) 0 0 calc(var(--shine-d) / -2); /* its middle starts on the card's top left corner */
+ background: radial-gradient(circle closest-side, rgba(255,255,255,0.18) 0%, transparent 65%);
+}
+```
```js
-shine.style.background = `radial-gradient(
- circle at ${(x + 0.5) * 100}% ${(y + 0.5) * 100}%,
- rgba(255,255,255,0.18) 0%,
- transparent 65%
-)`;
+// W and H: the card's width and height. D: --shine-d. R: how far the light reaches, out to the farthest corner
+const R = Math.hypot((0.5 + Math.abs(x)) * W, (0.5 + Math.abs(y)) * H);
+shine.style.transform = `translate(${(x + 0.5) * W}px, ${(y + 0.5) * H}px) scale(${2 * R / D})`;
```
**Reset transition** — only apply `transition` on leave, not while tracking:
@@ -50,7 +57,7 @@ card.addEventListener('pointerleave', () => card.classList.remove('active'));
```
```css
-.card { transition: transform 400ms ease, box-shadow 400ms ease; }
+.card { transition: transform 400ms ease; }
.card.active { transition: none; } /* instant tracking while the pointer moves */
```
@@ -67,7 +74,7 @@ function step(now) { // t0 = the time of the first fra
}
```
-`x0` and `y0` are where the card already leans when Show me is pressed, so a second press carries on from there. A real pointer, finger or key press cancels the frame and flattens the card, so the visitor is always in control.
+`x0` and `y0` are where the card already leans when Show me is pressed, so a second press carries on from there. A press, a finger or a wheel turn over the stage, or the pointer moving over the card, cancels the frame and flattens the card, so the visitor is always in control.
## Key parameters
| Parameter | Default | Effect |
@@ -84,7 +91,7 @@ function step(now) { // t0 = the time of the first fra
- **`will-change: transform`**: add only during hover (`mouseenter`/`mouseleave`) to avoid permanent GPU layer allocation. Permanent `will-change` on many cards multiplies GPU memory use.
- **VanillaTilt.js**: a zero-dependency library that handles this pattern with configurable tilt, glare, scale, and perspective. 2KB gzipped — use in production rather than hand-rolling.
- **`overflow: hidden` flattens 3D**: any ancestor with `overflow` other than `visible` (or a `filter`, or `opacity < 1`) forces `transform-style` to its flat used value, so `translateZ` on a child silently does nothing. A card that clips its own contents therefore cannot also be a `preserve-3d` container — give it its own `perspective` so its inner depth layers still project, or move the clip to a wrapper outside the 3D chain.
-- **Performance**: `rotateX`/`rotateY` on a GPU-composited element runs at 60fps with no paint. Avoid animating `box-shadow` simultaneously — shadows trigger paint; the demo does it for the depth cue on a single card, but it is not something to repeat across a grid.
+- **Performance**: `rotateX`/`rotateY` on a GPU-composited element runs at 60fps with no paint. Keep everything else that follows the pointer to `transform` and `opacity` as well: the demo moves its light as a layer (see Shine highlight) and leaves the card's shadow fixed in CSS, where it turns with the card. Rewriting a gradient or a `box-shadow` on every move repaints the whole card each frame, which shows on phones and adds up across a grid.
- **Reduced motion**: the demo does not tilt on its own, and the card snaps flat when the pointer leaves instead of easing back. In production, keep the card flat for these visitors.
## See also
diff --git a/animations/06-3d-advanced/parallax-3d-tilt/index.html b/animations/06-3d-advanced/parallax-3d-tilt/index.html
index bb57a820..bdeabc1e 100644
--- a/animations/06-3d-advanced/parallax-3d-tilt/index.html
+++ b/animations/06-3d-advanced/parallax-3d-tilt/index.html
@@ -36,15 +36,19 @@
projected by the perspective on .stage, never by a perspective() here. */
perspective:var(--perspective);
transform:rotateX(0deg) rotateY(0deg) scale(1);
- transition:transform var(--reset-dur) ease,box-shadow var(--reset-dur) ease;
+ transition:transform var(--reset-dur) ease;
cursor:pointer;touch-action:none;
will-change:transform
}
.tilt-card.active{transition:none}
+ /* The light is one fixed gradient on a layer of its own, twice as wide as the card's diagonal (--shine-d, set by the script)
+ and centred on the card's top left corner. The script moves it to the pointer with translate() and sizes it with scale(),
+ so following the pointer never repaints the card. The card clips it. */
.card-shine{
- position:absolute;inset:0;border-radius:16px;
- background:radial-gradient(circle at 50% 50%, rgba(255,255,255,.15) 0%, transparent 70%);
- opacity:0;pointer-events:none;transition:opacity .3s
+ position:absolute;left:0;top:0;width:var(--shine-d,0);height:var(--shine-d,0);
+ margin:calc(var(--shine-d,0px) / -2) 0 0 calc(var(--shine-d,0px) / -2);
+ background:radial-gradient(circle closest-side,rgba(255,255,255,.18) 0%,transparent 65%);
+ opacity:0;pointer-events:none;transition:opacity .3s;will-change:transform
}
.card-content{position:relative;z-index:1;height:100%;padding:28px;display:flex;flex-direction:column;justify-content:flex-end;gap:8px}
.card-over{font-size:11px;font-weight:600;letter-spacing:2px;text-transform:uppercase;color:rgba(255,255,255,.7)}
@@ -193,20 +197,25 @@
let maxTilt=15,invert=false,shineOn=true,scaleOn=true;
let tiltX=0,tiltY=0; // the last x and y tilt() was given (0 after onLeave()): a Show me run starts from them
let raf=0; // the frame of a Show me run that is under way, 0 when there is none
+ let shineD=0; // the size given to the shine layer (--shine-d), set again only when the card's size changes
// x and y run from -0.5 to 0.5 across the card. The pointer, a finger and Show me all move the card through this.
+ // Only transform and opacity change: the card keeps its CSS shadow, which turns with it, and the light moves as a layer.
function tilt(x,y){
tiltX=x;tiltY=y;
const inv=invert?-1:1;
const rx=(-y*maxTilt*inv).toFixed(1);
const ry=(x*maxTilt*inv).toFixed(1);
+ const W=card.clientWidth,H=card.clientHeight; // the card's inner size, read before the transform is written so no frame forces a layout
card.classList.add('active');
const scale=scaleOn?'scale(1.04)':'scale(1)';
card.style.transform=`rotateX(${rx}deg) rotateY(${ry}deg) ${scale}`;
- card.style.boxShadow=`${-ry*.5}px ${rx*.5}px 40px rgba(0,0,0,.7)`;
if(shineOn){
+ const D=2*Math.hypot(W,H);
+ if(D!==shineD){shineD=D;card.style.setProperty('--shine-d',D+'px')}
+ const R=Math.hypot((.5+Math.abs(x))*W,(.5+Math.abs(y))*H); // how far the light reaches: to the farthest corner
shine.style.opacity='1';
- shine.style.background=`radial-gradient(circle at ${(x+.5)*100}% ${(y+.5)*100}%,rgba(255,255,255,.18) 0%,transparent 65%)`;
+ shine.style.transform=`translate(${(x+.5)*W}px,${(y+.5)*H}px) scale(${2*R/D})`;
}
// Subtle inner parallax
orb1.style.transform=`translateZ(20px) translate(${-ry*.3}px,${rx*.3}px)`;
@@ -222,7 +231,6 @@
tiltX=tiltY=0;
card.classList.remove('active');
card.style.transform='rotateX(0deg) rotateY(0deg) scale(1)';
- card.style.boxShadow='';
shine.style.opacity='0';
orb1.style.transform='translateZ(20px)';
orb2.style.transform='translateZ(30px)';
diff --git a/docs/superpowers/plans/2026-09-29-3d-content.md b/docs/superpowers/plans/2026-09-29-3d-content.md
index 12194bc5..cf255cb0 100644
--- a/docs/superpowers/plans/2026-09-29-3d-content.md
+++ b/docs/superpowers/plans/2026-09-29-3d-content.md
@@ -283,15 +283,19 @@ None: leave out the `details.hb-options` block.
- **Player bar:** Show me. No Reset: the card settles flat by itself when the pointer leaves. No Slow motion: the tilt follows the pointer directly.
- **Show me:** one run of 3 seconds. A pretend pointer starts from where the card is, circles the card's middle one and a half times, reaching 80% of the way to the edges halfway through, and ends in the middle. For progress p from 0 to 1, `r = 0.4*Math.sin(Math.PI*p)` and `a = 3*Math.PI*p`, and each animation frame calls `tilt(x0*(1-p) + r*Math.cos(a), y0*(1-p) + r*Math.sin(a))`, where x0 and y0 are the card's tilt when the run starts (the last values `tilt()` was given; 0 after `onLeave()`). At the end it calls `onLeave()`, so the card settles flat over Return speed. A second press starts a new run from where the card is, as on 2.5D / Pseudo-3D.
- **Stopping a run:** on `hb:input`, only if a run is under way (its frame handle is set): cancel the frame and call `onLeave()`; otherwise do nothing, so a tap does not flatten the card under a resting finger. The card's `pointermove` handler also ends a run that is under way the same way, for the visitor's own (trusted) events, then tilts the card to the pointer.
-- **Sequence:** `onMove(e)` becomes `tilt(x, y)` (x and y from −0.5 to 0.5) plus a small `onMove(e)` that works out x and y from the event and calls it. `tilt()` keeps the last x and y it was given (Show me starts from them), and `onLeave()` sets them back to 0. Everything else runs as today: `pointerdown` and `pointermove` on the card tilt it, and `pointerleave` and `pointercancel` call `onLeave()`.
+- **Sequence:** `onMove(e)` becomes `tilt(x, y)` (x and y from −0.5 to 0.5) plus a small `onMove(e)` that works out x and y from the event and calls it. `tilt()` keeps the last x and y it was given (Show me starts from them), and `onLeave()` sets them back to 0. `onMove()` holds x and y between −0.5 and 0.5 (`edge()`): a finger dragged off the card keeps sending its moves to the card, and without the hold the card would lean without limit. Apart from the next bullet, everything else runs as today: `pointerdown` and `pointermove` on the card tilt it, and `pointerleave` and `pointercancel` call `onLeave()`.
+- **Only `transform` and `opacity` change while the card follows the pointer, a finger or Show me (fix round 1).** CLAUDE.md's rule "transform and opacity only — never animate `box-shadow`" governs over "as today" (the coordinator's ruling for this page, following the one on Task 7 that CLAUDE.md's rules govern over the sheet). Today's `tilt()` wrote `card.style.boxShadow` and rewrote the shine's gradient on every frame, and the shadow was part of the card's transition; either repaints the whole card. A trace of 1.5 s of Show me (phone stage, 375×812 at pixel ratio 2) showed 183 paint events and 98 to 135 raster tasks (about 250 ms of raster time); after the change it shows 4 paint events and 16 raster tasks (about 10 to 18 ms), and on a 1280×800 stage 4 paints against 181 to 185.
+ - The card keeps its resting CSS shadow (`0 20px 60px rgba(0,0,0,.6)`), which turns with the card. Both writes to `boxShadow` go (`tilt()` and `onLeave()`), and the transition is `transform var(--reset-dur) ease` only.
+ - The shine is one fixed gradient on a layer of its own: `.card-shine{position:absolute;left:0;top:0;width:var(--shine-d,0);height:var(--shine-d,0);margin:calc(var(--shine-d,0px) / -2) 0 0 calc(var(--shine-d,0px) / -2);background:radial-gradient(circle closest-side,rgba(255,255,255,.18) 0%,transparent 65%);opacity:0;pointer-events:none;transition:opacity .3s;will-change:transform}`, a square twice as wide as the card's diagonal with its middle on the card's top left corner (the card's `overflow:hidden` clips it). `tilt()` reads `W = card.clientWidth` and `H = card.clientHeight` before it writes the card's transform, sets `--shine-d` (`D = 2*Math.hypot(W,H)`) on the card only when it changes, and moves the layer with `shine.style.transform = translate((x+.5)*W px, (y+.5)*H px) scale(2*R/D)`, where `R = Math.hypot((.5+Math.abs(x))*W, (.5+Math.abs(y))*H)` is the radius today's gradient has (a `circle` reaches the farthest corner). The Shine switch, `onLeave()` and the reduced-motion rule still work on the shine's `opacity`.
+ - The card and the light look as before. Against the page as it was (shadow and gradient written every frame), at eight poses (two corners, the middle and five others), three pairs of settings (Medium and Medium, Large and Strong, Small and Subtle) and stages of 1280×800, 375×812 (pixel ratio 2) and 1280×590, no pixel of the stage differs by more than 27 levels of a channel, the mean difference is under 1.1 levels, and at most 0.45% of the pixels differ by more than 8 levels. Those pixels are the one-pixel rim of the card, where its edge blends with the shadow just outside it: at the middle pose the old shadow was centred under the card and the CSS one falls 20px below it.
- **Reduced motion:** Show me does not run on arrival (the shared script). The demo's reduced-motion rule loses `transform:none!important` on the card and the orbs and `opacity:0!important` on the shine, which would stop Show me and the visitor's own tilt. What stays is `.tilt-card{transition:none!important}` and `.card-shine{transition:none!important}`, so the card snaps flat when the pointer leaves instead of easing back.
-- **Stage font:** site font. `.card-h` drops Georgia and the italic and gets `font-weight:700`. `.card-over` gets 11px and `font-weight:600` (was 9px); `.card-sub` gets 13px and `rgba(255,255,255,.72)` (was 11px at .55).
+- **Stage font:** site font. `.card-h` drops Georgia and the italic and gets `font-weight:700`. `.card-over` gets 11px, `font-weight:600` (was 9px) and `rgba(255,255,255,.7)` (was .4, which measures 2.3 to 3.8:1); `.card-sub` gets 13px and `rgba(255,255,255,.9)` (was 11px at .55) with `text-wrap:pretty`, so the phone card does not leave "it." alone on a third line. The first choice for `.card-sub`, .72, measured 3.6:1 with the pointer beside the purple orb (its glow and the light both under the words), 4.44:1 beside the blue orb and 5.1:1 at rest; .9, with the purple orb at `rgba(210,168,255,.2)` (was .25), holds at least 4.95:1 for the card line, 5.2:1 for the over-line and 7.4:1 for the title in every pose measured.
- **Stage:**
- The card stays, with its shine and its two floating orbs. Card text: over-line "Feature card"; title "Three-dimensional perspective"; line "The card leans toward your pointer, and the light follows it." (was "Mouse position maps to rotateX and rotateY. The highlight follows the virtual light source.").
- The readout on the card and the "Move or drag the card →" line go.
- - The stage keeps `background:#060a10`, its flex centring and `perspective:var(--perspective)`. `hb-dots`: yes. Default height: the card is 240×240px on phones (30px of room above and below on the 300px stage) and 340×240px on a laptop.
+ - The stage keeps `background:#060a10`, its flex centring and `perspective:var(--perspective)`. `hb-dots`: yes. Default height: the card is 240×240px on phones (30px of room above and below on the 300px stage) and 340×240px on a laptop. Its height is `min(240px,86%)`, which is 240px on every stage of 280px or more and 222px on the 258px stage of a 1280×590 window and of a phone held sideways: with Large and Strong the corners of a 240px card left that stage by up to 6px, now they stay at least 4.2px inside (10.9px with the default settings).
- **Phone fallback:** none needed: one card moved with 3D transforms.
-- **Touch:** as today: the card has `touch-action:none`, so dragging a finger over it tilts it and a tap tilts it once toward the finger.
+- **Touch:** as today: the card has `touch-action:none`, so dragging a finger over it tilts it and a tap tilts it once toward the finger. A finger dragged off the card leans it no more than at its edge (`edge()`, above).
**Main settings**
@@ -342,9 +346,10 @@ None: leave out the `details.hb-options` block.
- **README How it works:** two edits, so the prose and the snippet match the demo and Key parameters:
- "The card's `rotateX`/`rotateY` are computed from cursor position relative to the card's center, normalized to ±maxAngle:" becomes "The card's `rotateX`/`rotateY` are computed from the cursor's position relative to the card's centre, normalized to ±0.5, then scaled by MAX, so the card leans MAX/2 at its edges:".
- In the first snippet, `scale(1.03)` becomes `scale(1.04)`, matching the demo and Grows a little.
+ - The Shine highlight snippet becomes the fixed gradient (CSS) and its `translate()` and `scale()` line; the Reset transition CSS loses `box-shadow 400ms ease`; and after those comes a Show me paragraph with its snippet (the `step()` function that calls `tilt()`), ending "A press, a finger or a wheel turn over the stage, or the pointer moving over the card, cancels the frame and flattens the card, so the visitor is always in control." (nothing in the stage takes focus, so a key press cannot reach it).
The rest is unchanged.
-- **README Production notes:** the Reduced motion bullet becomes: "**Reduced motion**: the demo does not tilt on its own, and the card snaps flat when the pointer leaves instead of easing back. In production, keep the card flat for these visitors." The rest is unchanged.
+- **README Production notes:** the Reduced motion bullet becomes: "**Reduced motion**: the demo does not tilt on its own, and the card snaps flat when the pointer leaves instead of easing back. In production, keep the card flat for these visitors." In the Performance bullet, everything after its first sentence becomes: "Keep everything else that follows the pointer to `transform` and `opacity` as well: the demo moves its light as a layer (see Shine highlight) and leaves the card's shadow fixed in CSS, where it turns with the card. Rewriting a gradient or a `box-shadow` on every move repaints the whole card each frame, which shows on phones and adds up across a grid." The rest is unchanged.
- **Category line:** `06.03 · 3D &amp; Advanced`
- **Pager:** Previous: Scroll-Driven 3D Rotation (`../scroll-driven-3d-rotation/`) · Next: Canvas Particle Effect (`../canvas-particle-effect/`)