fix(cctv): address review of #1543 (horizon test, resume guard, no per-frame allocation, mobile sheet)

- frames.test: viewerAt uses Cesium's default far (5e8) so the below-horizon
  test fails without occluder.isPointVisible
- cctvControls: install the resume observer for a still-only panel too
- cctvPresentation: panelShown also requires the Cameras sheet on phones;
  the observer watches body class/data-mobile-sheet
- frames.projectionInView: shared horizonOccluder + scratch sphere/array
This commit is contained in:
Byrd-IT authored and brandonabyrd committed 2026-11-18 17:13:06 -06:00
1 parent 0151c178a4
commit 9a3fc84935
6 files changed
+110 -25

No files matched your search

+2 -2
View File
@@ -1,4 +1,4 @@
- Stop fetching the active CCTV camera's pictures when nobody can see them. The
monitor plane skips its 10 s still while it is off screen or below the
horizon, and the panel's still preview waits for a visible tab and an open
panel (Byrd-IT, #1543).
horizon, and the panel's still preview waits for a visible tab, an open
panel and, on phones, the open Cameras sheet (Byrd-IT, #1543).
+15 -8
View File
@@ -1,4 +1,5 @@
import * as Cesium from 'cesium';
import { horizonOccluder } from '../../data/iconOrientation.js';
import {
FRAME_SIGNATURE_W,
FRAME_SIGNATURE_H,
@@ -42,6 +43,9 @@ export function fitToProjection(sourceW, sourceH) {
};
}
const _scratchPoints = [];
const _scratchSphere = new Cesium.BoundingSphere();
export function createFrames({ state: layerState, services, parts, source }) {
/**
* FNV-1a over the RGB channels of a downsampled frame. Pure (takes the raw
@@ -254,7 +258,10 @@ export function createFrames({ state: layerState, services, parts, source }) {
if (!camera || !scene || scene.mode !== Cesium.SceneMode.SCENE3D)
return true;
const placed = record?.frustumPositions;
const points = [
// Reused buffers: this runs on every projection tick while out of view.
const points = _scratchPoints;
points.length = 0;
for (const point of [
record?.billboard?.position,
placed?.mount,
placed?.capCenter,
@@ -262,16 +269,15 @@ export function createFrames({ state: layerState, services, parts, source }) {
placed?.tr,
placed?.br,
placed?.bl,
].filter(Boolean);
]) {
if (point) points.push(point);
}
if (
!points.length ||
typeof camera.frustum?.computeCullingVolume !== 'function'
)
return true;
const occluder = new Cesium.EllipsoidalOccluder(
Cesium.Ellipsoid.WGS84,
camera.positionWC,
);
const occluder = horizonOccluder(camera);
if (!points.some((point) => occluder.isPointVisible(point))) return false;
const culling = camera.frustum.computeCullingVolume(
camera.positionWC,
@@ -279,8 +285,9 @@ export function createFrames({ state: layerState, services, parts, source }) {
camera.upWC,
);
return (
culling.computeVisibility(Cesium.BoundingSphere.fromPoints(points)) !==
Cesium.Intersect.OUTSIDE
culling.computeVisibility(
Cesium.BoundingSphere.fromPoints(points, _scratchSphere),
) !== Cesium.Intersect.OUTSIDE
);
}
+3 -1
View File
@@ -185,7 +185,9 @@ function viewerAt(position, direction, up) {
fov: Cesium.Math.toRadians(60),
aspectRatio: 16 / 9,
near: 1,
far: 5e6,
// Cesium's default far plane, as the app uses: a smaller one rejects
// the far-side mount before the horizon check is ever reached.
far: 5e8,
}),
},
};
+10 -1
View File
@@ -73,7 +73,10 @@ export class CctvControls {
if (state) this._renderCctvState(state);
if (this._cctvChip) this._publishCctvChip({});
});
if (this._cctvVideo && typeof MutationObserver !== 'undefined') {
if (
(this._cctvVideo || this._cctvFrame) &&
typeof MutationObserver !== 'undefined'
) {
this._videoVisibilityObserver = new MutationObserver(() =>
this._renderCctvState(this._cctvState),
);
@@ -82,6 +85,12 @@ export class CctvControls {
attributes: true,
attributeFilter: ['class', 'hidden'],
});
// On phones the panel is on screen only while the Cameras sheet is open.
if (typeof document !== 'undefined' && document.body)
this._videoVisibilityObserver.observe(document.body, {
attributes: true,
attributeFilter: ['class', 'data-mobile-sheet'],
});
this.listen(document, 'visibilitychange', () =>
this._renderCctvState(this._cctvState),
);
+72 -12
View File
@@ -609,6 +609,15 @@ test('the meta line of a disabled layer does not describe its last camera', () =
assert.match(presentCctvPanel(activeState, true).meta, /^Austin · HDG /);
});
const settle = () => new Promise((resolve) => setTimeout(resolve, 0));
const stillState = (frameUrl) => ({
enabled: true,
count: 1,
cameraIndex: [camera],
activeCameraId: 'a',
activeCamera: { ...camera, frameUrl, sourceKind: 'snapshot' },
});
test('the still preview is fetched only while the tab and the panel are shown', async (t) => {
const prior = globalThis.Image;
const requests = [];
@@ -630,13 +639,7 @@ test('the still preview is fetched only while the tab and the panel are shown',
});
const { controls, byId } = panel(t);
const panelElement = byId('cctv-panel');
const state = (frameUrl) => ({
enabled: true,
count: 1,
cameraIndex: [camera],
activeCameraId: 'a',
activeCamera: { ...camera, frameUrl, sourceKind: 'snapshot' },
});
const state = stillState;
// Collapsed panel: nothing is requested, however often the state arrives.
assert.equal(panelElement.classList.contains('collapsed'), true);
@@ -644,22 +647,79 @@ test('the still preview is fetched only while the tab and the panel are shown',
controls._renderCctvState(state('a2.jpg'));
assert.equal(requests.length, 0, 'collapsed panel');
// Opening it renders again (the real panel does so from a class observer)
// and asks for the current picture once.
// Opening it resumes through the class observer, with no manual render.
panelElement.classList.remove('collapsed');
controls._renderCctvState(state('a2.jpg'));
await settle();
assert.equal(requests.length, 1, 'expanded panel');
assert.equal(requests[0].src, 'a2.jpg');
requests[0].onload();
// A background tab: refreshed URLs are not requested.
tabHidden = true;
document.dispatchEvent(new Event('visibilitychange'));
controls._renderCctvState(state('a3.jpg'));
assert.equal(requests.length, 1, 'hidden tab');
// Back in the tab: the next render fetches the current URL.
// Back in the tab: visibilitychange alone fetches the current URL.
tabHidden = false;
controls._renderCctvState(state('a3.jpg'));
document.dispatchEvent(new Event('visibilitychange'));
assert.equal(requests.length, 2, 'visible tab');
assert.equal(requests[1].src, 'a3.jpg');
});
test('the still preview waits for the Cameras sheet on phones', async (t) => {
const prior = globalThis.Image;
const requests = [];
globalThis.Image = class {
constructor() {
requests.push(this);
}
};
t.after(() => {
globalThis.Image = prior;
document.body.classList.remove('mobile-shell');
document.body.removeAttribute('data-mobile-sheet');
});
const { controls, byId } = panel(t);
byId('cctv-panel').classList.remove('collapsed');
document.body.classList.add('mobile-shell');
document.body.setAttribute('data-mobile-sheet', 'layers');
controls._renderCctvState(stillState('a1.jpg'));
assert.equal(requests.length, 0, 'another sheet is open');
document.body.removeAttribute('data-mobile-sheet');
controls._renderCctvState(stillState('a1.jpg'));
assert.equal(requests.length, 0, 'no sheet is open');
document.body.setAttribute('data-mobile-sheet', 'cameras');
await settle();
assert.equal(requests.length, 1, 'Cameras sheet opened');
assert.equal(requests[0].src, 'a1.jpg');
// Off a phone the sheet attribute means nothing.
requests[0].onload();
document.body.classList.remove('mobile-shell');
document.body.setAttribute('data-mobile-sheet', 'layers');
controls._renderCctvState(stillState('a2.jpg'));
assert.equal(requests.length, 2, 'desktop ignores the sheet');
});
test('a still-only panel (no video element) resumes from the class change', async (t) => {
const prior = globalThis.Image;
const requests = [];
globalThis.Image = class {
constructor() {
requests.push(this);
}
};
t.after(() => {
globalThis.Image = prior;
});
const { controls, byId } = panel(t);
assert.equal(controls._cctvVideo, undefined);
controls._renderCctvState(stillState('a1.jpg'));
assert.equal(requests.length, 0);
byId('cctv-panel').classList.remove('collapsed');
await settle();
assert.equal(requests.length, 1);
});
+8 -1
View File
@@ -277,9 +277,16 @@ export function _renderCctvState(state) {
// A background tab or a collapsed panel shows nothing, so neither plays the
// video nor fetches the still; the observer and the visibilitychange listener
// render again when the panel is shown.
// On phones the panel also belongs to one sheet: the shell hides it unless
// data-mobile-sheet is 'cameras'.
const body = typeof document !== 'undefined' ? document.body : null;
const sheetClosed =
!!body?.classList?.contains('mobile-shell') &&
body.getAttribute('data-mobile-sheet') !== 'cameras';
const panelShown =
!(typeof document !== 'undefined' && document.hidden) &&
!this._cctvPanel?.classList?.contains('collapsed');
!this._cctvPanel?.classList?.contains('collapsed') &&
!sheetClosed;
if (this._cctvVideo) {
const visible = liveIntent && panelShown;
if (!visible || this._cctvVideoCameraId !== activeId) {