mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 10:12:18 +00:00
Merge remote-tracking branch 'origin/main' into german/cctv-camera-counts
# Conflicts: # src/layers/cctv/lifecycle.test.mjs
This commit is contained in:
commit
2b7a6b0c63
1463 files changed
+48705
-10313
No files matched your search
@@ -0,0 +1,428 @@
|
||||
import { registerWorldOverlayPaintLane } from '../../overlays/worldOverlay.js';
|
||||
import { fromDegrees } from '../../geo/ellipsoid.js';
|
||||
import {
|
||||
earthquakeContourRings,
|
||||
earthquakeContourScale,
|
||||
earthquakeContourShape,
|
||||
earthquakeRippleRings,
|
||||
EARTHQUAKE_RIPPLE_PERIOD_MS,
|
||||
} from '../../layers/earthquakes/model.js';
|
||||
|
||||
/** Points around each contour line: smooth at the largest disc on screen. */
|
||||
export const RIPPLE_RING_SEGMENTS = 96;
|
||||
/** An index contour's width, and every other line's. */
|
||||
const INDEX_WIDTH_PX = 2;
|
||||
const LINE_WIDTH_PX = 1.1;
|
||||
/**
|
||||
* The overlay lane the lines paint in: the magnitude labels' own. A lane's
|
||||
* custom painters run before its entries, so the lines sit under the labels
|
||||
* and under the selected quake's card.
|
||||
*/
|
||||
export const RIPPLE_PAINT_LANE = 'ambient-label';
|
||||
/**
|
||||
* The selected disc's fill, [offset, alpha] from the epicentre (0) to its
|
||||
* outer circle (1): strongest in the middle, fading out to nothing at the
|
||||
* edge.
|
||||
*/
|
||||
export const DISC_FADE = Object.freeze([
|
||||
[0, 0.42],
|
||||
[0.35, 0.26],
|
||||
[0.75, 0.1],
|
||||
[1, 0],
|
||||
]);
|
||||
/**
|
||||
* Each contour wave's fill, out to its own line: clear inside, tinting
|
||||
* toward the line; times the wave's own fade.
|
||||
*/
|
||||
export const WAVE_FADE = Object.freeze([
|
||||
[0, 0],
|
||||
[0.55, 0],
|
||||
[1, 0.2],
|
||||
]);
|
||||
|
||||
/**
|
||||
* `rgb(r,g,b)` (or `rgba`) as a function of alpha, for gradient stops; any
|
||||
* other colour keeps its own alpha.
|
||||
*/
|
||||
export function cssWithAlpha(color) {
|
||||
const m = /^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/.exec(String(color));
|
||||
if (!m) return () => color;
|
||||
const rgb = `${m[1]},${m[2]},${m[3]}`;
|
||||
return (alpha) => `rgba(${rgb},${Math.max(0, Math.min(1, alpha))})`;
|
||||
}
|
||||
|
||||
/**
|
||||
* How many times the lines run out to the edge after a quake is selected
|
||||
* before they rest as still contours and stop asking for frames: earthquakes
|
||||
* otherwise hold no render (docs/CURRENT-STATE.md, "Earthquake discs are
|
||||
* STATIC geometry"), and a quake can stay selected for hours.
|
||||
*/
|
||||
export const RIPPLE_ACTIVE_CYCLES = 3;
|
||||
|
||||
let reducedMotionQuery;
|
||||
/**
|
||||
* The viewer's reduced-motion setting, live: the media query is kept, so a
|
||||
* change mid-session is read at the next paint and tick.
|
||||
*/
|
||||
const reducedMotion = () => {
|
||||
try {
|
||||
reducedMotionQuery ??=
|
||||
globalThis.matchMedia?.('(prefers-reduced-motion: reduce)') ?? null;
|
||||
return reducedMotionQuery?.matches === true;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
/** How often the rings re-read the ground height under the quake. */
|
||||
const HEIGHT_READ_INTERVAL_MS = 500;
|
||||
/**
|
||||
* A ground height the rings accept, in metres: the Dead Sea shore to above
|
||||
* Everest. Anything else is a bad sample (a hit on something drawn above
|
||||
* the ground, or a broken tile), and the rings stay where they are.
|
||||
*/
|
||||
const groundHeightOk = (h) => Number.isFinite(h) && h >= -500 && h <= 9000;
|
||||
|
||||
/**
|
||||
* The selected earthquake's contour lines, painted on the world overlay:
|
||||
* wavy closed lines, shaped by the quake's id (earthquakeContourShape), run
|
||||
* from the marker out to the disc's edge and fade there, one after another,
|
||||
* for RIPPLE_ACTIVE_CYCLES runs after a quake is selected and only while it
|
||||
* is on screen, then rest as still contours. They never cross, and every third is
|
||||
* a heavier index contour, as on a topographic map. Nothing is drawn, and no
|
||||
* frame is asked for, while no quake is selected. With reduced motion the
|
||||
* lines hold still.
|
||||
*
|
||||
* A line is drawn in the tangent plane at the epicentre: at the largest
|
||||
* disc (M6, 64 km) it sits about 320 m above the curved ground at its edge
|
||||
* (64² / 2R), about 1.5 px off the drawn disc at the 220 km, -45° framing a
|
||||
* label click flies to (900 px tall, 35° field of view); less for smaller
|
||||
* quakes, as the offset grows with the square of the radius.
|
||||
* @param {object} [deps]
|
||||
* @param {Function} [deps.registerLane] `registerWorldOverlayPaintLane`.
|
||||
* @param {() => number} [deps.now] Steady clock, ms.
|
||||
* @param {(cb: Function) => number} [deps.requestFrame]
|
||||
* @param {(handle: number) => void} [deps.cancelFrame]
|
||||
* @param {() => boolean} [deps.prefersReducedMotion]
|
||||
* @returns {{show: Function, hide: Function, dispose: Function}}
|
||||
*/
|
||||
export function createEarthquakeRipples({
|
||||
registerLane = registerWorldOverlayPaintLane,
|
||||
now = () => globalThis.performance?.now?.() ?? Date.now(),
|
||||
requestFrame = (cb) => globalThis.requestAnimationFrame?.(cb) ?? null,
|
||||
cancelFrame = (handle) => globalThis.cancelAnimationFrame?.(handle),
|
||||
prefersReducedMotion = reducedMotion,
|
||||
} = {}) {
|
||||
let lane = null;
|
||||
let target = null;
|
||||
let frameHandle = null;
|
||||
// When the running lines settle into still contours (the selection's
|
||||
// bound), and whether the last paint found the quake on screen.
|
||||
let startedAt = 0;
|
||||
let settlesAt = 0;
|
||||
let onScreen = false;
|
||||
/** Whether the lines run now: in the window, and motion allowed. */
|
||||
const running = () => now() < settlesAt && !prefersReducedMotion();
|
||||
// Unit offsets around the line in the epicentre's tangent plane, packed
|
||||
// x, y, z per point: north·cos + east·sin.
|
||||
const offsets = new Float64Array((RIPPLE_RING_SEGMENTS + 1) * 3);
|
||||
const angles = new Float64Array(RIPPLE_RING_SEGMENTS + 1);
|
||||
for (let i = 0; i <= RIPPLE_RING_SEGMENTS; i++)
|
||||
angles[i] = (2 * Math.PI * i) / RIPPLE_RING_SEGMENTS;
|
||||
|
||||
// Screen points, reused every frame.
|
||||
const at = { x: 0, y: 0 };
|
||||
const centerAt = { x: 0, y: 0 };
|
||||
const eastAt = { x: 0, y: 0 };
|
||||
const northAt = { x: 0, y: 0 };
|
||||
|
||||
/** Project an ECEF point to CSS pixels; false when it is behind the eye. */
|
||||
function project(frame, px, py, pz, out) {
|
||||
const m = frame.viewProjectionMatrix;
|
||||
const w = m[3] * px + m[7] * py + m[11] * pz + m[15];
|
||||
if (w <= 0) return false;
|
||||
out.x =
|
||||
((m[0] * px + m[4] * py + m[8] * pz + m[12]) / w / 2 + 0.5) * frame.width;
|
||||
out.y =
|
||||
(0.5 - (m[1] * px + m[5] * py + m[9] * pz + m[13]) / w / 2) *
|
||||
frame.height;
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Trace one closed line at `fraction` of the disc's radius (`shaped`:
|
||||
* as a contour, else the disc's own circle). True when all of it is in
|
||||
* front of the eye, so it can be filled.
|
||||
*/
|
||||
function trace(frame, fraction, shaped) {
|
||||
const { ctx } = frame;
|
||||
const center = target.center;
|
||||
const reach = fraction * target.radiusM;
|
||||
ctx.beginPath();
|
||||
let pen = false;
|
||||
let whole = true;
|
||||
for (let i = 0; i <= RIPPLE_RING_SEGMENTS; i++) {
|
||||
// The last point repeats the first angle, so the line closes.
|
||||
const r = shaped
|
||||
? reach * earthquakeContourScale(target.shape, angles[i], fraction)
|
||||
: reach;
|
||||
const visible = project(
|
||||
frame,
|
||||
center.x + offsets[i * 3] * r,
|
||||
center.y + offsets[i * 3 + 1] * r,
|
||||
center.z + offsets[i * 3 + 2] * r,
|
||||
at,
|
||||
);
|
||||
if (!visible) {
|
||||
pen = false;
|
||||
whole = false;
|
||||
continue;
|
||||
}
|
||||
if (pen) ctx.lineTo(at.x, at.y);
|
||||
else ctx.moveTo(at.x, at.y);
|
||||
pen = true;
|
||||
}
|
||||
return whole;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill the traced path with a radial gradient laid on the ground: the
|
||||
* canvas places a path when it is traced and a gradient when it fills, so
|
||||
* under the disc's ground-to-screen transform the gradient's circle
|
||||
* becomes the disc's ellipse on screen while the path stays exact.
|
||||
* @param {Array<[number, number]>} stops [offset, alpha], out to `reach`.
|
||||
*/
|
||||
function fillGround(ctx, reach, stops) {
|
||||
ctx.save();
|
||||
ctx.transform(
|
||||
eastAt.x - centerAt.x,
|
||||
eastAt.y - centerAt.y,
|
||||
northAt.x - centerAt.x,
|
||||
northAt.y - centerAt.y,
|
||||
centerAt.x,
|
||||
centerAt.y,
|
||||
);
|
||||
const gradient = ctx.createRadialGradient(0, 0, 0, 0, 0, reach);
|
||||
for (const [offset, alpha] of stops)
|
||||
gradient.addColorStop(offset, target.rgba(alpha));
|
||||
ctx.fillStyle = gradient;
|
||||
ctx.globalAlpha = 1;
|
||||
ctx.fill();
|
||||
ctx.restore();
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether any of the disc is in the viewport: its centre's screen point,
|
||||
* padded by its larger screen radius (or, seen edge-on, the centre alone).
|
||||
*/
|
||||
function discOnScreen(frame, c, e, n) {
|
||||
if (!Number.isFinite(c.x) || !Number.isFinite(c.y)) return false;
|
||||
const pad = Math.max(
|
||||
Math.hypot(e.x - c.x, e.y - c.y) || 0,
|
||||
Math.hypot(n.x - c.x, n.y - c.y) || 0,
|
||||
);
|
||||
return (
|
||||
c.x + pad >= 0 &&
|
||||
c.x - pad <= frame.width &&
|
||||
c.y + pad >= 0 &&
|
||||
c.y - pad <= frame.height
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Re-read the ground height under the quake, at most twice a second while
|
||||
* painting: the ground loads and refines as the camera comes in, and the
|
||||
* rings move onto it once it differs by more than a metre.
|
||||
*/
|
||||
function settleHeight() {
|
||||
if (!target.heightAt) return;
|
||||
const at = now();
|
||||
if (at - target.heightReadAt < HEIGHT_READ_INTERVAL_MS) return;
|
||||
target.heightReadAt = at;
|
||||
let height = null;
|
||||
try {
|
||||
height = target.heightAt();
|
||||
} catch {
|
||||
height = null;
|
||||
}
|
||||
if (!groundHeightOk(height) || Math.abs(height - target.heightM) <= 1)
|
||||
return;
|
||||
target.heightM = height;
|
||||
target.center = fromDegrees(target.lon, target.lat, height);
|
||||
}
|
||||
|
||||
function paint(frame) {
|
||||
if (!target) return;
|
||||
const { ctx } = frame;
|
||||
settleHeight();
|
||||
const center = target.center;
|
||||
onScreen = false;
|
||||
if (frame.occluder?.isPointVisible?.(center) === false) return;
|
||||
const R = target.radiusM;
|
||||
// The disc's ground frame on screen: its centre, and a radius east and
|
||||
// north. Seen edge-on (or partly behind the eye) it is not filled.
|
||||
const centerOk = project(frame, center.x, center.y, center.z, centerAt);
|
||||
const eastOk = project(
|
||||
frame,
|
||||
center.x + target.east.x * R,
|
||||
center.y + target.east.y * R,
|
||||
center.z + target.east.z * R,
|
||||
eastAt,
|
||||
);
|
||||
const northOk = project(
|
||||
frame,
|
||||
center.x + target.north.x * R,
|
||||
center.y + target.north.y * R,
|
||||
center.z + target.north.z * R,
|
||||
northAt,
|
||||
);
|
||||
let fills = centerOk && eastOk && northOk;
|
||||
if (fills) {
|
||||
const area =
|
||||
(eastAt.x - centerAt.x) * (northAt.y - centerAt.y) -
|
||||
(eastAt.y - centerAt.y) * (northAt.x - centerAt.x);
|
||||
fills = Math.abs(area) > 1;
|
||||
}
|
||||
onScreen =
|
||||
centerOk &&
|
||||
discOnScreen(
|
||||
frame,
|
||||
centerAt,
|
||||
eastOk ? eastAt : centerAt,
|
||||
northOk ? northAt : centerAt,
|
||||
);
|
||||
// The disc: strongest at the epicentre, fading out to its outer circle.
|
||||
if (fills && trace(frame, 1, false)) fillGround(ctx, 1, DISC_FADE);
|
||||
const animate = running();
|
||||
const rings = animate
|
||||
? earthquakeRippleRings(now() - startedAt)
|
||||
: earthquakeContourRings();
|
||||
// Brought back into view while the window lasts: run again.
|
||||
if (animate && onScreen && frameHandle === null && lane)
|
||||
frameHandle = requestFrame(tick);
|
||||
ctx.strokeStyle = target.color;
|
||||
ctx.lineJoin = 'round';
|
||||
for (const { fraction, alpha, index } of rings) {
|
||||
if (!(alpha > 0.01)) continue;
|
||||
const whole = trace(frame, fraction, true);
|
||||
// Each wave: clear inside, tinting toward its own line, as it fades.
|
||||
if (fills && whole)
|
||||
fillGround(
|
||||
ctx,
|
||||
fraction,
|
||||
WAVE_FADE.map(([offset, a]) => [offset, a * alpha]),
|
||||
);
|
||||
ctx.lineWidth = index ? INDEX_WIDTH_PX : LINE_WIDTH_PX;
|
||||
ctx.globalAlpha = alpha;
|
||||
ctx.stroke();
|
||||
}
|
||||
ctx.globalAlpha = 1;
|
||||
}
|
||||
|
||||
/** Ask for one more frame while the lines run (one request at a time). */
|
||||
function tick() {
|
||||
frameHandle = null;
|
||||
if (!target || !lane) return;
|
||||
// Settled, or out of sight: one last paint draws the still contours
|
||||
// (or nothing), and no more frames are asked for. A paint that finds it
|
||||
// on screen again inside the window starts the frames again.
|
||||
if (!running() || !onScreen) {
|
||||
lane.requestPaint();
|
||||
return;
|
||||
}
|
||||
lane.requestPaint();
|
||||
frameHandle = requestFrame(tick);
|
||||
}
|
||||
|
||||
function stopFrames() {
|
||||
if (frameHandle !== null) cancelFrame(frameHandle);
|
||||
frameHandle = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the contour lines around one quake, replacing any other.
|
||||
* @param {{id: string, lon: number, lat: number, heightM?: number, heightAt?: () => number|null, radiusM: number, color: string}} next
|
||||
* @param {{restart?: boolean}} [options] `restart: false` updates the
|
||||
* same quake in place (a feed refresh) without running it again.
|
||||
*/
|
||||
function show(next, { restart = true } = {}) {
|
||||
if (
|
||||
!next ||
|
||||
!Number.isFinite(next.lon) ||
|
||||
!Number.isFinite(next.lat) ||
|
||||
!(next.radiusM > 0)
|
||||
) {
|
||||
hide();
|
||||
return;
|
||||
}
|
||||
const sameQuake = target?.id === next.id;
|
||||
const lon = (next.lon * Math.PI) / 180;
|
||||
const lat = (next.lat * Math.PI) / 180;
|
||||
const [sinLon, cosLon] = [Math.sin(lon), Math.cos(lon)];
|
||||
const [sinLat, cosLat] = [Math.sin(lat), Math.cos(lat)];
|
||||
for (let i = 0; i <= RIPPLE_RING_SEGMENTS; i++) {
|
||||
const [c, s] = [Math.cos(angles[i]), Math.sin(angles[i])];
|
||||
offsets[i * 3] = c * -sinLat * cosLon + s * -sinLon;
|
||||
offsets[i * 3 + 1] = c * -sinLat * sinLon + s * cosLon;
|
||||
offsets[i * 3 + 2] = c * cosLat;
|
||||
}
|
||||
target = {
|
||||
id: next.id,
|
||||
// On the ground the disc is draped on: the height known now, then
|
||||
// re-read while painting (see `settleHeight`), since a label's fly-to
|
||||
// starts from far away, before that ground has loaded.
|
||||
lon: next.lon,
|
||||
lat: next.lat,
|
||||
heightM: groundHeightOk(next.heightM) ? next.heightM : 0,
|
||||
heightAt: typeof next.heightAt === 'function' ? next.heightAt : null,
|
||||
heightReadAt: -Infinity,
|
||||
center: fromDegrees(
|
||||
next.lon,
|
||||
next.lat,
|
||||
groundHeightOk(next.heightM) ? next.heightM : 0,
|
||||
),
|
||||
radiusM: next.radiusM,
|
||||
color: next.color || 'rgb(255,165,0)',
|
||||
shape: earthquakeContourShape(next.id),
|
||||
rgba: cssWithAlpha(next.color || 'rgb(255,165,0)'),
|
||||
// Angle 0 is north and a quarter turn is east (see `offsets`).
|
||||
north: { x: offsets[0], y: offsets[1], z: offsets[2] },
|
||||
east: {
|
||||
x: offsets[(RIPPLE_RING_SEGMENTS / 4) * 3],
|
||||
y: offsets[(RIPPLE_RING_SEGMENTS / 4) * 3 + 1],
|
||||
z: offsets[(RIPPLE_RING_SEGMENTS / 4) * 3 + 2],
|
||||
},
|
||||
};
|
||||
if (restart || !sameQuake) {
|
||||
startedAt = now();
|
||||
settlesAt =
|
||||
startedAt + RIPPLE_ACTIVE_CYCLES * EARTHQUAKE_RIPPLE_PERIOD_MS;
|
||||
// Assume it is in view until a paint says otherwise.
|
||||
onScreen = true;
|
||||
}
|
||||
lane ??= registerLane(RIPPLE_PAINT_LANE, paint, {
|
||||
id: 'earthquake-ripples',
|
||||
});
|
||||
lane.setActive(true);
|
||||
lane.requestPaint();
|
||||
// A refresh while the disc is out of sight asks for no frame; a paint
|
||||
// that finds it on screen again starts them (a restart sets onScreen).
|
||||
if (running() && onScreen && frameHandle === null)
|
||||
frameHandle = requestFrame(tick);
|
||||
}
|
||||
|
||||
function hide() {
|
||||
stopFrames();
|
||||
if (!target) return;
|
||||
target = null;
|
||||
lane?.setActive(false);
|
||||
}
|
||||
|
||||
function dispose() {
|
||||
hide();
|
||||
lane?.unregister();
|
||||
lane = null;
|
||||
}
|
||||
|
||||
return { show, hide, dispose };
|
||||
}
|
||||
@@ -0,0 +1,452 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import {
|
||||
DISC_FADE,
|
||||
cssWithAlpha,
|
||||
RIPPLE_ACTIVE_CYCLES,
|
||||
RIPPLE_PAINT_LANE,
|
||||
RIPPLE_RING_SEGMENTS,
|
||||
createEarthquakeRipples,
|
||||
} from './earthquakeRipples.js';
|
||||
import {
|
||||
EARTHQUAKE_RIPPLE_PERIOD_MS,
|
||||
EARTHQUAKE_RIPPLE_RINGS,
|
||||
} from '../../layers/earthquakes/model.js';
|
||||
import { fromDegrees } from '../../geo/ellipsoid.js';
|
||||
|
||||
/** A paint lane, frame clock and canvas the test steps by hand. */
|
||||
function harness({ reducedMotion = false } = {}) {
|
||||
let motionReduced = reducedMotion;
|
||||
const lanes = [];
|
||||
const frames = new Map();
|
||||
let nextFrame = 1;
|
||||
let clock = 0;
|
||||
const ripples = createEarthquakeRipples({
|
||||
registerLane(laneId, painter, options) {
|
||||
const lane = {
|
||||
laneId,
|
||||
painter,
|
||||
options,
|
||||
active: false,
|
||||
paints: 0,
|
||||
unregistered: false,
|
||||
setActive(next) {
|
||||
lane.active = next;
|
||||
},
|
||||
requestPaint() {
|
||||
lane.paints++;
|
||||
},
|
||||
unregister() {
|
||||
lane.unregistered = true;
|
||||
},
|
||||
};
|
||||
lanes.push(lane);
|
||||
return lane;
|
||||
},
|
||||
now: () => clock,
|
||||
requestFrame(cb) {
|
||||
const handle = nextFrame++;
|
||||
frames.set(handle, cb);
|
||||
return handle;
|
||||
},
|
||||
cancelFrame: (handle) => frames.delete(handle),
|
||||
prefersReducedMotion: () => motionReduced,
|
||||
});
|
||||
const strokes = [];
|
||||
const fills = [];
|
||||
let path = null;
|
||||
const ctx = {
|
||||
globalAlpha: 1,
|
||||
beginPath() {
|
||||
path = { moves: 0, lines: 0 };
|
||||
},
|
||||
moveTo() {
|
||||
path.moves++;
|
||||
},
|
||||
lineTo() {
|
||||
path.lines++;
|
||||
},
|
||||
stroke() {
|
||||
strokes.push({
|
||||
...path,
|
||||
alpha: ctx.globalAlpha,
|
||||
color: ctx.strokeStyle,
|
||||
width: ctx.lineWidth,
|
||||
});
|
||||
},
|
||||
// Fills record their path, gradient and the transform they fill under.
|
||||
saved: 0,
|
||||
transformed: null,
|
||||
save() {
|
||||
ctx.saved++;
|
||||
},
|
||||
restore() {
|
||||
ctx.saved--;
|
||||
ctx.transformed = null;
|
||||
},
|
||||
transform(...args) {
|
||||
ctx.transformed = args;
|
||||
},
|
||||
createRadialGradient(...circles) {
|
||||
const gradient = { circles, stops: [] };
|
||||
gradient.addColorStop = (offset, color) =>
|
||||
gradient.stops.push([offset, color]);
|
||||
return gradient;
|
||||
},
|
||||
fill() {
|
||||
fills.push({
|
||||
...path,
|
||||
gradient: ctx.fillStyle,
|
||||
transform: ctx.transformed,
|
||||
});
|
||||
},
|
||||
};
|
||||
// w = 1 everywhere: every point is in front of the eye.
|
||||
const matrix = new Float64Array(16);
|
||||
matrix[0] = 1e-7;
|
||||
matrix[5] = 1e-7;
|
||||
matrix[15] = 1;
|
||||
const frame = (visible = true) => ({
|
||||
ctx,
|
||||
width: 800,
|
||||
height: 600,
|
||||
viewProjectionMatrix: matrix,
|
||||
occluder: { isPointVisible: () => visible },
|
||||
});
|
||||
return {
|
||||
ripples,
|
||||
lanes,
|
||||
frames,
|
||||
strokes,
|
||||
fills,
|
||||
frame,
|
||||
matrix,
|
||||
setReducedMotion(next) {
|
||||
motionReduced = next;
|
||||
},
|
||||
advance(ms) {
|
||||
clock += ms;
|
||||
},
|
||||
/** Run the pending animation frames once. */
|
||||
step() {
|
||||
const pending = [...frames.entries()];
|
||||
frames.clear();
|
||||
for (const [, cb] of pending) cb();
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const target = {
|
||||
id: 'ak1',
|
||||
lon: -153.3,
|
||||
lat: 60,
|
||||
radiusM: 32000,
|
||||
color: 'rgb(255,0,0)',
|
||||
};
|
||||
|
||||
test('nothing is registered or animated until a quake is selected', () => {
|
||||
const h = harness();
|
||||
assert.equal(h.lanes.length, 0);
|
||||
assert.equal(h.frames.size, 0);
|
||||
h.ripples.hide();
|
||||
assert.equal(h.frames.size, 0);
|
||||
});
|
||||
|
||||
test('a selected quake animates one ring set, one frame request at a time', () => {
|
||||
const h = harness();
|
||||
h.ripples.show(target);
|
||||
const [lane] = h.lanes;
|
||||
assert.equal(lane.laneId, RIPPLE_PAINT_LANE);
|
||||
assert.equal(RIPPLE_PAINT_LANE, 'ambient-label', 'under the labels');
|
||||
assert.equal(lane.active, true);
|
||||
assert.equal(h.frames.size, 1);
|
||||
h.ripples.show({ ...target, id: 'ak2' });
|
||||
assert.equal(h.lanes.length, 1, 'the lane is reused');
|
||||
assert.equal(h.frames.size, 1, 'still one frame loop');
|
||||
const before = lane.paints;
|
||||
h.step();
|
||||
h.step();
|
||||
assert.equal(lane.paints, before + 2, 'one repaint per frame');
|
||||
assert.equal(h.frames.size, 1);
|
||||
|
||||
h.advance(600);
|
||||
lane.painter(h.frame());
|
||||
// At 600 ms every line is between the marker and the edge, so all draw.
|
||||
assert.equal(h.strokes.length, EARTHQUAKE_RIPPLE_RINGS);
|
||||
assert.deepEqual(
|
||||
h.strokes.map((stroke) => stroke.width),
|
||||
[2, 1.1, 1.1, 2, 1.1, 1.1],
|
||||
'every third line is an index contour',
|
||||
);
|
||||
for (const stroke of h.strokes) {
|
||||
assert.equal(stroke.moves, 1, 'one unbroken ring');
|
||||
assert.equal(stroke.lines, RIPPLE_RING_SEGMENTS);
|
||||
assert.equal(stroke.color, 'rgb(255,0,0)');
|
||||
assert.ok(stroke.alpha > 0 && stroke.alpha <= 1);
|
||||
}
|
||||
|
||||
h.ripples.hide();
|
||||
assert.equal(lane.active, false);
|
||||
assert.equal(h.frames.size, 0, 'no frames asked for once hidden');
|
||||
h.strokes.length = 0;
|
||||
lane.painter(h.frame());
|
||||
assert.equal(h.strokes.length, 0);
|
||||
});
|
||||
|
||||
test('rings are skipped behind the globe and broken behind the eye', () => {
|
||||
const h = harness();
|
||||
h.ripples.show(target);
|
||||
h.advance(600);
|
||||
const [lane] = h.lanes;
|
||||
lane.painter(h.frame(false));
|
||||
assert.equal(h.strokes.length, 0, 'epicentre over the horizon');
|
||||
|
||||
// Points with negative x fall behind the eye: each ring breaks there.
|
||||
const frame = h.frame();
|
||||
frame.viewProjectionMatrix[3] = 1e-6;
|
||||
const center = fromDegrees(target.lon, target.lat);
|
||||
frame.viewProjectionMatrix[15] = -1e-6 * center.x;
|
||||
lane.painter(frame);
|
||||
assert.equal(h.strokes.length, EARTHQUAKE_RIPPLE_RINGS);
|
||||
for (const stroke of h.strokes)
|
||||
assert.ok(stroke.lines < RIPPLE_RING_SEGMENTS, 'part of each ring hidden');
|
||||
});
|
||||
|
||||
test('reduced motion draws still contour rings and asks for no frames', () => {
|
||||
const h = harness({ reducedMotion: true });
|
||||
h.ripples.show(target);
|
||||
assert.equal(h.frames.size, 0);
|
||||
assert.equal(h.lanes[0].paints, 1, 'painted once');
|
||||
h.lanes[0].painter(h.frame());
|
||||
assert.equal(h.strokes.length, EARTHQUAKE_RIPPLE_RINGS);
|
||||
assert.ok(h.strokes.every((stroke) => stroke.alpha === 0.7));
|
||||
});
|
||||
|
||||
test('an unusable target hides the rings; dispose releases the lane', () => {
|
||||
const h = harness();
|
||||
h.ripples.show(target);
|
||||
h.ripples.show({ ...target, radiusM: 0 });
|
||||
assert.equal(h.lanes[0].active, false);
|
||||
assert.equal(h.frames.size, 0);
|
||||
h.ripples.dispose();
|
||||
assert.equal(h.lanes[0].unregistered, true);
|
||||
// Shown again after dispose, it registers a fresh lane.
|
||||
h.ripples.show(target);
|
||||
assert.equal(h.lanes.length, 2);
|
||||
h.ripples.dispose();
|
||||
});
|
||||
|
||||
test('contour lines are wavy, the same for a quake every time, and inside the disc', () => {
|
||||
const h = harness();
|
||||
h.ripples.show(target);
|
||||
h.advance(600);
|
||||
// An identity-like projection: screen x, y follow ECEF y, z, so the
|
||||
// distance from the projected epicentre measures each point's radius.
|
||||
const frame = h.frame();
|
||||
const m = frame.viewProjectionMatrix;
|
||||
m[0] = 0;
|
||||
m[5] = 0;
|
||||
m[4] = 1e-6;
|
||||
m[9] = 1e-6;
|
||||
const points = [];
|
||||
const ctx = frame.ctx;
|
||||
const record = (x, y) => points.at(-1).push([x, y]);
|
||||
ctx.beginPath = () => points.push([]);
|
||||
ctx.moveTo = record;
|
||||
ctx.lineTo = record;
|
||||
ctx.stroke = () => {};
|
||||
h.lanes[0].painter(frame);
|
||||
const center = fromDegrees(target.lon, target.lat);
|
||||
const cx = (center.y * 1e-6 * 0.5 + 0.5) * 800;
|
||||
const cy = (0.5 - center.z * 1e-6 * 0.5) * 600;
|
||||
const spread = (line) => {
|
||||
const radii = line.map(([x, y]) => Math.hypot(x - cx, y - cy));
|
||||
return Math.max(...radii) / Math.min(...radii);
|
||||
};
|
||||
// The disc's outer circle first (filled, not stroked), then each line.
|
||||
assert.equal(points.length, EARTHQUAKE_RIPPLE_RINGS + 1);
|
||||
points.shift();
|
||||
assert.ok(
|
||||
points.every((line) => spread(line) > 1.05),
|
||||
'not circles: each line dents in and out',
|
||||
);
|
||||
for (const line of points) {
|
||||
const [first, last] = [line[0], line.at(-1)];
|
||||
assert.ok(
|
||||
Math.hypot(first[0] - last[0], first[1] - last[1]) < 1e-6,
|
||||
'closed',
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
test('the disc fades out to its outer circle; each wave fills with a fade', () => {
|
||||
const h = harness();
|
||||
h.ripples.show(target);
|
||||
h.advance(600);
|
||||
// A projection where east and north span the screen: the disc is seen
|
||||
// from above, so its ground frame can be filled.
|
||||
const frame = h.frame();
|
||||
const m = frame.viewProjectionMatrix;
|
||||
m[0] = 0;
|
||||
m[5] = 0;
|
||||
m[4] = 1e-6;
|
||||
m[9] = 1e-6;
|
||||
h.lanes[0].painter(frame);
|
||||
assert.equal(h.fills.length, 1 + EARTHQUAKE_RIPPLE_RINGS);
|
||||
const [disc, ...waves] = h.fills;
|
||||
assert.equal(disc.lines, RIPPLE_RING_SEGMENTS, 'the whole outer circle');
|
||||
assert.deepEqual(disc.gradient.circles, [0, 0, 0, 0, 0, 1]);
|
||||
assert.deepEqual(
|
||||
disc.gradient.stops,
|
||||
DISC_FADE.map(([offset, alpha]) => [offset, `rgba(255,0,0,${alpha})`]),
|
||||
);
|
||||
assert.equal(disc.gradient.stops.at(-1)[1], 'rgba(255,0,0,0)', 'fades out');
|
||||
assert.ok(disc.transform, 'filled under the ground-to-screen transform');
|
||||
assert.equal(h.fills.length, waves.length + 1);
|
||||
for (const wave of waves) {
|
||||
const reach = wave.gradient.circles[5];
|
||||
assert.ok(reach > 0 && reach < 1, 'out to its own line');
|
||||
assert.equal(wave.gradient.stops[0][1], 'rgba(255,0,0,0)', 'clear inside');
|
||||
}
|
||||
// The transforms were undone: nothing leaks into the labels' painting.
|
||||
assert.equal(frame.ctx.saved, 0);
|
||||
// Seen edge-on, the ground frame has no area: lines only, no fills.
|
||||
h.fills.length = 0;
|
||||
m[9] = 0;
|
||||
m[5] = 0;
|
||||
h.lanes[0].painter(frame);
|
||||
assert.equal(h.fills.length, 0);
|
||||
});
|
||||
|
||||
test('cssWithAlpha turns rgb into rgba stops', () => {
|
||||
assert.equal(cssWithAlpha('rgb(255,165,0)')(0.25), 'rgba(255,165,0,0.25)');
|
||||
assert.equal(cssWithAlpha('rgba(1, 2, 3, 0.5)')(2), 'rgba(1,2,3,1)');
|
||||
assert.equal(cssWithAlpha('red')(0.3), 'red');
|
||||
});
|
||||
|
||||
test('the lines run a bounded time after a selection, then rest without frames', () => {
|
||||
const h = harness();
|
||||
h.ripples.show(target);
|
||||
const [lane] = h.lanes;
|
||||
const window = RIPPLE_ACTIVE_CYCLES * EARTHQUAKE_RIPPLE_PERIOD_MS;
|
||||
assert.ok(window <= 15_000, 'about a quarter of a minute at most');
|
||||
let frames = 0;
|
||||
for (let t = 0; t < window * 3; t += 16) {
|
||||
if (!h.frames.size) break;
|
||||
lane.painter(h.frame());
|
||||
h.advance(16);
|
||||
h.step();
|
||||
frames++;
|
||||
}
|
||||
assert.equal(h.frames.size, 0, 'no frame asked for once settled');
|
||||
assert.ok(frames <= Math.ceil(window / 16) + 2, `bounded: ${frames} frames`);
|
||||
// At rest the lines hold still as readable contours.
|
||||
h.strokes.length = 0;
|
||||
lane.painter(h.frame());
|
||||
assert.equal(h.strokes.length, EARTHQUAKE_RIPPLE_RINGS);
|
||||
assert.ok(h.strokes.every((stroke) => stroke.alpha === 0.7));
|
||||
// A parked camera repainting the overlay does not start them again.
|
||||
lane.painter(h.frame());
|
||||
assert.equal(h.frames.size, 0);
|
||||
// A new selection runs again.
|
||||
h.ripples.show({ ...target, id: 'ak2' });
|
||||
assert.equal(h.frames.size, 1);
|
||||
h.ripples.dispose();
|
||||
});
|
||||
|
||||
test('off screen the frames stop; back in view inside the window they resume', () => {
|
||||
const h = harness();
|
||||
h.ripples.show(target);
|
||||
const [lane] = h.lanes;
|
||||
// Behind the globe.
|
||||
lane.painter(h.frame(false));
|
||||
const paints = lane.paints;
|
||||
h.step();
|
||||
assert.equal(h.frames.size, 0, 'stopped while out of sight');
|
||||
assert.equal(lane.paints, paints + 1, 'one last paint, then none');
|
||||
h.step();
|
||||
assert.equal(lane.paints, paints + 1);
|
||||
// Panned away: the disc is far off the viewport.
|
||||
h.ripples.show(target);
|
||||
const away = h.frame();
|
||||
away.viewProjectionMatrix[12] = 50;
|
||||
lane.painter(away);
|
||||
h.step();
|
||||
assert.equal(h.frames.size, 0);
|
||||
// The camera brings it back while the window lasts.
|
||||
away.viewProjectionMatrix[12] = 0;
|
||||
lane.painter(h.frame());
|
||||
assert.equal(h.frames.size, 1, 'resumed');
|
||||
h.ripples.dispose();
|
||||
});
|
||||
|
||||
test('turning reduced motion on mid-run stills the lines and stops the frames', () => {
|
||||
const h = harness();
|
||||
h.ripples.show(target);
|
||||
const [lane] = h.lanes;
|
||||
lane.painter(h.frame());
|
||||
h.setReducedMotion(true);
|
||||
h.step();
|
||||
assert.equal(h.frames.size, 0);
|
||||
h.strokes.length = 0;
|
||||
lane.painter(h.frame());
|
||||
assert.ok(
|
||||
h.strokes.every((stroke) => stroke.alpha === 0.7),
|
||||
'still',
|
||||
);
|
||||
// Turned off again within the window: the next paint runs them again.
|
||||
h.setReducedMotion(false);
|
||||
lane.painter(h.frame());
|
||||
assert.equal(h.frames.size, 1);
|
||||
h.ripples.dispose();
|
||||
});
|
||||
|
||||
test('a refresh of the same quake does not run a settled selection again', () => {
|
||||
const h = harness();
|
||||
h.ripples.show(target);
|
||||
const [lane] = h.lanes;
|
||||
h.advance(RIPPLE_ACTIVE_CYCLES * EARTHQUAKE_RIPPLE_PERIOD_MS + 1);
|
||||
lane.painter(h.frame());
|
||||
h.step();
|
||||
assert.equal(h.frames.size, 0, 'settled');
|
||||
h.ripples.show({ ...target, radiusM: 40000 }, { restart: false });
|
||||
assert.equal(h.frames.size, 0, 'a refresh keeps it at rest');
|
||||
lane.painter(h.frame());
|
||||
assert.equal(h.frames.size, 0);
|
||||
// A different quake, or a user's restart, runs again.
|
||||
h.ripples.show({ ...target, id: 'other' }, { restart: false });
|
||||
assert.equal(h.frames.size, 1);
|
||||
h.ripples.dispose();
|
||||
});
|
||||
|
||||
// The rings sit on the ground the disc is draped on. A label's fly-to starts
|
||||
// far away, before that ground loads, so the painter reads the height again
|
||||
// while it paints and moves the rings onto it.
|
||||
test('the rings move onto the ground height once it loads', () => {
|
||||
const h = harness();
|
||||
let ground = null;
|
||||
h.ripples.show({ ...target, heightM: 0, heightAt: () => ground });
|
||||
h.advance(600);
|
||||
const [lane] = h.lanes;
|
||||
lane.painter(h.frame());
|
||||
const atSea = h.fills.at(-1).transform;
|
||||
h.fills.length = 0;
|
||||
ground = 4000;
|
||||
h.advance(600);
|
||||
lane.painter(h.frame());
|
||||
const onGround = h.fills.at(-1).transform;
|
||||
assert.notDeepEqual(onGround, atSea, 'the centre moved up onto the ground');
|
||||
// A change of a metre or less, or an impossible height, leaves them put.
|
||||
for (const next of [4000.5, 25000, -2000]) {
|
||||
ground = next;
|
||||
h.advance(600);
|
||||
lane.painter(h.frame());
|
||||
assert.deepEqual(h.fills.at(-1).transform, onGround, String(next));
|
||||
}
|
||||
// Reads are bounded: a second paint within the interval does not ask again.
|
||||
let reads = 0;
|
||||
h.ripples.show({ ...target, id: 'ak2', heightAt: () => (reads++, 4000) });
|
||||
lane.painter(h.frame());
|
||||
lane.painter(h.frame());
|
||||
assert.equal(reads, 1);
|
||||
});
|
||||
@@ -1,6 +1,49 @@
|
||||
import { createEarthquakesLayer } from '../../layers/earthquakes/index.js';
|
||||
import { overlayHost } from './overlayHost.js';
|
||||
/** Wire earthquake observations to the application overlay host. */
|
||||
import { inputFor } from '../../input/dispatcher.js';
|
||||
import * as picking from '../../data/pickRegistry.js';
|
||||
import * as context from '../../data/contextStore.js';
|
||||
import { isPointerFree } from '../../data/inputOwnership.js';
|
||||
import { requestWorldFocus } from '../../worldFocus.js';
|
||||
import { createEarthquakeRipples } from './earthquakeRipples.js';
|
||||
import { heightsOf } from '../../queries/heights.js';
|
||||
import { formatDate, formatNumber, t } from '../../i18n/index.js';
|
||||
|
||||
const EVENT_TIME = Object.freeze({
|
||||
day: 'numeric',
|
||||
month: 'short',
|
||||
year: 'numeric',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit',
|
||||
timeZone: 'UTC',
|
||||
timeZoneName: 'short',
|
||||
});
|
||||
|
||||
/** The clicked quake's card text, in the page's language. */
|
||||
export const earthquakeCardText = Object.freeze({
|
||||
t,
|
||||
formatTime: (time) => formatDate(time, EVENT_TIME),
|
||||
formatDepth: (km) => formatNumber(km, { maximumFractionDigits: 1 }),
|
||||
});
|
||||
|
||||
/**
|
||||
* Wire earthquake observations to the application overlay host, and its
|
||||
* discs to the globe's clicks: a clicked quake shows its detail card.
|
||||
*/
|
||||
export function createApplicationEarthquakes(options) {
|
||||
return createEarthquakesLayer({ overlayHost, ...options });
|
||||
return createEarthquakesLayer({
|
||||
overlayHost,
|
||||
input: (viewer) => inputFor(viewer),
|
||||
picking,
|
||||
context,
|
||||
pointer: { isPointerFree },
|
||||
openExternal: (url) => window.open(url, '_blank', 'noopener,noreferrer'),
|
||||
focus: { requestWorldFocus },
|
||||
ripples: createEarthquakeRipples(),
|
||||
// The loaded ground (terrain or tiles) the clamped disc is draped on.
|
||||
groundHeight: (viewer, lon, lat) =>
|
||||
viewer ? heightsOf(viewer).now({ lon, lat }) : null,
|
||||
text: earthquakeCardText,
|
||||
...options,
|
||||
});
|
||||
}
|
||||
@@ -2,11 +2,12 @@ import { createRocketLaunchesLayer } from '../../layers/launches/index.js';
|
||||
import * as geometry from '../../celestialRing.js';
|
||||
import * as overlays from '../../overlays/worldOverlay.js';
|
||||
import * as render from '../../renderGovernor.js';
|
||||
import * as picking from '../../data/pickRegistry.js';
|
||||
|
||||
/** Construct one layer using the application scene owners and a supplied source. */
|
||||
export function createApplicationLaunches({ source, satellites }) {
|
||||
return createRocketLaunchesLayer({
|
||||
source,
|
||||
services: { satellites, geometry, overlays, render },
|
||||
services: { satellites, geometry, overlays, render, picking },
|
||||
});
|
||||
}
|
||||
@@ -1,9 +1,11 @@
|
||||
import { createSubmarineCableLayer } from '../../layers/submarineCables/index.js';
|
||||
import { overlayHost } from './overlayHost.js';
|
||||
import * as picking from '../../data/pickRegistry.js';
|
||||
/** Wire cable source geometry to the application overlay host. */
|
||||
export function createApplicationCables(options) {
|
||||
return createSubmarineCableLayer({
|
||||
overlayHost,
|
||||
picking,
|
||||
mapStackEventTarget: typeof window !== 'undefined' ? window : null,
|
||||
...options,
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user