GODSIGH/js/lib.js
jing 7356d62381 phase 1: globe core — viewer, dual basemaps, clock, HUD, layer contract
- CesiumJS viewer (token-free, baseLayer:false), Carto dark + EOX Sentinel-2
- clock spans now +/-6h CLAMPED; built-in timeline/animation = the space-time slider
- HUD: brand, LIVE/SCRUBBED chip, Data/Photo mode toggle, layer rows, credits
- lighting is mode-aware: OFF in Data (high-contrast asset map), ON in Photo
  (realistic day/night terminator) — refines SPEC 6.1's global-lighting note
- main.js defines the layer-factory contract + dynamic loader; layers land next
- serve.py now forwards OpenSky X-Rate-Limit-* headers for the aircraft status line

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-13 12:06:05 +10:00

72 lines
2.0 KiB
JavaScript

// Shared helpers used across layers. Cesium and satellite are CDN globals.
const Cesium = window.Cesium;
// Build an offscreen canvas glyph for a BillboardCollection.
export function makeGlyphCanvas(size, drawFn) {
const c = document.createElement('canvas');
c.width = size;
c.height = size;
drawFn(c.getContext('2d'), size);
return c;
}
// White upward-pointing triangle (aircraft). Tinted per-billboard via .color.
export function aircraftGlyph() {
return makeGlyphCanvas(32, (g) => {
g.fillStyle = '#ffffff';
g.beginPath();
g.moveTo(16, 2);
g.lineTo(28, 30);
g.lineTo(16, 23);
g.lineTo(4, 30);
g.closePath();
g.fill();
});
}
// White diamond (ships).
export function shipGlyph() {
return makeGlyphCanvas(28, (g) => {
g.fillStyle = '#ffffff';
g.beginPath();
g.moveTo(14, 2);
g.lineTo(26, 14);
g.lineTo(14, 26);
g.lineTo(2, 14);
g.closePath();
g.fill();
});
}
// Great-circle initial bearing, degrees (0 = north, clockwise).
export function bearingDeg(lat1, lon1, lat2, lon2) {
const toR = Math.PI / 180;
const p1 = lat1 * toR;
const p2 = lat2 * toR;
const dl = (lon2 - lon1) * toR;
const y = Math.sin(dl) * Math.cos(p2);
const x = Math.cos(p1) * Math.sin(p2) - Math.sin(p1) * Math.cos(p2) * Math.cos(dl);
return (Math.atan2(y, x) * 180 / Math.PI + 360) % 360;
}
// Aircraft altitude → band color.
export function altitudeColor(CONFIG, altMeters) {
const c = CONFIG.colors;
if (altMeters == null) return c.aircraftHigh;
if (altMeters < 3000) return c.aircraftLow;
if (altMeters < 9000) return c.aircraftMid;
return c.aircraftHigh;
}
// CSS hex → Cesium.Color, optional alpha.
export function cz(hex, alpha) {
const col = Cesium.Color.fromCssColorString(hex);
return alpha == null ? col : col.withAlpha(alpha);
}
// Billboard screen-space rotation (CCW) for a compass heading (CW from north).
export function headingToRotation(headingDeg) {
return -Cesium.Math.toRadians(headingDeg || 0);
}