/* Each mounted card owns one paused finite timeline and abortable listeners. */ (() => { const ns='http://www.w3.org/2000/svg'; function node(tag,attrs={},text='') { const e=document.createElementNS(ns,tag); for(const [k,v] of Object.entries(attrs))e.setAttribute(k,v); e.textContent=text; return e; } function mount(host,c) { const abort=new AbortController(), signal=abort.signal, media=matchMedia('(prefers-reduced-motion: reduce)'); let destroyed=false; host.innerHTML=`

Complete`; host.querySelector('h2').textContent=c.title; host.querySelector('p').textContent=c.description; const svg=host.querySelector('svg'); svg.setAttribute('aria-label',c.description); const add=(tag,attrs,text)=>{const e=node(tag,attrs,text);svg.append(e);return e;}; const label=(x,y,text)=>add('text',{x,y,'text-anchor':'middle'},text); const tl=anime.createTimeline({autoplay:false,defaults:{ease:'linear'}}); if(c.id==='svg-draw'||c.id==='motion-path') { add('path',{d:c.path,class:'guide'}); const path=add('path',{d:c.path,class:'trace'}); label(65,190,c.start); label(325,190,c.end); if(c.id==='svg-draw')tl.add(anime.svg.createDrawable(path),{draw:['0 0','0 1'],duration:c.duration},0); else {const marker=add('circle',{r:9,cx:0,cy:0,class:'packet'});tl.add(marker,{...anime.svg.createMotionPath(path),duration:c.duration},0);} } else if(c.id==='spatial-stagger') { const cells=[]; for(let row=0;row{add('circle',{cx:x,cy:100,r:25,class:'cell'});label(x,155,c.labels[i]);}); tl.label('input',0).add(anime.svg.createDrawable(a),{draw:['0 0','0 1'],duration:c.duration},'input').label('output',c.duration).add(anime.svg.createDrawable(b),{draw:['0 0','0 1'],duration:c.duration},'output'); } else if(c.id==='assemble-explode') { c.labels.forEach((text,i)=>{ const part=add('g',{'data-part':i}); part.append(node('rect',{x:155+i*30,y:55+i*22,width:60,height:80,rx:6,class:'cell'})); // Static labels stay outside the motion wrappers, including while assembled. label(80+i*120,195,text); const x=(i-1)*c.distance; tl.add(part,{translateX:[0,x],duration:c.duration},0) .add(part,{translateX:[x,x],duration:400},c.duration) .add(part,{translateX:[x,0],duration:c.duration},c.duration+400); }); } const range=host.querySelector('input'),out=host.querySelector('output'); const sync=()=>{range.value=100*tl.currentTime/tl.duration;}; tl.onUpdate=sync; tl.onComplete=()=>{sync();out.textContent='Complete';}; function settle(){if(destroyed)return;tl.pause();tl.seek(tl.duration);sync();out.textContent='Complete';} function seek(ms){if(destroyed)return;if(media.matches)return settle();tl.pause();const n=Number(ms);tl.seek(Math.max(0,Math.min(tl.duration,Number.isFinite(n)?n:0)));sync();out.textContent='Paused';} function play(restart=false){if(destroyed)return;if(media.matches)return settle();if(restart||tl.currentTime>=tl.duration)tl.seek(0);tl.play();out.textContent='Playing';} host.querySelector('[data-action="play"]').addEventListener('click',()=>play(),{signal}); host.querySelector('[data-action="replay"]').addEventListener('click',()=>play(true),{signal}); host.querySelector('[data-action="settle"]').addEventListener('click',settle,{signal}); range.addEventListener('input',()=>seek(Number(range.value)*tl.duration/100),{signal}); media.addEventListener('change',settle,{signal}); window.addEventListener('beforeprint',settle,{signal}); document.addEventListener('visibilitychange',()=>{if(document.hidden)settle();},{signal}); window.addEventListener('pagehide',settle,{signal}); settle(); return {seek,settle,play:()=>play(),replay:()=>play(true),state:()=>({duration:tl.duration,time:tl.currentTime,paused:tl.paused,destroyed}),destroy(){if(destroyed)return;settle();abort.abort();tl.revert();destroyed=true;host.replaceChildren();}}; } const main=document.querySelector('main'), selected=document.body.dataset.example; if(selected !== undefined) { const config=window.animeExamples.find(c=>c.id===selected); if(!config)throw new Error('Unknown example ID: '+selected); if(window.self!==window.top)document.body.classList.add('embedded'); const host=document.createElement('article');host.id=config.id;main.append(host); window.animeGallery={controllers:{[config.id]:mount(host,config)},mount}; } else { const group=document.body.dataset.gallery; if(!['all','elements','compositions'].includes(group))throw new Error('Unknown gallery: '+group); for(const name of ['elements','compositions']) { if(group!=='all'&&group!==name)continue; const section=document.createElement('section'), heading=document.createElement('h2'), link=document.createElement('a'); section.dataset.registry=name;link.href=group==='all'?name+'/index.html':'index.html';link.textContent=name==='elements'?'Elements':'Compositions';heading.append(link);section.append(heading); const grid=document.createElement('div');grid.className='preview-grid';section.append(grid); for(const config of window.animeExamples.filter(c=>c.kind===(name==='elements'?'element':'composition'))) { const card=document.createElement('article'),frame=document.createElement('iframe'),open=document.createElement('a'); const href=(group==='all'?'':'../')+'examples/'+config.id+'.html'; frame.src=href;frame.title=config.title;frame.dataset.example=config.id; open.href=href;open.textContent='Open example: '+config.title;open.className='open-example'; card.append(frame,open);grid.append(card); } main.append(section); } } })();