ridge still: one pose, frozen at the first paint; arrivals repaint it, a resize takes a fresh one and asks for its tiles

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-23 05:38:24 +01:00
commit 0c614f9e22
2 changed files with 48 additions and 8 deletions

View file

@ -1522,6 +1522,31 @@ describe('the ridge cards in the scene', () => {
s.dispose();
});
it('a still shows one pose: a tile that arrives half a minute late is drawn at the pose of the first paint, and a resize takes the pose of its own clock', () => {
timers();
const { bd, arrive } = arriving([18]);
const { canvas } = host();
const s = createRidgeScene({ canvas, tier: 'still', sky: DAY, backdrop: bd })!;
const placed = (id: string) => images.filter((a) => (a[0] as any).id?.startsWith(id)).map((a: any) => [a.t, a[1], a[2], a[3], a[4], a.k]);
const first = placed('t18');
expect(first.length).toBeGreaterThan(0);
images.length = 0;
vi.spyOn(performance, 'now').mockReturnValue(T0 + 30_000); // the clock has turned the heading 18 degrees on
arrive(17);
vi.advanceTimersByTime(300);
expect(drawnTiles()).toEqual(new Set(['t18', 't17'])); // the tile chosen for the first pose is on the canvas
expect(placed('t18')).toEqual(first); // and the old ones have not moved
expect(bd.ensure).not.toHaveBeenCalled();
images.length = 0;
window.dispatchEvent(new Event('resize'));
vi.advanceTimersByTime(300);
expect(bd.ensure).toHaveBeenCalledTimes(1); // a new view: the tiles of the pose it takes
const arc = bd.ensure.mock.calls[0][0];
expect((arc.fromDeg + arc.toDeg) / 2).toBeCloseTo(poseAt(T0 + 30_000).h, 6);
expect(placed('t18')).not.toEqual(first);
s.dispose();
});
it('is the same after a settle from the live tier, and stops listening with the scene', () => {
timers();
const { bd, arrive, listeners } = arriving([18]);

View file

@ -3,11 +3,12 @@ import { overlayRequested } from '../debugOverlays';
import { FrameSampler, FrameStats } from './probe';
import { skyColours, SkyColours, Srgb } from './skyColour';
import { paintSky as paintPhysicalSky } from './physicalSky';
import { cardArc, cardPose, offsetRange, SEGMENT_S, sweepArc, windowFor } from './view';
import { cardArc, cardPose, HFOV_MAX_DEG, offsetRange, SEGMENT_S, sweepArc, windowFor } from './view';
import type { CardPose, View } from './view';
import type { TileCard } from './manifest';
import { residentTiles } from './ring';
import { freeze, normalize, poseAt as headingPose, setRange, settledAt, subscribe as onHeading } from './heading';
import type { Pose } from './heading';
import { activeAt, drawTwinkle as paintTwinkle, Star } from './stars';
import { nightBodies, paintNightBodies } from './nightBodies';
import type { NightBodies } from './nightBodies';
@ -168,6 +169,8 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null {
let raf = 0;
let readout: HTMLElement | undefined;
let resizeTimer = 0;
/** The pose a still shows: taken at its first paint and drawn at every repaint after, tile arrivals included; a resize takes a new one. A still has no clock. */
let stillPose: Pose | undefined;
const sampler = new FrameSampler();
const viewSize = () => {
@ -403,15 +406,14 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null {
};
};
/** Paint the lit tiles of every card into the host canvas, farthest first, each card at its pose about the rig's origin, at the heading now: the still scene's one image. */
/** Paint the lit tiles of every card into the host canvas, farthest first, each card at its pose about the rig's origin, at the pose the still froze at its first paint: the still scene's one image. */
const paintCards = (bd: Backdrop) => {
const ctx = context2d(canvas);
if (!ctx) return;
const { view } = bd.manifest;
const { w, h, scale: dpr } = viewSize();
const win = windowFor(view, w, h);
const t = clock();
const at = headingPose(t);
const at = (stillPose ??= headingPose(clock()));
const planeTop = h - win.plane.bottom - win.plane.h;
const horizonY = planeTop + view.horizonRow * win.plane.scale;
const poses = bd.manifest.cards.map((_, i) =>
@ -531,6 +533,7 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null {
/** Drop the twinkle and the rain, leaving the painted sky as a still image. */
const settleToStill = () => {
if (still && !built) return;
if (!still) stillPose = undefined; // a live scene that flattens freezes the pose it has now
still = true;
teardown();
cancelAnimationFrame(raf);
@ -599,11 +602,22 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null {
wake();
};
/** Make the scene again at the size it has now and settle as it was: after a resize, and after tiles arrive into a still. A burst of either is one rebuild, 250 ms after the last. */
const rebuild = () => {
let freshPose = false;
const rebuild = (fresh: boolean) => {
freshPose ||= fresh;
clearTimeout(resizeTimer);
resizeTimer = window.setTimeout(() => {
if (disposed) return;
const wasStill = still;
if (freshPose) {
// A resize is a new view: a still takes the pose the heading has now and asks for the tiles of it; the arrivals then repaint that pose.
stillPose = undefined;
if (wasStill && opts.backdrop) {
const { h } = headingPose(clock());
void opts.backdrop.ensure({ fromDeg: h - HFOV_MAX_DEG / 2, toDeg: h + HFOV_MAX_DEG / 2 }).catch(() => undefined);
}
}
freshPose = false;
if (!build()) return;
if (wasStill) settleToStill();
else {
@ -615,10 +629,11 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null {
// A still draws once and has no timer to ask for tiles, so it is told of each one that is made after its paint (the deadline of a scene start leaves the rest of the
// first arc loading); the live tier mounts them on its own timer. Nothing asks for more: a repaint adds no request, so the fill-in ends with the last tile.
const offTile = opts.backdrop?.onTile(() => {
if (still && !disposed) rebuild();
if (still && !disposed) rebuild(false);
});
document.addEventListener('visibilitychange', onVisibility);
window.addEventListener('resize', rebuild);
const onResize = () => rebuild(true);
window.addEventListener('resize', onResize);
const onMotionChange = () => {
built?.rig?.sync();
syncPlayState();
@ -637,7 +652,7 @@ export function createRidgeScene(opts: RidgeOptions): Scene | null {
opts.backdrop?.dispose();
clearTimeout(resizeTimer);
document.removeEventListener('visibilitychange', onVisibility);
window.removeEventListener('resize', rebuild);
window.removeEventListener('resize', onResize);
reducedMotion?.removeEventListener?.('change', onMotionChange);
teardown();
readout?.remove();