From eeeccc60d42bcdaed7c7895aef0c91d206563f77 Mon Sep 17 00:00:00 2001 From: Antoine <20756073+antt0n@users.noreply.github.com> Date: Wed, 4 Nov 2026 16:39:57 +0100 Subject: [PATCH] WebGL map: keep the cities' pictures while the zoom moves, paint them sharp once it rests The scale of a city's picture is in steps of 1/24, so a pinch repainted every city every ~4 % of zoom: hundreds of pictures a frame, and the atlas full and reset over and over. On the card, while the zoom moves, pictures that grow with it (cities, ships, badges) keep the step of the scale held when it began, stretched by up to 1.25x either way by the card's filtering, and take a new one beyond that; 250 ms after the zoom stops they are painted at their own step again, and a still map is drawn once more for it. The 2D map paints every step as before. --- CHANGELOG.md | 1 + src/game/96-map/59-gl-sprites.js | 18 +++++- src/game/96-map/60-draw.js | 2 +- src/game/96-map/80-cities.js | 11 ++-- src/game/96-map/88-cable-ships.js | 2 +- src/game/99-loop.js | 2 +- tests/webgl-zoom-sprites.test.js | 91 +++++++++++++++++++++++++++++++ 7 files changed, 115 insertions(+), 12 deletions(-) create mode 100644 tests/webgl-zoom-sprites.test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 2640db8..946f84e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ All notable changes to this project are documented here. The project follows - The map is now drawn by the graphics card wherever the browser can, so panning, zooming and fast play stay smooth on big networks and big screens. The map looks the same; Settings › Map drawing › Always 2D keeps the old drawing, and browsers without WebGL2 or without a usable graphics card keep it on their own, as does a game whose graphics card fails while you play. - Cities, labels, building cones, ships, offer bubbles and money pop-ups are painted once and then reused, so the usual map also draws far fewer shapes each frame. - Long zooms stay on the graphics card: the pictures of cities and labels are painted on one shared canvas instead of each keeping its own, so the browser no longer runs out of room for them and switches to the slower drawing. +- Zooming is smoother on big maps: while you zoom, cities and ships keep their picture and are only redrawn sharp once the zoom stops, instead of being repainted at every small step. ## [2.9.1] — 2026-11-04 diff --git a/src/game/96-map/59-gl-sprites.js b/src/game/96-map/59-gl-sprites.js index 234cf94..b2aef43 100644 --- a/src/game/96-map/59-gl-sprites.js +++ b/src/game/96-map/59-gl-sprites.js @@ -70,10 +70,22 @@ function blit(e,x,y,w,h){if(GLS.frame&&glsEnsure(e)){const o=glsNext(),d=GLS.dat function dot(x,y,r,col,a){if(GLS.frame){const c=glColor(col),o=glsNext(),d=GLS.data; d[o]=x;d[o+1]=y;d[o+2]=r;d[o+3]=d[o+4]=d[o+5]=d[o+6]=d[o+7]=0;d[o+8]=c[0];d[o+9]=c[1];d[o+10]=c[2];d[o+11]=(a===undefined?ctx.globalAlpha:a)*c[3];d[o+12]=1;} else{const sa=ctx.globalAlpha;if(a!==undefined)ctx.globalAlpha=a;ctx.fillStyle=col;ctx.beginPath();ctx.arc(x,y,r,0,7);ctx.fill();ctx.globalAlpha=sa;}} +/* Pictures that grow with the zoom (cities, ships, badges) are painted at steps of 1/24 of their scale. On the card, + while the zoom moves, they keep the step of the scale they had when it began, stretched by up to 1.25× either way + (the card filters them smoothly), and are painted again at the right step once the zoom rests (ZOOM_REST ms): a + pinch otherwise repainted every city at every step, hundreds of pictures a frame, and filled the atlas over and + over. ZH.sc is the icon scale held, ZH.f how much a picture is painted smaller (<1) or bigger than it shows. */ +const CSPR_STEP=24,ZOOM_REST=250,ZH={z:0,t:-1e9,sc:0,f:1}; +function zoomHold(sc,G){const t=performance.now();if(cam.z!==ZH.z){ZH.z=cam.z;ZH.t=t;} + if(!G||!ZH.sc||t-ZH.t>=ZOOM_REST||sc/ZH.sc>1.25||ZH.sc/sc>1.25)ZH.sc=sc;ZH.f=ZH.sc/sc;} +// The zoom moved a moment ago: the pictures may be stretched, so a still map is drawn again once it rests (mapStill()). +const zoomMoving=()=>performance.now()-ZH.tMath.max(1/CSPR_STEP,Math.round(k*ZH.f*CSPR_STEP)/CSPR_STEP); // A small picture w×h px at scale k with its anchor at (ax,ay), painted once by paint(g) with the origin on the anchor -// (k in steps of 1/24, as the cities), then drawn with the anchor at (x,y). Things that bob or pulse are drawn as these -// at a moving place, so the overlay above the card stays untouched (repainting it costs a whole screen of pixels). -function stamp(key,w,h,ax,ay,k,x,y,paint){const kq=Math.max(1/24,Math.round(k*24)/24),r=k/kq; +// (k in steps of 1/24, as the cities; zoomed: k grows with the zoom, and is held while it moves, sprQ()), then drawn with +// the anchor at (x,y). Things that bob or pulse are drawn as these at a moving place, so the overlay above the card +// stays untouched (repainting it costs a whole screen of pixels). +function stamp(key,w,h,ax,ay,k,x,y,paint,zoomed){const kq=zoomed?sprQ(k):Math.max(1/CSPR_STEP,Math.round(k*CSPR_STEP)/CSPR_STEP),r=k/kq; const e=spriteEntry(key+'|'+kq,Math.ceil(w*kq),Math.ceil(h*kq),ax*kq,ay*kq,g=>{g.translate(ax*kq,ay*kq);g.scale(kq,kq);paint(g);}); blit(e,x-e.ax*r,y-e.ay*r,e.w*r,e.h*r);} // Text with an outline, centred on (x,y), as a picture placed on whole device pixels; tw is its width, px its size. diff --git a/src/game/96-map/60-draw.js b/src/game/96-map/60-draw.js index f42754e..9448ad9 100644 --- a/src/game/96-map/60-draw.js +++ b/src/game/96-map/60-draw.js @@ -36,7 +36,7 @@ function drawFrame(now){ } ctx.setTransform(DPR,0,0,DPR,0,0); if(!S)return; - const sc=iconScale(),day=S.day; + const sc=iconScale(),day=S.day;zoomHold(sc,G); // cable systems drawCables(ks); // links diff --git a/src/game/96-map/80-cities.js b/src/game/96-map/80-cities.js index 9b40c4b..ebce4ff 100644 --- a/src/game/96-map/80-cities.js +++ b/src/game/96-map/80-cities.js @@ -32,8 +32,7 @@ function cityFarStatic(C,p,down,sel,hov,ph){ctx.strokeStyle='#1E2640'; if(CM.inc.has(C.id)){ctx.fillStyle='#FF4D5E';ctx.lineWidth=1.2;ctx.beginPath();ctx.arc(p?-9:-5,p?-7:-5,3.4,0,7);ctx.fill();ctx.stroke();}} // A city's picture is painted once per state and kept as a sprite (citySprite). What moves every frame is painted on top // (cityMoving): the lights, the steam, a flashing IX, the hot badges and the offer bubble. The rings and the glow move too, -// but in steps (cityPhase), so their sprite changes only at a step. -const CSPR_STEP=24; +// but in steps (cityPhase), so their sprite changes only at a step; so does its scale (sprQ(), 59-gl-sprites.js). // dd is -1 without an attack: the attack's ring at step 0 must not share a sprite with no ring at all. function cityPhase(C,p,sel,now){const ph={sel:0,dd:-1,gl:-1}; if(sel)ph.sel=Math.floor(now/40)%12; // dash period 12 px, lineDashOffset -now/40 @@ -94,13 +93,13 @@ function cityMoving(C,p,t,cs,k,sx,sy,now,h,st,hot){const down=st.down,K=k*cs; for(let i=0;i<4;i++){const lit=down?false:(((now/(160+i*37))|0)+i)%3!==0,[x,y]=at(-10+i*6.6,1),col=down?'#3B4260':lit?(sf<0.9?'#FF4D5E':'#7CFFB2'):'#2A3150';dot(x,y,2.1*K,col,1);} } // A flashing IX: two pictures, one per colour. - if(st.ixDraw&&st.ixHot){const on=Math.sin(now/180)>0,ix=p?19:SKY_W[t]/2+4;stamp('ixhot'+(on?1:0),22,22,11,11,K,sx+ix*K,sy+(2-h)*K,()=>{ctx.translate(-ix,h-2);ctx.strokeStyle='#1E2640';cityIX(C,p,t,h,true,on?1:-1);});} + if(st.ixDraw&&st.ixHot){const on=Math.sin(now/180)>0,ix=p?19:SKY_W[t]/2+4;stamp('ixhot'+(on?1:0),22,22,11,11,K,sx+ix*K,sy+(2-h)*K,()=>{ctx.translate(-ix,h-2);ctx.strokeStyle='#1E2640';cityIX(C,p,t,h,true,on?1:-1);},true);} // Hot badges: the red disc pulses, the ring and the letter over it stay. if(hot&&p){let y=-14;const pulse=.75+.25*Math.sin(now/200);for(const kk of ['r','t','p','c']){if(!hot[kk])continue;const bx=sx-29*K,by=sy+y*K;dot(bx,by,6.5*K,'#FF4D5E',pulse); - stamp('hotb|'+HOTL[kk],18,18,9,9,K,bx,by,g=>{g.lineWidth=2;g.strokeStyle='#1E2640';g.beginPath();g.arc(0,0,6.5,0,7);g.stroke();g.fillStyle='#fff';g.font='900 8px Nunito, sans-serif';g.textAlign='center';g.textBaseline='middle';g.fillText(HOTL[kk],0,0.5);});y+=14;}} + stamp('hotb|'+HOTL[kk],18,18,9,9,K,bx,by,g=>{g.lineWidth=2;g.strokeStyle='#1E2640';g.beginPath();g.arc(0,0,6.5,0,7);g.stroke();g.fillStyle='#fff';g.font='900 8px Nunito, sans-serif';g.textAlign='center';g.textBaseline='middle';g.fillText(HOTL[kk],0,0.5);},true);y+=14;}} // The offer bubble bobs over the city. if(st.off)stamp('offer|'+(st.big?2:1),30,28,15,12,K,sx-2*K,sy+(-h-16+Math.sin(now/250)*3)*K,g=>{g.fillStyle='#fff';g.strokeStyle='#1E2640';g.lineWidth=2.2;g.beginPath();g.ellipse(0,0,11,9,0,0,7);g.fill();g.stroke(); - g.beginPath();g.moveTo(-3,8);g.lineTo(-5,14);g.lineTo(2,8);g.fill();g.fillStyle=st.big?'#FF4F9A':'#16A866';g.font='900 13px "Baloo 2", sans-serif';g.textAlign='center';g.textBaseline='middle';g.fillText(st.big?'$$':'$',0,1);});} + g.beginPath();g.moveTo(-3,8);g.lineTo(-5,14);g.lineTo(2,8);g.fill();g.fillStyle=st.big?'#FF4F9A':'#16A866';g.font='900 13px "Baloo 2", sans-serif';g.textAlign='center';g.textBaseline='middle';g.fillText(st.big?'$$':'$',0,1);},true);} function drawCity(C,sx,sy,sc,now,lyOverride,dem){ if(CM.far)return drawCityFar(C,sx,sy,sc,now,lyOverride); const hot=(UI.hotMap||{})[C.id]; @@ -109,7 +108,7 @@ function drawCity(C,sx,sy,sc,now,lyOverride,dem){ if(dem&&!p&&!sel&&!hov){const r=(2.6+cTier(C.id)*0.35)*sc,rq=Math.round(r*4)/4,fill=hasIxAt(C.id)?'#FFD23F':'#FFFFFF',S2=Math.ceil(rq*2+4); const e=spriteEntry(['quiet',rq,fill].join('|'),S2,S2,S2/2,S2/2,g=>{g.translate(S2/2,S2/2);g.fillStyle=fill;g.strokeStyle='#1E2640';g.lineWidth=1.6;g.beginPath();g.arc(0,0,rq,0,7);g.fill();g.stroke();}); blit(e,sx-e.ax,sy+2*sc-e.ay,e.w,e.h);return;} - const t=cTier(C.id),cs=p?1:CSC[t],k=sc*(hov?1.15:1),kq=Math.round(k*CSPR_STEP)/CSPR_STEP; + const t=cTier(C.id),cs=p?1:CSC[t],k=sc*(hov?1.15:1),kq=sprQ(k); const ixn=ixName(C.id),ixDraw=!!ixn&&(ixShown(C.id,p)||(hot&&hot.ix)||sel||hov),ixHot=!!(ixDraw&&hot&&hot.ix); const st={down:!!down,feed:!!(UI.feed&&UI.feed.has(C.id)),sel:!!sel,inc:CM.inc.has(C.id),ixDraw,ixHot,off:CM.off.has(C.id),big:CM.big.has(C.id)}; const ph=cityPhase(C,p,sel,now),spr=citySprite(C,p,t,cs,kq,ph,st),r=k/kq; diff --git a/src/game/96-map/88-cable-ships.js b/src/game/96-map/88-cable-ships.js index f3a4744..881ac69 100644 --- a/src/game/96-map/88-cable-ships.js +++ b/src/game/96-map/88-cable-ships.js @@ -30,7 +30,7 @@ function shipSprite(x,y,face,sc,col,now,mode){const bl=mode==='work'&&Math.floor c.fillStyle=col;c.strokeStyle='#1E2640';c.lineWidth=1.6;c.lineJoin='round';c.beginPath();c.moveTo(-11,0);c.lineTo(11,0);c.lineTo(8,6);c.lineTo(-9,6);c.closePath();c.fill();c.stroke(); c.fillStyle='#fff';c.beginPath();c.rect(-7,-7,8,7);c.fill();c.stroke();c.fillStyle='#1E2640';c.fillRect(-5,-5,4,2); c.beginPath();c.arc(9,-2,2.6,0,7);c.fillStyle='#FFD23F';c.fill();c.stroke(); - if(bl){c.fillStyle='#FFD23F';c.font='900 11px sans-serif';c.textAlign='center';c.fillText('✦',12,-8);}});} + if(bl){c.fillStyle='#FFD23F';c.font='900 11px sans-serif';c.textAlign='center';c.fillText('✦',12,-8);}},true);} function drawShips(ks,now){SHIP_HITS.length=0;if(!S||pzOn()||!S.fleet)return; const T=S.day+(UI.speed?clamp(acc/DAY_MS,0,0.999):0),z=cam.z,sc=Math.max(0.85,Math.min(1.5,Math.sqrt(z)))*(lodFar()?0.9:1.05),list=[]; for(const s of SHIPS){const p=shipAt(s,T);if(p.m==='port'&&z<1.1)continue;list.push({p,s,col:s.club==='prv'?'#7A5CFF':'#FF4D5E'});} diff --git a/src/game/99-loop.js b/src/game/99-loop.js index b139bd3..83390ba 100644 --- a/src/game/99-loop.js +++ b/src/game/99-loop.js @@ -31,7 +31,7 @@ function mapHidden(){for(const id of ['dashCard','bankCard']){const c=$(id);if(c a second anyway, and every frame while the land picture is still catching up with a zoom (the WebGL map has none). */ const FRAME=[]; function mapStill(t,now){const sl=UI.sel,dr=UI.drag; - const v=[t,cam.x,cam.y,cam.z,CW,CH,DPR,S,R,S&&S.day,S&&S.links.length,S&&S.offers.length,S&&S.inc.length,sl&&sl.type,sl&&sl.id,UI.hover,UI.snap&&UI.snap.id,dr&&dr.mode,dr&&dr.cx,dr&&dr.cy,FLOATS.length,UI.mk?UI.mk.tx:0,UI.mk?UI.mk.mood:0]; + const v=[t,cam.x,cam.y,cam.z,CW,CH,DPR,S,R,S&&S.day,S&&S.links.length,S&&S.offers.length,S&&S.inc.length,sl&&sl.type,sl&&sl.id,UI.hover,UI.snap&&UI.snap.id,dr&&dr.mode,dr&&dr.cx,dr&&dr.cy,FLOATS.length,UI.mk?UI.mk.tx:0,UI.mk?UI.mk.mood:0,zoomMoving()]; let same=v.length===FRAME.length;for(let i=0;i__T,configurable:true});`); + return h; +} + +const STUB = `(()=>{const calls=[];const gl=new Proxy({calls},{get(t,k){if(k in t)return t[k];if(typeof k==='string'&&/^[A-Z_0-9]+$/.test(k))return k; + return (...a)=>{calls.push([k,...a]);return {};};}}); + const c=document.getElementById('mapgl'),f=document.getElementById('mapfx'); + const P=()=>({p:{},u:{}}),B=()=>({v:{},b:{}}); + Object.assign(GLX,{want:true,dead:false,gl,c,f,fx:f.getContext('2d'),line:P(),pk:P(),fill:P(),fan:P(),ell:P(),land:{},circ:P(),tex:{},lv:B(),hv:B(),pv:B(),cb:B(),wv:B(),cl:B(),empty:{},key:[],gpu:'stub'}); + GLS.prog=P();GLS.vao={};GLS.buf={};GLS.tex={};GLS.size=4096;SPRS.clear();GLS.shx=GLS.shy=GLS.shh=0;return gl;})()`; + +// One POP's city pictures painted during frames from zoom z0 to z1 (the clock moving 16 ms a frame), by scale. +const ZOOM = (z0, z1, n) => `(()=>{const id=S.home,C=CITY[id];cam.x=C.x;cam.y=C.y;const seen=new Set(),bad=[]; + for(let i=0;i<=${n};i++){cam.z=${z0}+(${z1}-${z0})*i/${n};__T+=16;draw(__T); + for(const k of SPRS.keys()){const p=k.split('|');if(p[1]==='city'&&p[2]===id)seen.add(+p[4]);} + const k=cityScale(iconScale()),kq=[...SPRS.keys()].filter(x=>x.split('|')[1]==='city'&&x.split('|')[2]===id).map(x=>+x.split('|')[4]); + if(kq.length&&!kq.some(q=>Math.max(q/k,k/q)<=1.25*(1+.5/24/0.7)))bad.push(i);} + return JSON.stringify({steps:[...seen].sort(),bad});})()`; + +test('on the card a zoom keeps each city’s picture for a while, and paints it at the right step once it rests', () => { + const { ev, errors } = open(); + ev(STUB); + ev(`cam.z=0.6;draw(__T);__T+=1000;draw(__T)`); + const r = JSON.parse(ev(ZOOM(0.6, 1.6, 60))); + // Without the hold, every 1/24 step between the two scales gets a picture. + const plain = ev(`(()=>{const s=new Set();for(let i=0;i<=60;i++){cam.z=0.6+i/60;s.add(Math.round(cityScale(iconScale())*24)/24);}return s.size;})()`); + assert.ok(plain >= 10, `the zoom crosses many steps (${plain})`); + assert.ok(r.steps.length <= 4, `the city was painted at ${r.steps.length} scales during the zoom (${r.steps}), not ${plain}`); + assert.deepEqual(r.bad, [], 'a picture is never stretched more than 1.25× either way (and half a step)'); + assert.equal(ev('zoomMoving()'), true); + ev('__T+=300;draw(__T)'); + const rest = JSON.parse(ev(`(()=>{const id=S.home,want=Math.round(cityScale(iconScale())*24)/24; + return JSON.stringify({f:ZH.f,want,has:[...SPRS.keys()].some(k=>{const p=k.split('|');return p[1]==='city'&&p[2]===id&&+p[4]===want;}),moving:zoomMoving()});})()`)); + assert.equal(rest.moving, false); + assert.equal(rest.f, 1, 'at rest nothing is stretched'); + assert.equal(rest.has, true, 'the city is painted again at its own step'); + assert.deepEqual(errors, []); +}); + +test('the 2D map paints cities at their own step on every frame of a zoom, as before', () => { + const { ev, errors } = open(); + ev(`mapGl(false);cam.z=0.8;draw(__T);`); + const r = JSON.parse(ev(`(()=>{const id=S.home,C=CITY[id];cam.x=C.x;cam.y=C.y;const bad=[]; + for(let i=0;i<=30;i++){cam.z=0.8+i/30;__T+=16;SPRS.clear();draw(__T);const want=Math.round(cityScale(iconScale())*24)/24; + if(ZH.f!==1||![...SPRS.keys()].some(k=>{const p=k.split('|');return p[1]==='city'&&p[2]===id&&+p[4]===want;}))bad.push(i);} + return JSON.stringify({bad});})()`)); + assert.deepEqual(r.bad, []); + assert.deepEqual(errors, []); +}); + +test('a still map is drawn again when the zoom comes to rest, so the stretched pictures do not stay', () => { + const { ev, errors } = open(); + ev(STUB); + // Animations off: the loop skips frames while nothing changes. + ev(`cam.z=1;draw(__T);mapStill(0,__T);UI.drawnAt=__T;cam.z=1.3;__T+=16;mapStill(0,__T);draw(__T);UI.drawnAt=__T;`); + ev(`__T+=16`); + assert.equal(ev('mapStill(0,__T)'), true, 'nothing moved since the last frame'); + ev(`__T+=300`); + assert.equal(ev('mapStill(0,__T)'), false, 'the zoom has rested: one more frame'); + ev(`draw(__T);UI.drawnAt=__T;__T+=16`); + assert.equal(ev('ZH.f'), 1); + assert.equal(ev('mapStill(0,__T)'), true, 'and then the map stays still again'); + assert.deepEqual(errors, []); +}); + +test('pictures that keep their size whatever the zoom (far-view cities, cones, diggers) are never held or stretched', () => { + const { ev, errors } = open(); + ev(STUB); + const r = JSON.parse(ev(`(()=>{cam.z=1;draw(__T);__T+=1000;cam.z=1;draw(__T);cam.z=1.4;__T+=16;draw(__T);GLS.frame=true;GLS.n=0;ctx=GLX.fx; + const f=ZH.f;drawCone(100,100,0);drawBackhoe(200,100,0,false);const D=GLS.data,o=[D[2],D[3],D[SPR_STRIDE+2],D[SPR_STRIDE+3]]; + const keys=[...SPRS.keys()].filter(k=>['cone','hoe'].includes(k.split('|')[1]));GLS.frame=false;GLS.n=0;ctx=CTX0;return JSON.stringify({f,o,keys});})()`)); + assert.notEqual(r.f, 1, 'the zoom is moving and city pictures are held'); + assert.deepEqual(r.o, [26, 28, 38, 32], 'drawn at their own size'); + assert.ok(r.keys.length >= 2 && r.keys.every(k => k.endsWith('|1')), 'painted at scale 1: ' + r.keys); + assert.deepEqual(errors, []); +});