/** * Eye viewer: the nearest public camera's live frame (DESIGN.md §11.2 Eye), * re-requested every refresh interval while open, with a LIVE timecode, * the camera id and the direction it looks. */ import { icon } from './icons.js'; import { esc, haptic } from './dom.js'; import { createSheet } from './sheet.js'; import { frame } from './cards.js'; import { cardinal, fmtKm, utc, llShort } from './bands.js'; export function createEyeViewer({ globe, onFly }) { const sheet = createSheet({ label: 'Live camera', detents: ['large'], modal: true, dismissible: true, closeButton: true, className: 'eye-sheet opaque', heights: { large: (H) => Math.round(Math.min(H * 0.86, 760)) }, }); let timer = 0; let clock = 0; let cur = null; function url() { return globe.eyeFrameUrl(cur.id) || cur.imageUrl || ''; } function refresh() { const a = sheet.body.querySelector('.ev-frame img.front'); const b = sheet.body.querySelector('.ev-frame img:not(.front)'); if (!a || !b) return; b.onload = () => { b.classList.add('front'); a.classList.remove('front'); sheet.body.querySelector('.ev-age').textContent = 'just now'; }; b.src = url(); } function open(eye, { from } = {}) { if (!eye) return; cur = eye; const hd = Number(eye.headingDeg); sheet.head.innerHTML = `
Public traffic camera via the app’s CCTV proxy. Frames are stills, not video; the time burned into the frame is the camera’s local clock.
`; const front = sheet.body.querySelector('.ev-frame img.front'); front.addEventListener( 'error', () => sheet.body.querySelector('.ev-frame')?.classList.add('offline'), { once: true }, ); sheet.open('large'); haptic('light'); clearInterval(timer); clearInterval(clock); timer = setInterval(refresh, 15000); let n = 0; clock = setInterval(() => { n += 1; const c = sheet.body.querySelector('[data-ev-clock]'); if (c) c.textContent = utc(); const age = sheet.body.querySelector('.ev-age'); if (age && n % 5 === 0) age.textContent = `${n % 15} s ago`; }, 1000); } sheet.on('close', () => { clearInterval(timer); clearInterval(clock); }); sheet.root.addEventListener('click', (e) => { if (e.target.closest('[data-ev="fly"]') && cur) { sheet.close(); onFly?.(cur); } }); return { open, close: () => sheet.close() }; }