Draw date-line links on both edges of the share card

Fixes #42
This commit is contained in:
Frederic Jung 2026-10-25 12:26:06 +01:00
parent 9d53436cb7
commit 31372c0205
No known key found for this signature in database
2 changed files with 22 additions and 1 deletions

View file

@ -1366,12 +1366,16 @@ function traceModal(src,to){const pops=Object.keys(S.pops).sort((a,b)=>CITY[a].n
<div class="btns">${btn(tr('Copy'),'traceCopy',{},'ghost sm')}</div>`);UI.trText=r.text;}
/* ---------- share card ---------- */
// Straight link segments in map coordinates for flat maps. A link across the date line leaves one edge
// and comes back in at the other, so it becomes two segments, one per edge (#42).
function linkSegs(){const out=[];for(const l of S.links){const A=CITY[l.a],B=CITY[l.b];if(!A||!B)continue;const d=Math.abs(B.x-A.x)>W/2?(B.x<A.x?W:-W):0;
for(const o of d?[0,-d]:[0])out.push([A.x+o,A.y,B.x+d+o,B.y]);}return out;}
function shareCanvas(){const Wc=1200,Hc=630,cv=document.createElement('canvas');cv.width=Wc;cv.height=Hc;const g=cv.getContext&&cv.getContext('2d');if(!g)return null;
g.fillStyle='#57C4E5';g.fillRect(0,0,Wc,Hc);
// mini map
const mx=40,my=150,mw=700,mh=350,sx=mw/W,sy=mh/H;g.save();g.beginPath();g.rect(mx,my,mw,mh);g.clip();g.fillStyle='#3AA6CF';g.fillRect(mx,my,mw,mh);
g.fillStyle='#9BDA6C';g.strokeStyle='#1E2640';g.lineWidth=1.5;for(const pts of COAST){g.beginPath();pts.forEach((q,i)=>i?g.lineTo(mx+q[0]*sx,my+q[1]*sy):g.moveTo(mx+q[0]*sx,my+q[1]*sy));g.closePath();g.fill();g.stroke();}
g.strokeStyle='#FF4F9A';g.lineWidth=2.5;for(const l of S.links){const A=CITY[l.a],B=CITY[l.b];let bx=B.x;if(Math.abs(bx-A.x)>W/2)bx+=bx<A.x?W:-W;g.beginPath();g.moveTo(mx+A.x*sx,my+A.y*sy);g.lineTo(mx+bx*sx,my+B.y*sy);g.stroke();}
g.strokeStyle='#FF4F9A';g.lineWidth=2.5;for(const [x1,y1,x2,y2] of linkSegs()){g.beginPath();g.moveTo(mx+x1*sx,my+y1*sy);g.lineTo(mx+x2*sx,my+y2*sy);g.stroke();}
for(const c in S.pops){const C=CITY[c];g.fillStyle='#FF4F9A';g.strokeStyle='#1E2640';g.lineWidth=2;g.beginPath();g.arc(mx+C.x*sx,my+C.y*sy,5,0,7);g.fill();g.stroke();}
g.restore();g.strokeStyle='#1E2640';g.lineWidth=5;g.strokeRect(mx,my,mw,mh);
// header

View file

@ -472,3 +472,20 @@ test('settings screen holds every option and opens from the menu and the title s
assert.equal(ev("document.getElementById('modal').hidden"), true);
assert.deepEqual(errors, []);
});
test('#42 the share card draws links across the date line on both edges of the map', () => {
const { ev, errors } = load();
base(ev);
ev("S.links.push({id:'x1',a:'tok',b:'sea',kind:'wave',tier:0,km:7700,mult:1,mo:0},{id:'x2',a:'sea',b:'tok',kind:'wave',tier:0,km:7700,mult:1,mo:0},{id:'x3',a:'fra',b:'ams',kind:'wave',tier:0,km:400,mult:1,mo:0})");
const segs = JSON.parse(ev('JSON.stringify(linkSegs())'));
assert.equal(segs.length, 5, 'two segments for each date-line link, one for the short one');
for (const id of ['x1', 'x2']) {
const mine = JSON.parse(ev(`JSON.stringify((()=>{S.links=S.links.filter(l=>l.id==='${id}');return linkSegs();})())`));
ev("S.links=[]");
assert.ok(mine.some(([a, , b]) => Math.min(a, b) < 0), `${id} reaches past the left edge`);
assert.ok(mine.some(([a, , b]) => Math.max(a, b) > ev('W')), `${id} reaches past the right edge`);
ev("S.links.push({id:'x1',a:'tok',b:'sea',kind:'wave',tier:0,km:7700,mult:1,mo:0},{id:'x2',a:'sea',b:'tok',kind:'wave',tier:0,km:7700,mult:1,mo:0})");
}
ev('shareCanvas()');
assert.deepEqual(errors, []);
});