Files
gods-eye-view/src/ui/frameRateMonitor.js
T
Sameh Khamis 30b6be1da0 Drive the camera orbit and the FPS readout through the frame port
The orbit controller and the frame-rate monitor had left the Cesium
allow-list but still hooked the scene's preRender and postRender events.
They now listen through the frame port; the monitor looks the port up
each time it is shown, so a renderer bound after it was created is the
one it counts.
2026-11-07 01:04:47 -08:00

103 lines
2.9 KiB
JavaScript

import { useStatusStore } from './stores/status.js';
import { applicationPinia } from './vueRuntime.js';
import { findFrame } from '../render/rendererOf.js';
/**
* Count rendered globe frames while the optional readout is visible. The
* readout is presented to the status store, which the title bar renders.
*/
export function createFrameRateMonitor({
viewer,
documentRef = document,
store = useStatusStore(applicationPinia()),
}) {
const frameOfViewer = () => findFrame(viewer);
// A page without the scene chrome has no title bar to show it in.
if (!documentRef.getElementById('title-bar') || !frameOfViewer())
return { destroy() {} };
// i18n-ignore: FPS is a unit code, the same in every language
let readout = { visible: false, text: 'FPS —' };
// Each monitor shows its readout until a newer one takes the title bar.
store.publishFps(readout);
const present = (patch) => {
const shown = store.fps === readout;
readout = { ...readout, ...patch };
if (shown) store.publishFps(readout);
};
let removeFrameListener = null;
let timer = null;
let frames = 0;
let startedAt = 0;
let destroyed = false;
function hide() {
present({ visible: false });
removeFrameListener?.();
removeFrameListener = null;
clearInterval(timer);
timer = null;
}
function show() {
frames = 0;
startedAt = performance.now();
// i18n-ignore: FPS is a unit code, the same in every language
present({ visible: true, text: 'FPS —' });
// Looked up when shown, so a renderer bound since creation is the one
// counted.
removeFrameListener = frameOfViewer()?.on('postRender', () => {
frames++;
});
timer = setInterval(() => {
const now = performance.now();
const elapsed = now - startedAt;
present({
text:
// i18n-ignore: FPS is a unit code, the same in every language
documentRef.hidden || elapsed <= 0
? 'FPS —'
: `FPS ${Math.round((frames * 1000) / elapsed)}`,
});
frames = 0;
startedAt = now;
}, 1000);
}
function onKeyDown(event) {
if (event.key !== '`' && event.code !== 'Backquote') return;
if (
event.defaultPrevented ||
event.repeat ||
event.isComposing ||
event.ctrlKey ||
event.altKey ||
event.metaKey ||
event.shiftKey
)
return;
if (
event.target?.isContentEditable ||
event.target?.closest?.(
'input, textarea, select, [contenteditable]:not([contenteditable="false"])',
)
)
return;
event.preventDefault();
if (readout.visible) hide();
else show();
}
documentRef.addEventListener('keydown', onKeyDown);
return {
destroy() {
if (destroyed) return;
destroyed = true;
hide();
documentRef.removeEventListener('keydown', onKeyDown);
// A newer monitor's readout stays.
if (store.fps === readout) store.publishFps(null);
},
};
}