feat: turn the camera by dragging the map, and put big-screen labels on glass

Dragging the map walks the camera round the black hole (yaw) and raises or
lowers it (lift); clicking the hole springs back to the default view. The
shader takes an azimuth and an elevation, and the orbit overlay is projected
through the same view (orbitPose, trailAngle and project take a View), so rings,
stars, trails and rockets turn with the disk. A star behind the hole's shadow is
hidden by it. Labels and moons wait until the camera rests. A camera at rest
costs nothing, and the still quality tier still draws while it turns.

Big-screen map labels sit on dark glass chips so the radar grid cannot swallow
them, and the picked label and its status read as one chip.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-14 17:02:50 +07:00
parent 5773f42d90
commit 228ca42801
10 changed files with 259 additions and 49 deletions

View File

@@ -1,18 +1,19 @@
import { useId, useLayoutEffect, useMemo, useRef, useState } from "react";
import { animate, motion, useMotionValue, useTransform, type MotionStyle, type MotionValue } from "motion/react";
import type { PointerEvent as ReactPointerEvent } from "react";
import type { Orbit, Snapshot } from "../../api";
import { orbitStatus } from "../../lib/format";
import { LABEL_HEIGHT, placeLabels, type Rect } from "../../lib/galaxyLabelLayout";
import {
assignOrbitSlots, moonPoses, orbitCapacity, orbitPose, orbitRadii, ORBIT_TILT,
SCENE_HEIGHT, SCENE_WIDTH, trailAngle, type Vec2,
assignOrbitSlots, DEFAULT_VIEW, LIFT_MAX, LIFT_MIN, moonPoses, orbitCapacity, orbitPose, orbitRadii,
SCENE_HEIGHT, SCENE_WIDTH, tiltOf, trailAngle, type Vec2, type View,
} from "../../lib/galaxyLayout";
import { useGalaxyMotion } from "../../hooks/useGalaxyMotion";
import { useScreenTransition } from "../../hooks/useScreenTransition";
import { gentleSpring, uiSpring } from "../../lib/motion";
import { VaultCore } from "./VaultCore";
import type { CoreSignal } from "../../lib/blackhole/controller";
import { SCENE_UNITS_PER_M } from "../../lib/blackhole/layout";
import { SCENE_UNITS_PER_M, SHADOW_SCENE_RADIUS } from "../../lib/blackhole/layout";
import { createActivity, observeStatuses, type FailureCause } from "../../lib/orbitActivity";
import { applyActivity, createField, fieldActive, starPose, stepField, type FieldEvent } from "../../lib/rocket";
import { RocketSprites, createSpriteRefs, paintSprites } from "./RocketSprites";
@@ -44,6 +45,12 @@ const MAX_LABELS = 12;
const MAX_BIG_LABELS = 60;
/** On the big screen the picked orbit's label stands off its star, on the end of an elbowed leader line. */
const BIG_LABEL_GAP = 64;
/** Radians of yaw and of lift per pixel dragged, and how far a press may wander and still be a click. */
const YAW_PER_PX = 0.006;
const LIFT_PER_PX = 0.003;
const CLICK_SLOP = 5;
/** A click this close to the hole, in scene units from its centre, sends the camera back to the default view. */
const HOLE_CLICK_RADIUS = 72;
const clamp = (value: number) => Math.max(0, Math.min(1, value));
const ACCRETION_SECONDS = 6.4;
/** Glow falloff, close to a gaussian so there is no visible edge. */
@@ -102,13 +109,23 @@ export function BlackholeGalaxy({
const selectionX = useMotionValue(0);
const selectionY = useMotionValue(0);
const selectionPlaced = useRef(false);
// The camera. The two values are what the loop paints from; `view` is what the labels and moons are laid out from, and
// only catches up once the camera comes to rest, so a drag never triggers a layout.
const viewYaw = useMotionValue(0);
const viewLift = useMotionValue(0);
const [view, setView] = useState<View>(DEFAULT_VIEW);
const [turning, setTurning] = useState(false);
const painted = useRef<View>(DEFAULT_VIEW);
const turn = useRef<{ x: number; y: number; yaw: number; lift: number; moved: boolean } | undefined>(undefined);
useLayoutEffect(() => () => {
rate.stop();
reveal.stop();
selectionX.stop();
selectionY.stop();
}, [rate, reveal, selectionX, selectionY]);
viewYaw.stop();
viewLift.stop();
}, [rate, reveal, selectionX, selectionY, viewYaw, viewLift]);
useLayoutEffect(() => {
if (reducedMotion) {
rate.jump(0);
@@ -144,7 +161,7 @@ export function BlackholeGalaxy({
useScreenTransition(frame, bigScreen, reducedMotion, setWidth);
const selectedSlot = selectedOrbitId ? slots.get(selectedOrbitId) : undefined;
const selectedPosition = selectedSlot && orbitPose(selectedSlot, lockedPhase);
const selectedPosition = selectedSlot && orbitPose(selectedSlot, lockedPhase, view);
const selectedX = selectedPosition?.x;
const selectedY = selectedPosition?.y;
useLayoutEffect(() => {
@@ -246,14 +263,25 @@ export function BlackholeGalaxy({
const wasEntering = entrance.current < 0.6;
if (slots.size) entrance.current = reducedMotion ? 0.6 : Math.min(0.6, entrance.current + delta);
const entering = !reducedMotion && slots.size > 0 && entrance.current < 0.6;
if (!delta || moving || (!reducedMotion && wasEntering)) {
const live: View = { yaw: viewYaw.get(), lift: viewLift.get() };
const viewChanged = live.yaw !== painted.current.yaw || live.lift !== painted.current.lift;
if (viewChanged) {
// The orbit plane tips as the camera rises and falls; the circles turn into ellipses and back.
const tilt = tiltOf(live);
for (const [radius, ring] of ringNodes.current) ring.setAttribute("ry", String(radius * tilt));
painted.current = live;
}
if (!delta || moving || viewChanged || (!reducedMotion && wasEntering)) {
for (const [node, { id, index }] of bodyNodes.current) {
const slot = slots.get(id)!;
const { x, y } = orbitPose(slot, elapsed.current);
const depth = y / (slot.radius * ORBIT_TILT);
const { x, y } = orbitPose(slot, elapsed.current, live);
const depth = y / (slot.radius * tiltOf(live));
const appear = entering ? clamp((entrance.current - Math.min(index * 0.013, 0.33)) / 0.23) : 1;
node.setAttribute("transform", `translate(${x} ${y}) scale(${0.95 + depth * 0.08})`);
node.setAttribute("opacity", String(appear * (0.88 + depth * 0.12)));
// A star on the far side of the hole, seen through its shadow, is hidden by it. This only happens once the
// camera has come down far enough for the far side to reach the shadow.
const hidden = depth < 0 ? clamp((Math.hypot(x, y) - SHADOW_SCENE_RADIUS * 0.95) / (SHADOW_SCENE_RADIUS * 0.4)) : 1;
node.setAttribute("opacity", String(appear * (0.88 + depth * 0.12) * hidden));
const body = bodyNodes.current.get(node)!;
body.x = x;
body.y = y;
@@ -261,7 +289,7 @@ export function BlackholeGalaxy({
// fades out as the map stops.
body.trail ??= node.querySelector<SVGGElement>(".marker-trail");
if (body.trail) {
body.trail.setAttribute("transform", `rotate(${(trailAngle(slot, elapsed.current) * 180) / Math.PI}) scale(${0.55 + 0.45 * Math.min(1, speed)} 1)`);
body.trail.setAttribute("transform", `rotate(${(trailAngle(slot, elapsed.current, live) * 180) / Math.PI}) scale(${0.55 + 0.45 * Math.min(1, speed)} 1)`);
body.trail.setAttribute("opacity", String(Math.min(1, speed * 1.6) * (1 - body.look)));
}
}
@@ -283,8 +311,8 @@ export function BlackholeGalaxy({
body.life.setAttribute("opacity", String(1 - depth * 0.45 * (1 - wave)));
}
}
if (moving && selectedSlot) {
const point = orbitPose(selectedSlot, elapsed.current);
if ((moving || viewChanged) && selectedSlot) {
const point = orbitPose(selectedSlot, elapsed.current, live);
selectionX.jump(point.x);
selectionY.jump(point.y);
}
@@ -298,7 +326,7 @@ export function BlackholeGalaxy({
return slot ? starPose(slot, elapsed.current, speed) : undefined;
};
if (dt > 0) for (const event of stepField(field.current, dt, starOf)) outcome(event);
paintSprites(sprites.current, field.current, { starOf, lookAt: signal.lookAt, time: ambient.current, dt });
paintSprites(sprites.current, field.current, { starOf, lookAt: signal.lookAt, time: ambient.current, dt, view: live });
absorption = Math.max(0, field.current.absorption.value);
}
// The snapshot flash waits for the rocket to arrive and for the confirmed snapshot to show up in the list.
@@ -313,9 +341,11 @@ export function BlackholeGalaxy({
const phase = (diskTime.current % ACCRETION_SECONDS) / ACCRETION_SECONDS;
core.current?.style.setProperty("--accretion-offset", String(-100 * phase));
}
if (signal.time !== diskTime.current || signal.absorption !== absorption) {
if (signal.time !== diskTime.current || signal.absorption !== absorption || signal.yaw !== live.yaw || signal.lift !== live.lift) {
signal.time = diskTime.current;
signal.absorption = absorption;
signal.yaw = live.yaw;
signal.lift = live.lift;
signal.wake?.();
}
const coreStyle = core.current?.style;
@@ -351,7 +381,7 @@ export function BlackholeGalaxy({
const scale = width / SCENE_WIDTH;
const height = SCENE_HEIGHT * scale;
const hitRadius = Math.max(12, scale ? 12 / scale : 12);
const positions = [...slots.values()].map((slot) => orbitPose(slot, lockedPhase));
const positions = [...slots.values()].map((slot) => orbitPose(slot, lockedPhase, view));
const moons = selectedPosition
? moonPoses(selectedPosition, recentSnapshots.length, 2 * hitRadius + 3, positions)
: [];
@@ -376,7 +406,7 @@ export function BlackholeGalaxy({
const markers = positions.map((point) => box(point, 7));
const core = box({ x: 0, y: 0 }, 65 * scale);
labels = placeLabels(labelCandidates.map(({ id, name }) => {
const point = screen(orbitPose(slots.get(id)!, lockedPhase));
const point = screen(orbitPose(slots.get(id)!, lockedPhase, view));
return { id, ...point, width: Math.min(146, Math.ceil(measure?.measureText(name).width ?? name.length * 7) + 16) };
}), width, height, [core, ...markers, ...moons.map((moon) => box(moon, 12))], selectedOrbitId, bigScreen ? BIG_LABEL_GAP : 0);
}
@@ -443,8 +473,78 @@ export function BlackholeGalaxy({
);
});
// Wake the loop whenever the camera moves, so a locked map draws while it turns and goes back to sleep after.
useLayoutEffect(() => {
const stops = [viewYaw.on("change", wake), viewLift.on("change", wake)];
return () => stops.forEach((stop) => stop());
}, [viewYaw, viewLift, wake]);
const settle = () => {
setView({ yaw: viewYaw.get(), lift: viewLift.get() });
setTurning(false);
};
/** Back to the default view by the short way round, with the shared spring. */
const resetView = () => {
const full = 2 * Math.PI;
const yawTarget = Math.round(viewYaw.get() / full) * full;
if (reducedMotion) {
viewYaw.jump(0);
viewLift.jump(0);
settle();
return;
}
setTurning(true);
animate(viewLift, 0, gentleSpring);
animate(viewYaw, yawTarget, {
...gentleSpring,
onComplete: () => {
viewYaw.jump(0);
settle();
},
});
};
const startTurn = (event: ReactPointerEvent<HTMLDivElement>) => {
if (!event.isPrimary || event.button !== 0 || (event.target as Element).closest("[data-orbit-marker], .orbit-hit, [data-snapshot-marker]")) return;
turn.current = { x: event.clientX, y: event.clientY, yaw: viewYaw.get(), lift: viewLift.get(), moved: false };
};
const moveTurn = (event: ReactPointerEvent<HTMLDivElement>) => {
const drag = turn.current;
if (!drag) return;
const dx = event.clientX - drag.x;
const dy = event.clientY - drag.y;
if (!drag.moved) {
if (Math.hypot(dx, dy) < CLICK_SLOP) return;
drag.moved = true;
viewYaw.stop();
viewLift.stop();
event.currentTarget.setPointerCapture(event.pointerId);
setTurning(true);
}
// Dragging right brings the near side of the orbits to the right; dragging down raises the camera.
viewYaw.set(drag.yaw - dx * YAW_PER_PX);
viewLift.set(Math.min(LIFT_MAX, Math.max(LIFT_MIN, drag.lift + dy * LIFT_PER_PX)));
};
const endTurn = (event: ReactPointerEvent<HTMLDivElement>) => {
const drag = turn.current;
turn.current = undefined;
if (!drag) return;
if (drag.moved) {
if (event.currentTarget.hasPointerCapture(event.pointerId)) event.currentTarget.releasePointerCapture(event.pointerId);
settle();
return;
}
// A click on the black hole itself puts the camera back.
const box = event.currentTarget.getBoundingClientRect();
const radius = (HOLE_CLICK_RADIUS * box.width) / SCENE_WIDTH;
if (event.type === "pointerup" && Math.hypot(event.clientX - (box.left + box.width / 2), event.clientY - (box.top + box.height / 2)) <= radius &&
(viewYaw.get() !== 0 || viewLift.get() !== 0)) {
resetView();
}
};
return (
<motion.div ref={frame} className="galaxy-frame" data-flowing={flowing} style={revealVariables}>
<motion.div ref={frame} className="galaxy-frame" data-flowing={flowing} data-turning={turning || undefined} style={revealVariables}
onPointerDown={startTurn} onPointerMove={moveTurn} onPointerUp={endTurn} onPointerCancel={endTurn}>
<svg viewBox={`${-HALF_WIDTH} ${-HALF_HEIGHT} ${SCENE_WIDTH} ${SCENE_HEIGHT}`} aria-hidden="true">
<defs>
<radialGradient id={knockoutId}>
@@ -482,7 +582,7 @@ export function BlackholeGalaxy({
</defs>
<g className="orbit-tracks" pointerEvents="none">
{orbitRadii.map((radius) => (
<ellipse key={radius} rx={radius} ry={radius * ORBIT_TILT} className={selectedSlot?.radius === radius ? "is-selected" : ""}
<ellipse key={radius} rx={radius} ry={radius * tiltOf(view)} className={selectedSlot?.radius === radius ? "is-selected" : ""}
ref={(node) => {
if (!node) return;
ringNodes.current.set(radius, node);
@@ -543,7 +643,7 @@ export function BlackholeGalaxy({
</span>
))}
{bigScreen && focusLabel && (
<span className="scene-label-note" style={{ left: `${100 * focusLabel.x / width}%`, top: `${100 * (focusLabel.y + LABEL_HEIGHT - 4) / height}%`, opacity: reducedMotion ? 1 : "var(--lock-reveal)" }}>
<span className="scene-label-note" style={{ left: `${100 * focusLabel.x / width}%`, top: `${100 * (focusLabel.y + LABEL_HEIGHT) / height}%`, width: focusLabel.width, opacity: reducedMotion ? 1 : "var(--lock-reveal)" }}>
{orbitStatus(shownStatus(visible.find(({ id }) => id === selectedOrbitId)!)).label}
</span>
)}