@chanmeng666/archlang-3d 0.2.0 → 0.3.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,29 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [0.3.0] - 2026-11-05
6
+
7
+ ### Added
8
+
9
+ - The replay's view turns freely: drag to rotate, scroll or pinch to zoom, right-drag or two fingers
10
+ to pan, and on the focused view the arrow keys rotate and + / − zoom (three.js's own
11
+ `OrbitControls`, inlined from the pinned three.js; no damping under reduced motion). The camera
12
+ stays above the ground and zooms within limits taken from the building's fitted framing. The first
13
+ movement takes the view and pauses the playback; a plain click on the view does nothing. A view
14
+ taken from the replay keeps that frame's aim and framing but switches to the end view's lens, so a
15
+ take in the top-down phase turns a top view in perspective, not the near-flat opening lens; the
16
+ plan drawing that fades over the opening frames is not laid over a taken view. The canvas is an
17
+ interactive element (`role="application"`) whose label says how to move it.
18
+ - Preset views: Iso (the replay's end view, exactly), Top, the four elevations from the plan's edges
19
+ (N, E, S, W) and Reset, and, for a plan of several storeys, buttons that hide the storeys above a
20
+ chosen one. A preset holds the view as a drag does (it pauses the playback), and its button shows
21
+ as pressed while it holds. The single-file replay HTML carries them; for a page of your own,
22
+ `mountReplay(canvas, payload, strip, { interactive: true })` returns `view` and `viewToolbar(view)`
23
+ builds the buttons (`./browser`). The presets are plain maths in `.`: `presetCamera`,
24
+ `distanceLimits`, `targetBounds`, `pivotAbove`, `takenLens`, `ELEVATION_RANGE`, `ZOOM`.
25
+ - Nothing changes for a replay nobody touches: `__seek(t)` still draws the replay's own frame at `t`,
26
+ and the exported frames and the video are the same.
27
+
5
28
  ## [0.2.0] - 2026-11-04
6
29
 
7
30
  ### Changed
package/dist/browser.d.ts CHANGED
@@ -1,7 +1,8 @@
1
+ import { P as PresetName, V as Vec3 } from './camera-CLNYQ1Xv.js';
1
2
  import { ReplayPayload } from './replay.js';
2
3
  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';
4
+ import { S as StripItem } from './strip-CN_dTKqy.js';
5
+ export { R as ReplayFacts, s as stripAt } from './strip-CN_dTKqy.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,102 @@ 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
+ /** Each preset's button text and its accessible name. */
93
+ declare const PRESET_LABELS: ReadonlyArray<readonly [PresetName, string, string]>;
94
+ declare const VIEW_HINT = "Drag to turn, right-drag to pan, scroll to zoom; on the focused view, arrow keys turn and + / \u2212 zoom.";
95
+ /**
96
+ * What an interactive replay canvas says it is, after its title (it is `role="application"`: it
97
+ * takes keys, so it is not an image).
21
98
  */
22
- declare function mountReplay(out: HTMLCanvasElement, p: ReplayPayload, strip?: HTMLElement): ReplayHandle;
99
+ declare const VIEW_CONTROLS_LABEL = "an interactive 3D view: drag or use the arrow keys to turn it, scroll or + and \u2212 to zoom";
100
+ interface ToolbarOptions {
101
+ /** A class for every button (the host's own button style). */
102
+ buttonClass?: string;
103
+ /** An id for the hint line, so the canvas can point at it (`aria-describedby`). */
104
+ hintId?: string;
105
+ }
106
+ /** The toolbar for `view`: preset buttons, Reset, the storeys (more than one), the hint. */
107
+ declare function viewToolbar(view: ViewControl, opts?: ToolbarOptions): HTMLElement;
23
108
 
24
109
  /** The strip's DOM half, apart from `strip.ts` so that the types of `.` and `./node` need no DOM lib. */
25
110
 
26
111
  /** Paint `items` into `el` (one span each); touches the DOM only when the text changed. */
27
112
  declare function paintStrip(el: HTMLElement, items: readonly StripItem[]): void;
28
113
 
29
- export { type ReplayHandle, ReplayPayload, StripItem, mountReplay, paintStrip };
114
+ 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,14 @@
1
1
  import {
2
+ PRESET_LABELS,
2
3
  PREVIEW_LABEL,
4
+ VIEW_CONTROLS_LABEL,
5
+ VIEW_HINT,
3
6
  replayPayload,
4
- viewBoxOf
5
- } from "./chunk-2ZINQMUB.js";
7
+ viewBoxOf,
8
+ viewToolbar
9
+ } from "./chunk-WBA4LABI.js";
6
10
  import {
11
+ ELEVATION_RANGE,
7
12
  END_AZIMUTH,
8
13
  END_FOV,
9
14
  KIND_PALETTE,
@@ -13,13 +18,18 @@ import {
13
18
  boxOfNodes,
14
19
  cameraPosition,
15
20
  cameraStops,
21
+ distanceLimits,
16
22
  drawingFrame,
17
23
  finishColour,
18
24
  frameCamera,
25
+ pivotAbove,
26
+ presetCamera,
19
27
  slotColour,
20
28
  stateAt,
21
- stripAt
22
- } from "./chunk-EYEEIS4R.js";
29
+ stripAt,
30
+ takenLens,
31
+ targetBounds
32
+ } from "./chunk-VPYTRKMK.js";
23
33
 
24
34
  // src/replay/mount.ts
25
35
  import {
@@ -41,6 +51,7 @@ import {
41
51
  Vector3,
42
52
  WebGLRenderer
43
53
  } from "three";
54
+ import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
44
55
 
45
56
  // src/replay/strip-dom.ts
46
57
  function paintStrip(el, items) {
@@ -60,6 +71,8 @@ function paintStrip(el, items) {
60
71
  }
61
72
 
62
73
  // src/replay/mount.ts
74
+ var KEY_TURN_DEG = 5;
75
+ var KEY_DOLLY = 0.9;
63
76
  var THEMES = {
64
77
  light: {
65
78
  background: "#f7f5f0",
@@ -97,7 +110,7 @@ var LIGHT = {
97
110
  fillFrom: [0.7, 0.5, -0.6]
98
111
  };
99
112
  var FOOTPRINT_MM = 12;
100
- function mountReplay(out, p, strip) {
113
+ function mountReplay(out, p, strip, opts = {}) {
101
114
  const theme = THEMES[p.theme];
102
115
  const furniture = theme.colors.furniture;
103
116
  const W = p.width;
@@ -233,35 +246,76 @@ function mountReplay(out, p, strip) {
233
246
  });
234
247
  img.src = `data:image/svg+xml;base64,${btoa(unescape(encodeURIComponent(p.svg)))}`;
235
248
  let disposed = false;
236
- function seek(t) {
237
- if (disposed) return;
249
+ const frameView = { cx, cz, oh, elevation: p.elevation };
250
+ const storeyOf = new Map(p.events.map((e) => [e.key, e.storey ?? 0]));
251
+ const storeys = Math.max(1, ...p.events.filter((e) => !e.hidden).map((e) => (e.storey ?? 0) + 1));
252
+ let shown = storeys;
253
+ let holder = "replay";
254
+ let tNow = 0;
255
+ const halfDiagonal = size.length() / 2;
256
+ const reachFrom = (target) => Math.max(radius, mid.distanceTo(target) + halfDiagonal);
257
+ function place(f, r) {
258
+ const { fov, dist, elevation, azimuth } = f;
259
+ const target = new Vector3(...f.target);
260
+ const el = rad(elevation);
261
+ persp.fov = fov;
262
+ persp.near = Math.max(10, dist - r);
263
+ persp.far = dist + r;
264
+ persp.updateProjectionMatrix();
265
+ persp.up.set(0, 0, -1);
266
+ if (el < rad(89.99)) persp.up.set(0, 1, 0);
267
+ persp.position.set(...cameraPosition([target.x, target.y, target.z], dist, { elevation, azimuth }));
268
+ persp.lookAt(target);
269
+ }
270
+ const viewScene = { events: p.events, fits, frame: frameView, box: boxOfNodes(p.nodes), aspect };
271
+ const presets = /* @__PURE__ */ new Map();
272
+ const presetOf = (name) => {
273
+ const known = presets.get(name);
274
+ if (known) return known;
275
+ const f = presetCamera(name, viewScene);
276
+ presets.set(name, f);
277
+ return f;
278
+ };
279
+ let controls;
280
+ let halt = () => {
281
+ };
282
+ let unhook = () => {
283
+ };
284
+ let primeAgain = () => {
285
+ };
286
+ const listeners = /* @__PURE__ */ new Set();
287
+ const notify = () => {
288
+ for (const fn of listeners) fn();
289
+ };
290
+ function draw(t) {
291
+ tNow = t;
238
292
  if (strip && p.facts) paintStrip(strip, stripAt(p.facts, p.events, Math.min(1, Math.max(0, t))));
239
293
  const s = stateAt(p.events, t);
240
294
  for (const [key2, { g, flat }] of nodes) {
241
295
  const st = s.nodes.get(key2);
242
- g.visible = st?.visible === true;
296
+ g.visible = st?.visible === true && (storeyOf.get(key2) ?? 0) < shown;
243
297
  g.scale.y = Math.max(flat, st?.rise ?? 0);
244
298
  }
245
299
  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;
300
+ if (holder === "free" && controls) {
301
+ const dist = persp.position.distanceTo(controls.target);
302
+ const r = reachFrom(controls.target);
303
+ persp.near = Math.max(10, dist - r);
304
+ persp.far = dist + r;
254
305
  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);
306
+ cam = persp;
307
+ } else if (holder !== "replay" && holder !== "free") {
308
+ const f = presetOf(holder);
309
+ place(f, reachFrom(new Vector3(...f.target)));
310
+ cam = persp;
311
+ } else if (s.tilt > 0 || s.dolly > 0) {
312
+ place(frameCamera(s, fits, frameView), radius);
259
313
  cam = persp;
260
314
  }
261
315
  renderer.render(scene, cam);
262
316
  ctx.globalAlpha = 1;
263
317
  ctx.drawImage(gl, 0, 0);
264
- if (s.svg > 0 && img.complete && img.naturalWidth > 0) {
318
+ if (holder === "replay" && s.svg > 0 && img.complete && img.naturalWidth > 0) {
265
319
  const k = Math.min(W / vw, H / vh);
266
320
  const dw = vw * k;
267
321
  const dh = vh * k;
@@ -270,25 +324,205 @@ function mountReplay(out, p, strip) {
270
324
  ctx.globalAlpha = 1;
271
325
  }
272
326
  }
327
+ function seek(t) {
328
+ if (disposed) return;
329
+ if (holder !== "replay") {
330
+ halt();
331
+ holder = "replay";
332
+ notify();
333
+ }
334
+ draw(t);
335
+ primeAgain();
336
+ }
273
337
  async function frameHash() {
274
338
  const data = ctx.getImageData(0, 0, W, H).data;
275
339
  const digest = await crypto.subtle.digest("SHA-256", data);
276
340
  return [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, "0")).join("");
277
341
  }
342
+ const view = opts.interactive ? interact() : void 0;
343
+ function interact() {
344
+ const c = new OrbitControls(persp, out);
345
+ controls = c;
346
+ c.enableDamping = false;
347
+ c.screenSpacePanning = false;
348
+ c.minPolarAngle = rad(90 - ELEVATION_RANGE.max);
349
+ c.maxPolarAngle = rad(90 - ELEVATION_RANGE.min);
350
+ const bounds = targetBounds(box);
351
+ c.cursor.set(...bounds.centre);
352
+ const tabIndex0 = out.getAttribute("tabindex");
353
+ out.tabIndex = 0;
354
+ let loop = 0;
355
+ let dragging = false;
356
+ let quiet = false;
357
+ const reduced = () => window.matchMedia?.("(prefers-reduced-motion: reduce)").matches === true;
358
+ const settle = () => {
359
+ loop = 0;
360
+ if (disposed || holder !== "free") return;
361
+ if (c.update() || dragging) loop = requestAnimationFrame(settle);
362
+ };
363
+ const kick = () => {
364
+ if (c.enableDamping && !loop) loop = requestAnimationFrame(settle);
365
+ };
366
+ halt = () => {
367
+ if (loop) cancelAnimationFrame(loop);
368
+ loop = 0;
369
+ dragging = false;
370
+ if (!c.enableDamping) return;
371
+ quiet = true;
372
+ c.enableDamping = false;
373
+ c.update();
374
+ quiet = false;
375
+ };
376
+ const aim = (position, target, fov) => {
377
+ persp.fov = fov;
378
+ persp.up.set(0, 1, 0);
379
+ persp.position.set(...position);
380
+ c.target.set(...pivotAbove(position, target, viewScene.box.lo[1]));
381
+ persp.lookAt(c.target);
382
+ const dist = persp.position.distanceTo(c.target);
383
+ const lim = distanceLimits(fits, fov);
384
+ c.minDistance = Math.min(lim.min, dist);
385
+ c.maxDistance = Math.max(lim.max, dist);
386
+ c.maxTargetRadius = Math.max(bounds.radius, c.target.distanceTo(c.cursor));
387
+ c.enableDamping = !reduced();
388
+ };
389
+ const prime = () => {
390
+ if (holder === "free") return;
391
+ const f = holder === "replay" ? takenLens(frameCamera(stateAt(p.events, tNow), fits, frameView)) : presetOf(holder);
392
+ aim(cameraPosition(f.target, f.dist, f), f.target, f.fov);
393
+ };
394
+ const own = () => {
395
+ if (holder === "free") return;
396
+ holder = "free";
397
+ opts.onTake?.();
398
+ notify();
399
+ kick();
400
+ };
401
+ let armed = false;
402
+ primeAgain = () => {
403
+ if (armed) prime();
404
+ };
405
+ c.addEventListener("start", () => {
406
+ prime();
407
+ armed = true;
408
+ dragging = true;
409
+ });
410
+ c.addEventListener("end", () => {
411
+ armed = false;
412
+ dragging = false;
413
+ });
414
+ c.addEventListener("change", () => {
415
+ if (quiet || disposed) return;
416
+ armed = false;
417
+ own();
418
+ draw(tNow);
419
+ });
420
+ const turn = rad(KEY_TURN_DEG);
421
+ const keys = /* @__PURE__ */ new Map([
422
+ ["ArrowLeft", () => c.rotateLeft(turn)],
423
+ ["ArrowRight", () => c.rotateLeft(-turn)],
424
+ ["ArrowUp", () => c.rotateUp(turn)],
425
+ ["ArrowDown", () => c.rotateUp(-turn)],
426
+ ["+", () => c.dollyIn(KEY_DOLLY)],
427
+ ["=", () => c.dollyIn(KEY_DOLLY)],
428
+ ["-", () => c.dollyOut(KEY_DOLLY)],
429
+ ["_", () => c.dollyOut(KEY_DOLLY)]
430
+ ]);
431
+ const onKey = (e) => {
432
+ const act = keys.get(e.key);
433
+ if (!act || disposed || e.ctrlKey || e.metaKey || e.altKey) return;
434
+ e.preventDefault();
435
+ prime();
436
+ act();
437
+ kick();
438
+ };
439
+ out.addEventListener("keydown", onKey);
440
+ unhook = () => {
441
+ out.removeEventListener("keydown", onKey);
442
+ c.dispose();
443
+ if (tabIndex0 === null) out.removeAttribute("tabindex");
444
+ else out.setAttribute("tabindex", tabIndex0);
445
+ listeners.clear();
446
+ };
447
+ const clampStoreys = (k) => Math.min(storeys, Math.max(1, Math.round(k)));
448
+ const shownState = () => shown === storeys ? "all" : shown;
449
+ return {
450
+ at: (t) => {
451
+ if (!disposed) draw(t);
452
+ },
453
+ // A preset takes the view as a drag does: the host pauses, or its next frame would hand the
454
+ // camera straight back to the replay.
455
+ preset: (name) => {
456
+ if (disposed) return;
457
+ halt();
458
+ holder = name;
459
+ opts.onTake?.();
460
+ notify();
461
+ draw(tNow);
462
+ },
463
+ reset: () => {
464
+ if (disposed) return;
465
+ halt();
466
+ holder = "replay";
467
+ shown = storeys;
468
+ notify();
469
+ draw(tNow);
470
+ },
471
+ storeys,
472
+ showStoreys: (k) => {
473
+ if (disposed) return;
474
+ shown = clampStoreys(k);
475
+ notify();
476
+ draw(tNow);
477
+ },
478
+ onChange: (fn) => {
479
+ listeners.add(fn);
480
+ return () => listeners.delete(fn);
481
+ },
482
+ get shown() {
483
+ return shown;
484
+ },
485
+ get camera() {
486
+ return holder;
487
+ },
488
+ state: () => holder === "free" ? {
489
+ camera: "free",
490
+ position: persp.position.toArray(),
491
+ target: c.target.toArray(),
492
+ fov: persp.fov,
493
+ storeys: shownState()
494
+ } : { camera: holder, storeys: shownState() },
495
+ restore: (s) => {
496
+ if (disposed) return;
497
+ halt();
498
+ shown = s.storeys === "all" ? storeys : clampStoreys(s.storeys);
499
+ if (s.camera === "free") aim(s.position, s.target, s.fov);
500
+ holder = s.camera;
501
+ if (holder !== "replay") opts.onTake?.();
502
+ notify();
503
+ }
504
+ };
505
+ }
278
506
  function dispose() {
279
507
  if (disposed) return;
508
+ halt();
280
509
  disposed = true;
510
+ unhook();
281
511
  for (const o of owned) o.dispose();
282
512
  renderer.dispose();
283
513
  renderer.forceContextLoss();
284
514
  }
285
- return { seek, frameHash, ready, dispose };
515
+ return { seek, frameHash, ready, dispose, ...view ? { view } : {} };
286
516
  }
287
517
  export {
518
+ PRESET_LABELS,
288
519
  PREVIEW_LABEL,
520
+ VIEW_CONTROLS_LABEL,
521
+ VIEW_HINT,
289
522
  mountReplay,
290
523
  paintStrip,
291
524
  replayPayload,
292
525
  stripAt,
293
- viewBoxOf
526
+ viewBoxOf,
527
+ viewToolbar
294
528
  };