mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
A renderer plugin may supply applicationHooks(renderer) for what only its renderer has (map engine, own credits, stale commands, QA handles). What every renderer should do moves onto the contract: controls.keepEarthFramed (behind the keepEarthFramed capability), query.occlusionTest (behind occlusionTest), and panel drawing, which keepPanelRendering now does through resize and frame.renderNow through the contract. renderNow throws when the frame fails to draw. The Cesium port installs its zoom-out guard through keepEarthFramed (a setView no longer cancels its turn) and answers occlusionTest with the scene's probe; the stub answers it with a horizon test.
66 lines
2.2 KiB
JavaScript
66 lines
2.2 KiB
JavaScript
/**
|
|
* @module app/panelRendering
|
|
* @description Keeping the globe drawing in an inline panel whose host stops
|
|
* animation frames (`src/app/embed.js`, embed mode), through the ports alone: the
|
|
* application scene hands it to the embed views (`src/app/scene.js`).
|
|
*/
|
|
|
|
// A panel keeps drawing at about this rate when its host stops animation
|
|
// frames; see keepPanelRendering.
|
|
const PANEL_FRAME_MS = 33;
|
|
const MISSED_FRAMES_MS = 250;
|
|
|
|
/** A render error's details, including the plain objects workers report. */
|
|
function describeError(error) {
|
|
if (error instanceof Error)
|
|
return `${error.name}: ${error.message}\n${error.stack}`;
|
|
try {
|
|
return JSON.stringify(error, Object.getOwnPropertyNames(error ?? {}));
|
|
} catch {
|
|
return String(error);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Keep `renderer` drawing in an inline panel. Some hosts report a panel on
|
|
* screen as hidden, which stops the browser's animation frames and with them
|
|
* the renderer's own loop; while frames stop arriving, draw from a timer
|
|
* (`resize`, then `frame.renderNow()`). A frame that fails stops the timer,
|
|
* its error reported in full. Returns a function that stops it.
|
|
* @param {import('../render/contract/renderer').Renderer} renderer
|
|
* @param {{windowRef?: Window, now?: () => number}} [options]
|
|
* @returns {() => void}
|
|
*/
|
|
export function keepPanelRendering(
|
|
renderer,
|
|
{ windowRef = globalThis.window, now = () => performance.now() } = {},
|
|
) {
|
|
let lastFrame = now();
|
|
let frameRequest = null;
|
|
const onFrame = () => {
|
|
lastFrame = now();
|
|
frameRequest = windowRef.requestAnimationFrame(onFrame);
|
|
};
|
|
frameRequest = windowRef.requestAnimationFrame(onFrame);
|
|
let failed = false;
|
|
const timer = windowRef.setInterval(() => {
|
|
if (failed || now() - lastFrame < MISSED_FRAMES_MS) return;
|
|
try {
|
|
renderer.resize();
|
|
renderer.frame.renderNow();
|
|
} catch (error) {
|
|
// Report the error in full: workers report plain objects, which
|
|
// would print as [object Object].
|
|
failed = true;
|
|
console.error(
|
|
"[God's Eye View panel] render error:",
|
|
describeError(error),
|
|
);
|
|
}
|
|
}, PANEL_FRAME_MS);
|
|
return () => {
|
|
windowRef.clearInterval(timer);
|
|
windowRef.cancelAnimationFrame(frameRequest);
|
|
};
|
|
}
|