feat(03-03): Rams shell stylesheet + MapLibre v6 bootstrap, position marker, follow controller

- shell.css: token-only chrome (zero hex, zero transition/animation), dark-slot activation, machined geometry (8px lamp/notch, 40x40 plate, data-map=absent field)
- map.ts: setWorkerUrl + ?worker&url, build-time-inlined liberty-rams.json, GPUInitializationError -> null gate, RAMS-BASEMAP-READY signal
- marker.ts: dot + accuracy circle (100m display cap) + heading cone hidden on null, DOM Marker SVG namespace discipline
- follow.ts: 96px camera padding, 400ms flyTo, user-gesture-only free mode
- UI-SPEC: planner amendments verified (Pitfall 4 present; Open Question 1 appended)
This commit is contained in:
araharah-executor
2026-10-22 23:53:18 +08:00
parent 83985c4a09
commit c437d312f6
4 changed files with 253 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
/*
* follow.ts — the following/free camera controller (UI-SPEC §Follow / Recenter Control).
* --------------------------------------------------------------------------------------
* The camera pads 96px (12 × space-sm) so the position marker never hides
* under the strip or the recenter control. Recenter is a 400ms flyTo (the
* MapLibre viewport behavior, permitted; duration ≤ 400ms per UI-SPEC
* Micro-Behaviors). A USER gesture (movestart carrying originalEvent) flips
* to free — a programmatic move never does (T-03-16: the fix update must
* never read as the user having panned).
*/
import type { Map as MapLibreMap } from "maplibre-gl";
import type { Fix } from "../location/types.ts";
export const CAMERA_PADDING_PX = 96; // 12 x space-sm — marker never hides under strip or controls
export const RECENTER_DURATION_MS = 400;
export type FollowMode = "following" | "free";
export function createFollowController(map: MapLibreMap, onModeChange: (m: FollowMode) => void) {
let mode: FollowMode = "following";
let lock = false;
const set = (m: FollowMode): void => {
if (m !== mode) {
mode = m;
onModeChange(m);
}
};
map.on("movestart", (e: { originalEvent?: unknown }) => {
if (lock) return;
if (e.originalEvent) set("free"); // a user gesture only, never a programmatic move
});
return {
get mode(): FollowMode {
return mode;
},
follow(fix: Fix): void {
lock = true;
map.flyTo({ center: [fix.lng, fix.lat], padding: CAMERA_PADDING_PX, duration: RECENTER_DURATION_MS });
set("following");
lock = false;
},
};
}
+49
View File
@@ -0,0 +1,49 @@
/*
* map.ts — the MapLibre v6 bootstrap (D-14, RESEARCH §Code Examples).
* ---------------------------------------------------------------------
* The style is the build-time-inlined Rams basemap JSON (liberty-rams.json
* via @araharah/theme/basemap — D-14, the style object is never re-set at
* runtime, D-10). v6 is ESM-only and WebGL2-only: `setWorkerUrl` is
* mandatory in bundlers (Vite's `?worker&url` emits a hashed asset),
* and the Map constructor throws `GPUInitializationError` on devices
* without WebGL2 — caught here and returned as null (RESEARCH Pitfall 5),
* so the shell can render its deliberate absent-map state instead of
* crashing (Open Question 1, answered in main.ts + the UI-SPEC amendment).
*
* The `RAMS-BASEMAP-READY` console prefix is load-bearing — the theme
* screenshot toolchain (theme/scripts/screenshot.mjs) and plan 03-04's
* capture race both wait on exactly that prefix.
*/
import { Map as MapLibreMap, setWorkerUrl, GPUInitializationError, type StyleSpecification } from "maplibre-gl";
import "maplibre-gl/dist/maplibre-gl.css";
import workerUrl from "maplibre-gl/dist/maplibre-gl-worker.mjs?worker&url";
import ramsStyle from "@araharah/theme/basemap/liberty-rams.json";
import { KL_CENTER } from "../location/types.ts";
setWorkerUrl(workerUrl); // required for ALL bundlers in v6
export function bootMap(container: HTMLElement): MapLibreMap | null {
try {
const map = new MapLibreMap({
container,
// resolveJsonModule infers `version: number`; the style spec needs the
// version literal — a Typed cast (never `any`, no shim) satisfies the
// StyleSpecification contract while keeping the build-time import.
style: ramsStyle as StyleSpecification, // inlined at build time (D-14) — NEVER re-set
center: KL_CENTER,
zoom: 13,
attributionControl: { compact: true },
});
map.on("error", (e: { error?: { message?: string } }) => {
const inner = e && e.error;
console.error("RAMS-BASEMAP-ERROR", inner && inner.message ? inner.message : inner);
});
map.on("load", () => {
console.log("RAMS-BASEMAP-READY layers=" + map.getStyle().layers.map((l) => l.id).length);
});
return map;
} catch (err) {
if (err instanceof GPUInitializationError) return null; // Pitfall 5: shell still renders
throw err;
}
}
+75
View File
@@ -0,0 +1,75 @@
/*
* marker.ts — the position marker (UI-SPEC §Position Marker + Accuracy Geometry).
* -------------------------------------------------------------------------------
* One DOM Marker carrying: the accuracy circle (radius = projected metres,
* display-capped at 100m — the truth stays in the strip telemetry), the 20px
* matte-black dot, and the heading cone rotated to `heading` only when a
* heading exists — hidden entirely on null (no default orientation guess,
* no half-open cone; T-03-16 honesty).
*
* Marker colors are Rams-constant under both Telegram themes (map-constant
* rule, D-10). Signal-lost dims the WHOLE group to 40% via data-stale.
*/
import { Marker, type Map as MapLibreMap } from "maplibre-gl";
import type { Fix } from "../location/types.ts";
const DISPLAY_ACCURACY_CAP_M = 100;
export function accuracyRadiusPx(map: MapLibreMap, lat: number, metres: number): number {
const sw = map.getCanvas().width;
const mPerPx = (40075016.686 * Math.cos((lat * Math.PI) / 180)) / (sw * Math.pow(2, map.getZoom()));
return Math.min(metres, DISPLAY_ACCURACY_CAP_M) / mPerPx;
}
export function createPositionMarker(): {
marker: Marker;
update(map: MapLibreMap, fix: Fix): void;
setStale(v: boolean): void;
} {
const group = document.createElement("div");
group.className = "pos-group";
const svgNs = "http://www.w3.org/2000/svg";
// The SVG carries EXPLICIT width/height/viewBox and every child carries explicit
// cx/cy. An <svg> with no width/height renders at 300x150 (or collapses) and a
// <circle> with no cx/cy lands at (0,0) — both make the marker invisible.
const svg = document.createElementNS(svgNs, "svg");
svg.setAttribute("width", "240");
svg.setAttribute("height", "240");
svg.setAttribute("viewBox", "-120 -120 240 240");
const circle = document.createElementNS(svgNs, "circle");
circle.setAttribute("class", "pos-accuracy");
circle.setAttribute("cx", "0");
circle.setAttribute("cy", "0");
const cone = document.createElementNS(svgNs, "polygon");
cone.setAttribute("class", "pos-cone");
cone.setAttribute("points", "0,-8 6,5 0,2 -6,5");
const dot = document.createElementNS(svgNs, "circle");
dot.setAttribute("class", "pos-dot");
dot.setAttribute("cx", "0");
dot.setAttribute("cy", "0");
dot.setAttribute("r", "10");
// cone + dot are SVG-NAMESPACE nodes: they MUST be children of the <svg>, never
// of the HTML <div> — an SVG element directly under a <div> is not rendered.
svg.append(circle, cone, dot);
// MapLibre owns this group's DOM once addTo(map) runs — it parents it into the
// map canvas wrapper. DO NOT document.body.appendChild(group): an un-parented
// body child has no positioning CSS and paints UNDER the `inset: 0` map, so the
// marker would be invisible even though it exists.
group.appendChild(svg);
const marker = new Marker({ element: group }).setLngLat([0, 0]);
return {
marker,
update(map: MapLibreMap, fix: Fix) {
marker.setLngLat([fix.lng, fix.lat]);
circle.setAttribute("r", String(accuracyRadiusPx(map, fix.lat, fix.accuracyM ?? 0)));
if (fix.headingDeg === null) cone.style.display = "none"; // hidden when unknown — never a half-open guess
else {
cone.style.display = "";
cone.style.transform = `rotate(${fix.headingDeg}deg)`;
}
},
setStale(v: boolean) {
group.dataset.stale = String(v);
},
};
}