Files
planet/frontend/public/earth/js/globe-navigation.test.js
T
rayd1o 5bfc790dec
ci / backend (push) Has been cancelled
ci / frontend (push) Has been cancelled
release / images (push) Has been cancelled
ci / delivery (push) Has been cancelled
release: bump version to 0.75.0
2026-09-23 21:20:12 +08:00

82 lines
3.6 KiB
JavaScript

import { expect, test } from "bun:test";
import * as THREE from "three";
import { CONFIG, EARTH_SURFACE_TEXTURE_ALTITUDE_OFFSET } from "./constants.js";
import { createSurfaceDrag, distanceForGlobeZoom, globeZoomForDistance } from "./globe-navigation.js";
const radius = CONFIG.earthRadius + EARTH_SURFACE_TEXTURE_ALTITUDE_OFFSET;
const distance = zoom => distanceForGlobeZoom(zoom, CONFIG.earthRadius,
CONFIG.defaultCameraZ, CONFIG.minimumCameraAltitude);
test("the entire zoom range stays outside the surface with near-plane clearance and an invertible scale", () => {
expect(distance(1)).toBe(CONFIG.defaultCameraZ);
let previous = Infinity;
for (let zoom = CONFIG.minZoom; zoom <= CONFIG.maxZoom; zoom += 0.025) {
const z = distance(zoom);
expect(z - 0.1).toBeGreaterThan(radius + 6);
expect(z).toBeLessThan(previous);
expect(globeZoomForDistance(z, CONFIG.earthRadius, CONFIG.defaultCameraZ)).toBeCloseTo(zoom, 10);
previous = z;
}
expect(distance(5)).toBeGreaterThan(110);
});
function scene(zoom, width, height) {
const camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 5000);
camera.position.z = distance(zoom);
camera.updateMatrixWorld();
const earth = new THREE.Object3D();
earth.rotation.set(0.6, -1.2, 0.2);
earth.updateMatrixWorld();
const viewport = { left: 17, top: 29, width, height };
const project = local => {
earth.updateMatrixWorld();
const p = local.clone().applyMatrix4(earth.matrixWorld).project(camera);
return { x: viewport.left + (p.x + 1) * width / 2, y: viewport.top + (1 - p.y) * height / 2 };
};
const grabbed = new THREE.Vector3(0, 0, radius).applyQuaternion(earth.quaternion.clone().invert());
return { camera, earth, viewport, project, grabbed };
}
for (const zoom of [0.5, 1, 2, 3, 5]) for (const [width, height] of [[1440, 900], [390, 844]]) {
test(`${zoom * 100}% at ${width}px: the grabbed surface follows the pointer pixel for pixel`, () => {
const f = scene(zoom, width, height);
const drag = createSurfaceDrag(radius);
const start = f.project(f.grabbed);
drag.begin(f.earth, f.camera, start, f.viewport, 0);
for (let step = 1; step <= 20; step++) {
const target = { x: start.x + step, y: start.y + step * 0.6 };
drag.move(f.earth, f.camera, target, f.viewport, step * 16);
const actual = f.project(f.grabbed);
expect(actual.x).toBeCloseTo(target.x, 6);
expect(actual.y).toBeCloseTo(target.y, 6);
expect(drag.advance(f.earth, 16)).toBe(false);
}
});
}
test("a paused release or cancellation cannot fling the globe", () => {
const f = scene(5, 1440, 900);
const drag = createSurfaceDrag(radius), start = f.project(f.grabbed);
drag.begin(f.earth, f.camera, start, f.viewport, 0);
drag.move(f.earth, f.camera, { x: start.x + 50, y: start.y }, f.viewport, 16);
drag.end(200);
expect(drag.hasInertia()).toBe(false);
drag.begin(f.earth, f.camera, start, f.viewport, 220);
drag.move(f.earth, f.camera, { x: start.x + 30, y: start.y }, f.viewport, 236);
drag.end(237, true);
expect(drag.hasInertia()).toBe(false);
});
test("pinch zoom can rebase the remaining finger without a jump", () => {
const f = scene(1, 390, 844), drag = createSurfaceDrag(radius);
const start = f.project(f.grabbed);
drag.begin(f.earth, f.camera, start, f.viewport, 0);
drag.stop();
f.camera.position.z = distance(5);
f.camera.updateMatrixWorld();
const before = f.earth.quaternion.clone();
drag.begin(f.earth, f.camera, start, f.viewport, 100);
drag.move(f.earth, f.camera, start, f.viewport, 116);
expect(f.earth.quaternion.angleTo(before)).toBeCloseTo(0, 7);
});