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.
HTML ai-promptsanimationcss-animationsfrontendgithub-pagesjavascriptmicrointeractionsmotion-designno-dependenciesreferencescroll-animationui-animationweb-animationweb-developmentwebgl
feat: GPGPU Particle System shows every particle as one pixel of a small square, one ringed and tied to its pixel, with arrows for the rule's push (See inside) and about 4,000 beside 65,000 (Compare)
matinbeb2c762f11e
diff --git a/animations/06-3d-advanced/gpgpu-particle-system/index.html b/animations/06-3d-advanced/gpgpu-particle-system/index.htmlindex b6c65a8e..8095bfeb 100644--- a/animations/06-3d-advanced/gpgpu-particle-system/index.html+++ b/animations/06-3d-advanced/gpgpu-particle-system/index.html@@ -26,6 +26,17 @@.stage{position:relative;background:#060a10}canvas{display:block;width:100%;height:100%}.no-wgl2{position:absolute;inset:0;display:none;align-items:center;justify-content:center;padding:clamp(16px,4vw,32px);background:#060a10;font-size:13px;line-height:1.7;text-align:center;color:rgba(255,255,255,.72);white-space:pre-wrap;overflow-y:auto}+ /* See inside and Compare: a canvas for the arrows, ring and line, and words; the small square (128px, 96px on phones) is drawn by the chip */+ .gp,.side{display:none}.marks{display:none;position:absolute;inset:0;pointer-events:none}+ .cap{position:absolute;padding:4px 8px;border-radius:6px;background:rgba(6,10,16,.9);color:#f4f4f2;font:600 13px/17px var(--hb-font)}+ .stage[data-hb-mode="inside"] .marks,.stage[data-hb-mode="inside"] .store,.stage[data-hb-mode="compare"] .side{display:block}.stage[data-hb-mode="inside"] .key{display:flex}+ .store{top:12px;right:148px;max-width:min(290px,calc(100% - 172px));text-align:right}+ .key{position:absolute;left:12px;bottom:12px;max-width:calc(100% - 24px);flex-direction:column;align-items:flex-start;gap:6px}+ .key .cap{position:static}.key .ring{color:#ffd84d}+ .side{top:12px;left:12px;white-space:nowrap}.side.second{left:calc(50% + 12px)}+ .stage.gl-off .marks,.stage.gl-off .gp,.stage.gl-off .side{display:none!important}+ @media (max-width:600px),(max-height:500px){.store{right:116px;max-width:calc(100% - 140px)}}+ @media (max-width:360px){.cap{font-size:12px;line-height:16px;padding:3px 7px}}</style><link rel="stylesheet" href="../../../assets/css/demo-page.css?v=10"><script src="../../../assets/js/demo-page.js?v=10" defer></script>@@ -48,14 +59,28 @@<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="seg hb-modes" role="group" aria-label="What the box shows" data-hb-modes>+ <button type="button" data-hb-mode="normal" class="on" aria-pressed="true">Normal</button>+ <button type="button" data-hb-mode="inside" aria-pressed="false">See inside</button>+ <button type="button" data-hb-mode="compare" aria-pressed="false">Compare</button>+ </div><div class="stage" id="stage"><canvas id="cvs" role="img" aria-label="Tens of thousands of glowing points flowing across a dark background"></canvas>+ <canvas class="marks" aria-hidden="true"></canvas>+ <p class="cap gp store" aria-hidden="true" data-hb-caption>Every particle is one pixel of this square: red, how far across; green, how far up</p>+ <div class="gp key" aria-hidden="true"><p class="cap ring" data-hb-caption>Ringed: one particle and its pixel</p><p class="cap rule" data-hb-caption>Arrows: which way the rule pushes, at each spot</p></div>+ <p class="cap side first" aria-hidden="true" data-hb-caption>About 4,000</p>+ <p class="cap side second" aria-hidden="true" data-hb-caption>65,000</p><div class="no-wgl2" id="no-wgl2"></div></div><div class="hb-player"><button class="hb-play" type="button" id="btn-pause" data-hb-pause data-state="playing"><svg class="hb-ic hb-i-pause" viewBox="0 0 24 24" aria-hidden="true"><rect x="6" y="4" width="4" height="16" rx="1"/><rect x="14" y="4" width="4" height="16" rx="1"/></svg><svg class="hb-ic hb-i-play" viewBox="0 0 24 24" aria-hidden="true"><path d="M6 3l14 9-14 9z"/></svg><span class="hb-pause-label">Pause</span></button><label class="hb-toggle"><input class="hb-switch" type="checkbox" role="switch" id="slow-tog" data-hb-slowmo autocomplete="off"><span>Slow motion</span></label></div>+ <div class="hb-mode-notes" aria-live="polite">+ <p data-hb-for="inside" hidden>Inside, every particle lives as one pixel of the small square, its place stored as a color. The graphics chip moves them all at once, each the way the arrows point.</p>+ <p data-hb-for="compare" hidden>About 4,000 and 65,000 follow the same rule at the same speed; only the count differs. The graphics chip moves every particle at once, so even the bigger count stays smooth.</p>+ </div></section><section class="hb-step hb-try" aria-labelledby="hb-try-title">@@ -221,6 +246,10 @@ void main(){fragColor=vCol;}`;const FADE_FS=`#version 300 esprecision mediump float;out vec4 fragColor;void main(){fragColor=vec4(.024,.039,.063,.12);}`;+// See inside's small square: the position texture in a viewport (uBox), a texel per particle, across as red, up as green; uCopy copies+const INSET_FS=`#version 300 es+precision highp float;uniform sampler2D uPos;uniform vec4 uBox;uniform float uN;uniform bool uCopy;out vec4 o;+void main(){vec4 p=texelFetch(uPos,ivec2(floor((gl_FragCoord.xy-uBox.xy)*uN/uBox.zw)),0);o=uCopy?p:vec4(p.x*.5+.5,p.y*.5+.5,.3,1.);}`;const stage=document.getElementById('stage'),cvs=document.getElementById('cvs'),noWgl2=document.getElementById('no-wgl2');const slowTog=document.getElementById('slow-tog');@@ -232,16 +261,32 @@ const PHONE=matchMedia('(max-width:600px),(max-height:500px)'),PHONE_N=+documentlet W=0,H=0,DPR=1,gl,physicsProg,renderProg,fadeProg,qBuf,fbo0,fbo1,tex0,tex1,raf=null;let paused=false,last=null,simT=0,TEX_N=PHONE.matches?PHONE_N:256,BEHAVIOR=0,SPD=0.6,COLOR_MODE=0,TRAILS=false;let idxBuf=null,particleCount=0,PT=1.5,shaderErr='';+// See inside and Compare: the mode, the square's program, the particle followed, Compare's small system, the marks, the names+let mode='normal',insetProg=null,follow=null,small=null;+const marksCvs=stage.querySelector('.marks'),mctx=marksCvs.getContext('2d'),one=new Float32Array(4);+const LABELS={normal:cvs.getAttribute('aria-label'),inside:'Glowing points flowing, with arrows for the rule that pushes them and a small square where every point is stored as one pixel'};// The pressed count follows the start (this runs before the shared script reads the settings for "Your settings")document.querySelectorAll('#count-seg button').forEach(b=>{const on=+b.dataset.n===TEX_N;b.classList.toggle('on',on);b.setAttribute('aria-pressed',String(on));});+names(); // Compare's half names and note for this start (on phones about 1,000 beside 16,000)function mk(t,s){const x=gl.createShader(t);gl.shaderSource(x,s);gl.compileShader(x);if(!gl.getShaderParameter(x,gl.COMPILE_STATUS)){shaderErr=gl.getShaderInfoLog(x)||'the shader did not compile';console.error(shaderErr);}return x;}function mkProg(vs,fs){const p=gl.createProgram();gl.attachShader(p,mk(gl.VERTEX_SHADER,vs));gl.attachShader(p,mk(gl.FRAGMENT_SHADER,fs));gl.linkProgram(p);if(!gl.getProgramParameter(p,gl.LINK_STATUS)){shaderErr=gl.getProgramInfoLog(p)||'the shaders did not link';console.error(shaderErr);}return p;}+// A float texture n pixels square (d: its first data, or null), and a framebuffer that draws into a texture+function makeTex(n,d){+ const t=gl.createTexture();gl.bindTexture(gl.TEXTURE_2D,t);+ gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA32F,n,n,0,gl.RGBA,gl.FLOAT,d);+ gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MIN_FILTER,gl.NEAREST);+ gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MAG_FILTER,gl.NEAREST);+ gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_S,gl.CLAMP_TO_EDGE);+ gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_T,gl.CLAMP_TO_EDGE);+ return t;+}+function makeFbo(t){const f=gl.createFramebuffer();gl.bindFramebuffer(gl.FRAMEBUFFER,f);gl.framebufferTexture2D(gl.FRAMEBUFFER,gl.COLOR_ATTACHMENT0,gl.TEXTURE_2D,t,0);return f;}function initTex(n){const data=new Float32Array(n*n*4);for(let i=0;i<n*n;i++){@@ -249,18 +294,8 @@ function initTex(n){data[i*4]=(Math.random()-.5)*2*r;data[i*4+1]=(Math.random()-.5)*2*r;data[i*4+2]=Math.cos(angle)*.004;data[i*4+3]=Math.sin(angle)*.004;}- function makeTex(d){- const t=gl.createTexture();gl.bindTexture(gl.TEXTURE_2D,t);- gl.texImage2D(gl.TEXTURE_2D,0,gl.RGBA32F,n,n,0,gl.RGBA,gl.FLOAT,d);- gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MIN_FILTER,gl.NEAREST);- gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_MAG_FILTER,gl.NEAREST);- gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_S,gl.CLAMP_TO_EDGE);- gl.texParameteri(gl.TEXTURE_2D,gl.TEXTURE_WRAP_T,gl.CLAMP_TO_EDGE);- return t;- }if(tex0){gl.deleteTexture(tex0);gl.deleteTexture(tex1);gl.deleteFramebuffer(fbo0);gl.deleteFramebuffer(fbo1);}- tex0=makeTex(data);tex1=makeTex(null);- function makeFbo(t){const f=gl.createFramebuffer();gl.bindFramebuffer(gl.FRAMEBUFFER,f);gl.framebufferTexture2D(gl.FRAMEBUFFER,gl.COLOR_ATTACHMENT0,gl.TEXTURE_2D,t,0);return f;}+ tex0=makeTex(n,data);tex1=makeTex(n,null);fbo0=makeFbo(tex0);fbo1=makeFbo(tex1);gl.bindFramebuffer(gl.FRAMEBUFFER,null);particleCount=n*n;@@ -270,8 +305,8 @@ function initTex(n){gl.bufferData(gl.ARRAY_BUFFER,idx,gl.STATIC_DRAW);}-// The message covers the stage. A canvas that can never draw is also taken out of the page for screen readers.-function fail(msg){gl=null;cvs.hidden=true;noWgl2.textContent=msg;noWgl2.style.display='flex';}+// The message covers the stage in every mode. A canvas that can never draw is also taken out of the page for screen readers.+function fail(msg){gl=null;cvs.hidden=true;noWgl2.textContent=msg;noWgl2.style.display='flex';stage.classList.add('gl-off');}function init(){// preserveDrawingBuffer keeps last frame for trails; alpha:false avoids premultiplied-alpha artifacts@@ -282,6 +317,7 @@ function init(){physicsProg=mkProg(PHYSICS_VS,PHYSICS_FS);renderProg=mkProg(RENDER_VS,RENDER_FS);fadeProg=mkProg(PHYSICS_VS,FADE_FS);+ insetProg=mkProg(PHYSICS_VS,INSET_FS);// A failed compile otherwise shows as a silent black canvas — say so insteadif(shaderErr){fail('Shader failed to build: '+shaderErr);return false;}// Full-screen quad shared by the physics pass and the trail fade@@ -325,19 +361,20 @@ function render(ts){if(!passes)return;last=ts;owed+=due-passes;const unit=slowTog.checked?1/3:1;- for(let i=0;i<passes;i++){simT+=unit/60;physics(simT,unit);}+ // In Compare every pass moves the small system too: the same clock, rule, Speed and step+ for(let i=0;i<passes;i++){simT+=unit/60;physics(simT,unit);if(mode==='compare'&&small)physics(simT,unit,small);}paint(simT,false,passes);}-// ── Physics pass (render to texture) ──-function physics(t,step){+// ── Physics pass (render to texture). sys is Compare's small system ({n, tex, fbo}, swapped on its own); without it, the page's ──+function physics(t,step,sys){gl.useProgram(physicsProg);- gl.bindFramebuffer(gl.FRAMEBUFFER,fbo1);- gl.viewport(0,0,TEX_N,TEX_N);+ gl.bindFramebuffer(gl.FRAMEBUFFER,sys?sys.fbo[1]:fbo1);+ gl.viewport(0,0,sys?sys.n:TEX_N,sys?sys.n:TEX_N);gl.disable(gl.BLEND);gl.bindBuffer(gl.ARRAY_BUFFER,qBuf);const pa=gl.getAttribLocation(physicsProg,'a');gl.enableVertexAttribArray(pa);gl.vertexAttribPointer(pa,2,gl.FLOAT,false,0,0);- gl.activeTexture(gl.TEXTURE0);gl.bindTexture(gl.TEXTURE_2D,tex0);+ gl.activeTexture(gl.TEXTURE0);gl.bindTexture(gl.TEXTURE_2D,sys?sys.tex[0]:tex0);gl.uniform1i(gl.getUniformLocation(physicsProg,'uPos'),0);gl.uniform1f(gl.getUniformLocation(physicsProg,'uT'),t);gl.uniform1f(gl.getUniformLocation(physicsProg,'uSpd'),SPD);@@ -346,15 +383,16 @@ function physics(t,step){gl.drawArrays(gl.TRIANGLE_STRIP,0,4);// Swap textures- [tex0,tex1]=[tex1,tex0];[fbo0,fbo1]=[fbo1,fbo0];+ if(sys){sys.tex.reverse();sys.fbo.reverse();}else{[tex0,tex1]=[tex1,tex0];[fbo0,fbo1]=[fbo1,fbo0];}}// 120 silent steps (two seconds of flow at the full step), so a picture shown still already shows the flow, not the starting cloudfunction settle(){for(let i=0;i<120;i++){simT+=1/60;physics(simT,1);}}// ── Render pass (draw points). A picture drawn on its own starts from a cleared canvas ("fresh"): with Trails on, a// normal frame only lays a thin fade over the last one, which would leave the old picture showing under a new setting.-// fades is how many times that fade is laid over it: once for every physics pass of the frame. ──+// fades is how many times that fade is laid over it: once for every physics pass of the frame. Compare draws its halves. ──function paint(t,fresh,fades=1){+ if(mode==='compare'&&(!small||small.n!==(PHONE.matches?32:64)))makeSmall();gl.bindFramebuffer(gl.FRAMEBUFFER,null);gl.viewport(0,0,W,H);if(TRAILS&&!fresh){// fade the previous frame instead of clearing it@@ -363,20 +401,123 @@ function paint(t,fresh,fades=1){const fa=gl.getAttribLocation(fadeProg,'a');gl.enableVertexAttribArray(fa);gl.vertexAttribPointer(fa,2,gl.FLOAT,false,0,0);for(let i=0;i<fades;i++)gl.drawArrays(gl.TRIANGLE_STRIP,0,4);}else{gl.clearColor(0.024,0.039,0.063,1);gl.clear(gl.COLOR_BUFFER_BIT);}+ if(mode==='compare')halves(t);else dots(tex0,particleCount,TEX_N,t);+ if(mode==='inside')inside(t);+}+// n points, their places read from a position texture size pixels square+function dots(tex,n,size,t){gl.enable(gl.BLEND);gl.blendFunc(gl.SRC_ALPHA,gl.ONE);gl.useProgram(renderProg);gl.bindBuffer(gl.ARRAY_BUFFER,idxBuf);const ra=gl.getAttribLocation(renderProg,'aIdx');gl.enableVertexAttribArray(ra);gl.vertexAttribPointer(ra,1,gl.FLOAT,false,0,0);- gl.activeTexture(gl.TEXTURE0);gl.bindTexture(gl.TEXTURE_2D,tex0);+ gl.activeTexture(gl.TEXTURE0);gl.bindTexture(gl.TEXTURE_2D,tex);gl.uniform1i(gl.getUniformLocation(renderProg,'uPos'),0);- gl.uniform2f(gl.getUniformLocation(renderProg,'uTexSz'),TEX_N,TEX_N);+ gl.uniform2f(gl.getUniformLocation(renderProg,'uTexSz'),size,size);gl.uniform1i(gl.getUniformLocation(renderProg,'uColorMode'),COLOR_MODE);gl.uniform1f(gl.getUniformLocation(renderProg,'uT'),t);gl.uniform1f(gl.getUniformLocation(renderProg,'uPS'),PT);- gl.drawArrays(gl.POINTS,0,particleCount);+ gl.drawArrays(gl.POINTS,0,n);gl.disable(gl.BLEND);}+// ── See inside and Compare ──+// The page's position texture drawn into a square of the target (x, y from the bottom left, as the graphics chip counts): as colors+// for See inside's small square, or copied as it is for Compare's small system+function square(x,y,s,copy){+ gl.disable(gl.BLEND);gl.useProgram(insetProg);gl.bindBuffer(gl.ARRAY_BUFFER,qBuf);+ const a=gl.getAttribLocation(insetProg,'a');gl.enableVertexAttribArray(a);gl.vertexAttribPointer(a,2,gl.FLOAT,false,0,0);+ gl.activeTexture(gl.TEXTURE0);gl.bindTexture(gl.TEXTURE_2D,tex0);+ gl.uniform1i(gl.getUniformLocation(insetProg,'uPos'),0);gl.uniform1i(gl.getUniformLocation(insetProg,'uCopy'),copy?1:0);+ gl.uniform4f(gl.getUniformLocation(insetProg,'uBox'),x,y,s,s);gl.uniform1f(gl.getUniformLocation(insetProg,'uN'),TEX_N);+ gl.viewport(x,y,s,s);gl.drawArrays(gl.TRIANGLE_STRIP,0,4);gl.viewport(0,0,W,H);+}+// Compare's first half: a second, small particle system in the same context, 64 pixels square (32 on phones), copied on the way in+// (and after a new count) from an even share of the page's own particles, so the halves show one flow, sparse and dense+function makeSmall(){+ const n=PHONE.matches?32:64;+ if(small&&small.n!==n){small.tex.forEach(x=>gl.deleteTexture(x));small.fbo.forEach(x=>gl.deleteFramebuffer(x));small=null;}+ if(!small){const tex=[makeTex(n,null),makeTex(n,null)];small={n,tex,fbo:tex.map(makeFbo)};}+ gl.bindFramebuffer(gl.FRAMEBUFFER,small.fbo[0]);square(0,0,n,true);gl.bindFramebuffer(gl.FRAMEBUFFER,null);names();+}+// Compare: one canvas, two viewports, each scissored so no point spills over, and a thin divider between them+function halves(t){+ const half=Math.floor(W/2),gap=Math.max(1,Math.round(DPR));+ gl.enable(gl.SCISSOR_TEST);gl.viewport(0,0,half-gap,H);gl.scissor(0,0,half-gap,H);dots(small.tex[0],small.n*small.n,small.n,t);+ gl.viewport(half+gap,0,W-half-gap,H);gl.scissor(half+gap,0,W-half-gap,H);dots(tex0,particleCount,TEX_N,t);+ gl.scissor(half-gap,0,2*gap,H);gl.clearColor(.17,.19,.24,1);gl.clear(gl.COLOR_BUFFER_BIT);gl.disable(gl.SCISSOR_TEST);gl.viewport(0,0,W,H);+}+// The small square's place in canvas pixels (from the bottom left), where the style's labels expect it+function box(){const s=Math.round((PHONE.matches?96:128)*DPR),m=Math.round(12*DPR);return{x:W-m-s,y:H-m-s,s};}+// See inside: the small square over the points (opaque, after the trail fade, so Trails cannot smear it), then the marks. The+// particle followed is read back after the frame's last pass, and chosen again when it hides (half a second apart at most).+function inside(t){+ const b=box();square(b.x,b.y,b.s,false);+ if(follow&&follow.s===b.s&&follow.n===TEX_N&&follow.w===W&&follow.h===H){+ gl.bindFramebuffer(gl.FRAMEBUFFER,fbo0);gl.readPixels(follow.tx,follow.ty,1,1,gl.RGBA,gl.FLOAT,one);gl.bindFramebuffer(gl.FRAMEBUFFER,null);+ follow.x=(one[0]+1)/2*W;follow.y=(1-one[1])/2*H;+ if(!follow.open(follow.x,follow.y)&&simT-follow.at>.5)follow=null;+ }else follow=null;+ marks(t,b,follow||(follow=choose(b)));+}+// The rule's push for one step at a place (x, y from -1 to 1, up is +), from the physics shader's own formulas+function push(x,y,t){+ const spd=SPD*.0002;+ if(BEHAVIOR===0){const a=Math.sin(x*2+t*.3)*Math.cos(y*2+t*.2)+Math.atan2(y,x)*.5;return[Math.cos(a)*spd,Math.sin(a)*spd];}+ if(BEHAVIOR===1){const l=Math.hypot(x,y)+.01;return[-y/l*spd*2-x*.0005,x/l*spd*2-y*.0005];}+ if(BEHAVIOR===2){const d=Math.hypot(x,y)||1e-6,f=spd*3/(d+.1)/d;return[-x*f,-y*f];}+ const a=Math.atan2(y,x)+t*.2;return[Math.cos(a)*spd-.003*x,Math.sin(a)*spd-.003*y];+}+// The particle followed: of the pixels near the middle of the small square, the one whose line to its pixel crosses no label and+// whose particle stays longest in the open part of the box (worked out ahead with the rule, 5 s at most), nearest the middle on+// a tie: so the ring is seen, in a still too+function choose(b){+ const N=TEX_N,c=Math.floor(b.s/2),R=6,at=i=>Math.floor((i+.5)*N/b.s),t0=at(c-R),w=at(c+R)-t0+1,k=DPR,top=H-b.y-b.s;+ const block=new Float32Array(w*w*4),st=cvs.getBoundingClientRect(),offs=[];let best=null,most=-1e9;+ gl.bindFramebuffer(gl.FRAMEBUFFER,fbo0);gl.readPixels(t0,t0,w,w,gl.RGBA,gl.FLOAT,block);gl.bindFramebuffer(gl.FRAMEBUFFER,null);+ const labels=[...stage.querySelectorAll('.store,.key')].map(el=>{const r=el.getBoundingClientRect();return[r.left-st.left,r.top-st.top,r.right-st.left,r.bottom-st.top].map(v=>v*k);});+ const on=(rs,x,y,m)=>rs.some(r=>x>r[0]-m&&x<r[2]+m&&y>r[1]-m&&y<r[3]+m),all=labels.concat([[b.x,top,b.x+b.s,top+b.s]]);+ const open=(x,y)=>x>20*k&&x<W-20*k&&y>20*k&&y<H-20*k&&!on(all,x,y,14*k);+ for(let j=-R;j<=R;j++)for(let i=-R;i<=R;i++)offs.push([i,j]);offs.sort((p,q)=>p[0]*p[0]+p[1]*p[1]-q[0]*q[0]-q[1]*q[1]);+ for(const[di,dj]of offs){+ const i=c+di,j=c+dj,tx=at(i),ty=at(j),p=((ty-t0)*w+tx-t0)*4,ix=b.x+i+.5,iy=top+b.s-j-.5;+ let x=block[p],y=block[p+1],vx=block[p+2],vy=block[p+3],tt=simT,n=0;const sx=(x+1)/2*W,sy=(1-y)/2*H;+ for(let q=1;q<32;q++)if(on(labels,ix+(sx-ix)*q/32,iy+(sy-iy)*q/32,0))n=-200;+ for(let s=0;s<300&&open((x+1)/2*W,(1-y)/2*H);s++,n++){const f=push(x,y,tt+=1/60);vx=(vx+f[0])*.985;vy=(vy+f[1])*.985;x+=vx;y+=vy;}+ if(n>most){most=n;best={s:b.s,n:N,w:W,h:H,i,j,tx,ty,x:sx,y:sy,open,labels,at:simT};}+ }+ return best;+}+// See inside's marks on the 2D canvas over the picture, cleared every drawn frame so Trails never smear them: an arrow for the+// rule's push at every spot of a 48px grid but under a label or the ring (longer where it pushes harder), the square's frame, and+// the particle followed (f): a ring around its point, an outline around its pixel in the square and a line joining them+function marks(t,b,f){+ if(marksCvs.width!==W||marksCvs.height!==H){marksCvs.width=W;marksCvs.height=H;}+ const c=mctx,k=DPR,G=48*k,top=H-b.y-b.s,ix=b.x+f.i+.5,iy=top+b.s-f.j-.5;+ c.clearRect(0,0,W,H);c.lineCap=c.lineJoin='round';c.beginPath();+ for(let gy=G/2;gy<H;gy+=G)for(let gx=G/2;gx<W;gx+=G){+ if(gx>b.x-G/2&&gy<top+b.s+G/2||Math.hypot(gx-f.x,gy-f.y)<16*k||f.labels.some(r=>gx>r[0]&&gx<r[2]&&gy>r[1]&&gy<r[3]))continue;+ const p=push(gx/W*2-1,1-gy/H*2,t),m=Math.hypot(p[0]*W,p[1]*H)||1,ux=p[0]*W/m,uy=-p[1]*H/m;+ const len=Math.min(38,Math.max(8,17*Math.pow(Math.hypot(p[0],p[1])/1.2e-4,.7)))*k,hx=gx+ux*len/2,hy=gy+uy*len/2;+ c.moveTo(gx-ux*len/2,gy-uy*len/2);c.lineTo(hx,hy);+ c.moveTo(hx-(ux*.85-uy*.53)*6*k,hy-(uy*.85+ux*.53)*6*k);c.lineTo(hx,hy);c.lineTo(hx-(ux*.85+uy*.53)*6*k,hy-(uy*.85-ux*.53)*6*k);+ }+ const both=(dark,light,wide,thin)=>{c.strokeStyle=dark;c.lineWidth=wide*k;c.stroke();c.strokeStyle=light;c.lineWidth=thin*k;c.stroke();};+ both('rgba(6,10,16,.8)','rgba(244,244,242,.95)',4,1.6);+ c.beginPath();c.rect(b.x-k,top-k,b.s+2*k,b.s+2*k);both('rgba(6,10,16,.8)','rgba(244,244,242,.9)',3.5,1.5);+ c.fillStyle=`rgb(${f.x/W*255|0},${(1-f.y/H)*255|0},77)`;c.fillRect(ix-6*k,iy-6*k,12*k,12*k); // its pixel's color, made bigger+ const lx=f.x-ix,ly=f.y-iy,ll=Math.hypot(lx,ly)||1,ux=lx/ll,uy=ly/ll,d=6*k/Math.max(Math.abs(ux),Math.abs(uy)); // the line runs edge to edge+ c.beginPath();if(ll>d+10*k){c.moveTo(ix+ux*d,iy+uy*d);c.lineTo(f.x-ux*10*k,f.y-uy*10*k);}c.moveTo(f.x+10*k,f.y);c.arc(f.x,f.y,10*k,0,Math.PI*2);c.rect(ix-6*k,iy-6*k,12*k,12*k);+ both('rgba(6,10,16,.85)','#ffd84d',4.5,2);+}+// Compare's half names, its note and the canvas's name, from the counts as they are (the second from Number of particles)+function names(){+ const first='About '+((small?small.n:PHONE.matches?32:64)===64?'4,000':'1,000'),second=document.querySelector('#count-seg .on').textContent;+ stage.querySelector('.side.first').textContent=first;stage.querySelector('.side.second').textContent=second;+ document.querySelector('[data-hb-for="compare"]').textContent=`${first} and ${second} follow the same rule at the same speed; only the count differs. The graphics chip moves every particle at once, so even the bigger count stays smooth.`;+ LABELS.compare=`The same flow twice: ${first.toLowerCase()} points beside ${second}`;+ if(mode==='compare')cvs.setAttribute('aria-label',LABELS.compare);+}+// Play carries on from the clock and the particles where they stopped; it never stacks a second loop.function start(){if(raf!==null||!gl)return;last=null;owed=0;raf=requestAnimationFrame(render);}document.addEventListener('visibilitychange',()=>{last=null;owed=0;});@@ -390,6 +531,16 @@ document.addEventListener('hb:pause',e=>{if(paused){if(raf!==null){cancelAnimationFrame(raf);raf=null;}}else start();});+// The mode switch draws the mode at once from a fresh canvas, so with Trails on nothing of another mode lingers; the page's particles+// and clock go on as they are in every mode. Compare copies its small system on the way in. The canvas's name says what it shows.+document.addEventListener('hb:mode',e=>{+ const was=mode;mode=e.detail.mode;+ if(mode!==was)follow=null;+ cvs.setAttribute('aria-label',LABELS[mode]);+ if(gl){if(mode==='compare'&&was!=='compare')makeSmall();paint(simT,true);}+});+// A lost context takes the small system with it (paint makes it again); the message covers every mode meanwhile+cvs.addEventListener('webglcontextlost',()=>{stage.classList.add('gl-off');small=follow=null;});cvs.addEventListener('webglcontextrestored',()=>stage.classList.remove('gl-off'));if(init()){resize();@@ -428,6 +579,9 @@ choices('count-seg',d=>{choices('speed-seg',d=>{SPD=+d.spd;});choices('color-seg',d=>{if(+d.mode===COLOR_MODE)return;COLOR_MODE=+d.mode;repaint();});document.getElementById('trail-tog').addEventListener('change',e=>{TRAILS=e.target.checked;repaint();});+// See inside's arrows follow Movement and Speed at once, paused too; a new count renames Compare's second half and copies the first+['move-seg','speed-seg'].forEach(id=>document.getElementById(id).addEventListener('click',()=>{if(mode==='inside'&&paused&&gl)paint(simT,true);}));+document.getElementById('count-seg').addEventListener('click',()=>{names();if(mode==='compare'&&gl){makeSmall();repaint();}});</script></body></html>