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: Image Trail (04.39): reduced motion switched on after the sweep or during the visitor's own trail fades the pictures out where they are

matin0e593e82f5f4
diff --git a/animations/04-micro-interactions/image-trail/README.md b/animations/04-micro-interactions/image-trail/README.md
index 8bcc46cc..5eeb40c4 100644
--- a/animations/04-micro-interactions/image-trail/README.md
+++ b/animations/04-micro-interactions/image-trail/README.md
@@ -36,7 +36,7 @@ el.animate([
`at(x, y, s, r)` writes `translate(x, y) translate(-50%, -50%) rotate(r) scale(s)`: every keyframe has the same list of functions, so the browser interpolates each one, and the frame stays centered on its spot while it turns and scales. When the pictures fall, the last step eases in more steeply, like something dropping. The demo's Slow motion sets `playbackRate` to a third on every animation in the hero, which slows each picture's whole life.
-On touch screens the hero has `touch-action: none` and takes pointer capture, so a finger drops a picture where it touches and leaves the trail as it drags, instead of scrolling the page. Under reduced motion there is no trail: one picture fades in where the pointer is, without moving or growing, and fades out when the next one appears or its time is up. Show me's sweep reads the setting on every frame, so if reduced motion is switched on while it runs, the sweep stops there and the pictures already out fade where they are.
+On touch screens the hero has `touch-action: none` and takes pointer capture, so a finger drops a picture where it touches and leaves the trail as it drags, instead of scrolling the page. Under reduced motion there is no trail: one picture fades in where the pointer is, without moving or growing, and fades out when the next one appears or its time is up. If reduced motion is switched on while pictures are out, every one of them fades where it is: a `change` listener on the media query does it, and Show me's sweep also reads the setting on every frame, so it stops there too.
## Key parameters
| Parameter | Default | Effect |
diff --git a/animations/04-micro-interactions/image-trail/index.html b/animations/04-micro-interactions/image-trail/index.html
index 55fe74d2..ea595ece 100644
--- a/animations/04-micro-interactions/image-trail/index.html
+++ b/animations/04-micro-interactions/image-trail/index.html
@@ -188,9 +188,11 @@
function measure(){W=stage.clientWidth;H=stage.clientHeight;stage.style.setProperty('--w',Math.round(sizeK*Math.min(W,H))+'px')}
// A frame centered on x,y, turned by r degrees and scaled by s
const at=(x,y,s,r)=>`translate(${x.toFixed(1)}px,${y.toFixed(1)}px) translate(-50%,-50%) rotate(${r.toFixed(1)}deg) scale(${s})`;
- // A picture fades out where it is: under reduced motion, and when reduced motion is switched on during Show me's sweep
+ // Pictures fade out where they are: one under reduced motion, and every one still out when reduced motion is switched on (its
+ // change event below; Show me's sweep also reads the setting every frame, as Chrome may not send the event while a loop reads it)
function fadeOut(el){const cs=getComputedStyle(el),from={transform:cs.transform,opacity:cs.opacity};
el.getAnimations().forEach(a=>a.cancel());el.animate([from,{transform:from.transform,opacity:0}],{duration:250})}
+ function fadeAll(){pool.forEach(el=>{if(el._a&&el._a.playState==='running')fadeOut(el)})}
// Drops the next picture at x,y, on top of the others, and says whether it did: a picture still out is never cut short, so when
// every frame is busy nothing drops and the trail thins. A picture's life is one animation (el._a) of transform and opacity: it
// glides in from the last drop (from), growing from half size with a small overshoot, stays, then leaves as How they leave says;
@@ -218,9 +220,8 @@
el._a=el.animate(frames,{duration:life});el._a.playbackRate=slowTog.checked?1/3:1;
return true;
}
- // Every pointer, finger and Show me position comes through here. A picture drops each time the pointer has gone the gap (How
- // often a picture appears, a share of the hero's shorter side) from the last drop, or as soon as a frame is free after that; a
- // stroke's first point only marks where it starts. Under reduced motion a picture also shows wherever the pointer is when none is showing.
+ // Every pointer, finger and Show me position comes through here: a picture drops once the pointer is the gap (How often a picture
+ // appears) from the last drop and a frame is free; a stroke's first point marks its start; under reduced motion, also when none shows.
function track(x,y){
const free=reduce.matches&&!(live&&live._a.playState==='running'); // reduced motion, and no picture is showing
if(last&&!free&&Math.hypot(x-last.x,y-last.y)<gapK*Math.min(W,H))return;
@@ -239,9 +240,8 @@
// The empty hero: every picture goes at once
function toRest(){pool.forEach(el=>el.getAnimations().forEach(a=>a.cancel()));last=null;live=null}
- // A mouse or a pen in the air leaves the trail while it moves over the hero; a finger drops a picture where it touches and
- // leaves the trail while it drags (the capture keeps its moves coming). Pointer events only. The visitor's own pointer stops a
- // run, and the trail follows them after it.
+ // A mouse or pen in the air leaves the trail while it moves over the hero; a finger drops a picture where it touches and leaves the
+ // trail as it drags (the capture keeps its moves coming). The visitor's own pointer stops a run, and the trail follows them after it.
stage.addEventListener('pointermove',e=>{
if(e.pointerType==='touch'&&e.pointerId!==touchId)return;
if(e.isTrusted){if(e.pointerType!=='touch')hovering=true;if(running())stopRun()}
@@ -255,9 +255,8 @@
});
['pointerup','pointercancel'].forEach(t=>stage.addEventListener(t,e=>{if(e.pointerId===touchId){touchId=null;last=null}}));
- // A pretend pointer sweeps across the hero in 2.4 s (three times as long in Slow motion), easing in and out, in one and a half
- // waves, by elapsed time, so it takes as long on every screen; at the defaults the hero is empty again at about 3.7 s. Reduced
- // motion switched on mid-sweep (read every frame) stops it there and fades the pictures out where they are. Under reduced motion
+ // A pretend pointer sweeps across the hero in 2.4 s (three times as long in Slow motion), easing in and out in one and a half waves,
+ // by elapsed time, so it takes as long on every screen; at the defaults the hero is empty again at about 3.7 s. Under reduced motion
// a picture fades in at three spots in turn, a second apart. The arrival press leaves alone a visitor already over the hero.
document.getElementById('btn-demo').addEventListener('click',e=>{
if(!e.isTrusted&&hovering)return;
@@ -268,7 +267,7 @@
}
let p=0,lastT=null;
runFrame=requestAnimationFrame(function frame(ts){
- if(reduce.matches){stopRun();pool.forEach(el=>{if(el._a&&el._a.playState==='running')fadeOut(el)});return}
+ if(reduce.matches){stopRun();fadeAll();return}
const dt=lastT===null?0:Math.min(ts-lastT,50);lastT=ts;
p=Math.min(1,p+dt/(RUN*(slowTog.checked?3:1)));
const s=p<.5?2*p*p:1-2*(1-p)*(1-p);
@@ -279,6 +278,7 @@
});
// The visitor pressed, typed, scrolled or touched inside the stage: the run stops, and the pictures already out leave as usual.
document.addEventListener('hb:input',()=>{if(running())stopRun()});
+ reduce.addEventListener('change',()=>{if(reduce.matches)fadeAll()});
function choices(id,pick){
const buttons=document.querySelectorAll('#'+id+' button');