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.

Merge branch 'fix/home-minors' into int/home2

matinac32e6440944
diff --cc index.html
index 340c0941,db7787ec..973dd112
--- a/index.html
+++ b/index.html
@@@ -1731,162 -1738,13 +1738,162 @@@ document.querySelectorAll('[data-count]
const n=ALL.filter(e=>e.places.includes(el.dataset.count)).length;
el.textContent=n+(n===1?' animation':' animations');
});
-showCards(PICKS.map(s=>BY_SLUG[s]));
+// ---------- Views, search and the address ----------
+const PAGE=8;
+const STOP=new Set(['a','an','and','the','that','this','to','for','with','when','on','in','of','my','it','i','want','make','some']);
+// A typed word drops the first of these endings that leaves three letters with a vowel ("ring" stays whole), never the s of
+// "ss"; after "ing" or "ed" a doubled last letter other than l, s, f or z goes too while three stay (snapping → snap, added → add).
+function stem(w){
+ if(w.endsWith('ss'))return w;
+ for(const end of ['ies','ing','ed','es','s']){
+ const rest=w.slice(0,-end.length);
+ if(!w.endsWith(end)||rest.length<3||!/[aeiouy]/.test(rest))continue;
+ return (end==='ing'||end==='ed')&&rest.length>3&&/([bcdghjkmnpqrtvwx])\1$/.test(rest)?rest.slice(0,-1):rest;
+ }
+ return w;
+}
+function queryWords(text){return [...new Set(wordsOf(text).filter(w=>!STOP.has(w)).map(stem))];}
+// A word counts 3 in the name, 2 in a place name, 1 in the description or category; each is matched by the start of a word.
+function score(e,words){
+ let s=0;
+ for(const w of words){
+ if(e.nameW.some(x=>x.startsWith(w)))s+=3;
+ if(e.placeW.some(x=>x.startsWith(w)))s+=2;
+ if(e.descW.some(x=>x.startsWith(w)))s+=1;
+ }
+ return s;
+}
+function search(text){
+ const words=queryWords(text);
+ if(!words.length)return [];
+ return ALL.map(e=>({e,s:score(e,words)})).filter(x=>x.s>0).sort((a,b)=>b.s-a.s||a.e.order-b.e.order).map(x=>x.e);
+}
+// All animations: a plain section per group, headed by an h3 (a section with no name adds no landmark). The card titles are
+// h3 too, so here they are set a level below the group headings; showCards() takes that away again.
+function showGroups(){
+ const boxes=[],cards=[];
+ CATS.forEach(cat=>{
+ const list=ALL.filter(e=>e.cat===cat),box=document.createElement('section');
+ box.className='group';box.id='cat-'+slugify(cat.name);
+ box.innerHTML=`<h3 class="ghead"><span class="gnum">${cat.n}</span><span class="gname">${esc(cat.name)}</span><span class="gcount">${list.length} animations</span></h3>`;
+ const grid=document.createElement('div');grid.className='grid';
+ list.forEach(e=>{const c=CARD[e.slug];c.querySelector('.title').setAttribute('aria-level','4');grid.appendChild(c);cards.push(c);});
+ box.appendChild(grid);boxes.push(box);
+ });
+ cardsBox.replaceChildren(...boxes);
+ place(cards);
+}
+const state={q:'',place:'',view:'',all:false};
+const isFiltered=()=>!!(state.q||state.place||state.view);
+const q=$('q'),q2=$('q2'),qClear=$('q-clear'),titleEl=$('results-title'),subEl=$('results-sub'),clearBtn=$('clear-view'),
+ emptyEl=$('empty'),emptyTitle=$('empty-title'),moreBox=$('more'),moreLabel=$('more-label');
+const phone=matchMedia('(max-width:600px)');
+let moreAction='all';
+const pinbar=$('pinbar');
+// The phone bar is there while a view other than Start is open and while its own search box has focus, so emptying the box does not take the box away.
+const syncBar=()=>resultsEl.classList.toggle('filtered',isFiltered()||document.activeElement===q2);
+// A pressed button that goes away with the view hands keyboard focus to the results heading. No scrolling: the callers that move the page scroll it themselves.
+const toTitle=()=>titleEl.focus({preventScroll:true});
+const gone=el=>!el.getClientRects().length;
+function render(){
+ const filtered=isFiltered();
+ let list=null,title,sub,tags=true;
+ if(state.q){
+ list=search(state.q);title='Results for “'+state.q+'”';
+ sub=list.length===1?'1 animation matches.':list.length+' animations match.';
+ }else if(state.place){
+ list=ALL.filter(e=>e.places.includes(state.place));title=PLACE[state.place].name;sub=list.length+' animations';tags=false;
+ }else if(state.view==='all'){
+ title='All animations';sub=ALL.length+' animations in '+CATS.length+' groups.';
+ }else{
+ list=PICKS.map(s=>BY_SLUG[s]);title='Good places to start';sub='Eight favorites from different places. Pick a place above to see more.';
+ }
+ titleEl.textContent=title;subEl.textContent=sub;
+ syncBar();
+ resultsEl.classList.toggle('no-tags',!tags);
+ clearBtn.hidden=!filtered;
+ if(list)showCards(state.all||!filtered?list:list.slice(0,PAGE));else showGroups();
+ const empty=!!list&&!list.length;
+ emptyEl.hidden=!empty;
+ if(empty)emptyTitle.textContent='Nothing matches “'+state.q+'” yet';
+ if(!filtered||empty){moreAction='all';moreLabel.textContent='Browse all '+ALL.length+' animations';moreBox.hidden=false;}
+ else if(list&&!state.all&&list.length>PAGE){moreAction='expand';moreLabel.textContent='Show all '+list.length;moreBox.hidden=false;}
+ else moreBox.hidden=true;
+ document.querySelectorAll('[data-place]').forEach(b=>b.setAttribute('aria-pressed',String(!state.q&&b.dataset.place===state.place)));
+ [q,q2].forEach(input=>{if(input!==document.activeElement&&input.value!==state.q)input.value=state.q;});
+ qClear.hidden=!q.value;
+}
+// Spaces go in as %20, not +, as the spec shows; readURL() reads both. A press adds a history step only when the address
+// changes (the same view written with + counts as the same), and a history call the browser refuses (Safari allows 100 in
+// 10 seconds, which fast typing can reach) never stops the page.
+function writeURL(push){
+ const p=[];
+ if(state.q)p.push('q='+encodeURIComponent(state.q));
+ else if(state.place)p.push('place='+PLACE[state.place].url);
+ else if(state.view)p.push('view=all');
+ if(state.all&&(state.q||state.place))p.push('all=1');
+ const search=p.length?'?'+p.join('&'):'';
+ const moved=String(new URLSearchParams(search))!==String(new URLSearchParams(location.search));
+ try{history[push&&moved?'pushState':'replaceState'](null,'',search||location.pathname);}catch(e){}
+}
+// Reads the view from the address. Returns the category of an old ?cat= link, to scroll to.
+function readURL(){
+ const p=new URLSearchParams(location.search),found=PLACE_LIST.find(x=>x.url===p.get('place')),cat=p.get('cat')||'';
+ Object.assign(state,{q:(p.get('q')||'').trim(),place:'',view:'',all:p.get('all')==='1'});
+ if(!state.q&&found)state.place=found.key;
+ else if(!state.q&&(p.get('view')==='all'||cat))state.view='all';
+ q.value=q2.value=state.q;
+ return cat;
+}
+// go() never runs while typing, so both boxes take the new view's words even when one keeps focus (a tap on iOS leaves it there).
+function go(next,push){Object.assign(state,{q:'',place:'',view:'',all:false},next);q.value=q2.value=state.q;render();writeURL(push);}
+const toResults=()=>resultsEl.scrollIntoView({behavior:reduce?'auto':'smooth',block:'start'});
+document.addEventListener('click',e=>{
+ const tile=e.target.closest('[data-place]');
+ if(tile){
+ const key=tile.dataset.place;
+ go(!state.q&&state.place===key?{}:{place:key},true);
+ if(phone.matches&&state.place)toResults();
+ if(gone(tile))toTitle();
+ return;
+ }
+ const pick=e.target.closest('[data-q]');
+ if(pick){go({q:pick.dataset.q},true);if(phone.matches)toResults();if(gone(pick))toTitle();}
+});
+function typed(input){
+ (input===q?q2:q).value=input.value;
+ Object.assign(state,{q:input.value.trim(),place:'',view:'',all:false});
+ render();writeURL(false);
+}
+q.addEventListener('input',()=>typed(q));
+q2.addEventListener('input',()=>typed(q2));
+// Moving between the bar's own buttons does not count: a tap on a chip must still find the chip.
+pinbar.addEventListener('focusout',e=>{if(!pinbar.contains(e.relatedTarget))syncBar();});
+$('search-form').addEventListener('submit',e=>{e.preventDefault();toResults();});
+qClear.addEventListener('click',()=>{q.value='';typed(q);q.focus();});
+clearBtn.addEventListener('click',()=>{go({},true);toTitle();});
+// The arrow goes away with the bar, and the page goes back to the top, where the first place tile takes keyboard focus.
+$('pin-back').addEventListener('click',()=>{go({},true);scrollTo({top:0,behavior:reduce?'auto':'smooth'});$('places').querySelector('.place').focus({preventScroll:true});});
+$('more-btn').addEventListener('click',()=>{
+ if(moreAction==='all'){go({view:'all'},true);toResults();toTitle();}
+ else{state.all=true;render();writeURL(false);(cardsBox.querySelectorAll('.card .title a')[PAGE]||titleEl).focus();} // the first new card
+});
+// A click with Ctrl, Cmd, Shift or Alt, or with another button, is left to the browser (a new tab or window).
+$('nav-all').addEventListener('click',e=>{if(e.button||e.ctrlKey||e.metaKey||e.shiftKey||e.altKey)return;e.preventDefault();go({view:'all'},true);toResults();toTitle();});
+// Back and Forward can take away the control that had keyboard focus (the ninth card after Show all): the heading gets it.
+addEventListener('popstate',()=>{readURL();render();const a=document.activeElement;if(!a||a===document.body||gone(a))toTitle();});
+addEventListener('keydown',e=>{
+ const el=document.activeElement,t=el&&el.tagName;
+ if(e.key==='/'&&t!=='INPUT'&&t!=='TEXTAREA'){e.preventDefault();q.focus();}
+ else if(e.key==='Escape'&&(el===q||el===q2)){if(el.value){el.value='';typed(el);}el.blur();} // an empty box is only left: a picked place stays
+});
+const startCat=readURL();
+render();
+if(startCat){const g=$('cat-'+startCat);if(g)g.scrollIntoView();}
(function(){const btn=$('theme-toggle');if(!btn)return;
- btn.addEventListener('click',()=>{const next=document.documentElement.dataset.theme==='light'?'dark':'light';
+ btn.addEventListener('click',()=>{const next=document.documentElement.dataset.theme==='dark'?'light':'dark';
document.documentElement.dataset.theme=next;try{localStorage.setItem('ah-theme',next);}catch(e){}});})();
-addEventListener('keydown',e=>{const t=document.activeElement&&document.activeElement.tagName;
- if(e.key==='/'&&t!=='INPUT'&&t!=='TEXTAREA'){e.preventDefault();$('q').focus();}});
// ---------- Copy prompt on the cards ----------
// The text is the page's own: the home page reads the page's HTML and builds the text with the shared page script