Read the canvas from the renderer in the layers, voice and tools

Files off the Cesium list sized themselves by, listened on and set the
cursor of `viewer.scene.canvas`. They now read `canvasOf(viewer)` from
src/render/rendererOf.js: the bound renderer's canvas, else the viewer's
own, the same element. Recent Imagery's divider moves the split through the
globe port (`globeSplitTarget`, moved from the flood event into
src/ui/imagerySplit.js), and traffic flow passes the viewer to the listed
surface module (`viewerRoadSurface`) instead of reaching for its scene.

The test fakes give their viewers a canvas, and the fake renderers report
it, as the Cesium renderer reports the viewer's.
This commit is contained in:
Sameh Khamis committed 2026-11-07 18:10:39 -08:00
1 parent abe9f97768
commit b19fa56565
49 files changed
+269 -119

No files matched your search

+4 -1
View File
@@ -80,7 +80,7 @@ test('superseding a stalled pass aborts promptly without clearing newer loading'
// and the near plane, and window positions from the scene itself.
const ANGLE = 35 * Math.PI / 180;
function portView(scene) {
const viewer = { scene };
const viewer = { scene, canvas: scene.canvas };
const position = { x: 0, y: 0, z: 450 };
const direction = { x: 0, y: Math.cos(ANGLE), z: -Math.sin(ANGLE) };
bindRenderer(viewer, {
@@ -213,6 +213,9 @@ function flowFixture(source) {
_roadMode: 'osm',
_parseRoads: (data) => data.roads,
_viewer: {
get canvas() {
return this.scene?.canvas;
},
scene: {
globe: {
show: true,
+19 -20
View File
@@ -1,13 +1,13 @@
import { roadIdentity } from './retention.js';
import { fromDegrees, toCartographicRadians } from '../../geo/ellipsoid.js';
import { copyInto, distance, distanceSquared } from '../../geo/vector.js';
import { findCamera, findFrame, findQuery } from '../../render/rendererOf.js';
import {
drawnSurfaceReady,
prepareRoadSurfaces,
roadHeightStations,
trafficSurfaceReady,
} from './surface.js';
canvasOf,
findCamera,
findFrame,
findQuery,
} from '../../render/rendererOf.js';
import { roadHeightStations, viewerRoadSurface } from './surface.js';
import { matchFlowToRoads } from '../../data/flowMatch.js';
import {
DOT_HEIGHT_OFFSET,
@@ -260,7 +260,8 @@ export function createFlow({ state: layerState, services, parts, source }) {
// Separate whole-graph preparation from tile completion and publication.
await nextTask();
if (!current()) return false;
const scene = layerState._viewer.scene;
const surface = viewerRoadSurface(layerState._viewer);
const canvas = canvasOf(layerState._viewer);
// The enable-time estimate can predate the first local mesh (especially
// after an intercontinental jump). Once previews have measured roads, use
// that local evidence to select the detail view at its real elevation.
@@ -283,11 +284,11 @@ export function createFlow({ state: layerState, services, parts, source }) {
// its camera port says where the eye is.
const query = findQuery(layerState._viewer);
let depthHeight;
if (query && scene.canvas?.clientWidth) {
if (query && canvas?.clientWidth) {
try {
const hit = query.pickPosition(
scene.canvas.clientWidth / 2,
scene.canvas.clientHeight * 0.6,
canvas.clientWidth / 2,
canvas.clientHeight * 0.6,
);
const height = hit && toCartographicRadians(hit)?.height;
if (Number.isFinite(height) && Math.abs(height) <= 9000)
@@ -344,7 +345,7 @@ export function createFlow({ state: layerState, services, parts, source }) {
)
return false;
// Spend surface work on roads crossing the canvas, with a small pan margin.
if (!scene.canvas?.clientWidth) return true;
if (!canvas?.clientWidth) return true;
let minX = Infinity,
minY = Infinity,
maxX = -Infinity,
@@ -357,7 +358,7 @@ export function createFlow({ state: layerState, services, parts, source }) {
minY = Math.min(minY, screen.y);
maxY = Math.max(maxY, screen.y);
}
road.coverageBand = maxY < scene.canvas.clientHeight / 2 ? 1 : 0;
road.coverageBand = maxY < canvas.clientHeight / 2 ? 1 : 0;
if (clamped.coverage && camera) {
road.baseDensityWeight ??= road.densityWeight || 1;
road.densityWeight =
@@ -368,9 +369,9 @@ export function createFlow({ state: layerState, services, parts, source }) {
if (!query) return true;
return (
maxX >= -100 &&
minX <= scene.canvas.clientWidth + 100 &&
minX <= canvas.clientWidth + 100 &&
maxY >= -100 &&
minY <= scene.canvas.clientHeight + 100
minY <= canvas.clientHeight + 100
);
});
const budgets = parts.model.allocateRoadDotBudgets(
@@ -475,9 +476,8 @@ export function createFlow({ state: layerState, services, parts, source }) {
// The first bounded surface slice needs its own task, not the prefix's tail.
await nextTask();
if (!current()) return false;
let prepared = await prepareRoadSurfaces(
let prepared = await viewerRoadSurface(layerState._viewer).prepare(
unresolved,
layerState._viewer.scene,
services.ground,
[layerState._pointCollection],
signal,
@@ -497,9 +497,8 @@ export function createFlow({ state: layerState, services, parts, source }) {
) {
await new Promise((resolve) => setTimeout(resolve, 100));
if (!current()) return false;
prepared = await prepareRoadSurfaces(
prepared = await viewerRoadSurface(layerState._viewer).prepare(
prepared.pending,
layerState._viewer.scene,
services.ground,
[layerState._pointCollection],
signal,
@@ -517,7 +516,7 @@ export function createFlow({ state: layerState, services, parts, source }) {
!preview &&
!label.includes('refined') &&
frame &&
(!trafficSurfaceReady(scene) ||
(!surface.ready() ||
prepared.pending.length ||
ready.some((road) => road.surfaceSettled === false))
) {
@@ -533,7 +532,7 @@ export function createFlow({ state: layerState, services, parts, source }) {
stop();
return;
}
if (!drawnSurfaceReady(scene)) return;
if (!surface.drawnReady()) return;
stop();
// Revalidate only local samples taken at a coarser rendered LOD. This
// background upgrade never holds road acquisition or first paint.
+2 -3
View File
@@ -2,7 +2,7 @@ import { requestWithDeadline } from '../../sources/requestDeadline.js';
import { resolveRoadMode, ROAD_SOURCE_LABELS } from './roadModes.js';
import { phaseTiming } from '../../sources/phaseTiming.js';
import { RoadRequestError, roadRequestError } from './source.js';
import { loadedSurfaceHeight } from './surface.js';
import { viewerRoadSurface } from './surface.js';
import {
isUnavailableCapability,
sourceResponseError,
@@ -273,8 +273,7 @@ export function createIngestion({
let estimated = services.ground?.cachedGroundFloor?.(centerLat, centerLon);
// A miss is the local mesh still loading.
if (!Number.isFinite(estimated))
estimated = loadedSurfaceHeight(
layerState._viewer?.scene,
estimated = viewerRoadSurface(layerState._viewer).loadedHeight(
{ lon: centerLon, lat: centerLat },
[layerState._pointCollection],
);
+13 -2
View File
@@ -32,7 +32,12 @@ function setup(t) {
state._enabled = true;
state._liveMode = true;
state._pointCollection = new C.PointPrimitiveCollection();
state._viewer = { scene: {} };
state._viewer = {
scene: {},
get canvas() {
return this.scene?.canvas;
},
};
const parts = {
rendering: {
removeHeatLines() {},
@@ -735,6 +740,9 @@ test('selection asks the query port for depth and screen positions, and the came
segmentDist: [11],
});
const viewer = {
get canvas() {
return this.scene?.canvas;
},
scene: {
canvas: { clientWidth: 1000, clientHeight: 800 },
globe: { show: true, tilesLoaded: true, getHeight: () => 0 },
@@ -813,6 +821,9 @@ test('selection asks the query port for depth and screen positions, and the came
test('height easing stops visible waypoints at the camera near plane', () => {
const viewer = {
get canvas() {
return this.scene?.canvas;
},
scene: {
canvas: { clientWidth: 1000, clientHeight: 800 },
camera: {},
@@ -879,7 +890,7 @@ test('height easing through the Cesium ports stops at the near plane of a looked
const centre = C.Cartesian3.fromDegrees(0, 0, 0);
// A transformed camera: lookAt leaves the camera in the target's frame.
scene.camera.lookAt(centre, new C.HeadingPitchRange(0, -0.5, 600));
const viewer = { scene, camera: scene.camera };
const viewer = { scene, camera: scene.camera, canvas: scene.canvas };
const eyeDepth = (p) =>
-C.Matrix4.multiplyByPoint(scene.camera.viewMatrix, p, new C.Cartesian3())
.z;
+5 -5
View File
@@ -4,7 +4,7 @@ import {
equalsEpsilon,
lerpInto,
} from '../../geo/vector.js';
import { findCamera, findQuery } from '../../render/rendererOf.js';
import { canvasOf, findCamera, findQuery } from '../../render/rendererOf.js';
import { MAX_DOTS, roadDotBudget } from './policy.js';
export const DOT_CHANGE_BUDGET = 768;
@@ -207,15 +207,15 @@ export function createRetention({ state, parts }) {
*/
function frameView() {
const viewer = state._viewer;
const scene = viewer?.scene;
if (!scene?.canvas?.clientHeight) return null;
const canvas = canvasOf(viewer);
if (!canvas?.clientHeight) return null;
const query = findQuery(viewer);
if (!query) return null;
return {
query,
pose: findCamera(viewer)?.pose() ?? null,
width: scene.canvas.clientWidth,
height: scene.canvas.clientHeight,
width: canvas.clientWidth,
height: canvas.clientHeight,
};
}
function easeHeights(dt) {
+16
View File
@@ -80,6 +80,22 @@ export function trafficSurfaceReady(scene) {
return currentRenderedFrame(scene)?.ready === true;
}
/**
* The surface reads here over the scene `viewer` draws, for code that holds
* the viewer: the scene is read once, as this is called.
* @param {object|undefined} viewer
*/
export function viewerRoadSurface(viewer) {
const scene = viewer?.scene;
return {
drawnReady: () => drawnSurfaceReady(scene),
ready: () => trafficSurfaceReady(scene),
prepare: (roads, ...rest) => prepareRoadSurfaces(roads, scene, ...rest),
loadedHeight: (location, exclude) =>
loadedSurfaceHeight(scene, location, exclude),
};
}
/** Preserve bends and insert height samples at most 150 m apart, splitting long roads. */
export function roadSurfaceChunks(coordinates) {
const chunks = [];
+2 -2
View File
@@ -1,6 +1,6 @@
import { createReticleFootprint } from './footprint.js';
import { toLonLatH } from '../../geo/ellipsoid.js';
import { findCamera, findQuery } from '../../render/rendererOf.js';
import { canvasOf, findCamera, findQuery } from '../../render/rendererOf.js';
import {
deriveFetchCenter,
clampBoundsAroundCenter,
@@ -27,7 +27,7 @@ export function createViewport({ state: layerState, services, parts, source }) {
/** The canvas size in CSS pixels, as the layer has always read it. */
function canvasSize() {
const canvas = layerState._viewer.scene.canvas;
const canvas = canvasOf(layerState._viewer);
return {
width: canvas.clientWidth || canvas.width,
height: canvas.clientHeight || canvas.height,