/* Pie in the Sky globe (Pie Control panel): an orthographic Earth on a 2D canvas with a sample of your pies in their crusts, laser links, pie-dish beams and launches. Never drawn on the world map. It costs nothing until the panel calls pieGlobeOpen(canvas,getData): then one rAF loop, at most 30 frames a second, idle while the canvas is hidden; it stops by itself (pieGlobeClose()) once the canvas leaves the page. Positions come from the clock, so nothing is saved. */ const PG_D=Math.PI/180,PG_XF=2.6,PG_FPS=30,PG_FULL={short:1584,frozen:520,upper:1200,flaky:2500},PG_CI={short:0,frozen:1,upper:2,flaky:3}; const PG_ST={ day:{sea:'#57C4E5',sea2:'#3AA6CF',land:'#9BDA6C',ink:'#1E2640',grid:'rgba(255,255,255,.35)',sat:['#FFFFFF','#FFD23F','#FF4F9A','#B9A6FF'],laser:'#FF4F9A',beam:['#FF8A3D','#FFD23F'],dish:'#FF4F9A',off:'#8A91A8'}, night:{bg:'#0B1030',sea:'#14234F',sea2:'#0B1638',land:'#24396F',ink:'#3E5BB0',grid:'rgba(120,150,255,.10)',sat:['#7DF9FF','#FFD23F','#FF7AC0','#C9B8FF'],laser:'rgba(255,79,154,.75)',beam:['rgba(255,210,63,.65)','rgba(255,138,61,.5)'],dish:'#FFD23F',off:'#4A5378'}}; // The whole state of the globe. cv is null whenever it is closed, and then nothing below runs. const PG={cv:null,ctx:null,get:null,raf:0,last:0,t:0,yaw:-20,tilt:18,vy:0,drag:null,night:null,anim:true,dirty:true, data:null,dAt:-1e9,sig:'',sm:[],dish:[],pads:[],lch:[],on:null,g:null,frames:0,m:new Float64Array(9)}; let PG_LAND=null,PG_GRID=null,PG_LIGHT=null,PG_STAR=null; const pgNightPref=()=>{if(PG.night===null){try{PG.night=localStorage.getItem('sot-globe')==='night';}catch(e){PG.night=false;}}return PG.night;}; // Unit vectors for the land rings (LANDS_LL), the graticule, city lights and stars: worked out once, on first open. function pgUnit(lo,la,a,i){const b=la*PG_D,c=Math.cos(b),o=lo*PG_D;a[i]=c*Math.cos(o);a[i+1]=c*Math.sin(o);a[i+2]=Math.sin(b);} function pgVecs(){if(PG_LAND)return; PG_LAND=LANDS_LL.map(l=>{const a=new Float32Array(l.ll.length*3);l.ll.forEach((q,i)=>pgUnit(q[0],q[1],a,i*3));return a;}); PG_GRID=[];for(let lo=-180;lo<180;lo+=30){const a=new Float32Array(31*3);for(let j=0;j<=30;j++)pgUnit(lo,-90+j*6,a,j*3);PG_GRID.push(a);} for(let la=-60;la<=60;la+=30){const a=new Float32Array(61*3);for(let j=0;j<=60;j++)pgUnit(-180+j*6,la,a,j*3);PG_GRID.push(a);} const cs=CITIES.filter(c=>isFinite(c.lat)&&isFinite(c.lon));PG_LIGHT={v:new Float32Array(cs.length*3),r:cs.map(c=>1.1+Math.min(1.6,(c.P||0)/400))};cs.forEach((c,i)=>pgUnit(c.lon,c.lat,PG_LIGHT.v,i*3)); let s=7;const r=()=>(s=(s*16807)%2147483647)/2147483647;PG_STAR=[];for(let i=0;i<140;i++)PG_STAR.push([r(),r(),r()*9]);} // Camera: yaw about the poles, then tilt towards the viewer. Out: d (towards the viewer), h (right), v (up). function pgCam(){const m=PG.m,cy=Math.cos(PG.yaw*PG_D),sy=Math.sin(PG.yaw*PG_D),ct=Math.cos(PG.tilt*PG_D),st=Math.sin(PG.tilt*PG_D); m[0]=cy*ct;m[1]=-sy*ct;m[2]=st;m[3]=sy;m[4]=cy;m[5]=0;m[6]=-cy*st;m[7]=sy*st;m[8]=ct;} function pgRot(src,dst,n){const m=PG.m;for(let i=0;i(PG.buf&&PG.buf.length>=n*3)?PG.buf:(PG.buf=new Float32Array(Math.max(n*3,4096))); // Data from the panel, read once a second, never trusted: bad entries are dropped, not drawn. const pgNum=(x,d)=>typeof x==='number'&&isFinite(x)?x:d,pgArr=x=>Array.isArray(x)?x:[]; function pgPlace(list,cap){const out=[];for(const o of pgArr(list)){if(out.length>=cap)break;if(!o)continue;const C=o.city&&typeof CITY!=='undefined'?CITY[o.city]:null; const lat=pgNum(o.lat,C?C.lat:NaN),lon=pgNum(o.lon,C?C.lon:NaN);if(!isFinite(lat)||!isFinite(lon))continue;const v=new Float32Array(3);pgUnit(lon,lat,v,0);out.push({v,up:o.up!==false});}return out;} function pgRefresh(now){PG.dAt=now;let d=null;try{d=PG.get?PG.get():null;}catch(e){d=null;}d=d&&typeof d==='object'?d:{}; const cr=pgArr(d.crusts).filter(c=>c&&pgNum(c.n,0)>0).slice(0,8),ph=isPhone(),cap=ph?80:160; const sig=ph+'|'+cr.map(c=>[c.id,c.n,c.alt,c.inc,!!c.las].join(':')).join(','); if(sig!==PG.sig){PG.sig=sig;PG.sm=cr.map((c,j)=>{const n=Math.floor(pgNum(c.n,0)),full=PG_FULL[c.id]||1000,alt=Math.max(200,Math.min(2000,pgNum(c.alt,550))); // A sample, not every pie: more dots as the crust fills, up to the cap. const cnt=Math.min(n,Math.max(Math.min(n,6),Math.round(cap*Math.min(1,n/full)))),inc=Math.max(0,Math.min(180,pgNum(c.inc,53))),polar=inc>=80; const P=Math.max(1,Math.round(polar?Math.sqrt(cnt/2):Math.sqrt(cnt*1.2))),K=Math.ceil(cnt/P),span=polar?Math.PI:2*Math.PI; const co=new Float32Array(P),so=new Float32Array(P);for(let p=0;pa[i]<0&&a[i+1]*a[i+1]+a[i+2]*a[i+2]<1; function pgFrame(now,force){ const cv=PG.cv;if(!cv)return false; if(!cv.isConnected){pieGlobeClose();return false;} let W=cv.clientWidth,H=cv.clientHeight;if((!W||!H)&&force){W=300;H=300;} if(!W||!H||document.hidden)return false; if(!force&&now-PG.last<1000/PG_FPS-2)return false; const dt=Math.min(0.1,Math.max(0,(now-(PG.last||now))/1000));PG.last=now; if(now-PG.dAt>1000)pgRefresh(now); if(PG.drag){}else if(Math.abs(PG.vy)>0.01){PG.yaw+=PG.vy*dt;PG.vy*=Math.pow(0.04,dt);PG.dirty=true;}else if(PG.anim){PG.yaw+=4*dt;} if(PG.anim)PG.t+=dt;else if(!PG.dirty&&!force)return false; PG.dirty=false;PG.frames++; const ctx=PG.ctx,dpr=Math.min(2,window.devicePixelRatio||1),pw=Math.round(W*dpr),ph=Math.round(H*dpr); if(cv.width!==pw||cv.height!==ph){cv.width=pw;cv.height=ph;} ctx.setTransform(dpr,0,0,dpr,0,0);pgDraw(ctx,W,H);return true;} function pgDraw(ctx,W,H){ const night=!!PG.night,st=night?PG_ST.night:PG_ST.day,t=PG.t;pgCam();pgSats(); let mr=1;for(const c of PG.sm)if(c.rr>mr)mr=c.rr; const cx=W/2,cy=H/2,R=Math.min(W,H)*0.47/(mr+0.04),P=(a,i)=>[cx+R*a[i+1],cy-R*a[i+2]]; const gk=W+'|'+H+'|'+R+'|'+night;if(!PG.g||PG.g.k!==gk){const sea=ctx.createRadialGradient(cx-R*.35,cy-R*.35,R*.1,cx,cy,R);sea.addColorStop(0,st.sea);sea.addColorStop(1,st.sea2); let sh;if(night){sh=ctx.createRadialGradient(cx-R*.7,cy-R*.35,R*.2,cx-R*.7,cy-R*.35,R*2);sh.addColorStop(0,'rgba(0,0,20,0)');sh.addColorStop(.45,'rgba(0,0,20,0)');sh.addColorStop(1,'rgba(0,0,20,.62)');} else{sh=ctx.createRadialGradient(cx-R*.45,cy-R*.5,0,cx-R*.45,cy-R*.5,R*.6);sh.addColorStop(0,'rgba(255,255,255,.35)');sh.addColorStop(1,'rgba(255,255,255,0)');} PG.g={k:gk,sea,sh};} ctx.clearRect(0,0,W,H); if(night){ctx.fillStyle=st.bg;ctx.fillRect(0,0,W,H); // Twinkling stars, in four brightness steps: four fills instead of one per star. for(let b=0;b<4;b++){ctx.beginPath();for(const s of PG_STAR){const a=Math.abs(Math.sin(t*0.7+s[2]));if(Math.min(3,a*4|0)===b)ctx.rect(s[0]*W,s[1]*H,1.4,1.4);}ctx.fillStyle=`rgba(255,255,255,${.25+b*.2})`;ctx.fill();}} const sats=(front)=>{for(const c of PG.sm){const a=c.pos,col=st.sat[c.col%4],r=front?3.2:2.2;let any=false; if(night){ctx.beginPath();for(let i=0;i=0)!==front||pgHid(a,i))continue;const x=cx+R*a[i+1],y=cy-R*a[i+2];ctx.moveTo(x+r*2.4,y);ctx.arc(x,y,r*2.4,0,7);any=true;} if(!any)continue;ctx.fillStyle=col;ctx.globalAlpha=front?.25:.1;ctx.fill(); ctx.beginPath();for(let i=0;i=0)!==front||pgHid(a,i))continue;const x=cx+R*a[i+1],y=cy-R*a[i+2];ctx.moveTo(x+r*.8,y);ctx.arc(x,y,r*.8,0,7);}ctx.globalAlpha=front?1:.45;ctx.fill();ctx.globalAlpha=1;} else{ctx.beginPath();for(let i=0;i=0)!==front||pgHid(a,i))continue;const x=cx+R*a[i+1],y=cy-R*a[i+2];ctx.moveTo(x+r,y);ctx.arc(x,y,r,0,7);any=true;} if(!any)continue;ctx.globalAlpha=front?1:.45;ctx.fillStyle=col;ctx.fill();ctx.lineWidth=front?1.4:1;ctx.strokeStyle=st.ink;ctx.stroke();ctx.globalAlpha=1;}}}; sats(false); // The Earth: offset ink shadow, sea, graticule, land, then light and shade. if(!night){ctx.beginPath();ctx.arc(cx+6,cy+6,R,0,7);ctx.fillStyle=st.ink;ctx.fill();} ctx.beginPath();ctx.arc(cx,cy,R,0,7);ctx.fillStyle=PG.g.sea;ctx.fill(); ctx.save();ctx.beginPath();ctx.arc(cx,cy,R,0,7);ctx.clip(); const buf=pgBuf(61);ctx.beginPath();for(const g of PG_GRID){const n=g.length/3;pgRot(g,buf,n);let on=false; for(let i=0;i=0){any=true;break;}if(!any)continue; for(let i=0;i0.08)ds.push(d);} ctx.beginPath();let nb=0;for(const d of ds){if(!d.up)continue;const g=d.s;let best=-1,bc=null,bd=0.3; for(const c of PG.sm){const a=c.pos;for(let i=0;i=0){ctx.moveTo(cx+R*g[1],cy-R*g[2]);ctx.lineTo(cx+R*bc[best+1],cy-R*bc[best+2]);nb++;}} if(nb){ctx.lineWidth=1.6;ctx.setLineDash([3,3]);ctx.lineDashOffset=-t*8;ctx.strokeStyle=st.beam[0];ctx.stroke();ctx.lineDashOffset=-t*8+3;ctx.strokeStyle=st.beam[1];ctx.stroke();ctx.setLineDash([]);ctx.lineDashOffset=0;} // Laser links between neighbouring pies, along the plane and to the next plane, near side only. for(const c of PG.sm){if(!c.las||c.cnt<2)continue;const a=c.pos,K=c.K,n=c.cnt;ctx.beginPath(); for(let i=0;i=0){ctx.moveTo(x,y);ctx.lineTo(cx+R*a[j*3+1],cy-R*a[j*3+2]);} const q=((p+1)%c.P)*K+k;if(i%2===0&&q=0){ctx.moveTo(x,y);ctx.lineTo(cx+R*a[q*3+1],cy-R*a[q*3+2]);}} ctx.strokeStyle=st.laser;ctx.lineWidth=1.2;ctx.globalAlpha=.55;ctx.stroke();ctx.globalAlpha=1;} for(const up of [true,false]){ctx.beginPath();let any=false;for(const d of ds){if(d.up!==up)continue;const [x,y]=P(d.s,0);ctx.moveTo(x+4.5,y);ctx.arc(x,y,4.5,0,7);any=true;} if(any){ctx.fillStyle=up?st.dish:st.off;ctx.fill();ctx.lineWidth=1.5;ctx.strokeStyle=night?'#0B1030':st.ink;ctx.stroke();}} sats(true); // Launch pads, and a rocket climbing from each launch every five seconds, staggered. ctx.beginPath();let np=0;for(const d of PG.pads){const b=pgRot(d.v,d.s||(d.s=new Float32Array(3)),1);if(b[0]<0.05)continue;const [x,y]=P(b,0);ctx.moveTo(x-4,y+3);ctx.lineTo(x,y-5);ctx.lineTo(x+4,y+3);ctx.closePath();np++;} if(np){ctx.fillStyle='#FF8A3D';ctx.fill();ctx.lineWidth=1.3;ctx.strokeStyle=night?'#0B1030':st.ink;ctx.stroke();} const tv=new Float32Array(33),tp=new Float32Array(33); PG.lch.forEach((L,li)=>{const ph=((t+li*2.3)%5)/2.6;if(ph>1)return;const b=pgRot(L.v,tp,1);if(b[0]<0.05)return; // Climb and lean east: the trail is the launch's path so far. const la=Math.asin(L.v[2])/PG_D,lo=Math.atan2(L.v[1],L.v[0])/PG_D;for(let s=0;s<=10;s++){const f=ph*s/10,r=1+f*f*0.7;pgUnit(lo+f*f*28,la+f*6,tv,s*3);tv[s*3]*=r;tv[s*3+1]*=r;tv[s*3+2]*=r;} pgRot(tv,tp,11);ctx.beginPath();for(let s=0;s<=10;s++){const x=cx+R*tp[s*3+1],y=cy-R*tp[s*3+2];if(s)ctx.lineTo(x,y);else ctx.moveTo(x,y);} ctx.lineCap='round';ctx.lineWidth=4;ctx.strokeStyle='rgba(255,138,61,.85)';ctx.stroke();ctx.lineWidth=1.5;ctx.strokeStyle='#FFD23F';ctx.stroke();ctx.lineCap='butt'; ctx.beginPath();ctx.arc(cx+R*tp[31],cy-R*tp[32],3.5,0,7);ctx.fillStyle='#FFFFFF';ctx.fill();ctx.strokeStyle=night?'#0B1030':st.ink;ctx.lineWidth=1.2;ctx.stroke();});} function pgLoop(now){PG.raf=0;if(!PG.cv)return;pgFrame(now,false);if(PG.cv)PG.raf=requestAnimationFrame(pgLoop);} // Drag to turn the Earth; it keeps turning a little after you let go. function pgDown(e){const cv=PG.cv;if(!cv)return;PG.drag={x:e.clientX,y:e.clientY,yaw:PG.yaw,tilt:PG.tilt,lx:e.clientX,lt:performance.now(),v:0};PG.vy=0;try{cv.setPointerCapture(e.pointerId);}catch(_){}cv.style.cursor='grabbing';} function pgMove(e){const d=PG.drag;if(!d)return;PG.yaw=d.yaw+(e.clientX-d.x)*0.4;PG.tilt=Math.max(-60,Math.min(60,d.tilt+(e.clientY-d.y)*0.3)); const now=performance.now(),dt=(now-d.lt)/1000;if(dt>0.008){d.v=(e.clientX-d.lx)*0.4/dt;d.lx=e.clientX;d.lt=now;}PG.dirty=true;} function pgUp(){const d=PG.drag;if(!d)return;PG.drag=null;PG.vy=performance.now()-d.lt<120?Math.max(-240,Math.min(240,d.v)):0;if(PG.cv)PG.cv.style.cursor='grab';} function pieGlobeOpen(canvas,getData){ if(!canvas||!canvas.getContext)return false; if(PG.cv===canvas){PG.get=getData||null;return true;} if(PG.cv)pieGlobeClose(); const ctx=canvas.getContext('2d');if(!ctx)return false; pgVecs();pgNightPref(); Object.assign(PG,{cv:canvas,ctx,get:getData||null,last:0,dAt:-1e9,sig:'',sm:[],dish:[],pads:[],lch:[],g:null,dirty:true,drag:null,vy:0,anim:animOn()}); PG.on=[['pointerdown',pgDown],['pointermove',pgMove],['pointerup',pgUp],['pointercancel',pgUp],['lostpointercapture',pgUp]]; for(const [k,f] of PG.on)canvas.addEventListener(k,f); canvas.style.touchAction='none';canvas.style.cursor='grab'; PG.raf=requestAnimationFrame(pgLoop);return true;} function pieGlobeClose(){ if(PG.raf)cancelAnimationFrame(PG.raf);PG.raf=0; const cv=PG.cv;if(cv&&PG.on)for(const [k,f] of PG.on)cv.removeEventListener(k,f); Object.assign(PG,{cv:null,ctx:null,get:null,on:null,drag:null,sm:[],dish:[],pads:[],lch:[],g:null,sig:'',buf:null});} // Night view: a per-device preference, like the other display settings. function pieGlobeNight(on){PG.night=!!on;PG.g=null;PG.dirty=true;try{localStorage.setItem('sot-globe',PG.night?'night':'day');}catch(e){} for(const b of document.querySelectorAll('.pgnight')){b.setAttribute('aria-pressed',String(PG.night));b.setAttribute('aria-label',pgNightLbl());b.textContent=PG.night?'☀️':'🌙';} return PG.night;} const pgNightLbl=()=>pgNightPref()?tr('pie_globe.switch_to_day_view'):tr('pie_globe.switch_to_night_view'); const pieGlobeBtn=()=>``; // What the panel puts where the globe goes; then it calls pieGlobeOpen(that canvas, pieGlobeData). const pieGlobeHtml=()=>`
${pieGlobeBtn()}
`;