@chanmeng666/archlang-3d 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -2,6 +2,83 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.4.0] - 2026-11-06
6
+
7
+ The core peer range is now `^1.57.0` (was `^1.45.0`): 1.57.0 is the first core that exports
8
+ `treadCount`, which the stairs read. With an older core (npm warns of the peer range) everything
9
+ else works as before and no stair is built (`stair-no-flights`).
10
+
11
+ ### Added
12
+
13
+ - Stairs. A straight stair and a turning stair (flights and landings) are built as stepped solids,
14
+ read from the core's own stair: each flight's rectangle, the edge it is climbed from and the
15
+ number of treads the plan draws on it (the core's new `treadCount` export), so the treads stand
16
+ on the drawing's tread lines. A stair climbs to the floor of the storey above in equal risers, one
17
+ per tread plus one at the head of each flight; a landing stands at the height its flight ends on.
18
+ The flight is as wide as the stair's `width`, centred as the plan centres it, and solid down to
19
+ its floor. The void in the floor above stays where the plan puts it.
20
+ - A stair is built once, on the storey it climbs from: a `dir up` stair climbs from its own
21
+ storey, and the `dir down` stop above it is the same stair. A `dir down` stair with no stair of
22
+ its id below is built from the storey below. A `dir up` stair on the top storey climbs that
23
+ storey's height; a `dir down` stair on the lowest storey is not built. Each case is in the
24
+ ledger.
25
+ - Stairs are oak, from the palette's `wood` (the core gives a stair no material), and the ledger
26
+ says so (`stair-material-default`), with the solid form (`stair-solid-default`) and each stair's
27
+ riser count (`stair-risers-from-treads`). In the GLB a stair is a `stair:<id>` node under its
28
+ storey, in a `stair` material, its facts (direction, width, flights, risers, rise) in
29
+ `extras.archlang`. In the replay it rises after its storey's walls. `build --json` counts stairs
30
+ per storey.
31
+ - Scene3D: a stair carries `flights` (each `at`, `size`, `foot` and `treads`) and `landings`. Both
32
+ are optional, so a Scene3D written before 0.4.0 still validates; its stairs are not built
33
+ (`stair-no-flights`). New exports: `stairSolid`, `stairRisers`, `StairSolid`, `STAIR_MATERIAL`.
34
+
35
+ ### Changed
36
+
37
+ - A replay of several storeys ends with the storeys assembled: they still rise one by one, each
38
+ lifted 3 m clear of the one below, and once everything is up they come down to their real heights
39
+ while the camera moves to frame the assembled building. The end frame, the Iso view and Reset show
40
+ the storeys stacked; hiding the storeys above one is the cutaway. Every earlier frame is unchanged.
41
+ `ReplayState` gains `settle` (0 while exploded, 1 at the end) and its `stop` runs on to the storey
42
+ count during the settle; `cameraStops` returns one more fit for several storeys (the assembled
43
+ building, last); `boxOfNodes` takes `settle`. New exports: `settledLift`, `stopCount`. A one-storey
44
+ replay is the same in every frame.
45
+ - The preview's label is the line every ArchLang surface uses: "Same source, plan and 3D preview.
46
+ The 3D preview is extruded from the same .arch file by the separate archlang-3d package, and
47
+ checked against the plan." (`PREVIEW_LABEL`; its first sentence is `PREVIEW_LEAD`). The replay
48
+ HTML also carries the limits line (`PREVIEW_LIMITS`): walls, openings, floors, stairs and furniture
49
+ in flat colours; no pitched roofs yet, and no sections, elevations or BIM. `PREVIEW_ABOUT_URL`
50
+ is the page that says what the preview shows.
51
+ - The side views' buttons are named "View from north" (east, south, west) instead of "North
52
+ elevation" and so on: the 3D preview draws no elevations. The buttons still read N, E, S and W.
53
+ - The ledger row `m1-omits-stairs-outdoor-fences` is now `m1-omits-outdoor-fences`, written once
54
+ for outdoor surfaces and once for fences, which are still not built.
55
+ - `PREVIEW_ABOUT_URL`, the page that says what the 3D preview shows and does not, is the docs
56
+ site's `https://archlang.uk/3d` (it was this package's npm page). The playground's "What it
57
+ shows" link follows it. The replay HTML does not carry the link, so its bytes are unchanged.
58
+
59
+ ## [0.3.0] - 2026-11-05
60
+
61
+ ### Added
62
+
63
+ - The replay's view turns freely: drag to rotate, scroll or pinch to zoom, right-drag or two fingers
64
+ to pan, and on the focused view the arrow keys rotate and + / − zoom (three.js's own
65
+ `OrbitControls`, inlined from the pinned three.js; no damping under reduced motion). The camera
66
+ stays above the ground and zooms within limits taken from the building's fitted framing. The first
67
+ movement takes the view and pauses the playback; a plain click on the view does nothing. A view
68
+ taken from the replay keeps that frame's aim and framing but switches to the end view's lens, so a
69
+ take in the top-down phase turns a top view in perspective, not the near-flat opening lens; the
70
+ plan drawing that fades over the opening frames is not laid over a taken view. The canvas is an
71
+ interactive element (`role="application"`) whose label says how to move it.
72
+ - Preset views: Iso (the replay's end view, exactly), Top, the four elevations from the plan's edges
73
+ (N, E, S, W) and Reset, and, for a plan of several storeys, buttons that hide the storeys above a
74
+ chosen one. A preset holds the view as a drag does (it pauses the playback), and its button shows
75
+ as pressed while it holds. The single-file replay HTML carries them; for a page of your own,
76
+ `mountReplay(canvas, payload, strip, { interactive: true })` returns `view` and `viewToolbar(view)`
77
+ builds the buttons (`./browser`). The presets are plain maths in `.`: `presetCamera`,
78
+ `distanceLimits`, `targetBounds`, `pivotAbove`, `takenLens`, `ELEVATION_RANGE`, `ZOOM`.
79
+ - Nothing changes for a replay nobody touches: `__seek(t)` still draws the replay's own frame at `t`,
80
+ and the exported frames and the video are the same.
81
+
5
82
  ## [0.2.0] - 2026-11-04
6
83
 
7
84
  ### Changed
package/README.md CHANGED
@@ -5,7 +5,10 @@
5
5
  your source and the core's drawing as they are.
6
6
 
7
7
  From a plan it writes a replay (one HTML file: the plan draws itself, then the walls rise) and a GLB
8
- for any glTF viewer.
8
+ for any glTF viewer. Same source, plan and 3D preview. The 3D preview is extruded from the same .arch file by the separate archlang-3d package, and checked against the plan.
9
+
10
+ 3D preview: walls, openings, floors, stairs and furniture in flat colours; no pitched roofs yet,
11
+ and no sections, elevations or BIM.
9
12
 
10
13
  ## Install
11
14
 
@@ -13,7 +16,7 @@ for any glTF viewer.
13
16
  npm install @chanmeng666/archlang-3d @chanmeng666/archlang
14
17
  ```
15
18
 
16
- Needs Node 20 or newer. The core, `@chanmeng666/archlang`, is a peer dependency at `^1.45.0`. To
19
+ Needs Node 20 or newer. The core, `@chanmeng666/archlang`, is a peer dependency at `^1.57.0`. To
17
20
  follow along, take a plan from the examples the core ships:
18
21
 
19
22
  ```sh
@@ -29,7 +32,15 @@ npx archlang3d view plan.arch --replay -o plan.html
29
32
  `plan.html` opens offline. three.js 0.186.1 is inside the file with its licence notice, and nothing
30
33
  is fetched from a network. `--theme dark` and `--storey <n>` change what it shows.
31
34
 
32
- To render a video as well, add a browser and `ffmpeg` on your `PATH`:
35
+ A plan of several storeys rises storey by storey, each lifted clear of the one below so the floor
36
+ underneath stays in view, and at the end the storeys come down to stand on each other at their real
37
+ heights.
38
+
39
+ The view turns freely: drag to rotate, scroll or pinch to zoom, right-drag or two fingers to pan,
40
+ and the buttons give Iso, Top, the views from north, east, south and west, and Reset (and, with several storeys, hide the
41
+ storeys above one). Moving the view pauses the playback.
42
+
43
+ To make a video as well, add a browser and `ffmpeg` on your `PATH`:
33
44
 
34
45
  ```sh
35
46
  npx playwright-core install chromium
package/dist/browser.d.ts CHANGED
@@ -1,7 +1,8 @@
1
+ import { P as PresetName, V as Vec3 } from './camera-DVOiag0F.js';
1
2
  import { ReplayPayload } from './replay.js';
2
- export { PREVIEW_LABEL, ReplayNode, ReplayOptions, replayPayload, viewBoxOf } from './replay.js';
3
- import { S as StripItem } from './strip-CdTeMoHA.js';
4
- export { R as ReplayFacts, s as stripAt } from './strip-CdTeMoHA.js';
3
+ export { PREVIEW_ABOUT_URL, PREVIEW_LABEL, PREVIEW_LEAD, PREVIEW_LIMITS, ReplayNode, ReplayOptions, replayPayload, viewBoxOf } from './replay.js';
4
+ import { S as StripItem } from './strip-Nt7eJ-4E.js';
5
+ export { R as ReplayFacts, s as stripAt } from './strip-Nt7eJ-4E.js';
5
6
 
6
7
  interface ReplayHandle {
7
8
  /** Render the frame at replay time `t` (clamped to [0, 1]) into the canvas, synchronously. */
@@ -12,18 +13,105 @@ interface ReplayHandle {
12
13
  ready: Promise<void>;
13
14
  /** Release the WebGL context and the GPU buffers. The handle draws nothing afterwards. */
14
15
  dispose: () => void;
16
+ /** The camera the user moves (card CR4); present when mounted with `interactive: true`. */
17
+ view?: ViewControl;
18
+ }
19
+
20
+ /** Who holds the camera: the replay, a preset, or the user's own orbit. */
21
+ type CameraHolder = "replay" | PresetName | "free";
22
+ /** A view to carry over to a new mount of an edited plan (`ViewControl.state`, `restore`). */
23
+ type ViewState = {
24
+ camera: "replay" | PresetName;
25
+ storeys: Shown;
26
+ } | {
27
+ camera: "free";
28
+ position: Vec3;
29
+ target: Vec3;
30
+ fov: number;
31
+ storeys: Shown;
32
+ };
33
+ /** The storeys shown: 1…n, or every one (so an edit that adds a storey shows it too). */
34
+ type Shown = number | "all";
35
+ /**
36
+ * The free camera (card CR4, decision CR-D2), browser side. `seek(t)` still draws the replay's own
37
+ * frame at `t` and hands a taken camera back to the replay; `at(t)` draws `t` from whatever camera
38
+ * holds the view. The storeys shown persist across both. Nothing here reaches the default path: a
39
+ * handle nobody touches draws exactly the frames it drew before.
40
+ */
41
+ interface ViewControl {
42
+ /** Draw replay time `t` (clamped) from the current camera, without handing it back. */
43
+ at: (t: number) => void;
44
+ /** Draw the current `t` from a preset camera (`presetCamera`); `iso` is the end camera exactly. */
45
+ preset: (name: PresetName) => void;
46
+ /** The replay's own camera and every storey: at t = 1 the end frame, byte for byte. */
47
+ reset: () => void;
48
+ /** How many storeys the replay has (1: nothing to hide). */
49
+ readonly storeys: number;
50
+ /** Show storeys 1…`upTo` (1-based, clamped) and hide those above; the camera stays. */
51
+ showStoreys: (upTo: number) => void;
52
+ /** How many storeys are shown, from the lowest. */
53
+ readonly shown: number;
54
+ readonly camera: CameraHolder;
55
+ state: () => ViewState;
56
+ /**
57
+ * Take over a state from an earlier mount (an edit rebuilt the replay); draws nothing. A held view
58
+ * (a preset, the user's orbit) is taken as a preset is: `onTake`.
59
+ */
60
+ restore: (s: ViewState) => void;
61
+ /** Hear when `camera` or `shown` changes (a drag, a preset, a seek, a restore); returns the unsubscribe. */
62
+ onChange: (fn: () => void) => () => void;
63
+ }
64
+ interface MountOptions {
65
+ /**
66
+ * Drag to turn, wheel or pinch to zoom, right-drag or two fingers to pan, arrow keys and + / −
67
+ * on the focused canvas (three.js's `OrbitControls`). Off by default.
68
+ */
69
+ interactive?: boolean;
70
+ /**
71
+ * The user took the view (the first movement of a drag, the wheel or a key, a preset, a restored
72
+ * held view): a host that is playing pauses, or its next `seek` hands the camera back.
73
+ */
74
+ onTake?: () => void;
15
75
  }
16
76
  /**
17
77
  * Mount the replay `p` on `out`: sizes the canvas to `p.width`×`p.height` and draws nothing yet.
18
78
  * `strip`, an element the host lays over the canvas, receives the metrics strip (card G4) as DOM
19
79
  * text at every `seek`; it is never drawn into the canvas, so `frameHash` is the same with and
20
- * without it.
80
+ * without it. `opts.interactive` adds the free camera (`handle.view`, card CR4); until someone
81
+ * drags, scrolls, presses a key or calls `view`, every frame is the one it is without it.
82
+ */
83
+ declare function mountReplay(out: HTMLCanvasElement, p: ReplayPayload, strip?: HTMLElement, opts?: MountOptions): ReplayHandle;
84
+
85
+ /**
86
+ * The free camera's buttons (card CR4), browser-only DOM shared by both hosts of the replay: the
87
+ * single-file replay HTML (`viewer.ts`) and the playground's 3D preview tab. The presets and Reset
88
+ * (`ViewControl`), the storeys shown, and a line saying how to move the view. Nothing here draws into
89
+ * the canvas.
90
+ */
91
+
92
+ /**
93
+ * Each preset's button text and its accessible name. The side views are "View from north" and so
94
+ * on, never "elevation": the 3D preview draws no elevations (the PS-D12 limits line).
21
95
  */
22
- declare function mountReplay(out: HTMLCanvasElement, p: ReplayPayload, strip?: HTMLElement): ReplayHandle;
96
+ declare const PRESET_LABELS: ReadonlyArray<readonly [PresetName, string, string]>;
97
+ declare const VIEW_HINT = "Drag to turn, right-drag to pan, scroll to zoom; on the focused view, arrow keys turn and + / \u2212 zoom.";
98
+ /**
99
+ * What an interactive replay canvas says it is, after its title (it is `role="application"`: it
100
+ * takes keys, so it is not an image).
101
+ */
102
+ declare const VIEW_CONTROLS_LABEL = "an interactive 3D view: drag or use the arrow keys to turn it, scroll or + and \u2212 to zoom";
103
+ interface ToolbarOptions {
104
+ /** A class for every button (the host's own button style). */
105
+ buttonClass?: string;
106
+ /** An id for the hint line, so the canvas can point at it (`aria-describedby`). */
107
+ hintId?: string;
108
+ }
109
+ /** The toolbar for `view`: preset buttons, Reset, the storeys (more than one), the hint. */
110
+ declare function viewToolbar(view: ViewControl, opts?: ToolbarOptions): HTMLElement;
23
111
 
24
112
  /** The strip's DOM half, apart from `strip.ts` so that the types of `.` and `./node` need no DOM lib. */
25
113
 
26
114
  /** Paint `items` into `el` (one span each); touches the DOM only when the text changed. */
27
115
  declare function paintStrip(el: HTMLElement, items: readonly StripItem[]): void;
28
116
 
29
- export { type ReplayHandle, ReplayPayload, StripItem, mountReplay, paintStrip };
117
+ export { type CameraHolder, type MountOptions, PRESET_LABELS, PresetName, type ReplayHandle, ReplayPayload, StripItem, type ToolbarOptions, VIEW_CONTROLS_LABEL, VIEW_HINT, type ViewControl, type ViewState, mountReplay, paintStrip, viewToolbar };
package/dist/browser.js CHANGED
@@ -1,9 +1,17 @@
1
1
  import {
2
+ PRESET_LABELS,
3
+ PREVIEW_ABOUT_URL,
2
4
  PREVIEW_LABEL,
5
+ PREVIEW_LEAD,
6
+ PREVIEW_LIMITS,
7
+ VIEW_CONTROLS_LABEL,
8
+ VIEW_HINT,
3
9
  replayPayload,
4
- viewBoxOf
5
- } from "./chunk-2ZINQMUB.js";
10
+ viewBoxOf,
11
+ viewToolbar
12
+ } from "./chunk-KF5ME6KH.js";
6
13
  import {
14
+ ELEVATION_RANGE,
7
15
  END_AZIMUTH,
8
16
  END_FOV,
9
17
  KIND_PALETTE,
@@ -13,13 +21,20 @@ import {
13
21
  boxOfNodes,
14
22
  cameraPosition,
15
23
  cameraStops,
24
+ distanceLimits,
16
25
  drawingFrame,
17
26
  finishColour,
18
27
  frameCamera,
28
+ pivotAbove,
29
+ presetCamera,
30
+ settledLift,
19
31
  slotColour,
20
32
  stateAt,
21
- stripAt
22
- } from "./chunk-EYEEIS4R.js";
33
+ stopCount,
34
+ stripAt,
35
+ takenLens,
36
+ targetBounds
37
+ } from "./chunk-EQBMPXB5.js";
23
38
 
24
39
  // src/replay/mount.ts
25
40
  import {
@@ -41,6 +56,7 @@ import {
41
56
  Vector3,
42
57
  WebGLRenderer
43
58
  } from "three";
59
+ import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
44
60
 
45
61
  // src/replay/strip-dom.ts
46
62
  function paintStrip(el, items) {
@@ -60,6 +76,8 @@ function paintStrip(el, items) {
60
76
  }
61
77
 
62
78
  // src/replay/mount.ts
79
+ var KEY_TURN_DEG = 5;
80
+ var KEY_DOLLY = 0.9;
63
81
  var THEMES = {
64
82
  light: {
65
83
  background: "#f7f5f0",
@@ -81,7 +99,8 @@ var THEMES = {
81
99
  "window:frame": "#5d86a8",
82
100
  "window:glass": "#86c5ee",
83
101
  "wall:demolish": "#e07a5f",
84
- furniture: "#3f6a8f"
102
+ furniture: "#3f6a8f",
103
+ stair: "#5d86a8"
85
104
  }
86
105
  }
87
106
  };
@@ -97,7 +116,7 @@ var LIGHT = {
97
116
  fillFrom: [0.7, 0.5, -0.6]
98
117
  };
99
118
  var FOOTPRINT_MM = 12;
100
- function mountReplay(out, p, strip) {
119
+ function mountReplay(out, p, strip, opts = {}) {
101
120
  const theme = THEMES[p.theme];
102
121
  const furniture = theme.colors.furniture;
103
122
  const W = p.width;
@@ -202,7 +221,7 @@ function mountReplay(out, p, strip) {
202
221
  }
203
222
  g.visible = false;
204
223
  scene.add(g);
205
- nodes.set(n.key, { g, flat: Math.min(1, FOOTPRINT_MM / Math.max(1e-6, topY - base)) });
224
+ nodes.set(n.key, { g, flat: Math.min(1, FOOTPRINT_MM / Math.max(1e-6, topY - base)), base, lift: n.lift ?? 0 });
206
225
  }
207
226
  const [vx, vy, vw, vh] = p.viewBox;
208
227
  const aspect = W / H;
@@ -220,11 +239,12 @@ function mountReplay(out, p, strip) {
220
239
  const fits = cameraStops(p.nodes, p.events, orbit);
221
240
  const size = hi.clone().sub(lo);
222
241
  const mid = lo.clone().add(hi).multiplyScalar(0.5);
223
- const reach = Math.max(...fits.map((f) => mid.distanceTo(new Vector3(...f.target))));
224
- const radius = Math.max(
225
- size.length() * 0.75 + reach + Math.hypot(mid.x - (vx + vw / 2), mid.z - (vy + vh / 2)),
226
- 1e3
227
- );
242
+ const radiusOver = (cams) => {
243
+ const reach = Math.max(...cams.map((f) => mid.distanceTo(new Vector3(...f.target))));
244
+ return Math.max(size.length() * 0.75 + reach + Math.hypot(mid.x - (vx + vw / 2), mid.z - (vy + vh / 2)), 1e3);
245
+ };
246
+ const radius = radiusOver(fits.slice(0, stopCount(p.events)));
247
+ const endRadius = radiusOver(fits);
228
248
  const rad = (d) => d * Math.PI / 180;
229
249
  const img = new Image();
230
250
  const ready = new Promise((resolve) => {
@@ -233,35 +253,83 @@ function mountReplay(out, p, strip) {
233
253
  });
234
254
  img.src = `data:image/svg+xml;base64,${btoa(unescape(encodeURIComponent(p.svg)))}`;
235
255
  let disposed = false;
236
- function seek(t) {
237
- if (disposed) return;
256
+ const frameView = { cx, cz, oh, elevation: p.elevation };
257
+ const storeyOf = new Map(p.events.map((e) => [e.key, e.storey ?? 0]));
258
+ const storeys = Math.max(1, ...p.events.filter((e) => !e.hidden).map((e) => (e.storey ?? 0) + 1));
259
+ let shown = storeys;
260
+ let holder = "replay";
261
+ let tNow = 0;
262
+ const halfDiagonal = size.length() / 2;
263
+ const reachFrom = (target) => Math.max(endRadius, mid.distanceTo(target) + halfDiagonal);
264
+ function place(f, r) {
265
+ const { fov, dist, elevation, azimuth } = f;
266
+ const target = new Vector3(...f.target);
267
+ const el = rad(elevation);
268
+ persp.fov = fov;
269
+ persp.near = Math.max(10, dist - r);
270
+ persp.far = dist + r;
271
+ persp.updateProjectionMatrix();
272
+ persp.up.set(0, 0, -1);
273
+ if (el < rad(89.99)) persp.up.set(0, 1, 0);
274
+ persp.position.set(...cameraPosition([target.x, target.y, target.z], dist, { elevation, azimuth }));
275
+ persp.lookAt(target);
276
+ }
277
+ const viewScene = {
278
+ events: p.events,
279
+ fits,
280
+ frame: frameView,
281
+ box: boxOfNodes(p.nodes, { settle: 1 }),
282
+ aspect
283
+ };
284
+ const presets = /* @__PURE__ */ new Map();
285
+ const presetOf = (name) => {
286
+ const known = presets.get(name);
287
+ if (known) return known;
288
+ const f = presetCamera(name, viewScene);
289
+ presets.set(name, f);
290
+ return f;
291
+ };
292
+ let controls;
293
+ let halt = () => {
294
+ };
295
+ let unhook = () => {
296
+ };
297
+ let primeAgain = () => {
298
+ };
299
+ const listeners = /* @__PURE__ */ new Set();
300
+ const notify = () => {
301
+ for (const fn of listeners) fn();
302
+ };
303
+ function draw(t) {
304
+ tNow = t;
238
305
  if (strip && p.facts) paintStrip(strip, stripAt(p.facts, p.events, Math.min(1, Math.max(0, t))));
239
306
  const s = stateAt(p.events, t);
240
- for (const [key2, { g, flat }] of nodes) {
307
+ for (const [key2, { g, flat, base, lift }] of nodes) {
241
308
  const st = s.nodes.get(key2);
242
- g.visible = st?.visible === true;
309
+ g.visible = st?.visible === true && (storeyOf.get(key2) ?? 0) < shown;
243
310
  g.scale.y = Math.max(flat, st?.rise ?? 0);
311
+ if (lift !== 0) g.position.y = base + settledLift(lift, s.settle);
244
312
  }
245
313
  let cam = ortho;
246
- if (s.tilt > 0 || s.dolly > 0) {
247
- const f = frameCamera(s, fits, { cx, cz, oh, elevation: p.elevation });
248
- const { fov, dist, elevation, azimuth } = f;
249
- const target = new Vector3(...f.target);
250
- const el = rad(elevation);
251
- persp.fov = fov;
252
- persp.near = Math.max(10, dist - radius);
253
- persp.far = dist + radius;
314
+ if (holder === "free" && controls) {
315
+ const dist = persp.position.distanceTo(controls.target);
316
+ const r = reachFrom(controls.target);
317
+ persp.near = Math.max(10, dist - r);
318
+ persp.far = dist + r;
254
319
  persp.updateProjectionMatrix();
255
- persp.up.set(0, 0, -1);
256
- if (el < rad(89.99)) persp.up.set(0, 1, 0);
257
- persp.position.set(...cameraPosition([target.x, target.y, target.z], dist, { elevation, azimuth }));
258
- persp.lookAt(target);
320
+ cam = persp;
321
+ } else if (holder !== "replay" && holder !== "free") {
322
+ const f = presetOf(holder);
323
+ place(f, reachFrom(new Vector3(...f.target)));
324
+ cam = persp;
325
+ } else if (s.tilt > 0 || s.dolly > 0) {
326
+ place(frameCamera(s, fits, frameView), s.settle > 0 ? endRadius : radius);
259
327
  cam = persp;
260
328
  }
261
329
  renderer.render(scene, cam);
262
330
  ctx.globalAlpha = 1;
263
331
  ctx.drawImage(gl, 0, 0);
264
- if (s.svg > 0 && img.complete && img.naturalWidth > 0) {
332
+ if (holder === "replay" && s.svg > 0 && img.complete && img.naturalWidth > 0) {
265
333
  const k = Math.min(W / vw, H / vh);
266
334
  const dw = vw * k;
267
335
  const dh = vh * k;
@@ -270,25 +338,208 @@ function mountReplay(out, p, strip) {
270
338
  ctx.globalAlpha = 1;
271
339
  }
272
340
  }
341
+ function seek(t) {
342
+ if (disposed) return;
343
+ if (holder !== "replay") {
344
+ halt();
345
+ holder = "replay";
346
+ notify();
347
+ }
348
+ draw(t);
349
+ primeAgain();
350
+ }
273
351
  async function frameHash() {
274
352
  const data = ctx.getImageData(0, 0, W, H).data;
275
353
  const digest = await crypto.subtle.digest("SHA-256", data);
276
354
  return [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, "0")).join("");
277
355
  }
356
+ const view = opts.interactive ? interact() : void 0;
357
+ function interact() {
358
+ const c = new OrbitControls(persp, out);
359
+ controls = c;
360
+ c.enableDamping = false;
361
+ c.screenSpacePanning = false;
362
+ c.minPolarAngle = rad(90 - ELEVATION_RANGE.max);
363
+ c.maxPolarAngle = rad(90 - ELEVATION_RANGE.min);
364
+ const bounds = targetBounds(box);
365
+ c.cursor.set(...bounds.centre);
366
+ const tabIndex0 = out.getAttribute("tabindex");
367
+ out.tabIndex = 0;
368
+ let loop = 0;
369
+ let dragging = false;
370
+ let quiet = false;
371
+ const reduced = () => window.matchMedia?.("(prefers-reduced-motion: reduce)").matches === true;
372
+ const settle = () => {
373
+ loop = 0;
374
+ if (disposed || holder !== "free") return;
375
+ if (c.update() || dragging) loop = requestAnimationFrame(settle);
376
+ };
377
+ const kick = () => {
378
+ if (c.enableDamping && !loop) loop = requestAnimationFrame(settle);
379
+ };
380
+ halt = () => {
381
+ if (loop) cancelAnimationFrame(loop);
382
+ loop = 0;
383
+ dragging = false;
384
+ if (!c.enableDamping) return;
385
+ quiet = true;
386
+ c.enableDamping = false;
387
+ c.update();
388
+ quiet = false;
389
+ };
390
+ const aim = (position, target, fov) => {
391
+ persp.fov = fov;
392
+ persp.up.set(0, 1, 0);
393
+ persp.position.set(...position);
394
+ c.target.set(...pivotAbove(position, target, viewScene.box.lo[1]));
395
+ persp.lookAt(c.target);
396
+ const dist = persp.position.distanceTo(c.target);
397
+ const lim = distanceLimits(fits, fov);
398
+ c.minDistance = Math.min(lim.min, dist);
399
+ c.maxDistance = Math.max(lim.max, dist);
400
+ c.maxTargetRadius = Math.max(bounds.radius, c.target.distanceTo(c.cursor));
401
+ c.enableDamping = !reduced();
402
+ };
403
+ const prime = () => {
404
+ if (holder === "free") return;
405
+ const f = holder === "replay" ? takenLens(frameCamera(stateAt(p.events, tNow), fits, frameView)) : presetOf(holder);
406
+ aim(cameraPosition(f.target, f.dist, f), f.target, f.fov);
407
+ };
408
+ const own = () => {
409
+ if (holder === "free") return;
410
+ holder = "free";
411
+ opts.onTake?.();
412
+ notify();
413
+ kick();
414
+ };
415
+ let armed = false;
416
+ primeAgain = () => {
417
+ if (armed) prime();
418
+ };
419
+ c.addEventListener("start", () => {
420
+ prime();
421
+ armed = true;
422
+ dragging = true;
423
+ });
424
+ c.addEventListener("end", () => {
425
+ armed = false;
426
+ dragging = false;
427
+ });
428
+ c.addEventListener("change", () => {
429
+ if (quiet || disposed) return;
430
+ armed = false;
431
+ own();
432
+ draw(tNow);
433
+ });
434
+ const turn = rad(KEY_TURN_DEG);
435
+ const keys = /* @__PURE__ */ new Map([
436
+ ["ArrowLeft", () => c.rotateLeft(turn)],
437
+ ["ArrowRight", () => c.rotateLeft(-turn)],
438
+ ["ArrowUp", () => c.rotateUp(turn)],
439
+ ["ArrowDown", () => c.rotateUp(-turn)],
440
+ ["+", () => c.dollyIn(KEY_DOLLY)],
441
+ ["=", () => c.dollyIn(KEY_DOLLY)],
442
+ ["-", () => c.dollyOut(KEY_DOLLY)],
443
+ ["_", () => c.dollyOut(KEY_DOLLY)]
444
+ ]);
445
+ const onKey = (e) => {
446
+ const act = keys.get(e.key);
447
+ if (!act || disposed || e.ctrlKey || e.metaKey || e.altKey) return;
448
+ e.preventDefault();
449
+ prime();
450
+ act();
451
+ kick();
452
+ };
453
+ out.addEventListener("keydown", onKey);
454
+ unhook = () => {
455
+ out.removeEventListener("keydown", onKey);
456
+ c.dispose();
457
+ if (tabIndex0 === null) out.removeAttribute("tabindex");
458
+ else out.setAttribute("tabindex", tabIndex0);
459
+ listeners.clear();
460
+ };
461
+ const clampStoreys = (k) => Math.min(storeys, Math.max(1, Math.round(k)));
462
+ const shownState = () => shown === storeys ? "all" : shown;
463
+ return {
464
+ at: (t) => {
465
+ if (!disposed) draw(t);
466
+ },
467
+ // A preset takes the view as a drag does: the host pauses, or its next frame would hand the
468
+ // camera straight back to the replay.
469
+ preset: (name) => {
470
+ if (disposed) return;
471
+ halt();
472
+ holder = name;
473
+ opts.onTake?.();
474
+ notify();
475
+ draw(tNow);
476
+ },
477
+ reset: () => {
478
+ if (disposed) return;
479
+ halt();
480
+ holder = "replay";
481
+ shown = storeys;
482
+ notify();
483
+ draw(tNow);
484
+ },
485
+ storeys,
486
+ showStoreys: (k) => {
487
+ if (disposed) return;
488
+ shown = clampStoreys(k);
489
+ notify();
490
+ draw(tNow);
491
+ },
492
+ onChange: (fn) => {
493
+ listeners.add(fn);
494
+ return () => listeners.delete(fn);
495
+ },
496
+ get shown() {
497
+ return shown;
498
+ },
499
+ get camera() {
500
+ return holder;
501
+ },
502
+ state: () => holder === "free" ? {
503
+ camera: "free",
504
+ position: persp.position.toArray(),
505
+ target: c.target.toArray(),
506
+ fov: persp.fov,
507
+ storeys: shownState()
508
+ } : { camera: holder, storeys: shownState() },
509
+ restore: (s) => {
510
+ if (disposed) return;
511
+ halt();
512
+ shown = s.storeys === "all" ? storeys : clampStoreys(s.storeys);
513
+ if (s.camera === "free") aim(s.position, s.target, s.fov);
514
+ holder = s.camera;
515
+ if (holder !== "replay") opts.onTake?.();
516
+ notify();
517
+ }
518
+ };
519
+ }
278
520
  function dispose() {
279
521
  if (disposed) return;
522
+ halt();
280
523
  disposed = true;
524
+ unhook();
281
525
  for (const o of owned) o.dispose();
282
526
  renderer.dispose();
283
527
  renderer.forceContextLoss();
284
528
  }
285
- return { seek, frameHash, ready, dispose };
529
+ return { seek, frameHash, ready, dispose, ...view ? { view } : {} };
286
530
  }
287
531
  export {
532
+ PRESET_LABELS,
533
+ PREVIEW_ABOUT_URL,
288
534
  PREVIEW_LABEL,
535
+ PREVIEW_LEAD,
536
+ PREVIEW_LIMITS,
537
+ VIEW_CONTROLS_LABEL,
538
+ VIEW_HINT,
289
539
  mountReplay,
290
540
  paintStrip,
291
541
  replayPayload,
292
542
  stripAt,
293
- viewBoxOf
543
+ viewBoxOf,
544
+ viewToolbar
294
545
  };