WebGL map: join each line's segments, and draw a link being dragged out under the cities

A see-through line on the card was one round-ended quad per segment, so
where two met it was blended twice: the selected tube's halo showed a
darker blob at every point, and the cables a notch at every corner. Each
segment now knows its neighbours' directions and keeps only its side of
the corner's bisector, as the 2D canvas strokes a path once.

The link being dragged out was painted on the overlay, above the cities
and their labels; it now goes to the card, under them, as in 2D, and its
price label is a picture like the others.

Auto after Always 2D, when the browser will not give the context back,
gives the card up instead of waiting for it all session. Firefox reads
the card's name from RENDERER, without the extension it warns about.
This commit is contained in:
Frederic Jung 2026-11-05 10:31:19 +01:00
parent f9c85ab0c3
commit 7f4fdf6043
No known key found for this signature in database
13 changed files with 201 additions and 39 deletions

View file

@ -1,7 +1,8 @@
/* How each tube on the map looks today, worked out once for both maps (the 2D canvas in drawFrame() and the card in
58-webgl-links.js), so they cannot drift apart. */
// Dash patterns, by number: none, cut, virtual circuit, being built, standby; 5 the fiber's dots, 6 an old cable.
const LDASH=[[],[8,8],[2,7],[10,8],[4,6],[1,6],[6,5]];
// Dash patterns, by number: none, cut, virtual circuit, being built, standby; 5 the fiber's dots, 6 an old cable,
// 7 a link being dragged out.
const LDASH=[[],[8,8],[2,7],[10,8],[4,6],[1,6],[6,5],[2,12]];
// A tube is a group of links drawn as one line: its capacity, load and state, its base width, colour and dash.
function linkLook(grp,day){const l=grp[0],many=grp.length>1,up=many?grp.filter(x=>!linkDown(x)):null;
const cap=many?up.reduce((a,x)=>a+capOf(x),0):capOf(l),load=many?grp.reduce((a,x)=>a+(R.load[x.id]||0),0):(R.load[l.id]||0),u=cap?load/cap:0,df=l.kind==='df',bld=df&&l.ready>day,cut=!bld&&(many?!up.length:linkDown(l))&&!(df&&!(l.ch||[]).length),dark=df&&!bld&&!(l.ch||[]).length,vx=l.kind==='vxc',idle=!cut&&!bld&&load<1e-3&&(many?up.length>0&&up.every(x=>x.standby):!!l.standby);

View file

@ -19,7 +19,9 @@ const glCan=()=>!GLX.dead&&typeof WebGL2RenderingContext!=='undefined';
// Switches the WebGL map on or off: the setting keeps the choice in this browser (keep), the GPU benchmark does not.
// Chosen in Settings, Always 2D also lets the card's context go (glSleep()), and Auto takes it back.
function mapGl(on,keep=true){GLX.want=!!on;if(keep)try{on?localStorage.removeItem('sot-map'):localStorage.setItem('sot-map','2d');}catch(e){}
if(!on){glShow(false);if(keep)glSleep();}else if(GLX.sleep){GLX.sleep=false;try{GLX.lose.restoreContext();}catch(e){}}
if(!on){glShow(false);if(keep)glSleep();}else if(GLX.sleep){GLX.sleep=false;
// A context the browser will not give back would leave the map waiting for it all session: give the card up instead.
try{GLX.lose.restoreContext();}catch(e){if(GLX.ctx&&GLX.ctx.isContextLost&&GLX.ctx.isContextLost()===false)GLX.lost=false;else glKill(e);}}
GLX.key=[];UI.drawnAt=0;return on&&!GLX.dead;}
function glShow(on){on=!!on;if(GLX.shown===on)return;GLX.shown=on;const e=document.getElementById('mapgl'),fx=document.getElementById('mapfx');if(e)e.hidden=!on;if(fx)fx.hidden=!on;}
// Marks an error as the card's own, so draw() falls back to 2D instead of trying WebGL again every frame.
@ -72,7 +74,8 @@ function glInit(){if(GLX.gl)return true;if(GLX.dead||GLX.lost)return false;
const gl=c.getContext('webgl2',{alpha:true,premultipliedAlpha:true,antialias:false,depth:true,stencil:true,powerPreference:'high-performance'});
if(!gl||typeof gl.createShader!=='function')throw new Error('no WebGL2');GLX.ctx=gl;
if(!GLX.lose){c.addEventListener('webglcontextlost',glLost);c.addEventListener('webglcontextrestored',glRestored);try{GLX.lose=gl.getExtension('WEBGL_lose_context')||{};}catch(e){GLX.lose={};}}
try{const x=gl.getExtension('WEBGL_debug_renderer_info');GLX.gpu=String(x?gl.getParameter(x.UNMASKED_RENDERER_WEBGL):gl.getParameter(gl.RENDERER));}catch(e){}
// Firefox names the card in RENDERER and warns that the extension is going away; other browsers need it.
try{const x=/Firefox\//.test(navigator.userAgent)?null:gl.getExtension('WEBGL_debug_renderer_info');GLX.gpu=String(x?gl.getParameter(x.UNMASKED_RENDERER_WEBGL):gl.getParameter(gl.RENDERER));}catch(e){}
if(!GLX.soft&&GL_SOFT.test(GLX.gpu))throw new Error('no graphics card, only '+GLX.gpu);
const P=(vs,fs)=>glProg(gl,vs,fs),progs={line:P(GL_LINE_VS,GL_LINE_FS),pk:P(GL_PK_VS,GL_PK_FS),fill:P(FILL_VS,FILL_FS),fan:P(FAN_VS,FAN_FS),ell:P(ELL_VS,ELL_FS),circ:P(CIRC_VS,CIRC_FS)};
// The tubes' points for the packets: floats read with texelFetch, never filtered.
@ -83,7 +86,7 @@ function glInit(){if(GLX.gl)return true;if(GLX.dead||GLX.lost)return false;
const fx=f.getContext('2d'),CP=CanvasRenderingContext2D.prototype;
if(!fx.__fxw){fx.__fxw=true;for(const m of ['fill','stroke','fillRect','strokeRect','fillText','strokeText','drawImage','putImageData']){const fn=CP[m];fx[m]=function(){GLX.fxUsed=true;return fn.apply(this,arguments);};}}
const V=s=>glVao(gl,s);
Object.assign(GLX,progs,{gl,c,f,fx,fxUsed:true,tex,land:{},lv:V([4,4,4]),hv:V([4,4,4]),pv:V([4,4]),cb:V([4,4,4]),wv:V([4,4,4]),cl:V([4]),empty:gl.createVertexArray()});return true;}
Object.assign(GLX,progs,{gl,c,f,fx,fxUsed:true,tex,land:{},lv:V([4,4,4,4]),hv:V([4,4,4,4]),pv:V([4,4]),cb:V([4,4,4,4]),wv:V([4,4,4,4]),dv:V([4,4,4,4]),cl:V([4]),empty:gl.createVertexArray()});return true;}
catch(e){glKill(e);return false;}}
// Colours as [r,g,b,a] from '#rrggbb', '#rgb' or 'rgba(r,g,b,a)', kept by string.
const GLCOL=new Map();

View file

@ -53,11 +53,12 @@ function glLandGeo(gl,far,lv){const key=(far?'far':'all')+(lv===undefined?'':'@'
COAST.forEach((ring,ri)=>{if(keep&&!keep.has(LANDP_ALL[ri]))return;if(ring.length<3)return;
const P=coastSimplify(coastPts(ri),tol);if(P.length<4)return;
coastFan(P,fan);
// Round ends make the joins round, as lineJoin='round' does; both lines are opaque, so the overlaps do not show.
for(let i=0;i+1<P.length;i++){glSeg(sand,P[i],P[i+1],0,10,0,LINE_ROUND,SAND);glSeg(edge,P[i],P[i+1],0,2.5,0,LINE_ROUND,EDGE);}});
// A closed ring (its first point repeated at the end), joined all round.
const s0=sand.length/LINE_STRIDE;for(let i=0;i+1<P.length;i++){glSeg(sand,P[i],P[i+1],0,10,0,LINE_ROUND,SAND);glSeg(edge,P[i],P[i+1],0,2.5,0,LINE_ROUND,EDGE);}
const s1=sand.length/LINE_STRIDE;glJoin(sand,s0,s1,true);glJoin(edge,s0,s1,true);});
const hills=[],shine=[];for(const [x,y,r] of HILLS){hills.push(x,y,r*1.4,r*.8);shine.push(x-r*.3,y-r*.25,r*.6,r*.3);}
const F=a=>new Float32Array(a);
G={fan:glVao(gl,[2],0,F(fan)).v,nFan:fan.length/2,sand:glVao(gl,[4,4,4],1,F(sand)).v,edge:glVao(gl,[4,4,4],1,F(edge)).v,nSeg:sand.length/LINE_STRIDE,
G={fan:glVao(gl,[2],0,F(fan)).v,nFan:fan.length/2,sand:glVao(gl,[4,4,4,4],1,F(sand)).v,edge:glVao(gl,[4,4,4,4],1,F(edge)).v,nSeg:sand.length/LINE_STRIDE,
hills:glVao(gl,[4],1,F(hills)).v,shine:glVao(gl,[4],1,F(shine)).v,nHill:HILLS.length};
glMem(gl,'the land');return GLX.land[key]=G;}
// Fills the land's shape (shifted by ox, oy CSS px) with one colour, for every copy of the map.
@ -84,10 +85,11 @@ function glLand(gl,ks){const G=glLandGeo(gl,lodFar(),coastLevel(cam.z));
// written into the same array every frame (no garbage for the collector to pick up while you play).
const WAVE_COL=glColor('rgba(255,255,255,.45)');
function glWaves(gl,now,ks){const A=GLX.wa||(GLX.wa=new Float32Array(WAVES.length*8*LINE_STRIDE)),ph0=now/900;let m=0;
for(const [x,y,ph] of WAVES){const o=Math.sin(ph0+ph*6)*3;
for(const [x,y,ph] of WAVES){const o=Math.sin(ph0+ph*6)*3,m0=m;
// Two curves: up from x-9 to x, then down to x+9 (no array literals per wave: they were garbage every frame).
for(let c=0;c<2;c++){const x0=x+o+(c?0:-9),cx=x0+4.5,cy=y+(c?4:-4),x1=x0+9;let px=x0,py=y;
for(let j=1;j<=4;j++){const t=j/4,u=1-t,qx=u*u*x0+2*u*t*cx+t*t*x1,qy=u*u*y+2*u*t*cy+t*t*y;glSegAt(A,m++,px,py,qx,qy,0,2,0,0,WAVE_COL[3],WAVE_COL);px=qx;py=qy;}}}
for(let j=1;j<=4;j++){const t=j/4,u=1-t,qx=u*u*x0+2*u*t*cx+t*t*x1,qy=u*u*y+2*u*t*cy+t*t*y;glSegAt(A,m++,px,py,qx,qy,0,2,0,0,WAVE_COL[3],WAVE_COL);px=qx;py=qy;}}
glJoin(A,m0,m,false);}
gl.bindBuffer(gl.ARRAY_BUFFER,GLX.wv.b);gl.bufferData(gl.ARRAY_BUFFER,A.subarray(0,m*LINE_STRIDE),gl.DYNAMIC_DRAW);glLines(gl,GLX.wv.v,m,ks);}
// The clouds: their puffs are stencilled as one shape, then filled once, so overlapping puffs do not stack.
function glClouds(gl,now,ks){const need=ks.length*CLOUDS.length*12;if(!need)return;
@ -103,8 +105,9 @@ function glClouds(gl,now,ks){const need=ks.length*CLOUDS.length*12;if(!need)retu
function glCables(gl,ks,selC,ar){const key=[selC,ar];
if(!GLX.ckey||key.some((v,i)=>v!==GLX.ckey[i])){const A=[];
for(const cp of CABLE_PAIRS){const hot=!!selC&&(cp.a===selC||cp.b===selC);if(ar&&!hot&&!(arKind(cp.a)&&arKind(cp.b)))continue;
const C=CAB[cp.cid],[r,g,b]=glColor(cableCol(C)),col=[r,g,b,hot?.95:.4];let cum=0;
for(let j=0;j+1<cp.pts.length;j++){const p=cp.pts[j],q=cp.pts[j+1];glSeg(A,p,q,cum,hot?3:1.6,C.age==='old'?6:0,0,col);cum+=Math.hypot(q[0]-p[0],q[1]-p[1]);}}
const C=CAB[cp.cid],[r,g,b]=glColor(cableCol(C)),col=[r,g,b,hot?.95:.4],s0=A.length/LINE_STRIDE;let cum=0;
for(let j=0;j+1<cp.pts.length;j++){const p=cp.pts[j],q=cp.pts[j+1];glSeg(A,p,q,cum,hot?3:1.6,C.age==='old'?6:0,0,col);cum+=Math.hypot(q[0]-p[0],q[1]-p[1]);}
glJoin(A,s0,A.length/LINE_STRIDE,false);}
const F=new Float32Array(A);gl.bindBuffer(gl.ARRAY_BUFFER,GLX.cb.b);gl.bufferData(gl.ARRAY_BUFFER,F,gl.STATIC_DRAW);glMem(gl,'the cables');GLX.cn=F.length/LINE_STRIDE;GLX.ckey=key;}
if(GLX.cn)glLines(gl,GLX.cb.v,GLX.cn,ks);}
// Starts a frame on the card: clears it and paints the background. The tubes come next (glFrame()), then the pictures

View file

@ -7,17 +7,27 @@
const LP={halo:0,casing:1,colour:2,dots:3,plain:4};
// A segment's flags: on the selected tube, lit fiber (its dots show), round ends for a plain line.
const LINE_SEL=1,LINE_FIBER=2,LINE_ROUND=4;
/* One segment is 12 floats: x0,y0,x1,y1 (map units) | the length before it, width (CSS px), dash (LDASH), flags |
a, r,g,b, where a is the depth for a tube and the opacity for a plain line. */
const LINE_STRIDE=12;
/* One segment is 16 floats: x0,y0,x1,y1 (map units) | the length before it, width (CSS px), dash (LDASH), flags |
a, r,g,b, where a is the depth for a tube and the opacity for a plain line | the directions of the segments before
and after it on the same line (0,0: none, the line ends here; glJoin() fills them). */
const LINE_STRIDE=16;
function glSegAt(A,i,ax,ay,bx,by,cum,w,dash,fl,a,col){const o=i*LINE_STRIDE;
A[o]=ax;A[o+1]=ay;A[o+2]=bx;A[o+3]=by;A[o+4]=cum;A[o+5]=w;A[o+6]=dash;A[o+7]=fl;A[o+8]=a;A[o+9]=col[0];A[o+10]=col[1];A[o+11]=col[2];}
function glSeg(A,p,q,cum,w,dash,fl,col){A.push(p[0],p[1],q[0],q[1],cum,w,dash,fl,col[3],col[0],col[1],col[2]);}
A[o]=ax;A[o+1]=ay;A[o+2]=bx;A[o+3]=by;A[o+4]=cum;A[o+5]=w;A[o+6]=dash;A[o+7]=fl;A[o+8]=a;A[o+9]=col[0];A[o+10]=col[1];A[o+11]=col[2];A[o+12]=A[o+13]=A[o+14]=A[o+15]=0;}
function glSeg(A,p,q,cum,w,dash,fl,col){A.push(p[0],p[1],q[0],q[1],cum,w,dash,fl,col[3],col[0],col[1],col[2],0,0,0,0);}
// Segments i0..i1-1 of A are one line: each learns its neighbours' directions (closed: the last meets the first).
function glJoin(A,i0,i1,closed){closed=closed&&i1-i0>2;
for(let i=i0;i<i1;i++){const o=i*LINE_STRIDE,p=i>i0?i-1:closed?i1-1:-1,n=i+1<i1?i+1:closed?i0:-1;
if(p>=0){const q=p*LINE_STRIDE;A[o+12]=A[q+2]-A[q];A[o+13]=A[q+3]-A[q+1];}else A[o+12]=A[o+13]=0;
if(n>=0){const q=n*LINE_STRIDE;A[o+14]=A[q+2]-A[q];A[o+15]=A[q+3]-A[q+1];}else A[o+14]=A[o+15]=0;}}
/* One instance per segment, a quad around it; the fragment keeps what lies within half the line width of the segment
(or of a dash of it, both with round ends), which also gives round joins and caps. */
const GL_LINE_VS=GL_HEAD+GL_CLIP+`layout(location=0) in vec4 aSeg;layout(location=1) in vec4 aP;layout(location=2) in vec4 aC;
(or of a dash of it, both with round ends), which also gives round joins and caps. Where two segments of a line
meet, their round ends overlap: each keeps only its side of the corner's bisector, so a see-through line (the
halo, the cables, the waves, the fiber's dots) is blended once there, as the 2D canvas strokes a path once. */
const GL_LINE_VS=GL_HEAD+GL_CLIP+`layout(location=0) in vec4 aSeg;layout(location=1) in vec4 aP;layout(location=2) in vec4 aC;layout(location=3) in vec4 aN;
uniform vec2 uView;uniform vec3 uCam;uniform float uWMul,uDpr,uBusy;uniform int uPass;
out vec2 vLoc;out float vLen,vS0,vHalf,vButt;flat out vec4 vCol;flat out vec2 vDash;
out vec2 vLoc;out float vLen,vS0,vHalf,vButt;flat out vec4 vCol,vK;flat out vec2 vDash,vEnd;
// The corner's bisector between directions u and v, in the segment's own frame (t along it, n across), and whether there is one.
vec3 cut(vec2 u,vec2 v,vec2 t,vec2 n){vec2 m=u+v;float l=length(m);if(length(u)<.5||length(v)<.5||l<1e-3)return vec3(0.);m/=l;return vec3(dot(t,m),dot(n,m),1.);}
vec2 dashOf(int k){${LDASH.map((d,i)=>d.length?`if(k==${i})return vec2(${d[0]}.,${d[1]}.);`:'').join('')}return vec2(0.);}
void main(){vec2 h=uView*.5,a=(aSeg.xy-uCam.xy)*uCam.z+h,b=(aSeg.zw-uCam.xy)*uCam.z+h;
float w=aP.y*uWMul,hw=w*.5;int fl=int(aP.w+.5);vec4 col=vec4(aC.yzw,1.);vec2 dash=dashOf(int(aP.z+.5));bool skip=false;
@ -29,10 +39,16 @@ void main(){vec2 h=uView*.5,a=(aSeg.xy-uCam.xy)*uCam.z+h,b=(aSeg.zw-uCam.xy)*uCa
vec2 d=b-a;float L=length(d);vec2 t=L>1e-4?d/L:vec2(1.,0.),n=vec2(-t.y,t.x);float m=hw+1./uDpr;
float sx=(gl_VertexID&1)==0?-m:L+m,sy=(gl_VertexID&2)==0?-m:m;
vLoc=vec2(sx,sy);vLen=L;vS0=aP.x*uCam.z;vHalf=hw;vCol=col;vDash=dash;vButt=uPass==${LP.plain}&&(fl&${LINE_ROUND})==0?1.:0.;
float lp=length(aN.xy),ln=length(aN.zw);vec3 c0=L>1e-4&&lp>1e-9?cut(aN.xy/lp,t,t,n):vec3(0.),c1=L>1e-4&&ln>1e-9?cut(t,aN.zw/ln,t,n):vec3(0.);
vK=vec4(c0.xy,c1.xy);vEnd=vec2(c0.z,c1.z);
gl_Position=clip(a+t*sx+n*sy,uView,(uPass==${LP.plain}?.9998:aC.x)*2.-1.);}`;
const GL_LINE_FS=GL_HEAD+`in vec2 vLoc;in float vLen,vS0,vHalf,vButt;flat in vec4 vCol;flat in vec2 vDash;uniform float uDpr;out vec4 o;
const GL_LINE_FS=GL_HEAD+`in vec2 vLoc;in float vLen,vS0,vHalf,vButt;flat in vec4 vCol,vK;flat in vec2 vDash,vEnd;uniform float uDpr;out vec4 o;
void main(){float x=vLoc.x,y=vLoc.y,d;
if(vButt>.5&&(x<0.||x>vLen))discard;
// A corner: the side of the bisector that belongs to the neighbour is the neighbour's to paint.
if(vEnd.x>.5&&x*vK.x+y*vK.y<0.)discard;
if(vEnd.y>.5&&(x-vLen)*vK.z+y*vK.w>0.)discard;
// Flat ends only where the line really ends; its corners are joined.
if(vButt>.5&&((vEnd.x<.5&&x<0.)||(vEnd.y<.5&&x>vLen)))discard;
if(vDash.x<=0.){float c=clamp(x,0.,vLen);d=length(vec2(x-c,y));}
else{float P=vDash.x+vDash.y,g=vS0+x,k=floor(g/P);d=1e9;
for(int i=-1;i<=1;i++){float a0=(k+float(i))*P,lo=max(a0,vS0),hi=min(a0+vDash.x,vS0+vLen);if(lo<=hi){float c=clamp(g,lo,hi);d=min(d,length(vec2(g-c,y)));}}}
@ -67,7 +83,8 @@ function glBuild(LG,GM,looks){const gl=GLX.gl,N=LG.length;let ns=0,np=0,nk=0;
for(let gi=0;gi<N;gi++){const L=looks[gi],{pts,cum,len}=GM[gi],dep=glDepth(gi,N),col=glColor(L.col);
const fl=L.df&&!L.bld&&!L.dark?LINE_FIBER:0,p0=pi;
for(let j=0;j<pts.length;j++){PT[pi*4]=pts[j][0];PT[pi*4+1]=pts[j][1];PT[pi*4+2]=cum[j];pi++;}
for(let j=0;j+1<pts.length;j++)glSegAt(SEG,si++,pts[j][0],pts[j][1],pts[j+1][0],pts[j+1][1],cum[j]*len,L.w0,L.dash,fl,dep,col);
const s0=si;for(let j=0;j+1<pts.length;j++)glSegAt(SEG,si++,pts[j][0],pts[j][1],pts[j+1][0],pts[j+1][1],cum[j]*len,L.w0,L.dash,fl,dep,col);
glJoin(SEG,s0,si,false);
for(let i=0;i<L.pk;i++){const o=ki*8;PK[o]=p0;PK[o+1]=pts.length;PK[o+2]=len;PK[o+3]=L.pk;PK[o+4]=i;PK[o+5]=L.w0;PK[o+6]=dep;ki++;}}
gl.bindBuffer(gl.ARRAY_BUFFER,GLX.lv.b);gl.bufferData(gl.ARRAY_BUFFER,SEG,gl.DYNAMIC_DRAW);
gl.bindBuffer(gl.ARRAY_BUFFER,GLX.pv.b);gl.bufferData(gl.ARRAY_BUFFER,PK,gl.DYNAMIC_DRAW);
@ -76,7 +93,8 @@ function glBuild(LG,GM,looks){const gl=GLX.gl,N=LG.length;let ns=0,np=0,nk=0;
// The selected tube's halo: its segments again, at its own depth (so later tubes still cover it), and nothing else.
function glHalo(LG,GM,looks,selId){const gl=GLX.gl,N=LG.length,A=[];
if(selId)for(let gi=0;gi<N;gi++){if(!LG[gi].some(x=>x.id===selId))continue;const L=looks[gi],{pts,cum,len}=GM[gi],dep=glDepth(gi,N),col=glColor(L.col);
for(let j=0;j+1<pts.length;j++)A.push(pts[j][0],pts[j][1],pts[j+1][0],pts[j+1][1],cum[j]*len,L.w0,L.dash,LINE_SEL,dep,col[0],col[1],col[2]);}
const s0=A.length/LINE_STRIDE;for(let j=0;j+1<pts.length;j++)A.push(pts[j][0],pts[j][1],pts[j+1][0],pts[j+1][1],cum[j]*len,L.w0,L.dash,LINE_SEL,dep,col[0],col[1],col[2],0,0,0,0);
glJoin(A,s0,A.length/LINE_STRIDE,false);}
const F=new Float32Array(A);if(F.length){gl.bindBuffer(gl.ARRAY_BUFFER,GLX.hv.b);gl.bufferData(gl.ARRAY_BUFFER,F,gl.DYNAMIC_DRAW);}
GLX.nHalo=F.length/LINE_STRIDE;return F;}
// Draws the tubes and their packets over the background glBegin() painted (ML: mapLinks(), sc: the icon scale).
@ -93,3 +111,11 @@ function glFrame(ML,ks,far,busy,now,sc){const gl=GLX.gl;
// The selection's halo goes over earlier tubes but under its own line and later ones, as in 2D.
if(GLX.nHalo){gl.depthFunc(gl.LESS);gl.depthMask(false);lines(LP.halo,GLX.hv.v,GLX.nHalo);gl.depthMask(true);}
gl.bindVertexArray(null);}
// The link being dragged out (x,y in CSS px): the dark dotted line, then the pink one running along it, over the tubes
// and, since the pictures are drawn after it, under the cities, as in 2D.
const DRAG_INK=glColor('#1E2640'),DRAG_PINK=glColor('#FF4F9A');
function glDrag(ax,ay,tx,ty,now){const gl=GLX.gl,A=GLX.da||(GLX.da=new Float32Array(2*LINE_STRIDE)),z=cam.z,mx=x=>(x-CW/2)/z+cam.x,my=y=>(y-CH/2)/z+cam.y;
glSegAt(A,0,mx(ax),my(ay),mx(tx),my(ty),0,7,7,LINE_ROUND,1,DRAG_INK);
glSegAt(A,1,mx(ax),my(ay),mx(tx),my(ty),-now/30/z,4,7,LINE_ROUND,1,DRAG_PINK);
gl.bindBuffer(gl.ARRAY_BUFFER,GLX.dv.b);gl.bufferData(gl.ARRAY_BUFFER,A,gl.DYNAMIC_DRAW);
gl.disable(gl.DEPTH_TEST);glLines(gl,GLX.dv.v,2,[0]);gl.bindVertexArray(null);}

View file

@ -86,12 +86,14 @@ function drawFrame(now){
if(UI.snap){const sv=UI.snap;tx=sv.sx;ty=sv.sy;}
let [ax,ay]=toS(A.x,A.y,0);if(UI.snap){const B=CITY[UI.snap.id];const bxw=A.x+wrapDx(A.x,B.x);[tx,ty]=toS(bxw,B.y,0);}else{/* nearest copy of origin to cursor */for(const k of [-1,1]){const [px,py]=toS(A.x,A.y,k);if(Math.abs(px-tx)<Math.abs(ax-tx)){ax=px;ay=py;}}}
if(UI.snap){const B=CITY[UI.snap.id];const wx=A.x+wrapDx(A.x,B.x);for(const k of [-1,1]){const [px,py]=toS(A.x,A.y,k),[qx]=toS(wx,B.y,k);if(Math.abs(qx-CW/2)<Math.abs(tx-CW/2)){ax=px;ay=py;tx=qx;}}}
ctx.strokeStyle='#1E2640';ctx.lineWidth=7;ctx.lineCap='round';ctx.setLineDash([2,12]);ctx.beginPath();ctx.moveTo(ax,ay);ctx.lineTo(tx,ty);ctx.stroke();
ctx.strokeStyle='#FF4F9A';ctx.lineWidth=4;ctx.lineDashOffset=-now/30;ctx.stroke();ctx.setLineDash([]);ctx.lineDashOffset=0;
// On the card it goes under the cities and their labels, as in 2D (the overlay would put it over them).
if(G)try{glDrag(ax,ay,tx,ty,now);}catch(e){throw glErr(e);}
else{ctx.strokeStyle='#1E2640';ctx.lineWidth=7;ctx.lineCap='round';ctx.setLineDash(LDASH[7]);ctx.beginPath();ctx.moveTo(ax,ay);ctx.lineTo(tx,ty);ctx.stroke();
ctx.strokeStyle='#FF4F9A';ctx.lineWidth=4;ctx.lineDashOffset=-now/30;ctx.stroke();ctx.setLineDash([]);ctx.lineDashOffset=0;}
if(UI.snap&&UI.snap.id!==UI.drag.from){const b=UI.snap.id,km=kmBetween(UI.drag.from,b),sub=isSub(UI.drag.from,b);
const t=`${km.toLocaleString()} km${sub?' · '+tr('common.subsea'):''} · ${sub&&!cablesFor(UI.drag.from,b).length?tr('draw.no_cable_lands_here'):tr('draw.from_price_mo',{price:fmt$(waveMo(km,sub,0))})}`;
ctx.font='800 13px "Baloo 2", sans-serif';const tw=ctx.measureText(t).width+18;const lx=(ax+tx)/2,ly=(ay+ty)/2-22;
ctx.fillStyle='#1E2640';rr(lx-tw/2,ly-12,tw,24,12);ctx.fill();ctx.fillStyle='#fff';ctx.textAlign='center';ctx.textBaseline='middle';ctx.fillText(t,lx,ly+1);}
pill(t,ctx.font,tw,24,12,'#1E2640','#fff','#1E2640',1,lx,ly,1);}
}
// cities
UI.feed=null;if(UI.sel&&UI.sel.type==='link'){const sl=S.links.find(x=>x.id===UI.sel.id);const rec=sl&&R.ef[edgeKey(sl)];if(rec){UI.feed=new Set(Object.keys(rec.eg).map(k=>R.egDesc[k].city));UI.dest=new Set(Object.keys(rec.dst));}}