// HUD for the RPO replay (v4), sized for phone playback: a 16:9 video in a
// feed plays ~390 px wide (≈0.2×), so body copy is ≥34 px at 1080p and nothing
// that must be read is under ~24 px. Animation is a function of video time `s`,
// except label placement, which carries state between consecutive frames so
// labels hold their side (hysteresis) and ease instead of jumping.
export function createHud(ctx) {
const { D, W, H, U, PORTRAIT, sec, rate, sOfTau, CA_T, VANTOR_T, BURNS, CUTS, SHOTS, shotAt, DUR, project } = ctx;
const $ = (q) => document.querySelector(q);
const CA_M = Math.round(D.ca.m / 5) * 5;
const END = SHOTS.find((x) => x.id === 'end').s0;
// -------------------------------------------------------------------------
// Story
// -------------------------------------------------------------------------
const span = (id) => {
const x = SHOTS.find((sh) => sh.id === id);
return [x.s0, x.s1];
};
const CHAPTERS = [
{ key: 'climb', label: 'Climb', s0: span('climb')[0], s1: span('lap')[1] },
{ key: 'drop', label: 'Drop in', s0: span('drop')[0], s1: span('close')[1] },
{ key: 'dance', label: 'Close in', s0: span('dance')[0], s1: span('approach')[1] },
{ key: 'closest', label: 'Closest', s0: span('ca')[0], s1: span('sunrise')[1] },
{ key: 'leave', label: 'Leave', s0: span('leave')[0], s1: span('leave')[1] },
];
const cap = (id, title, body, extra = {}) => ({ s0: span(id)[0], s1: span(id)[1], title, body, ...extra });
const CAPTIONS = [
cap('hook', `A Russian military satellite came within ~${CA_M} m of a U.S.-registered one`,
'This is how it got there, rebuilt from public tracking data.', { hook: true, s0: -1 }),
cap('climb', 'Climb higher to fall behind',
'Same orbit, a quarter-orbit apart. Two burns lift Cosmos 261247 km, and a higher orbit is a slower one.'),
cap('lap', 'Fall behind, and lap it',
'Drifting back ~6,700 km a day, it laps almost twice, passing over X36 on Oct 5 and Oct 12, as its orbit plane swings into line.'),
cap('drop', 'Drop into the same orbit',
'Oct 12: from 47 km directly overhead it dives into X36\'s orbit, and lands about 480 km behind.'),
cap('close', 'Speed up, then brake',
'A 2 km dip makes it faster, closing ~350 km a day. Then it brakes 3 km behind.'),
cap('dance', 'The dance',
'Every orbit is a day and a night. Each one, Cosmos loops ~1.5 km around X36, 3 km behind it.'),
cap('dance-ff', 'Flatten the loops',
'A tiny burn puts Cosmos almost exactly in X36\'s path. It creeps in ~3 km a day.'),
cap('approach', 'Closing in, slowly',
'Creeping in at a few centimetres per second, while both fly at 27,000 km/h.'),
cap('ca', `Closest pass: ~${CA_M} m`,
'True size. LeoLabs measured ~500 m; other estimates run 150 to 650 m.'),
cap('sunrise', 'Then the sun comes up',
'Twenty minutes later both leave Earth\'s shadow. Within hours, Cosmos backs away.'),
cap('leave', 'Back off, and wait',
'It drifts back ~350 km a day. By Oct 25 it sat ~480 km behind, in the same orbit.'),
];
const BURN_NOTES = {
'Phasing raise': 'raises its orbit 46 km',
'Plane trim': 'tilts its orbit into line',
'Descent to X36 orbit': 'drops 47 km into X36\'s orbit',
'Closing burn': 'dips 2.3 km to speed up',
'Braking burn': 'rises 2.5 km to stop',
'Back-off': 'rises 140 m and starts to drift back',
'Ellipse trim': 'flattens its loops',
'Departure burn': 'rises 2.4 km to drift away',
'Re-match orbit': 'drops 2.6 km to hold position',
};
const EVENTS = [
...BURNS.map((b) => ({
t: b.t, kind: 'burn', title: 'Engine burn',
sub: `${BURN_NOTES[b.label] || b.label} · Δv ${b.dv < 1 ? b.dv.toFixed(1) : Math.round(b.dv)} m/s`,
})),
{ t: sec('2026-10-05T12:28:00Z'), kind: 'note', title: 'Passes overhead', sub: '55 km above its target' },
{ t: sec('2026-10-12T00:30:00Z'), kind: 'note', title: 'Directly overhead', sub: '54 km above its target' },
{ t: VANTOR_T, kind: 'vantor', title: 'Vantor photographs the pair', sub: `from its own satellite · ~${D.vantor.km.toFixed(0)} km apart · Oct 17` },
{ t: CA_T, kind: 'ca', title: 'Closest approach', sub: `${D.ca.t.slice(11, 16)} UTC, Oct 18 · in Earth's shadow` },
].map((e) => ({ ...e, s: sOfTau(e.t) })).filter((e) => e.s !== null).sort((a, b) => a.s - b.s);
const caS = EVENTS.find((e) => e.kind === 'ca')?.s ?? 1e9;
// -------------------------------------------------------------------------
// DOM
// -------------------------------------------------------------------------
const fade = (s, s0, s1, ramp = 0.4) => Math.min(1, Math.max(0, (s - s0) / ramp), Math.max(0, (s1 - s) / ramp));
const capEl = $('#caption');
const tagsEl = $('#tags');
const leaders = $('#leaders');
const evEl = $('#events');
leaders.setAttribute('viewBox', `0 0 ${W} ${H}`);
$('#chapters').innerHTML = CHAPTERS.map((c) => `
${c.label}
`).join('');
// Linear 22-day timeline with event markers (replaces the log chart).
const TL = { t0: sec('2026-09-30T00:00:00Z'), t1: sec('2026-10-21T23:59:00Z') };
const tlBox = $('#timeline');
const timeline = (() => {
const cw = tlBox.clientWidth;
const ch = tlBox.clientHeight;
const y = ch * 0.42;
const x = (t) => ((t - TL.t0) / (TL.t1 - TL.t0)) * cw;
const fs = (PORTRAIT ? 24 : 26) * U;
let ticks = '';
for (let d = 0; d <= 21; d++) {
const t = sec(`2026-${d === 0 ? '09-30' : `10-${String(d).padStart(2, '0')}`}T00:00:00Z`);
ticks += ``;
}
const labs = [['2026-09-30', 'SEP 30', 'start'], ['2026-10-12', 'OCT 12', 'middle'], ['2026-10-18', 'OCT 18', 'middle'], ['2026-10-21T23:59:00Z', 'OCT 21', 'end']]
.map(([d, l, a]) => `${l}`).join('');
const burns = BURNS.map((b) => ``).join('');
const ca = ``;
tlBox.innerHTML = ``;
return { x };
})();
function tag(id, name, cls) {
let el = document.getElementById(id);
if (!el) {
el = document.createElement('div');
el.id = id;
el.className = 'tag';
el.innerHTML = `