@graview/graverse 0.0.0-stage → 0.2.20

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/dist/space.js ADDED
@@ -0,0 +1,757 @@
1
+ import { AdditiveBlending, BufferAttribute, BufferGeometry, CanvasTexture, CircleGeometry, Color, DoubleSide, FogExp2, Group, Line, LineBasicMaterial, LineDashedMaterial, LineLoop, LineSegments, Mesh, MeshBasicMaterial, PerspectiveCamera, Points, PointsMaterial, QuadraticBezierCurve3, Scene, Sprite, SpriteMaterial, Vector3, WebGLRenderer, } from "three";
2
+ import { add, distance, dragTo, framing, glide, pointUnder, positionOf, project, rayAt, turnAbout, vec, viewOffset, zoomAbout } from "./camera.js";
3
+ import { labelsFor } from "./labels.js";
4
+ import { kindPlaces, recordPlaces } from "./layout.js";
5
+ /*
6
+ * THE GRAVERSE, DRAWN (Graview HQ feature:f-graverse-page). The page's
7
+ * `./space` entry, the only module that imports `three`, fetched by the page
8
+ * after it is up and only where WebGL is there.
9
+ *
10
+ * Spaces are faint grounds that firm up when pointed at or chosen. An app far
11
+ * away is one point of light, as bright as it is recent. Come near and its
12
+ * records arrive, asked of the app live as this seat (its cloud), and gather
13
+ * into its kinds where the app's own city map puts them; the point of light
14
+ * gives way to them, one object becoming the other, never a cut. Links
15
+ * inside an app are drawn only when near; lanes between apps only for what
16
+ * they share. Every move is the camera's arithmetic (`./camera`): a drag
17
+ * keeps the grabbed point under the pointer, a zoom keeps the point under
18
+ * the pointer, a turn keeps the pressed point, a choice glides, and any
19
+ * input stops a glide.
20
+ */
21
+ const WARM = new Color(1, 0.84, 0.52);
22
+ const clamp = (x, low, high) => Math.max(low, Math.min(high, x));
23
+ const v3 = (p) => new Vector3(p.x, p.y, p.z);
24
+ function disc(stops) {
25
+ const canvas = document.createElement("canvas");
26
+ canvas.width = canvas.height = 64;
27
+ const g = canvas.getContext("2d");
28
+ const r = g.createRadialGradient(32, 32, 0, 32, 32, 32);
29
+ for (const [at, alpha] of stops)
30
+ r.addColorStop(at, `rgba(255,255,255,${alpha})`);
31
+ g.fillStyle = r;
32
+ g.fillRect(0, 0, 64, 64);
33
+ return new CanvasTexture(canvas);
34
+ }
35
+ function ringTexture() {
36
+ const canvas = document.createElement("canvas");
37
+ canvas.width = canvas.height = 64;
38
+ const g = canvas.getContext("2d");
39
+ g.strokeStyle = "#fff";
40
+ g.lineWidth = 3;
41
+ g.beginPath();
42
+ g.arc(32, 32, 25, 0, Math.PI * 2);
43
+ g.stroke();
44
+ return new CanvasTexture(canvas);
45
+ }
46
+ function circle(radius, segments = 128) {
47
+ const points = [];
48
+ for (let i = 0; i < segments; i++) {
49
+ const t = (i / segments) * Math.PI * 2;
50
+ points.push(new Vector3(Math.cos(t) * radius, 0, Math.sin(t) * radius));
51
+ }
52
+ return new BufferGeometry().setFromPoints(points);
53
+ }
54
+ export function mountSpace(stage, host) {
55
+ let renderer;
56
+ try {
57
+ renderer = new WebGLRenderer({ antialias: true, alpha: true });
58
+ }
59
+ catch {
60
+ return null;
61
+ }
62
+ const first = host.view();
63
+ const layout = first.layout;
64
+ renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
65
+ renderer.setClearColor(0, 0);
66
+ const canvas = renderer.domElement;
67
+ canvas.tabIndex = 0;
68
+ canvas.setAttribute("role", "application");
69
+ canvas.setAttribute("aria-label", "Every app, drawn as points of light. Arrow keys move, plus and minus zoom, Q and E turn, Enter opens what is chosen. The list holds the same apps.");
70
+ stage.append(canvas);
71
+ const labelsLayer = document.createElement("div");
72
+ labelsLayer.className = "gv-labels";
73
+ labelsLayer.setAttribute("aria-hidden", "true");
74
+ stage.append(labelsLayer);
75
+ const tip = document.createElement("div");
76
+ tip.className = "gv-tip";
77
+ tip.hidden = true;
78
+ stage.append(tip);
79
+ const scene = new Scene();
80
+ // Depth said by fog, measured from where the camera looks: what it looks at is clear at any distance, what lies far behind fades.
81
+ const fog = new FogExp2(0x070910, 0.32 / Math.max(layout.extent, 200));
82
+ scene.fog = fog;
83
+ const camera3 = new PerspectiveCamera(46, 1, 1, 200_000);
84
+ const POINT = disc([
85
+ [0, 1],
86
+ [0.22, 0.9],
87
+ [0.5, 0.18],
88
+ [1, 0],
89
+ ]);
90
+ const HAZE = disc([
91
+ [0, 0.5],
92
+ [0.4, 0.18],
93
+ [1, 0],
94
+ ]);
95
+ const RING = ringTexture();
96
+ const size = () => ({ width: stage.clientWidth || window.innerWidth, height: stage.clientHeight || window.innerHeight });
97
+ /** What the panels cover now, eased toward what they will cover, so the picture slides aside rather than jumps. */
98
+ let inset = host.inset();
99
+ const view = () => ({ ...size(), inset });
100
+ /** The view once the panels have settled: what a glide aims for. */
101
+ const settled = () => ({ ...size(), inset: host.inset() });
102
+ const base = { target: layout.center, distance: 1000, yaw: 0.32, pitch: 0.78, fov: 46 };
103
+ const homeOf = () => framing(base, layout.center, layout.extent, settled());
104
+ let home = homeOf();
105
+ let cam = { ...home };
106
+ let gliding = null;
107
+ const farthest = () => home.distance * 2.5;
108
+ /* ── spaces ───────────────────────────────────────────────────────── */
109
+ const hues = new Map(layout.spaces.map((space, i) => [space.space, (i * 0.618034 + 0.58) % 1]));
110
+ const grounds = layout.spaces.map((space) => {
111
+ const group = new Group();
112
+ group.position.copy(v3(space.at));
113
+ const ring = new LineLoop(circle(space.radius), new LineBasicMaterial({ color: 0xaab8dc, transparent: true, opacity: 0.1, depthWrite: false }));
114
+ const floor = new Mesh(new CircleGeometry(space.radius, 96), new MeshBasicMaterial({ color: 0x8fa3d8, transparent: true, opacity: 0.012, depthWrite: false, side: DoubleSide }));
115
+ floor.rotation.x = -Math.PI / 2;
116
+ group.add(ring, floor);
117
+ scene.add(group);
118
+ return { space, ring, floor };
119
+ });
120
+ /* ── apps ─────────────────────────────────────────────────────────── */
121
+ const apps = new Map();
122
+ for (const placed of layout.apps) {
123
+ const hue = hues.get(placed.space) ?? 0.6;
124
+ const coreColor = new Color().setHSL(hue, 0.5, 0.82);
125
+ const core = new Sprite(new SpriteMaterial({ map: POINT, color: coreColor.clone(), transparent: true, depthWrite: false, sizeAttenuation: false, blending: AdditiveBlending }));
126
+ core.position.copy(v3(placed.at));
127
+ const haze = new Sprite(new SpriteMaterial({ map: HAZE, color: new Color().setHSL(hue, 0.55, 0.6), transparent: true, opacity: 0.12, depthWrite: false, blending: AdditiveBlending }));
128
+ haze.position.copy(v3(placed.at));
129
+ haze.scale.setScalar(placed.radius * 2.6);
130
+ scene.add(haze, core);
131
+ let ring;
132
+ if (!placed.readable) {
133
+ ring = new LineLoop(circle(placed.radius * 0.55, 48), new LineDashedMaterial({ color: 0x9aa8cc, dashSize: 4, gapSize: 5, transparent: true, opacity: 0.5 }));
134
+ ring.computeLineDistances();
135
+ ring.position.copy(v3(placed.at));
136
+ scene.add(ring);
137
+ }
138
+ apps.set(placed.address, { placed, hue, core, coreColor, haze, ...(ring ? { ring } : {}) });
139
+ }
140
+ /* ── lanes: only for what two apps share ──────────────────────────── */
141
+ const lanes = [];
142
+ {
143
+ const pairs = new Set();
144
+ const addLane = (a, b) => {
145
+ const key = a < b ? `${a} ${b}` : `${b} ${a}`;
146
+ const A = apps.get(a);
147
+ const B = apps.get(b);
148
+ if (!A || !B || a === b || pairs.has(key))
149
+ return;
150
+ pairs.add(key);
151
+ const from = v3(A.placed.at);
152
+ const to = v3(B.placed.at);
153
+ const lift = from.distanceTo(to) * 0.18;
154
+ const curve = new QuadraticBezierCurve3(from, from.clone().lerp(to, 0.5).setY(lift * 2), to);
155
+ const line = new Line(new BufferGeometry().setFromPoints(curve.getPoints(60)), new LineDashedMaterial({ color: 0xe2bd78, dashSize: 9, gapSize: 9, transparent: true, opacity: 0.45 }));
156
+ line.computeLineDistances();
157
+ line.visible = false;
158
+ scene.add(line);
159
+ lanes.push({ a, b, line });
160
+ };
161
+ for (const one of first.description.apps) {
162
+ for (const out of one.shape?.reachesOut ?? []) {
163
+ const target = first.description.apps.find((other) => out.address.toLowerCase().startsWith(other.app.address.toLowerCase()));
164
+ if (target)
165
+ addLane(one.app.address, target.app.address);
166
+ }
167
+ }
168
+ for (const overlap of first.description.overlap)
169
+ if (overlap.records || overlap.kinds.length >= 2)
170
+ addLane(overlap.apps[0], overlap.apps[1]);
171
+ }
172
+ /* ── a record chosen: its ring and its links ──────────────────────── */
173
+ const marked = new Group();
174
+ scene.add(marked);
175
+ let markedFor = "";
176
+ function drawCloud(drawn, source) {
177
+ const kinds = kindPlaces(host.view().description.apps.find((one) => one.app.address === drawn.placed.address) ?? { app: { address: drawn.placed.address, name: "", url: "", space: drawn.placed.space } }, source, drawn.placed.radius);
178
+ const at = recordPlaces(drawn.placed.address, source, kinds);
179
+ const n = at.length;
180
+ const positions = new Float32Array(n * 3);
181
+ const colors = new Float32Array(n * 3);
182
+ const kindColors = source.kinds.map((_, i) => new Color().setHSL((drawn.hue + (source.kinds.length === 1 ? 0 : (i / (source.kinds.length - 1) - 0.5) * 0.16) + 1) % 1, 0.6, i % 2 ? 0.74 : 0.62));
183
+ at.forEach((p, i) => {
184
+ positions.set([drawn.placed.at.x + p.x, drawn.placed.at.y + p.y, drawn.placed.at.z + p.z], i * 3);
185
+ const c = kindColors[source.points[i].k] ?? new Color(0.7, 0.75, 0.9);
186
+ colors.set([c.r, c.g, c.b], i * 3);
187
+ });
188
+ const geometry = new BufferGeometry();
189
+ geometry.setAttribute("position", new BufferAttribute(positions, 3));
190
+ geometry.setAttribute("color", new BufferAttribute(colors.slice(), 3));
191
+ const size = clamp(drawn.placed.radius * 0.13, 4, 11);
192
+ const points = new Points(geometry, new PointsMaterial({ size, map: POINT, vertexColors: true, transparent: true, opacity: 0, depthWrite: false, blending: AdditiveBlending }));
193
+ scene.add(points);
194
+ let links;
195
+ const pairs = [];
196
+ source.points.forEach((point, i) => {
197
+ for (const j of point.to)
198
+ if (j > i)
199
+ pairs.push(i, j);
200
+ });
201
+ if (pairs.length > 0) {
202
+ const lp = new Float32Array(pairs.length * 3);
203
+ pairs.forEach((index, k) => lp.set(positions.subarray(index * 3, index * 3 + 3), k * 3));
204
+ const lg = new BufferGeometry();
205
+ lg.setAttribute("position", new BufferAttribute(lp, 3));
206
+ links = new LineSegments(lg, new LineBasicMaterial({ color: new Color().setHSL(drawn.hue, 0.5, 0.75), transparent: true, opacity: 0, depthWrite: false, blending: AdditiveBlending }));
207
+ scene.add(links);
208
+ }
209
+ drawn.cloud = { source, kinds, at, points, base: colors, ...(links ? { links } : {}), born: performance.now(), size };
210
+ }
211
+ /** Where a record of an app's cloud stands in the world. */
212
+ const recordAt = (drawn, index) => add(drawn.placed.at, drawn.cloud.at[index]);
213
+ /* ── picking, in the camera's own arithmetic ──────────────────────── */
214
+ function pick(x, y) {
215
+ const v = view();
216
+ const eye = positionOf(cam);
217
+ let best = null;
218
+ // Records first, in an app near enough to read them.
219
+ for (const drawn of apps.values()) {
220
+ if (!drawn.cloud || distance(eye, drawn.placed.at) / drawn.placed.radius > 8)
221
+ continue;
222
+ drawn.cloud.at.forEach((_, i) => {
223
+ const at = recordAt(drawn, i);
224
+ const seen = project(cam, v, at);
225
+ if (seen.depth <= 0)
226
+ return;
227
+ const by = Math.hypot(seen.x - x, seen.y - y);
228
+ if (by < 9 && (!best || by < best.by))
229
+ best = { picked: { at, space: drawn.placed.space, app: drawn.placed.address, point: i }, by };
230
+ });
231
+ }
232
+ if (best)
233
+ return best.picked;
234
+ const half = Math.tan((cam.fov * Math.PI) / 360);
235
+ for (const drawn of apps.values()) {
236
+ const seen = project(cam, v, drawn.placed.at);
237
+ if (seen.depth <= 0)
238
+ continue;
239
+ const reach = Math.max(14, (drawn.placed.radius / (seen.depth * half)) * (v.height / 2) * 0.9);
240
+ const by = Math.hypot(seen.x - x, seen.y - y);
241
+ if (by < reach && (!best || by < best.by))
242
+ best = { picked: { at: drawn.placed.at, space: drawn.placed.space, app: drawn.placed.address }, by };
243
+ }
244
+ if (best)
245
+ return best.picked;
246
+ // A label under the pointer: what it names.
247
+ const box = canvas.getBoundingClientRect();
248
+ for (const [key, node] of [...shown].reverse()) {
249
+ const r = node.getBoundingClientRect();
250
+ if (x + box.left < r.left || x + box.left > r.right || y + box.top < r.top || y + box.top > r.bottom)
251
+ continue;
252
+ const label = lastLabels.find((one) => one.key === key);
253
+ if (!label)
254
+ continue;
255
+ const drawn = label.app ? apps.get(label.app) : undefined;
256
+ if (drawn && key.startsWith("record:") && drawn.cloud) {
257
+ const index = drawn.cloud.source.points.findIndex((point) => `record:${drawn.placed.address}#${point.id}` === key);
258
+ if (index !== -1)
259
+ return { at: recordAt(drawn, index), space: label.space, app: drawn.placed.address, point: index };
260
+ }
261
+ if (drawn)
262
+ return { at: drawn.placed.at, space: label.space, app: drawn.placed.address };
263
+ const space = layout.spaces.find((one) => one.space === label.space);
264
+ if (space)
265
+ return { at: space.at, space: space.space };
266
+ }
267
+ // The ground of a space, where the ray from the pointer meets it.
268
+ const ray = rayAt(cam, v, x, y);
269
+ if (Math.abs(ray.direction.y) > 1e-9) {
270
+ const t = -ray.origin.y / ray.direction.y;
271
+ if (t > 0) {
272
+ const at = vec(ray.origin.x + ray.direction.x * t, 0, ray.origin.z + ray.direction.z * t);
273
+ const space = layout.spaces.find((one) => distance(one.at, at) <= one.radius);
274
+ if (space)
275
+ return { at, space: space.space };
276
+ }
277
+ }
278
+ return null;
279
+ }
280
+ /* ── where a choice is framed ─────────────────────────────────────── */
281
+ function goalFor(chosen) {
282
+ const drawn = chosen.app ? apps.get(chosen.app) : undefined;
283
+ if (drawn && chosen.record && drawn.cloud) {
284
+ const index = drawn.cloud.source.points.findIndex((point) => point.id === chosen.record.id);
285
+ if (index !== -1)
286
+ return { ...cam, target: recordAt(drawn, index), distance: Math.max(24, drawn.placed.radius * 0.7) };
287
+ }
288
+ if (drawn)
289
+ return framing(cam, drawn.placed.at, drawn.placed.radius * (drawn.cloud ? 0.95 : 1.3), settled());
290
+ const space = layout.spaces.find((one) => one.space === chosen.space);
291
+ if (space)
292
+ return framing(cam, space.at, space.radius * 1.05, settled());
293
+ home = homeOf();
294
+ return { ...home, yaw: cam.yaw, pitch: home.pitch };
295
+ }
296
+ function frame(chosen) {
297
+ const goal = goalFor(chosen);
298
+ gliding = { glide: glide(cam, goal, { reducedMotion: host.reducedMotion() }), started: performance.now() };
299
+ wake();
300
+ }
301
+ const halt = () => {
302
+ gliding = null;
303
+ };
304
+ /* ── input ────────────────────────────────────────────────────────── */
305
+ const pointers = new Map();
306
+ let drag = null;
307
+ let pointed = null;
308
+ let mouse = null;
309
+ const local = (event) => {
310
+ const box = canvas.getBoundingClientRect();
311
+ return { x: event.clientX - box.left, y: event.clientY - box.top };
312
+ };
313
+ canvas.addEventListener("contextmenu", (event) => event.preventDefault());
314
+ canvas.addEventListener("pointerdown", (event) => {
315
+ halt();
316
+ const at = local(event);
317
+ pointers.set(event.pointerId, at);
318
+ canvas.setPointerCapture(event.pointerId);
319
+ if (pointers.size === 2) {
320
+ const [a, b] = [...pointers.values()];
321
+ const middle = { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };
322
+ drag = { x: middle.x, y: middle.y, grab: pointUnder(cam, view(), middle.x, middle.y, cam.target), turn: false, moved: 99, button: 0, pinch: { span: Math.hypot(a.x - b.x, a.y - b.y), anchor: pointUnder(cam, view(), middle.x, middle.y, cam.target) } };
323
+ return;
324
+ }
325
+ const picked = pick(at.x, at.y);
326
+ drag = { x: at.x, y: at.y, // The pressed pixel itself, at the depth of what it pressed: a drag holds that point, and a turn swings about it.
327
+ grab: pointUnder(cam, view(), at.x, at.y, picked?.at ?? cam.target), turn: event.button !== 0 || event.altKey || event.shiftKey, moved: 0, button: event.button };
328
+ });
329
+ canvas.addEventListener("pointermove", (event) => {
330
+ const at = local(event);
331
+ mouse = at;
332
+ if (pointers.has(event.pointerId))
333
+ pointers.set(event.pointerId, at);
334
+ if (!drag) {
335
+ wake();
336
+ return;
337
+ }
338
+ if (drag.pinch && pointers.size === 2) {
339
+ const [a, b] = [...pointers.values()];
340
+ const span = Math.hypot(a.x - b.x, a.y - b.y);
341
+ if (span > 0)
342
+ cam = zoomAbout(cam, drag.pinch.anchor, drag.pinch.span / span, farthest());
343
+ drag.pinch = { span, anchor: drag.pinch.anchor };
344
+ wake();
345
+ return;
346
+ }
347
+ const dx = at.x - drag.x;
348
+ const dy = at.y - drag.y;
349
+ drag.moved += Math.abs(dx) + Math.abs(dy);
350
+ if (drag.moved < 5)
351
+ return;
352
+ if (drag.turn) {
353
+ cam = turnAbout(cam, drag.grab, -dx * 0.0052, dy * 0.0052);
354
+ drag.x = at.x;
355
+ drag.y = at.y;
356
+ }
357
+ else
358
+ cam = dragTo(cam, view(), drag.grab, at.x, at.y);
359
+ wake();
360
+ });
361
+ const release = (event) => {
362
+ pointers.delete(event.pointerId);
363
+ // One finger lifted from a pinch: the other carries on as a fresh drag from where it is, never a jump.
364
+ if (pointers.size === 1 && drag?.pinch) {
365
+ const [left] = [...pointers.values()];
366
+ drag = { x: left.x, y: left.y, grab: pointUnder(cam, view(), left.x, left.y, drag.pinch.anchor), turn: false, moved: 99, button: 0 };
367
+ return;
368
+ }
369
+ const was = drag;
370
+ if (pointers.size > 0)
371
+ return;
372
+ drag = null;
373
+ if (!was || was.moved >= 5 || was.button !== 0 || event.type === "pointercancel")
374
+ return;
375
+ const at = local(event);
376
+ const picked = pick(at.x, at.y);
377
+ if (!picked)
378
+ return;
379
+ const now = host.view().chosen;
380
+ if (picked.app && picked.point !== undefined) {
381
+ const drawn = apps.get(picked.app);
382
+ const record = host.recordOf(picked.app, drawn.cloud.source.points[picked.point]);
383
+ if (!record)
384
+ return;
385
+ // A record chosen again opens.
386
+ if (now.record?.id === record.id && now.app === picked.app)
387
+ return host.open(record.url);
388
+ host.choose({ space: picked.space, app: picked.app, record });
389
+ frame(host.view().chosen);
390
+ return;
391
+ }
392
+ if (picked.app) {
393
+ const app = host.view().description.apps.find((one) => one.app.address === picked.app);
394
+ // An app chosen again is entered: its own page, through the hub.
395
+ if (now.app === picked.app && !now.record && app && (app.state === "ready" || app.state === "seed-only"))
396
+ return host.open(app.app.url);
397
+ host.choose({ space: picked.space, app: picked.app });
398
+ frame(host.view().chosen);
399
+ return;
400
+ }
401
+ host.choose({ space: picked.space });
402
+ frame(host.view().chosen);
403
+ };
404
+ canvas.addEventListener("pointerup", release);
405
+ canvas.addEventListener("pointercancel", release);
406
+ canvas.addEventListener("pointerleave", () => {
407
+ mouse = null;
408
+ wake();
409
+ });
410
+ canvas.addEventListener("wheel", (event) => {
411
+ event.preventDefault();
412
+ halt();
413
+ const at = local(event);
414
+ const dy = clamp(event.deltaY * (event.ctrlKey ? 4 : 1) * (event.deltaMode === 1 ? 32 : 1), -240, 240);
415
+ // The app under the pointer is held where it is; over nothing, the point under the pointer is.
416
+ const anchor = pick(at.x, at.y)?.at ?? pointUnder(cam, view(), at.x, at.y, cam.target);
417
+ cam = zoomAbout(cam, anchor, Math.exp(dy * 0.0021), farthest());
418
+ wake();
419
+ }, { passive: false });
420
+ canvas.addEventListener("keydown", (event) => {
421
+ if (event.metaKey || event.ctrlKey || event.altKey)
422
+ return;
423
+ const step = cam.distance * 0.08;
424
+ const forward = vec(-Math.sin(cam.yaw), 0, -Math.cos(cam.yaw));
425
+ const right = vec(Math.cos(cam.yaw), 0, -Math.sin(cam.yaw));
426
+ const move = (by) => {
427
+ halt();
428
+ cam = { ...cam, target: add(cam.target, by) };
429
+ wake();
430
+ };
431
+ const key = event.key.toLowerCase();
432
+ if (key === "arrowup" || key === "w")
433
+ move({ x: forward.x * step, y: 0, z: forward.z * step });
434
+ else if (key === "arrowdown" || key === "s")
435
+ move({ x: -forward.x * step, y: 0, z: -forward.z * step });
436
+ else if (key === "arrowleft" || key === "a")
437
+ move({ x: -right.x * step, y: 0, z: -right.z * step });
438
+ else if (key === "arrowright" || key === "d")
439
+ move({ x: right.x * step, y: 0, z: right.z * step });
440
+ else if (key === "+" || key === "=") {
441
+ halt();
442
+ cam = zoomAbout(cam, cam.target, 0.8, farthest());
443
+ wake();
444
+ }
445
+ else if (key === "-" || key === "_") {
446
+ halt();
447
+ cam = zoomAbout(cam, cam.target, 1.25, farthest());
448
+ wake();
449
+ }
450
+ else if (key === "q" || key === "e") {
451
+ halt();
452
+ cam = turnAbout(cam, cam.target, key === "q" ? 0.12 : -0.12, 0);
453
+ wake();
454
+ }
455
+ else if (key === "enter") {
456
+ const now = host.view();
457
+ const app = now.description.apps.find((one) => one.app.address === now.chosen.app);
458
+ if (now.chosen.record)
459
+ host.open(now.chosen.record.url);
460
+ else if (app && (app.state === "ready" || app.state === "seed-only"))
461
+ host.open(app.app.url);
462
+ else
463
+ return;
464
+ }
465
+ else
466
+ return;
467
+ event.preventDefault();
468
+ });
469
+ /* ── labels ───────────────────────────────────────────────────────── */
470
+ const shown = new Map();
471
+ let lastLabels = [];
472
+ function placeLabels() {
473
+ const state = host.view();
474
+ const v = view();
475
+ const kinds = new Map();
476
+ for (const [address, drawn] of apps)
477
+ if (drawn.cloud)
478
+ kinds.set(address, drawn.cloud.kinds);
479
+ const labels = labelsFor(layout, cam, v, {
480
+ selected: { ...(state.chosen.space ? { space: state.chosen.space } : {}), ...(state.chosen.app ? { app: state.chosen.app } : {}) },
481
+ matches: state.matches,
482
+ ...(pointed ? { pointed: { space: pointed.space, ...(pointed.app ? { app: pointed.app } : {}) } } : {}),
483
+ kinds,
484
+ });
485
+ // Inside the chosen app: its matches named where they stand, and the chosen record with what it links.
486
+ const drawn = state.chosen.app ? apps.get(state.chosen.app) : undefined;
487
+ if (drawn?.cloud) {
488
+ const names = new Set();
489
+ const hits = state.hits.get(drawn.placed.address) ?? [];
490
+ if (state.matches) {
491
+ const ids = new Set(hits.map((hit) => hit.id));
492
+ drawn.cloud.source.points.forEach((point, i) => ids.has(point.id) && names.size < 18 && names.add(i));
493
+ }
494
+ const index = state.chosen.record ? drawn.cloud.source.points.findIndex((point) => point.id === state.chosen.record.id) : -1;
495
+ if (index !== -1) {
496
+ names.add(index);
497
+ for (const j of drawn.cloud.source.points[index].to.slice(0, 12))
498
+ names.add(j);
499
+ drawn.cloud.source.points.forEach((point, i) => point.to.includes(index) && names.size < 26 && names.add(i));
500
+ }
501
+ for (const i of names) {
502
+ const seen = project(cam, v, recordAt(drawn, i));
503
+ if (seen.depth <= 0)
504
+ continue;
505
+ const point = drawn.cloud.source.points[i];
506
+ labels.push({ key: `record:${drawn.placed.address}#${point.id}`, kind: "record", text: point.label, space: drawn.placed.space, app: drawn.placed.address, x: seen.x + 10, y: seen.y, ...(state.matches && hits.some((hit) => hit.id === point.id) ? { hit: true } : {}) });
507
+ }
508
+ }
509
+ lastLabels = labels;
510
+ const keep = new Set();
511
+ for (const label of labels) {
512
+ keep.add(label.key);
513
+ let node = shown.get(label.key);
514
+ if (!node) {
515
+ // Words, not controls: a press on one is the canvas's, which reads what it names (`pick`), so a label never stands over another app's light.
516
+ node = document.createElement("div");
517
+ labelsLayer.append(node);
518
+ shown.set(label.key, node);
519
+ }
520
+ const html = `${escapeText(label.text)}${label.sub ? `<small>${escapeText(label.sub)}</small>` : ""}`;
521
+ if (node.dataset["html"] !== html) {
522
+ node.innerHTML = html;
523
+ node.dataset["html"] = html;
524
+ }
525
+ const record = label.kind === "record";
526
+ node.className = `gv-lb ${label.kind}${label.found ? " found" : ""}${record && label.hit ? " hit" : ""}${(label.app && pointed?.app === label.app) || (label.kind === "space" && pointed?.space === label.space && !pointed.app) ? " on" : ""}`;
527
+ node.dataset["label"] = label.kind;
528
+ node.dataset["space"] = label.space;
529
+ if (label.app)
530
+ node.dataset["app"] = label.app;
531
+ node.style.transform = label.kind === "space" || label.kind === "kind" ? `translate(${label.x}px, ${label.y}px) translate(-50%, -50%)` : `translate(${label.x + 8}px, ${label.y}px) translateY(-50%)`;
532
+ }
533
+ for (const [key, node] of shown) {
534
+ if (keep.has(key))
535
+ continue;
536
+ node.remove();
537
+ shown.delete(key);
538
+ }
539
+ }
540
+ /* ── each frame ───────────────────────────────────────────────────── */
541
+ let awake = false;
542
+ let raf = 0;
543
+ let destroyed = false;
544
+ let fading = 0;
545
+ function wake() {
546
+ if (awake || destroyed)
547
+ return;
548
+ awake = true;
549
+ raf = requestAnimationFrame(tick);
550
+ }
551
+ function resize() {
552
+ const v = view();
553
+ home = homeOf();
554
+ renderer.setSize(v.width, v.height, false);
555
+ camera3.aspect = v.width / v.height;
556
+ camera3.updateProjectionMatrix();
557
+ wake();
558
+ }
559
+ const observer = new ResizeObserver(resize);
560
+ observer.observe(stage);
561
+ let last = 0;
562
+ /** The apps asked for their records on this approach. */
563
+ const approached = new Set();
564
+ /** The picture still sliding aside for a panel. */
565
+ let sliding = false;
566
+ function tick(now) {
567
+ awake = false;
568
+ if (destroyed)
569
+ return;
570
+ let again = false;
571
+ const dt = last ? Math.min(0.05, (now - last) / 1000) : 0.016;
572
+ last = now;
573
+ // The panels' cover, eased: the picture slides aside as a panel opens, the same picture throughout.
574
+ const goal = host.inset();
575
+ const ease = host.reducedMotion() ? 1 : 1 - Math.exp(-dt * 12);
576
+ const near = (a, b) => Math.abs(a - b) < 0.5;
577
+ if (!near(goal.top, inset.top) || !near(goal.right, inset.right) || !near(goal.bottom, inset.bottom) || !near(goal.left, inset.left)) {
578
+ inset = { top: inset.top + (goal.top - inset.top) * ease, right: inset.right + (goal.right - inset.right) * ease, bottom: inset.bottom + (goal.bottom - inset.bottom) * ease, left: inset.left + (goal.left - inset.left) * ease };
579
+ again = true;
580
+ sliding = true;
581
+ }
582
+ else {
583
+ inset = goal;
584
+ sliding = false;
585
+ }
586
+ if (gliding) {
587
+ cam = gliding.glide.at(now - gliding.started);
588
+ if (now - gliding.started >= gliding.glide.ms)
589
+ gliding = null;
590
+ else
591
+ again = true;
592
+ }
593
+ const state = host.view();
594
+ const eye = positionOf(cam);
595
+ fog.density = 0.42 / Math.max(cam.distance * 2.2, 1);
596
+ camera3.fov = cam.fov;
597
+ {
598
+ const v = view();
599
+ const offset = viewOffset(v);
600
+ camera3.setViewOffset(v.width, v.height, offset.x, offset.y, v.width, v.height);
601
+ }
602
+ camera3.position.copy(v3(eye));
603
+ camera3.up.set(0, 1, 0);
604
+ camera3.lookAt(v3(cam.target));
605
+ camera3.updateMatrixWorld();
606
+ if (!drag && mouse) {
607
+ const was = pointed;
608
+ pointed = pick(mouse.x, mouse.y);
609
+ canvas.style.cursor = pointed ? "pointer" : "grab";
610
+ const record = pointed?.app && pointed.point !== undefined ? apps.get(pointed.app)?.cloud?.source.points[pointed.point] : undefined;
611
+ tip.hidden = !record;
612
+ if (record) {
613
+ tip.textContent = record.label;
614
+ tip.style.transform = `translate(${mouse.x + 14}px, ${mouse.y + 14}px)`;
615
+ }
616
+ if (was?.app !== pointed?.app || was?.space !== pointed?.space)
617
+ again = true;
618
+ }
619
+ else if (!mouse && pointed) {
620
+ pointed = null;
621
+ tip.hidden = true;
622
+ }
623
+ const searching = state.matches !== null;
624
+ for (const [address, drawn] of apps) {
625
+ const near = distance(eye, drawn.placed.at) / drawn.placed.radius;
626
+ const found = state.matches?.get(address) ?? 0;
627
+ const dim = searching && found === 0;
628
+ const cloud = state.clouds.get(address);
629
+ // The camera came near: the app's records are asked for, live, as this seat.
630
+ // Asked once each approach: one refused or slow is asked again when the camera has gone and come back.
631
+ if (near > 12)
632
+ approached.delete(address);
633
+ else if (drawn.placed.readable && near < 9 && !approached.has(address) && (cloud === undefined || (typeof cloud === "object" && "refused" in cloud))) {
634
+ approached.add(address);
635
+ host.ask(address);
636
+ }
637
+ if (cloud && typeof cloud === "object" && "points" in cloud && !drawn.cloud)
638
+ drawCloud(drawn, cloud);
639
+ const chosen = state.chosen.app === address;
640
+ // One object becoming another: the point of light gives way to the records as they arrive and as the camera nears.
641
+ const arrived = drawn.cloud ? clamp((now - drawn.cloud.born) / 600, 0, 1) : 0;
642
+ if (arrived < 1 && drawn.cloud) {
643
+ again = true;
644
+ fading = now;
645
+ }
646
+ const recordsShow = drawn.cloud ? arrived * clamp((14 - near) / 8, 0, 1) : 0;
647
+ const coreMaterial = drawn.core.material;
648
+ coreMaterial.color.copy(searching && found > 0 ? WARM : drawn.coreColor);
649
+ coreMaterial.opacity = (drawn.placed.readable ? 0.4 + 0.6 * drawn.placed.brightness : 0.25) * (dim ? 0.15 : 1) * (1 - recordsShow * 0.85);
650
+ const coreSize = (0.02 + 0.008 * Math.log10(1 + drawn.placed.records)) * (searching && found > 0 ? 1.3 + Math.log10(1 + found) * 0.3 : 1) * (chosen ? 1.25 : 1);
651
+ drawn.core.scale.setScalar(coreSize);
652
+ drawn.haze.material.opacity = 0.12 * drawn.placed.brightness * (dim ? 0.25 : 1) * (1 - recordsShow * 0.6);
653
+ if (drawn.cloud) {
654
+ const material = drawn.cloud.points.material;
655
+ material.opacity = recordsShow * (dim ? 0.25 : 1);
656
+ material.size = drawn.cloud.size * clamp(near / 1.8, 0.35, 1);
657
+ drawn.cloud.points.visible = material.opacity > 0.01;
658
+ // While searching, the matches are lit and the rest are dimmed, in place.
659
+ const colors = drawn.cloud.points.geometry.getAttribute("color");
660
+ const hits = searching ? new Set((state.hits.get(address) ?? []).map((hit) => hit.id)) : null;
661
+ const key = hits ? [...hits].join("\n") : "";
662
+ if (drawn.cloud.points.userData["lit"] !== key) {
663
+ drawn.cloud.points.userData["lit"] = key;
664
+ drawn.cloud.source.points.forEach((point, i) => {
665
+ if (!hits)
666
+ colors.setXYZ(i, drawn.cloud.base[i * 3], drawn.cloud.base[i * 3 + 1], drawn.cloud.base[i * 3 + 2]);
667
+ else if (hits.has(point.id))
668
+ colors.setXYZ(i, WARM.r, WARM.g, WARM.b);
669
+ else
670
+ colors.setXYZ(i, drawn.cloud.base[i * 3] * 0.12, drawn.cloud.base[i * 3 + 1] * 0.12, drawn.cloud.base[i * 3 + 2] * 0.12);
671
+ });
672
+ colors.needsUpdate = true;
673
+ }
674
+ if (drawn.cloud.links) {
675
+ drawn.cloud.links.material.opacity = recordsShow * clamp((5 - near) / 3, 0, 1) * (searching ? 0.04 : 0.16);
676
+ drawn.cloud.links.visible = drawn.cloud.links.material.opacity > 0.005;
677
+ }
678
+ }
679
+ }
680
+ for (const { space, ring, floor } of grounds) {
681
+ const active = (state.chosen.space === space.space && !state.chosen.app) || (pointed?.space === space.space && !pointed.app);
682
+ ring.material.opacity = (active ? 0.42 : 0.1) * (searching ? 0.5 : 1);
683
+ floor.material.opacity = active ? 0.04 : 0.012;
684
+ }
685
+ for (const lane of lanes) {
686
+ const involved = (address) => address === lane.a || address === lane.b;
687
+ lane.line.visible = !searching && (involved(state.chosen.app) || involved(pointed?.app));
688
+ }
689
+ // The chosen record: a ring, and lines to what it links.
690
+ const drawn = state.chosen.app ? apps.get(state.chosen.app) : undefined;
691
+ const recordKey = drawn?.cloud && state.chosen.record ? `${drawn.placed.address}#${state.chosen.record.id}` : "";
692
+ if (recordKey !== markedFor) {
693
+ markedFor = recordKey;
694
+ for (const child of [...marked.children])
695
+ marked.remove(child);
696
+ if (drawn?.cloud && state.chosen.record) {
697
+ const index = drawn.cloud.source.points.findIndex((point) => point.id === state.chosen.record.id);
698
+ if (index !== -1) {
699
+ const at = v3(recordAt(drawn, index));
700
+ const ring = new Sprite(new SpriteMaterial({ map: RING, color: 0xffffff, transparent: true, depthTest: false, sizeAttenuation: false }));
701
+ ring.scale.setScalar(0.032);
702
+ ring.position.copy(at);
703
+ marked.add(ring);
704
+ const linked = [...new Set([...drawn.cloud.source.points[index].to, ...drawn.cloud.source.points.flatMap((point, i) => (point.to.includes(index) ? [i] : []))])];
705
+ if (linked.length > 0) {
706
+ const lines = linked.slice(0, 400).flatMap((j) => [at, v3(recordAt(drawn, j))]);
707
+ marked.add(new LineSegments(new BufferGeometry().setFromPoints(lines), new LineBasicMaterial({ color: 0xdfe6ff, transparent: true, opacity: 0.6, depthTest: false })));
708
+ }
709
+ }
710
+ }
711
+ }
712
+ renderer.render(scene, camera3);
713
+ placeLabels();
714
+ published = cam;
715
+ if (again || gliding || now - fading < 700)
716
+ wake();
717
+ }
718
+ let published = cam;
719
+ const debug = {
720
+ camera: () => published,
721
+ project: (address) => {
722
+ const drawn = apps.get(address);
723
+ return drawn ? project(cam, view(), drawn.placed.at) : null;
724
+ },
725
+ labels: () => lastLabels.map((label) => ({ kind: label.kind, text: label.text, sub: label.sub ?? null, space: label.space, app: label.app ?? null })),
726
+ /** Whether the picture is still moving on its own: a glide, or sliding aside for a panel. */
727
+ gliding: () => gliding !== null || sliding,
728
+ /** What a press at a point of the screen would choose. */
729
+ pick: (x, y) => {
730
+ const picked = pick(x, y);
731
+ return picked ? { space: picked.space, app: picked.app ?? null, point: picked.point ?? null } : null;
732
+ },
733
+ /** Where what the camera looks at stands on the screen: the middle of what the panels leave. */
734
+ middle: () => project(cam, view(), cam.target),
735
+ };
736
+ window.__graverse = debug;
737
+ resize();
738
+ wake();
739
+ return {
740
+ changed: () => wake(),
741
+ frame,
742
+ camera: () => cam,
743
+ destroy() {
744
+ destroyed = true;
745
+ cancelAnimationFrame(raf);
746
+ observer.disconnect();
747
+ renderer.dispose();
748
+ canvas.remove();
749
+ labelsLayer.remove();
750
+ tip.remove();
751
+ },
752
+ };
753
+ }
754
+ function escapeText(text) {
755
+ return text.replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
756
+ }
757
+ //# sourceMappingURL=space.js.map