fix(globe): the Navara follow orbits on drag and zooms to the floor

Review of Task 5: while following on Navara a left drag panned the globe
about the ground point under the cursor (range 2,154 m → 54,136 m) and the
wheel zoomed toward that point (462 m after 40 notches, Cesium stops at
the 150 m floor). While a follow runs, Navara's spin, zoom and tilt are
now suspended (input.suspendCameraInputs, which gives the app's own
setCameraInputs choice back after the release) and followGestures.js turns
a drag into heading and pitch of the ENU offset (a canvas width is one
turn, its height half a turn; elevation kept within 2°-89°) and a wheel
notch into 20 % of its range, never inside the minimum range, as Cesium's
EntityView orbits the target. The offset is no longer read back from a
camera the follow alone moves.

Also (review item 7): a follow requests a render only when the target, the
offset or a gesture changed, so a still target lets Navara idle.

The contract follow check drags (range within ±5 %, target within 3 px)
and turns the wheel 40 notches (never below 149 m, ends within 5 % of
150 m) on both engines with real input (qa-globe-contract's pointer gains
drag and wheel); after stop it waits for Cesium's gesture inertia to end
before checking that the camera is free. Contract: Cesium 73/73, Navara
79/79.
This commit is contained in:
German Khokhlov committed 2026-10-16 20:57:10 +04:00
1 parent 68ff4e1c25
commit 4e195dac6b
9 files changed
+362 -15

No files matched your search

+6
View File
@@ -19,6 +19,7 @@ import { createNavaraCredits } from './credits.js';
import { createNavaraInput } from './input.js';
import { createNavaraPicking } from './picking.js';
import { createFollowController } from '../follow.js';
import { navaraFollowGestures } from './followGestures.js';
import { createNavaraMaps } from './maps.js';
import { createNavaraSurface } from './surface.js';
import { createGroundFloor, GROUND_FLOOR } from './groundFloor.js';
@@ -153,6 +154,11 @@ export function createNavaraEngine(
aim: (pose) => model.aim(pose),
frame: followStep,
requestRender: () => view.forceUpdate(),
// Drag orbits and wheel scales the follow's offset (followGestures.js).
gestures: (handlers) =>
navaraFollowGestures(view.canvas, () =>
input.input.suspendCameraInputs(),
)(handlers),
});
Object.assign(model.camera, {
follow: (options) => follow.follow(options),
+59
View File
@@ -0,0 +1,59 @@
// The follow camera's drag and wheel on Navara (src/globe/follow.js
// `gestures`): while following, Navara's own spin, zoom and tilt are off
// (they pan about the ground point under the cursor and zoom toward it) and
// a left drag turns the offset around the target, a wheel scales its range,
// as Cesium's tracked camera does: a drag across the whole canvas width is
// one turn of heading, across its height half a turn of pitch; one wheel
// notch (deltaY 100) is 20 % of the range.
const WHEEL_STEP = 1.2;
/**
* @param {HTMLCanvasElement} canvas
* @param {() => () => void} suspendCameraInputs The engine input's.
* @param {Window | EventTarget} [windowTarget] Where moves and releases
* outside the canvas arrive.
* @returns {(handlers: { orbit: Function, zoom: Function }) => () => void}
*/
export function navaraFollowGestures(
canvas,
suspendCameraInputs,
windowTarget = globalThis.window ?? canvas,
) {
return ({ orbit, zoom }) => {
const resume = suspendCameraInputs();
let last = null;
const down = (event) => {
if (event.button !== 0) return;
last = { x: event.clientX, y: event.clientY };
};
const move = (event) => {
if (!last) return;
const dx = event.clientX - last.x;
const dy = event.clientY - last.y;
last = { x: event.clientX, y: event.clientY };
const width = canvas.clientWidth || 1;
const height = canvas.clientHeight || 1;
orbit((-dx / width) * 2 * Math.PI, (dy / height) * Math.PI);
};
const up = () => {
last = null;
};
const wheel = (event) => {
if (!Number.isFinite(event.deltaY) || event.deltaY === 0) return;
event.preventDefault?.();
zoom(WHEEL_STEP ** (event.deltaY / 100));
};
canvas.addEventListener('pointerdown', down);
windowTarget.addEventListener('pointermove', move);
windowTarget.addEventListener('pointerup', up);
canvas.addEventListener('wheel', wheel, { passive: false });
return () => {
canvas.removeEventListener('pointerdown', down);
windowTarget.removeEventListener('pointermove', move);
windowTarget.removeEventListener('pointerup', up);
canvas.removeEventListener('wheel', wheel, { passive: false });
resume();
};
};
}
+45
View File
@@ -0,0 +1,45 @@
import test from 'node:test';
import assert from 'node:assert/strict';
import { navaraFollowGestures } from './followGestures.js';
const event = (type, fields = {}) =>
Object.assign(new Event(type, { cancelable: true }), fields);
test('drag turns, wheel scales, and Navara inputs are off until release', () => {
const canvas = new EventTarget();
canvas.clientWidth = 1000;
canvas.clientHeight = 500;
const win = new EventTarget();
let suspended = 0;
const gestures = navaraFollowGestures(
canvas,
() => {
suspended++;
return () => suspended--;
},
win,
);
const calls = [];
const release = gestures({
orbit: (h, p) => calls.push(['orbit', h, p]),
zoom: (f) => calls.push(['zoom', f]),
});
assert.equal(suspended, 1);
canvas.dispatchEvent(
event('pointerdown', { button: 0, clientX: 100, clientY: 100 }),
);
win.dispatchEvent(event('pointermove', { clientX: 350, clientY: 225 }));
win.dispatchEvent(event('pointerup'));
win.dispatchEvent(event('pointermove', { clientX: 0, clientY: 0 }));
const wheel = event('wheel', { deltaY: 100 });
canvas.dispatchEvent(wheel);
assert.equal(calls.length, 2);
assert.ok(Math.abs(calls[0][1] + Math.PI / 2) < 1e-12, 'quarter turn');
assert.ok(Math.abs(calls[0][2] - Math.PI / 4) < 1e-12);
assert.deepEqual(calls[1], ['zoom', 1.2]);
assert.equal(wheel.defaultPrevented, true);
release();
assert.equal(suspended, 0);
canvas.dispatchEvent(event('wheel', { deltaY: 100 }));
assert.equal(calls.length, 2, 'no handling after release');
});
+26 -5
View File
@@ -111,6 +111,15 @@ export function createNavaraInput(
passive: true,
});
let suspended = 0;
const sendCameraInputs = () => {
const enabled = cameraInputs && suspended === 0;
input.setCameraOptions({
enableSpin: enabled,
enableZoom: enabled,
enableTilt: enabled,
});
};
const input = {
on(type, listener, options = {}) {
const { modifier, pick, drill } = normalizeInputOptions(type, options);
@@ -128,11 +137,23 @@ export function createNavaraInput(
},
setCameraInputs(enabled) {
cameraInputs = Boolean(enabled);
input.setCameraOptions({
enableSpin: cameraInputs,
enableZoom: cameraInputs,
enableTilt: cameraInputs,
});
sendCameraInputs();
},
/**
* Navara-only: Navara's spin, zoom and tilt off until the returned
* release (the follow camera takes drag and wheel itself); the app's
* setCameraInputs choice comes back after the last release.
*/
suspendCameraInputs() {
suspended++;
sendCameraInputs();
let released = false;
return () => {
if (released) return;
released = true;
suspended--;
sendCameraInputs();
};
},
/**
* Navara-only extension: camera controller options (zoom distances,