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: freeze Rotate Word Carousel and Text Morphing at once on Pause, and let the sentence wrap

matin4bf11e4409e5
diff --git a/animations/05-text-typography/rotate-word-carousel/index.html b/animations/05-text-typography/rotate-word-carousel/index.html
index 65d249e3..71da7c48 100644
--- a/animations/05-text-typography/rotate-word-carousel/index.html
+++ b/animations/05-text-typography/rotate-word-carousel/index.html
@@ -26,7 +26,7 @@
.stage{display:flex;align-items:center;justify-content:center;background:#04060c;padding:32px;overflow:hidden}
.headline{font-weight:700;font-size:clamp(22px,3.5vw,42px);line-height:1.3;color:#fff;text-align:center;display:flex;flex-direction:column;align-items:center;gap:0}
.static-line{display:flex;align-items:center;flex-wrap:wrap;justify-content:center;gap:0.2em}
- .static-text{white-space:nowrap;color:rgba(255,255,255,.75)}
+ .static-text{color:rgba(255,255,255,.75)}
.rot-container{position:relative;display:inline-block;overflow:hidden;vertical-align:middle}
.rot-word{
display:block;
@@ -67,7 +67,7 @@
<span class="hb-num" aria-hidden="true">1</span>
<div><h2 id="hb-watch-title">Watch it</h2><p>It moves by itself. Pause it to look closely, or turn on slow motion to see each part of the movement.</p></div>
</div>
- <div class="stage hb-dots hb-grow">
+ <div class="stage hb-dots hb-grow" id="stage">
<div class="headline" id="headline">
<div class="static-line">
<span class="static-text" id="static-text">We craft for</span>
@@ -183,6 +183,7 @@ Startups</textarea>
<script>
const rotWord=document.getElementById('rot-word');
const staticText=document.getElementById('static-text');
+ const stage=document.getElementById('stage');
const vars=document.documentElement.style;
const slowTog=document.getElementById('slow-tog');
const colorTog=document.getElementById('color-tog');
@@ -193,9 +194,16 @@ Startups</textarea>
const enterClasses=['enter-below','enter-above','enter-left','enter-right'];
const outMap={up:'out-up',down:'out-down',left:'out-left',right:'out-right'};
const enterMap={up:'enter-below',down:'enter-above',left:'enter-right',right:'enter-left'};
- let dir='up',rotDur=400,hold=2000,wordIdx=0,timer=null,run=0,sliding=false,paused=false;
+ const DEFAULT_WORDS=['Designers','Developers','Humans','Teams','Startups'];
+ let dir='up',rotDur=400,hold=2000,wordIdx=0;
+ // Captured once at the start of each slide (startSlide), then reused by every phase of that same slide, so a
+ // setting change mid-slide never mismatches the exit and entry sides or the two waits' durations.
+ let curDir='up',stepDur=rotDur,nextIdx=0,nextWordVal='';
- function getWords(){return wordsIn.value.split('\n').map(w=>w.trim()).filter(Boolean)}
+ function getWords(){
+ const words=wordsIn.value.split('\n').map(w=>w.trim()).filter(Boolean);
+ return words.length?words:DEFAULT_WORDS; // an empty or blank list falls back to the defaults
+ }
function dur(){return slowTog.checked?rotDur*3:rotDur}
function setColor(el,i){
@@ -204,46 +212,63 @@ Startups</textarea>
else el.style.color='var(--word-color)';
}
- // One slide: the current word leaves, the next word is swapped in off-screen (transition off) and then
- // slides to rest. Slow motion stretches --rot-dur and the two timers below that wait for it; the 20ms
- // step between the swap and the entry transition stays the same either way.
- function transitionTo(nextWord,nextIdx,done){
- const d=dur(),id=run;
- vars.setProperty('--rot-dur',d+'ms');
- rotWord.classList.add(outMap[dir]);
- setTimeout(()=>{
- if(id!==run)return; // a restart superseded this transition
- rotWord.textContent=nextWord;
- setColor(rotWord,nextIdx);
- // jump to the entry side without transitioning from the exit side
- rotWord.style.transition='none';
- rotWord.classList.remove(...outClasses,...enterClasses);
- rotWord.classList.add(enterMap[dir]);
- void rotWord.offsetWidth;
- rotWord.style.transition='';
- setTimeout(()=>{
- if(id!==run)return;
- rotWord.classList.remove(...enterClasses);
- setTimeout(()=>{if(id===run)done()},d+50);
- },20);
- },d+30);
+ // Pause freezes the step under way: its transition stops where it is and its timer keeps the time it had
+ // left. Every timer of the loop goes through wait(), so there is always exactly one chain and Play can
+ // never start a second one.
+ let timer=0,next=null,due=0,left=0,held=[],paused=false;
+ function wait(fn,ms){clearTimeout(timer);next=fn;if(paused){left=ms;return}due=performance.now()+ms;timer=setTimeout(()=>{next=null;fn()},ms)}
+ function freeze(){paused=true;clearTimeout(timer);if(next)left=Math.max(0,due-performance.now());held=stage.getAnimations({subtree:true}).filter(a=>a.playState==='running');held.forEach(a=>a.pause())}
+ function thaw(){
+ paused=false;
+ held.forEach(a=>{
+ if(a.playState!=='paused')return;
+ // play() on an animation that has already reached its end rewinds it to the start, so one that ended
+ // while its pause was still pending (Pause pressed in the last frame of a transition) is finished instead
+ if(a.currentTime>=a.effect.getComputedTiming().endTime)a.finish();else a.play();
+ });
+ held=[];
+ if(next)wait(next,left);
}
+ document.addEventListener('hb:pause',e=>e.detail.paused?freeze():thaw());
- // A slide, then done() marks it finished and, while not paused, schedules the next one after `hold`.
- function cycle(){
- const words=getWords();if(words.length<2)return;
- const next=(wordIdx+1)%words.length;
- sliding=true;
- transitionTo(words[next],next,()=>{
- wordIdx=next;
- sliding=false;
- if(!paused)timer=setTimeout(cycle,hold);
- });
+ // One slide, in four waited phases: exit transition, an instant snap to the entry side (needs a tick to
+ // register before it can transition), entry transition, then the hold before the next slide. Slow motion
+ // stretches --rot-dur and the two transition waits; the 20ms step and the hold stay the same either way.
+ function startSlide(){
+ const words=getWords();
+ if(words.length<2)return;
+ nextIdx=(wordIdx+1)%words.length;
+ nextWordVal=words[nextIdx];
+ curDir=dir;
+ stepDur=dur();
+ vars.setProperty('--rot-dur',stepDur+'ms');
+ rotWord.classList.add(outMap[curDir]);
+ wait(afterExit,stepDur+30);
+ }
+ function afterExit(){
+ rotWord.textContent=nextWordVal;
+ wordIdx=nextIdx; // update as soon as the incoming word is shown, so a color change mid-entry uses it
+ setColor(rotWord,wordIdx);
+ // jump to the entry side without transitioning from the exit side
+ rotWord.style.transition='none';
+ rotWord.classList.remove(...outClasses,...enterClasses);
+ rotWord.classList.add(enterMap[curDir]);
+ void rotWord.offsetWidth;
+ rotWord.style.transition='';
+ wait(afterSnap,20);
+ }
+ function afterSnap(){
+ rotWord.classList.remove(...enterClasses);
+ wait(afterEnter,stepDur+50);
+ }
+ function afterEnter(){
+ wait(startSlide,hold);
}
function start(){
- clearTimeout(timer);wordIdx=0;run++;sliding=false;
- const words=getWords();if(!words.length)return;
+ clearTimeout(timer);next=null;
+ wordIdx=0;
+ const words=getWords();
// snap back to rest without replaying the exit slide in reverse
rotWord.style.transition='none';
rotWord.classList.remove(...outClasses,...enterClasses);
@@ -251,24 +276,9 @@ Startups</textarea>
rotWord.style.transition='';
rotWord.textContent=words[0];
setColor(rotWord,0);
- if(!paused)timer=setTimeout(cycle,hold);
+ wait(startSlide,hold); // while paused, wait() only records this; Play resumes from a fresh hold
}
- // Registered at the top level, before the shared script (deferred) can run: under reduced motion it pauses
- // the loop on arrival, which this listener must catch. Paused clears only the hold timer — a slide already
- // under way finishes and its own done() schedules nothing. Unpausing mid-slide does nothing here; the
- // slide's done() will schedule the next hold once it sees the loop is no longer paused. Only one chain of
- // timers ever runs, so the next Pause always stops the loop.
- document.addEventListener('hb:pause',e=>{
- paused=e.detail.paused;
- if(paused){
- clearTimeout(timer);
- }else if(!sliding){
- clearTimeout(timer);
- timer=setTimeout(cycle,hold);
- }
- });
-
sentenceIn.addEventListener('input',e=>{staticText.textContent=e.target.value});
wordsIn.addEventListener('input',start);
diff --git a/animations/05-text-typography/text-morphing/index.html b/animations/05-text-typography/text-morphing/index.html
index 18c97177..11f3e39c 100644
--- a/animations/05-text-typography/text-morphing/index.html
+++ b/animations/05-text-typography/text-morphing/index.html
@@ -23,7 +23,7 @@
<style>
:root{--ui-accent:#ff6f8b;--morph-dur:400ms}
*,*::before,*::after{box-sizing:border-box;margin:0;padding:0}
- .stage{display:flex;align-items:center;justify-content:center;position:relative;background:#08080a;overflow:hidden}
+ .stage{position:relative;background:#08080a;padding:32px;overflow:hidden}
.morph-word{position:absolute;top:50%;left:50%;transform:translate(-50%,-50%);display:flex;align-items:center;gap:0;font-size:clamp(56px,9vw,100px)}
.morph-char{
font-weight:700;
@@ -176,102 +176,110 @@ SOUND</textarea>
const vars=document.documentElement.style;
const slowTog=document.getElementById('slow-tog');
const wordsIn=document.getElementById('words-inp');
- let dir='up',morphDur=400,stag=0,hold=1500,wordIdx=0,morphTimer=null,morphing=false,paused=false;
+ const DEFAULT_WORDS=['OCEAN','RIVER','STORM','LIGHT','SOUND'];
+ let dir='up',morphDur=400,stag=0,hold=1500,wordIdx=0;
+ // Captured once at the start of each morph (startMorph), then reused by every phase of that same morph, so a
+ // setting change mid-morph never mismatches the exit and entry sides or the waits' durations.
+ let curDir='up',stepDur=morphDur,stepStag=stag,nextIdx=0,nextWordVal='';
- function getWords(){return wordsIn.value.split('\n').map(w=>w.trim().toUpperCase()).filter(Boolean)}
+ function getWords(){
+ const words=wordsIn.value.split('\n').map(w=>w.trim().toUpperCase()).filter(Boolean);
+ return words.length?words:DEFAULT_WORDS; // an empty or blank list falls back to the defaults
+ }
function curDur(){return slowTog.checked?morphDur*3:morphDur}
function curStag(){return slowTog.checked?stag*3:stag}
- // The CSS formula is viewport-based; measure against the real stage width and shrink further if needed,
- // so a long typed word still fits instead of being cut off at the stage edge.
+ // The CSS formula is viewport-based; measure against the room inside the stage's own side padding and
+ // shrink further if needed, so a long typed word still fits instead of being cut off at the stage edge.
function fit(){
wordEl.style.fontSize='';
- const room=stage.clientWidth-64;
+ const pad=getComputedStyle(stage);
+ const room=stage.clientWidth-parseFloat(pad.paddingLeft)-parseFloat(pad.paddingRight);
if(wordEl.offsetWidth>room)wordEl.style.fontSize=Math.max(16,parseFloat(getComputedStyle(wordEl).fontSize)*room/wordEl.offsetWidth)+'px';
}
function showWord(word){
wordEl.innerHTML='';
- word.split('').forEach((c,i)=>{
- const s=document.createElement('span');
- s.className='morph-char';
- s.textContent=c;
- if(stag>0)s.style.transitionDelay=(stag*i)+'ms';
- wordEl.appendChild(s);
+ word.split('').forEach(c=>{
+ const span=document.createElement('span');
+ span.className='morph-char';
+ span.textContent=c;
+ wordEl.appendChild(span);
});
fit();
}
- // Slides the current letters out, then builds the next word's letters off-screen and slides them in.
- // Slow motion stretches --morph-dur and the per-letter delay, so both waits below stretch with them;
- // the 60ms and 50ms parts stay the same either way.
- function morphTo(toWord,done){
- const d=curDur(),s=curStag();
- vars.setProperty('--morph-dur',d+'ms');
+ // Pause freezes the step under way: its transitions stop where they are and its timer keeps the time it had
+ // left. Every timer of the loop goes through wait(), so there is always exactly one chain and Play can
+ // never start a second one.
+ let timer=0,next=null,due=0,left=0,held=[],paused=false;
+ function wait(fn,ms){clearTimeout(timer);next=fn;if(paused){left=ms;return}due=performance.now()+ms;timer=setTimeout(()=>{next=null;fn()},ms)}
+ function freeze(){paused=true;clearTimeout(timer);if(next)left=Math.max(0,due-performance.now());held=stage.getAnimations({subtree:true}).filter(a=>a.playState==='running');held.forEach(a=>a.pause())}
+ function thaw(){
+ paused=false;
+ held.forEach(a=>{
+ if(a.playState!=='paused')return;
+ // play() on an animation that has already reached its end rewinds it to the start, so one that ended
+ // while its pause was still pending (Pause pressed in the last frame of a transition) is finished instead
+ if(a.currentTime>=a.effect.getComputedTiming().endTime)a.finish();else a.play();
+ });
+ held=[];
+ if(next)wait(next,left);
+ }
+ document.addEventListener('hb:pause',e=>e.detail.paused?freeze():thaw());
+
+ // One morph, in four waited phases: the hold on the current word, the letters leaving, the next word's
+ // letters arriving, then a 100ms gap before the next hold. Slow motion stretches --morph-dur and the letter
+ // delay, so the two transition waits stretch with them; the 60ms and 50ms parts, the hold and the 100ms
+ // gap stay the same either way.
+ function startMorph(){
+ const words=getWords();
+ if(words.length<2)return;
+ nextIdx=(wordIdx+1)%words.length;
+ nextWordVal=words[nextIdx];
+ curDir=dir;
+ stepDur=curDur();
+ stepStag=curStag();
+ vars.setProperty('--morph-dur',stepDur+'ms');
const fromChars=[...wordEl.querySelectorAll('.morph-char')];
- const outClass='out-'+dir;
fromChars.forEach((c,i)=>{
- c.style.transitionDelay=(s>0?s*i:0)+'ms';
- c.classList.add(outClass);
+ c.style.transitionDelay=(stepStag*i)+'ms';
+ c.classList.add('out-'+curDir);
});
- const exitDur=d+(s*(fromChars.length-1));
- morphTimer=setTimeout(()=>{
- wordEl.innerHTML='';
- const enterDir={up:'enter-from-below',down:'enter-from-above',left:'enter-from-right',right:'enter-from-left'}[dir];
- const toChars=[];
- toWord.split('').forEach((c,i)=>{
- const s2=document.createElement('span');s2.className='morph-char '+enterDir;
- if(s>0)s2.style.transitionDelay=(s*i)+'ms';
- s2.textContent=c;wordEl.appendChild(s2);toChars.push(s2);
- });
- fit();
- void wordEl.offsetWidth;
- toChars.forEach(c=>c.classList.remove(enterDir));
- const enterDur=d+(s*(toChars.length-1));
- morphTimer=setTimeout(done,enterDur+50);
- },exitDur+60);
+ wait(afterExit,stepDur+stepStag*(fromChars.length-1)+60);
}
-
- // Shows the current word, then (once Time on each word has passed) starts a morph to the next word.
- // The morph's own callback schedules the next cycle() after a short 100ms gap. The list repeats forever.
- function cycle(){
- const words=getWords();if(words.length<2)return;
- if(wordIdx>=words.length)wordIdx=0; // word list shortened mid-cycle
- morphTimer=setTimeout(()=>{
- morphing=true;
- const next=(wordIdx+1)%words.length;
- morphTo(words[next],()=>{
- wordIdx=next;
- morphing=false;
- if(!paused)morphTimer=setTimeout(cycle,100);
- });
- },hold);
+ function afterExit(){
+ wordEl.innerHTML='';
+ const enterDir={up:'enter-from-below',down:'enter-from-above',left:'enter-from-right',right:'enter-from-left'}[curDir];
+ const toChars=[];
+ nextWordVal.split('').forEach((c,i)=>{
+ const span=document.createElement('span');
+ span.className='morph-char '+enterDir;
+ if(stepStag>0)span.style.transitionDelay=(stepStag*i)+'ms';
+ span.textContent=c;
+ wordEl.appendChild(span);
+ toChars.push(span);
+ });
+ wordIdx=nextIdx;
+ fit();
+ void wordEl.offsetWidth;
+ toChars.forEach(c=>c.classList.remove(enterDir));
+ wait(afterEnter,stepDur+stepStag*(toChars.length-1)+50);
+ }
+ function afterEnter(){
+ wait(afterGap,100);
+ }
+ function afterGap(){
+ wait(startMorph,hold);
}
function start(){
- clearTimeout(morphTimer);morphing=false;wordIdx=0;
- const words=getWords();
- if(!words.length){wordEl.innerHTML='';return}
- showWord(words[0]);
- if(!paused)cycle();
+ clearTimeout(timer);next=null;
+ wordIdx=0;
+ showWord(getWords()[0]);
+ wait(startMorph,hold); // while paused, wait() only records this; Play resumes from a fresh hold
}
- // Registered at the top level, before the shared script (deferred) can run: under reduced motion it pauses
- // the loop on arrival, which this listener must catch. morphTimer holds whichever wait is current (the
- // hold, either half of a morph, or the 100ms gap), so clearing it always cancels the right thing. Pausing
- // mid-morph does nothing here — clearing morphTimer then would strand the letters out of sight — the morph
- // finishes on its own and its callback schedules nothing. Unpausing mid-morph also does nothing here; the
- // morph's callback will schedule the next step once it sees the loop is no longer paused.
- document.addEventListener('hb:pause',e=>{
- paused=e.detail.paused;
- if(paused){
- if(!morphing)clearTimeout(morphTimer);
- }else if(!morphing){
- clearTimeout(morphTimer);
- cycle();
- }
- });
-
wordsIn.addEventListener('input',start);
addEventListener('resize',fit);
if(document.fonts)document.fonts.ready.then(fit); // the site font can still be loading when fit() first runs