@mark1russell7/visuals 0.3.4 → 0.3.6
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/README.md +164 -2
- package/build/globe.d.ts +2 -2
- package/build/graph.d.ts +584 -2
- package/build/graph.d.ts.map +1 -1
- package/build/graph.js +1371 -18
- package/build/graph.js.map +1 -1
- package/build/highlight-BJ6xGp6V.d.ts +30 -0
- package/build/highlight-BJ6xGp6V.d.ts.map +1 -0
- package/build/index.d.ts +2 -1
- package/build/index.d.ts.map +1 -1
- package/build/index.js +1 -1
- package/build/labels.d.ts +1 -1
- package/build/markers.d.ts +2 -2
- package/build/{motion-DViuJNyh.js → motion-VS13e4Pw.js} +6 -2
- package/build/{motion-DViuJNyh.js.map → motion-VS13e4Pw.js.map} +1 -1
- package/build/{stage-DmFfah2e.d.ts → stage-D8JVM6wZ.d.ts} +3 -2
- package/build/stage-D8JVM6wZ.d.ts.map +1 -0
- package/build/{theme-3EGbXXoP.d.ts → theme-DBbBlCbC.d.ts} +2 -30
- package/build/theme-DBbBlCbC.d.ts.map +1 -0
- package/build/three.d.ts +2 -2
- package/build/three.js +2 -8
- package/build/three.js.map +1 -1
- package/package.json +2 -2
- package/build/stage-DmFfah2e.d.ts.map +0 -1
- package/build/theme-3EGbXXoP.d.ts.map +0 -1
package/build/graph.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { a as partAt, c as highlightOf, f as KEY_ZOOM, l as isClick, n as easeInOut, r as lessMotion, s as NO_HIGHLIGHTS, t as approach, u as KEY_PART } from "./motion-VS13e4Pw.js";
|
|
2
|
+
import { grow, overlapOf, pathBounds, pathEnd, pathStart, pictureBounds, pointPolylineDistance, pointRegionDistance, rectCenter, samplePath, translate } from "@mark1russell7/layout-contract";
|
|
2
3
|
// src/graph/camera.ts
|
|
3
4
|
/** The default limits of the zoom. */
|
|
4
5
|
const ZOOM_LIMITS = {
|
|
@@ -123,12 +124,11 @@ function freeArea(size, insets = {}) {
|
|
|
123
124
|
}
|
|
124
125
|
/** The default distance between a rectangle that a camera shows and each edge of the area, in CSS pixels. */
|
|
125
126
|
const FIT_PADDING = 24;
|
|
126
|
-
/** This function gives the
|
|
127
|
-
function
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
};
|
|
127
|
+
/** This function gives the zoom at which a rectangle of the picture fills an area of the screen, with a padding at each edge. */
|
|
128
|
+
function fillZoom(rect, area, padding) {
|
|
129
|
+
const width = Math.max(area.w - 2 * padding, 1);
|
|
130
|
+
const height = Math.max(area.h - 2 * padding, 1);
|
|
131
|
+
return Math.min(width / Math.max(rect.w, 0), height / Math.max(rect.h, 0));
|
|
132
132
|
}
|
|
133
133
|
/**
|
|
134
134
|
* This function gives the view that shows a rectangle of the picture in an area of the screen, as large as the area permits.
|
|
@@ -136,11 +136,50 @@ function middleOf(rect) {
|
|
|
136
136
|
*/
|
|
137
137
|
function fitRect(rect, area, options = {}) {
|
|
138
138
|
const limits = options.limits ?? ZOOM_LIMITS;
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
139
|
+
const k = clampZoom(Math.min(options.most ?? limits.max, fillZoom(rect, area, options.padding ?? 24)), limits);
|
|
140
|
+
return viewAt(rectCenter(rect), rectCenter(area), k);
|
|
141
|
+
}
|
|
142
|
+
/**
|
|
143
|
+
* This function gives the limits of the zoom for a picture with the bounds `bounds`.
|
|
144
|
+
*
|
|
145
|
+
* The smallest zoom of the result shows all of the picture in the area, with a padding at each edge.
|
|
146
|
+
* A picture that fits at the smallest zoom of `limits` keeps `limits`. Thus a person can show all of a very large picture.
|
|
147
|
+
*/
|
|
148
|
+
function zoomLimitsFor(bounds, area, limits = ZOOM_LIMITS, padding = 24) {
|
|
149
|
+
const all = fillZoom(bounds, area, padding);
|
|
150
|
+
return all > 0 && all < limits.min ? {
|
|
151
|
+
min: all,
|
|
152
|
+
max: limits.max
|
|
153
|
+
} : limits;
|
|
154
|
+
}
|
|
155
|
+
/** The length of a picture that stays in the free area when a person moves the picture, in CSS pixels. */
|
|
156
|
+
const KEEP_IN_VIEW = 64;
|
|
157
|
+
/** This function gives the start of a span of the screen that keeps the length `least` in an area, on one axis. */
|
|
158
|
+
function keepSpan(start, length, areaStart, areaLength, least) {
|
|
159
|
+
const keep = Math.min(Math.max(least, 0), length, areaLength);
|
|
160
|
+
return Math.min(Math.max(start, areaStart + keep - length), areaStart + areaLength - keep);
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* This function gives the view that keeps a part of a rectangle of the picture in an area of the screen.
|
|
164
|
+
* A person who moves the picture then cannot lose it.
|
|
165
|
+
*
|
|
166
|
+
* `least` is the length of the rectangle that stays in the area on each axis, in CSS pixels.
|
|
167
|
+
* A rectangle that is smaller on the screen stays in the area fully. The zoom does not change.
|
|
168
|
+
* The result is `view` itself when the view keeps the rectangle.
|
|
169
|
+
*/
|
|
170
|
+
function keepRect(view, rect, area, least = 64) {
|
|
171
|
+
const shown = rectToScreen(view, rect);
|
|
172
|
+
const x = keepSpan(shown.x, shown.w, area.x, area.w, least);
|
|
173
|
+
const y = keepSpan(shown.y, shown.h, area.y, area.h, least);
|
|
174
|
+
return x === shown.x && y === shown.y ? view : {
|
|
175
|
+
x: view.x + x - shown.x,
|
|
176
|
+
y: view.y + y - shown.y,
|
|
177
|
+
k: view.k
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
/** This function tells if two views are equal. */
|
|
181
|
+
function sameView(a, b) {
|
|
182
|
+
return a.x === b.x && a.y === b.y && a.k === b.k;
|
|
144
183
|
}
|
|
145
184
|
/**
|
|
146
185
|
* This function gives the view for the first rectangle of a list that an area of the screen shows at the zoom `least` or more.
|
|
@@ -156,7 +195,7 @@ function frameRects(rects, area, options) {
|
|
|
156
195
|
}
|
|
157
196
|
const last = rects.at(-1);
|
|
158
197
|
if (last === void 0) return void 0;
|
|
159
|
-
return viewAt(
|
|
198
|
+
return viewAt(rectCenter(last), rectCenter(area), clampZoom(options.least, options.limits));
|
|
160
199
|
}
|
|
161
200
|
/**
|
|
162
201
|
* This function tells if a view shows all of a rectangle of the picture in an area of the screen.
|
|
@@ -217,6 +256,895 @@ function pictureMoveAt(move, now) {
|
|
|
217
256
|
done: false
|
|
218
257
|
};
|
|
219
258
|
}
|
|
259
|
+
// src/graph/aim.ts
|
|
260
|
+
/** This function makes the aim that shows all of the picture, as large as the free area permits. */
|
|
261
|
+
function aimAll(options = {}) {
|
|
262
|
+
return (frame) => fitRect(frame.bounds, frame.area, {
|
|
263
|
+
...options,
|
|
264
|
+
limits: options.limits ?? frame.limits
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* This function makes the aim that shows a group of boxes at a zoom that a person reads.
|
|
269
|
+
* The rectangles are the bounds of the groups, the largest group first: `frameRects` selects one. An empty list keeps the view.
|
|
270
|
+
*/
|
|
271
|
+
function aimRects(rects, options) {
|
|
272
|
+
return (frame) => frameRects(rects, frame.area, {
|
|
273
|
+
...options,
|
|
274
|
+
limits: options.limits ?? frame.limits
|
|
275
|
+
}) ?? frame.view;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* This function makes the aim that shows a selection, and keeps the view when the selection is in view.
|
|
279
|
+
* The rectangles are the bounds of the groups of the selection, the largest group first: `revealRects` selects one.
|
|
280
|
+
*/
|
|
281
|
+
function aimReveal(rects, options) {
|
|
282
|
+
return (frame) => revealRects(frame.view, rects, frame.area, {
|
|
283
|
+
...options,
|
|
284
|
+
limits: options.limits ?? frame.limits
|
|
285
|
+
});
|
|
286
|
+
}
|
|
287
|
+
// src/graph/draw.ts
|
|
288
|
+
const TURN = 2 * Math.PI;
|
|
289
|
+
/** This function adds a path of the picture to the path of a context. */
|
|
290
|
+
function tracePath(context, path) {
|
|
291
|
+
for (const segment of path) switch (segment.kind) {
|
|
292
|
+
case "move":
|
|
293
|
+
context.moveTo(segment.to.x, segment.to.y);
|
|
294
|
+
break;
|
|
295
|
+
case "line":
|
|
296
|
+
context.lineTo(segment.to.x, segment.to.y);
|
|
297
|
+
break;
|
|
298
|
+
case "cubic":
|
|
299
|
+
context.bezierCurveTo(segment.c1.x, segment.c1.y, segment.c2.x, segment.c2.y, segment.to.x, segment.to.y);
|
|
300
|
+
break;
|
|
301
|
+
case "arc": context.arc(segment.center.x, segment.center.y, segment.radius, segment.start, segment.start + segment.sweep, segment.sweep < 0);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
/** This function makes the path of an outline in a context. */
|
|
305
|
+
function trace(context, outline) {
|
|
306
|
+
context.beginPath();
|
|
307
|
+
switch (outline.kind) {
|
|
308
|
+
case "rect":
|
|
309
|
+
if (outline.radius > 0 && typeof context.roundRect === "function") context.roundRect(outline.x, outline.y, outline.w, outline.h, outline.radius);
|
|
310
|
+
else context.rect(outline.x, outline.y, outline.w, outline.h);
|
|
311
|
+
break;
|
|
312
|
+
case "ellipse":
|
|
313
|
+
context.ellipse(outline.cx, outline.cy, outline.rx, outline.ry, 0, 0, TURN);
|
|
314
|
+
break;
|
|
315
|
+
case "polygon":
|
|
316
|
+
for (const ring of outline.rings) {
|
|
317
|
+
ring.forEach(([x, y], i) => i === 0 ? context.moveTo(x, y) : context.lineTo(x, y));
|
|
318
|
+
context.closePath();
|
|
319
|
+
}
|
|
320
|
+
break;
|
|
321
|
+
case "path": tracePath(context, outline.path);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* This function draws a paint list on a canvas, in the order of the list. `paintPicture` makes the list.
|
|
326
|
+
*
|
|
327
|
+
* The unit of the context is the CSS pixel, as a flat stage gives it: the function applies the view of the camera.
|
|
328
|
+
* The function keeps the state of the context: after it, the context has the transform and the styles that it had before.
|
|
329
|
+
*/
|
|
330
|
+
function drawShapes(context, shapes, view) {
|
|
331
|
+
if (shapes.length === 0) return;
|
|
332
|
+
context.save();
|
|
333
|
+
context.transform(view.k, 0, 0, view.k, view.x, view.y);
|
|
334
|
+
context.lineJoin = "round";
|
|
335
|
+
context.lineCap = "round";
|
|
336
|
+
context.textBaseline = "middle";
|
|
337
|
+
/** This value is true while the context has dashes. Most shapes have none, so the function sets them only at a change. */
|
|
338
|
+
let dashed = false;
|
|
339
|
+
for (const shape of shapes) {
|
|
340
|
+
context.globalAlpha = shape.opacity;
|
|
341
|
+
if (shape.type === "text") {
|
|
342
|
+
context.font = shape.font;
|
|
343
|
+
context.textAlign = shape.align;
|
|
344
|
+
if (shape.halo !== void 0) {
|
|
345
|
+
if (dashed) {
|
|
346
|
+
context.setLineDash([]);
|
|
347
|
+
dashed = false;
|
|
348
|
+
}
|
|
349
|
+
context.strokeStyle = shape.halo.color;
|
|
350
|
+
context.lineWidth = shape.halo.width;
|
|
351
|
+
context.strokeText(shape.text, shape.x, shape.y);
|
|
352
|
+
}
|
|
353
|
+
context.fillStyle = shape.fill;
|
|
354
|
+
context.fillText(shape.text, shape.x, shape.y);
|
|
355
|
+
continue;
|
|
356
|
+
}
|
|
357
|
+
trace(context, shape.outline);
|
|
358
|
+
if (shape.fill !== void 0) {
|
|
359
|
+
context.fillStyle = shape.fill;
|
|
360
|
+
context.fill("evenodd");
|
|
361
|
+
}
|
|
362
|
+
if (shape.stroke !== void 0 && shape.lineWidth !== void 0 && shape.lineWidth > 0) {
|
|
363
|
+
const dash = shape.dash;
|
|
364
|
+
const next = dash !== void 0 && dash.length > 0;
|
|
365
|
+
if (next || dashed) {
|
|
366
|
+
context.setLineDash(next ? [...dash] : []);
|
|
367
|
+
dashed = next;
|
|
368
|
+
}
|
|
369
|
+
context.strokeStyle = shape.stroke;
|
|
370
|
+
context.lineWidth = shape.lineWidth;
|
|
371
|
+
context.stroke();
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
context.restore();
|
|
375
|
+
}
|
|
376
|
+
// src/graph/focus.ts
|
|
377
|
+
/** The share of the color that a record keeps at each focus level. */
|
|
378
|
+
const FOCUS_OPACITY = {
|
|
379
|
+
all: 1,
|
|
380
|
+
in: 1,
|
|
381
|
+
beside: .55,
|
|
382
|
+
out: .2
|
|
383
|
+
};
|
|
384
|
+
/**
|
|
385
|
+
* This function gives the focus of a list of records, or `undefined` when the picture has none of them.
|
|
386
|
+
*
|
|
387
|
+
* - A box brings each box below it in the tree.
|
|
388
|
+
* - Each route at one of these boxes is in the focus, with the box at its other end.
|
|
389
|
+
* - A route brings its two boxes.
|
|
390
|
+
* - Each box of the focus brings each box above it in the tree, so the card of a row stays clear.
|
|
391
|
+
*/
|
|
392
|
+
function focusOf(index, seeds) {
|
|
393
|
+
const boxes = /* @__PURE__ */ new Set();
|
|
394
|
+
const routes = /* @__PURE__ */ new Set();
|
|
395
|
+
/** The boxes of the list, and each box below them. */
|
|
396
|
+
const own = [];
|
|
397
|
+
const addRoute = (route) => {
|
|
398
|
+
routes.add(route.id);
|
|
399
|
+
if (index.boxes.has(route.source)) boxes.add(route.source);
|
|
400
|
+
if (index.boxes.has(route.target)) boxes.add(route.target);
|
|
401
|
+
};
|
|
402
|
+
const seedRoutes = [];
|
|
403
|
+
for (const seed of seeds) {
|
|
404
|
+
const route = index.routes.get(seed);
|
|
405
|
+
if (route !== void 0) seedRoutes.push(route);
|
|
406
|
+
if (!index.boxes.has(seed)) continue;
|
|
407
|
+
const stack = [seed];
|
|
408
|
+
for (let id = stack.pop(); id !== void 0; id = stack.pop()) {
|
|
409
|
+
if (boxes.has(id)) continue;
|
|
410
|
+
boxes.add(id);
|
|
411
|
+
own.push(id);
|
|
412
|
+
for (const child of index.children.get(id) ?? []) stack.push(child.id);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
for (const id of own) for (const route of index.routesAt.get(id) ?? []) addRoute(route);
|
|
416
|
+
for (const route of seedRoutes) addRoute(route);
|
|
417
|
+
for (const id of [...boxes]) for (let parent = index.boxes.get(id)?.parent; parent !== void 0 && index.boxes.has(parent) && !boxes.has(parent); parent = index.boxes.get(parent)?.parent) boxes.add(parent);
|
|
418
|
+
return boxes.size === 0 && routes.size === 0 ? void 0 : {
|
|
419
|
+
boxes,
|
|
420
|
+
routes
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
/** This function gives the focus level of a box. */
|
|
424
|
+
function boxFocus(focus, box) {
|
|
425
|
+
if (focus === void 0) return "all";
|
|
426
|
+
if (focus.boxes.has(box.id)) return "in";
|
|
427
|
+
return box.parent !== void 0 && focus.boxes.has(box.parent) ? "beside" : "out";
|
|
428
|
+
}
|
|
429
|
+
/** This function gives the focus level of a route: `all`, `in` or `out`. */
|
|
430
|
+
function routeFocus(focus, route) {
|
|
431
|
+
if (focus === void 0) return "all";
|
|
432
|
+
return focus.routes.has(route.id) ? "in" : "out";
|
|
433
|
+
}
|
|
434
|
+
// src/graph/records.ts
|
|
435
|
+
/** This function adds a value to the list of a key. */
|
|
436
|
+
function add(lists, key, value) {
|
|
437
|
+
const list = lists.get(key);
|
|
438
|
+
if (list === void 0) lists.set(key, [value]);
|
|
439
|
+
else list.push(value);
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* This function makes the tables of a picture. It does not examine the picture: `checkPicture` of `@mark1russell7/layout-contract` does that.
|
|
443
|
+
* For an id that two boxes have, the table has the last box. A label that names a record that the picture does not have is in no table.
|
|
444
|
+
* Thus a page that removes a route from a picture can keep the label of the route.
|
|
445
|
+
*/
|
|
446
|
+
function indexPicture(picture) {
|
|
447
|
+
const boxes = /* @__PURE__ */ new Map();
|
|
448
|
+
const routes = /* @__PURE__ */ new Map();
|
|
449
|
+
const children = /* @__PURE__ */ new Map();
|
|
450
|
+
const routesAt = /* @__PURE__ */ new Map();
|
|
451
|
+
const boxLabels = /* @__PURE__ */ new Map();
|
|
452
|
+
const routeLabels = /* @__PURE__ */ new Map();
|
|
453
|
+
const freeLabels = [];
|
|
454
|
+
for (const box of picture.boxes) {
|
|
455
|
+
boxes.set(box.id, box);
|
|
456
|
+
if (box.parent !== void 0) add(children, box.parent, box);
|
|
457
|
+
}
|
|
458
|
+
for (const route of picture.routes) {
|
|
459
|
+
routes.set(route.id, route);
|
|
460
|
+
add(routesAt, route.source, route);
|
|
461
|
+
if (route.target !== route.source) add(routesAt, route.target, route);
|
|
462
|
+
}
|
|
463
|
+
for (const label of picture.labels) if (label.box !== void 0) {
|
|
464
|
+
if (boxes.has(label.box)) add(boxLabels, label.box, label);
|
|
465
|
+
} else if (label.route !== void 0) {
|
|
466
|
+
if (routes.has(label.route)) add(routeLabels, label.route, label);
|
|
467
|
+
} else freeLabels.push(label);
|
|
468
|
+
return {
|
|
469
|
+
picture,
|
|
470
|
+
boxes,
|
|
471
|
+
routes,
|
|
472
|
+
children,
|
|
473
|
+
routesAt,
|
|
474
|
+
boxLabels,
|
|
475
|
+
routeLabels,
|
|
476
|
+
freeLabels
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
const lines = /* @__PURE__ */ new WeakMap();
|
|
480
|
+
/**
|
|
481
|
+
* This function gives the line of a route, or `undefined` for a route with an empty path.
|
|
482
|
+
* It keeps the line of each route, so a second question about a route costs nothing.
|
|
483
|
+
*/
|
|
484
|
+
function routeLine(route) {
|
|
485
|
+
if (route.path.length === 0) return void 0;
|
|
486
|
+
const known = lines.get(route);
|
|
487
|
+
if (known !== void 0) return known;
|
|
488
|
+
const line = {
|
|
489
|
+
bounds: pathBounds(route.path),
|
|
490
|
+
points: samplePath(route.path)
|
|
491
|
+
};
|
|
492
|
+
lines.set(route, line);
|
|
493
|
+
return line;
|
|
494
|
+
}
|
|
495
|
+
// src/graph/hit.ts
|
|
496
|
+
/** This function gives the distance from a point to a box, and 0 for a point in the box. A box with a `shape` is that region. */
|
|
497
|
+
function boxDistance(box, point) {
|
|
498
|
+
if (box.shape === void 0) return pointRegionDistance(point, {
|
|
499
|
+
kind: "rect",
|
|
500
|
+
x: box.x,
|
|
501
|
+
y: box.y,
|
|
502
|
+
w: box.w,
|
|
503
|
+
h: box.h
|
|
504
|
+
});
|
|
505
|
+
return pointRegionDistance({
|
|
506
|
+
x: point.x - box.x,
|
|
507
|
+
y: point.y - box.y
|
|
508
|
+
}, box.shape);
|
|
509
|
+
}
|
|
510
|
+
const boxesOnly$1 = (record) => record.type === "box";
|
|
511
|
+
/**
|
|
512
|
+
* This function gives the record of a picture below a point, or `undefined` for no record.
|
|
513
|
+
*
|
|
514
|
+
* - A box with no child comes first: the top box at the point, then the nearest box in the slack.
|
|
515
|
+
* - A route comes next: the nearest route in the slack. A route is thin, so it comes before a container.
|
|
516
|
+
* - A box with children comes last: the top one at the point, then the nearest one in the slack.
|
|
517
|
+
*
|
|
518
|
+
* The last record of a list of the picture is the top one. The distance to a route has an error of 0.5 units or less.
|
|
519
|
+
*/
|
|
520
|
+
function pickRecord(index, point, options = {}) {
|
|
521
|
+
const slack = Math.max(0, options.slack ?? 0);
|
|
522
|
+
const picks = options.picks ?? boxesOnly$1;
|
|
523
|
+
let leaf;
|
|
524
|
+
let nearLeaf;
|
|
525
|
+
let nearLeafAt = 1e400;
|
|
526
|
+
let container;
|
|
527
|
+
let nearContainer;
|
|
528
|
+
let nearContainerAt = 1e400;
|
|
529
|
+
for (const box of index.picture.boxes) {
|
|
530
|
+
if (point.x < box.x - slack || point.x > box.x + box.w + slack || point.y < box.y - slack || point.y > box.y + box.h + slack) continue;
|
|
531
|
+
const distance = boxDistance(box, point);
|
|
532
|
+
if (distance > slack) continue;
|
|
533
|
+
const record = {
|
|
534
|
+
type: "box",
|
|
535
|
+
id: box.id,
|
|
536
|
+
box
|
|
537
|
+
};
|
|
538
|
+
if (!picks(record)) continue;
|
|
539
|
+
if (index.children.has(box.id)) {
|
|
540
|
+
if (distance === 0) container = record;
|
|
541
|
+
else if (distance <= nearContainerAt) {
|
|
542
|
+
nearContainer = record;
|
|
543
|
+
nearContainerAt = distance;
|
|
544
|
+
}
|
|
545
|
+
} else if (distance === 0) leaf = record;
|
|
546
|
+
else if (distance <= nearLeafAt) {
|
|
547
|
+
nearLeaf = record;
|
|
548
|
+
nearLeafAt = distance;
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
if (leaf !== void 0) return leaf;
|
|
552
|
+
if (nearLeaf !== void 0) return nearLeaf;
|
|
553
|
+
let found;
|
|
554
|
+
let foundAt = 1e400;
|
|
555
|
+
for (const route of index.picture.routes) {
|
|
556
|
+
const line = routeLine(route);
|
|
557
|
+
if (line === void 0) continue;
|
|
558
|
+
const { bounds } = line;
|
|
559
|
+
if (point.x < bounds.x - slack || point.x > bounds.x + bounds.w + slack || point.y < bounds.y - slack || point.y > bounds.y + bounds.h + slack) continue;
|
|
560
|
+
const record = {
|
|
561
|
+
type: "route",
|
|
562
|
+
id: route.id,
|
|
563
|
+
route
|
|
564
|
+
};
|
|
565
|
+
if (!picks(record)) continue;
|
|
566
|
+
const distance = pointPolylineDistance(point, line.points);
|
|
567
|
+
if (distance > slack || distance > foundAt) continue;
|
|
568
|
+
found = record;
|
|
569
|
+
foundAt = distance;
|
|
570
|
+
}
|
|
571
|
+
return found ?? container ?? nearContainer;
|
|
572
|
+
}
|
|
573
|
+
// src/graph/looks.ts
|
|
574
|
+
/**
|
|
575
|
+
* This function gives the size of one unit of a look, in the unit of the picture.
|
|
576
|
+
* The widths of the lines, the dashes and the sizes of the signs of a look are in this unit.
|
|
577
|
+
*
|
|
578
|
+
* At the zoom 1 or less, one unit is one CSS pixel. Thus a thin line stays clear in a small picture.
|
|
579
|
+
* Above the zoom 1, one unit is one unit of the picture. Thus a line grows with its box.
|
|
580
|
+
*/
|
|
581
|
+
function lookUnit(zoom) {
|
|
582
|
+
return zoom > 0 && zoom < 1 ? 1 / zoom : 1;
|
|
583
|
+
}
|
|
584
|
+
/** The colors of a plain picture on a light page. */
|
|
585
|
+
const LIGHT_PICTURE_THEME = {
|
|
586
|
+
box: "#ffffff",
|
|
587
|
+
boxLine: "#454f67",
|
|
588
|
+
container: "#eef1f6",
|
|
589
|
+
containerLine: "#aeb6c7",
|
|
590
|
+
text: "#151c2e",
|
|
591
|
+
mutedText: "#5f6880",
|
|
592
|
+
route: "#7a8399",
|
|
593
|
+
accent: "#2346c4",
|
|
594
|
+
paper: "#f5f6f9"
|
|
595
|
+
};
|
|
596
|
+
/** The colors of a plain picture on a dark page. */
|
|
597
|
+
const DARK_PICTURE_THEME = {
|
|
598
|
+
box: "#151a2b",
|
|
599
|
+
boxLine: "#b3bad0",
|
|
600
|
+
container: "#1c2236",
|
|
601
|
+
containerLine: "#46507a",
|
|
602
|
+
text: "#e6e9f3",
|
|
603
|
+
mutedText: "#949cb3",
|
|
604
|
+
route: "#6f7aa3",
|
|
605
|
+
accent: "#93abff",
|
|
606
|
+
paper: "#0e1220"
|
|
607
|
+
};
|
|
608
|
+
/** The smallest height of a line of text on the screen that a person can read, in CSS pixels. A smaller line is a bar in a plain picture. */
|
|
609
|
+
const SMALLEST_TEXT_LINE = 11;
|
|
610
|
+
/** The smallest height of a line of text on the screen that a plain picture shows as a bar, in CSS pixels. */
|
|
611
|
+
const SMALLEST_BAR_LINE = 2.5;
|
|
612
|
+
/**
|
|
613
|
+
* This function gives the looks of a plain picture: boxes with a border, routes as lines and the text of the labels.
|
|
614
|
+
*
|
|
615
|
+
* - A record below the pointer has the color `accent`. The selected record has a wider line too.
|
|
616
|
+
* - A box with children has the colors of a container, and its label has the muted color.
|
|
617
|
+
* - The label of a route has a line of the color `paper` around its letters.
|
|
618
|
+
* - A text that is too small to read is a row of bars. A text that is smaller than a bar has no paint.
|
|
619
|
+
*/
|
|
620
|
+
function plainLooks(options = {}) {
|
|
621
|
+
const radius = options.radius ?? 6;
|
|
622
|
+
return {
|
|
623
|
+
box(_box, state, theme) {
|
|
624
|
+
const plain = state.container ? theme.containerLine : theme.boxLine;
|
|
625
|
+
return {
|
|
626
|
+
fill: state.container ? theme.container : theme.box,
|
|
627
|
+
stroke: state.highlight > 0 ? theme.accent : plain,
|
|
628
|
+
width: state.highlight === 2 ? 2 : 1,
|
|
629
|
+
radius
|
|
630
|
+
};
|
|
631
|
+
},
|
|
632
|
+
route(_route, state, theme) {
|
|
633
|
+
return {
|
|
634
|
+
stroke: state.highlight > 0 ? theme.accent : theme.route,
|
|
635
|
+
width: state.highlight === 2 ? 2 : 1.25,
|
|
636
|
+
end: options.end
|
|
637
|
+
};
|
|
638
|
+
},
|
|
639
|
+
label(label, state, theme, of) {
|
|
640
|
+
const tall = label.lineHeight * state.zoom;
|
|
641
|
+
if (tall < 2.5) return void 0;
|
|
642
|
+
if (tall < 11) return {
|
|
643
|
+
color: theme.containerLine,
|
|
644
|
+
as: "bars"
|
|
645
|
+
};
|
|
646
|
+
if (of?.type === "route") return {
|
|
647
|
+
color: theme.text,
|
|
648
|
+
halo: theme.paper
|
|
649
|
+
};
|
|
650
|
+
return { color: state.container ? theme.mutedText : theme.text };
|
|
651
|
+
}
|
|
652
|
+
};
|
|
653
|
+
}
|
|
654
|
+
// src/graph/paint.ts
|
|
655
|
+
/** The key of the labels that name no record, for `fade`. */
|
|
656
|
+
const FREE_LABELS = "labels";
|
|
657
|
+
/** The distance out of the view at which a record keeps its shapes, in units of a look. A ring, a sign or a wide line can be this far from its record. */
|
|
658
|
+
const VIEW_MARGIN = 24;
|
|
659
|
+
/** The height of a bar of a label, as a share of the height of its line. */
|
|
660
|
+
const BAR_HEIGHT = .42;
|
|
661
|
+
/** The width of the line around the letters of a label, as a share of the height of its line. */
|
|
662
|
+
const HALO_WIDTH = .25;
|
|
663
|
+
/** The half width of an arrow at the end of a route, as a share of its length. */
|
|
664
|
+
const ARROW_SPREAD = .45;
|
|
665
|
+
/** This function gives the key of a record for `fade`: its type, a colon and its id. A box and a route with the same id have different keys. */
|
|
666
|
+
function recordKey(type, id) {
|
|
667
|
+
return `${type}:${id}`;
|
|
668
|
+
}
|
|
669
|
+
/** This function gives the state of a box for its look. */
|
|
670
|
+
function boxState(index, box, state) {
|
|
671
|
+
return {
|
|
672
|
+
highlight: state.highlight(box.id),
|
|
673
|
+
focus: boxFocus(state.focus, box),
|
|
674
|
+
zoom: state.zoom,
|
|
675
|
+
container: index.children.has(box.id)
|
|
676
|
+
};
|
|
677
|
+
}
|
|
678
|
+
/** This function gives the state of a route for its look. */
|
|
679
|
+
function routeState(route, state) {
|
|
680
|
+
return {
|
|
681
|
+
highlight: state.highlight(route.id),
|
|
682
|
+
focus: routeFocus(state.focus, route),
|
|
683
|
+
zoom: state.zoom,
|
|
684
|
+
container: false
|
|
685
|
+
};
|
|
686
|
+
}
|
|
687
|
+
/** This function gives the outline of a box, larger by `more` at each side. A polygon does not become larger. */
|
|
688
|
+
function outlineOf(box, radius, more) {
|
|
689
|
+
const region = box.shape === void 0 ? {
|
|
690
|
+
kind: "rect",
|
|
691
|
+
x: box.x,
|
|
692
|
+
y: box.y,
|
|
693
|
+
w: box.w,
|
|
694
|
+
h: box.h
|
|
695
|
+
} : translate(box.shape, box.x, box.y);
|
|
696
|
+
switch (region.kind) {
|
|
697
|
+
case "rect": {
|
|
698
|
+
const w = Math.max(0, region.w + 2 * more);
|
|
699
|
+
const h = Math.max(0, region.h + 2 * more);
|
|
700
|
+
return {
|
|
701
|
+
kind: "rect",
|
|
702
|
+
x: region.x - more,
|
|
703
|
+
y: region.y - more,
|
|
704
|
+
w,
|
|
705
|
+
h,
|
|
706
|
+
radius: Math.max(0, Math.min(radius + more, w / 2, h / 2))
|
|
707
|
+
};
|
|
708
|
+
}
|
|
709
|
+
case "circle": return {
|
|
710
|
+
kind: "ellipse",
|
|
711
|
+
cx: region.cx,
|
|
712
|
+
cy: region.cy,
|
|
713
|
+
rx: Math.max(0, region.r + more),
|
|
714
|
+
ry: Math.max(0, region.r + more)
|
|
715
|
+
};
|
|
716
|
+
case "ellipse": return {
|
|
717
|
+
kind: "ellipse",
|
|
718
|
+
cx: region.cx,
|
|
719
|
+
cy: region.cy,
|
|
720
|
+
rx: Math.max(0, region.rx + more),
|
|
721
|
+
ry: Math.max(0, region.ry + more)
|
|
722
|
+
};
|
|
723
|
+
case "polygon": return {
|
|
724
|
+
kind: "polygon",
|
|
725
|
+
rings: region.holes === void 0 ? [region.points] : [region.points, ...region.holes]
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
/** This function gives the lengths of a dash in the unit of the picture. */
|
|
730
|
+
function dashOf(dash, unit) {
|
|
731
|
+
return dash === void 0 || dash.length === 0 ? void 0 : dash.map((length) => length * unit);
|
|
732
|
+
}
|
|
733
|
+
/** This function adds the shapes of a box: its ring, its inside and border, and its signs. */
|
|
734
|
+
function addBox(shapes, box, look, unit, share) {
|
|
735
|
+
const opacity = Math.min(1, (look.opacity ?? 1) * share);
|
|
736
|
+
if (!(opacity > 0)) return;
|
|
737
|
+
const radius = look.radius ?? 0;
|
|
738
|
+
if (look.ring !== void 0) {
|
|
739
|
+
const more = (look.ring.gap + look.ring.width / 2) * unit;
|
|
740
|
+
shapes.push({
|
|
741
|
+
type: "shape",
|
|
742
|
+
outline: outlineOf(box, radius, more),
|
|
743
|
+
stroke: look.ring.color,
|
|
744
|
+
lineWidth: look.ring.width * unit,
|
|
745
|
+
opacity
|
|
746
|
+
});
|
|
747
|
+
}
|
|
748
|
+
if (look.fill !== void 0 || look.stroke !== void 0) {
|
|
749
|
+
const line = look.stroke === void 0 ? void 0 : (look.width ?? 1) * unit;
|
|
750
|
+
shapes.push({
|
|
751
|
+
type: "shape",
|
|
752
|
+
outline: outlineOf(box, radius, 0),
|
|
753
|
+
fill: look.fill,
|
|
754
|
+
stroke: look.stroke,
|
|
755
|
+
lineWidth: line,
|
|
756
|
+
dash: dashOf(look.dash, unit),
|
|
757
|
+
opacity
|
|
758
|
+
});
|
|
759
|
+
}
|
|
760
|
+
for (const mark of look.marks ?? []) {
|
|
761
|
+
const r = Math.max(0, mark.radius * unit);
|
|
762
|
+
const outline = {
|
|
763
|
+
kind: "ellipse",
|
|
764
|
+
cx: box.x + mark.x,
|
|
765
|
+
cy: box.y + mark.y,
|
|
766
|
+
rx: r,
|
|
767
|
+
ry: r
|
|
768
|
+
};
|
|
769
|
+
if (mark.shape === "dot") shapes.push({
|
|
770
|
+
type: "shape",
|
|
771
|
+
outline,
|
|
772
|
+
fill: mark.color,
|
|
773
|
+
opacity
|
|
774
|
+
});
|
|
775
|
+
else shapes.push({
|
|
776
|
+
type: "shape",
|
|
777
|
+
outline,
|
|
778
|
+
stroke: mark.color,
|
|
779
|
+
lineWidth: (mark.width ?? 1.5) * unit,
|
|
780
|
+
opacity
|
|
781
|
+
});
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
/** This function adds the shapes of a label: one text or one bar for each line that is not empty. */
|
|
785
|
+
function addLabel(shapes, label, look, share, measure) {
|
|
786
|
+
if (look === void 0) return;
|
|
787
|
+
const opacity = Math.min(1, (look.opacity ?? 1) * share);
|
|
788
|
+
if (!(opacity > 0)) return;
|
|
789
|
+
const align = label.align ?? "left";
|
|
790
|
+
const left = align === "left" ? 0 : align === "center" ? .5 : 1;
|
|
791
|
+
for (let i = 0; i < label.lines.length; i++) {
|
|
792
|
+
const text = label.lines[i];
|
|
793
|
+
if (text.trim() === "") continue;
|
|
794
|
+
const y = label.y + (i + .5) * label.lineHeight;
|
|
795
|
+
if (look.as !== "bars") {
|
|
796
|
+
const halo = look.halo === void 0 ? void 0 : {
|
|
797
|
+
color: look.halo,
|
|
798
|
+
width: label.lineHeight * HALO_WIDTH
|
|
799
|
+
};
|
|
800
|
+
shapes.push({
|
|
801
|
+
type: "text",
|
|
802
|
+
text,
|
|
803
|
+
x: label.x + left * label.w,
|
|
804
|
+
y,
|
|
805
|
+
font: label.font,
|
|
806
|
+
align,
|
|
807
|
+
fill: look.color,
|
|
808
|
+
halo,
|
|
809
|
+
opacity
|
|
810
|
+
});
|
|
811
|
+
continue;
|
|
812
|
+
}
|
|
813
|
+
const measured = measure === void 0 ? label.w : measure(label.font, text);
|
|
814
|
+
const wide = label.w > 0 ? Math.min(measured, label.w) : measured;
|
|
815
|
+
if (!(wide > 0)) continue;
|
|
816
|
+
const tall = label.lineHeight * BAR_HEIGHT;
|
|
817
|
+
const outline = {
|
|
818
|
+
kind: "rect",
|
|
819
|
+
x: label.x + left * (label.w - wide),
|
|
820
|
+
y: y - tall / 2,
|
|
821
|
+
w: wide,
|
|
822
|
+
h: tall,
|
|
823
|
+
radius: Math.min(tall, wide) / 2
|
|
824
|
+
};
|
|
825
|
+
shapes.push({
|
|
826
|
+
type: "shape",
|
|
827
|
+
outline,
|
|
828
|
+
fill: look.color,
|
|
829
|
+
opacity
|
|
830
|
+
});
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
/** This function adds the sign at one end of a route. `out` is 1 at the target and -1 at the source: an arrow points out of the route. */
|
|
834
|
+
function addEnd(shapes, at, out, end, stroke, lineWidth, unit, opacity) {
|
|
835
|
+
const size = end.size * unit;
|
|
836
|
+
if (!(size > 0)) return;
|
|
837
|
+
const { point } = at;
|
|
838
|
+
if (end.shape === "dot") {
|
|
839
|
+
shapes.push({
|
|
840
|
+
type: "shape",
|
|
841
|
+
outline: {
|
|
842
|
+
kind: "ellipse",
|
|
843
|
+
cx: point.x,
|
|
844
|
+
cy: point.y,
|
|
845
|
+
rx: size,
|
|
846
|
+
ry: size
|
|
847
|
+
},
|
|
848
|
+
fill: stroke,
|
|
849
|
+
opacity
|
|
850
|
+
});
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
const dx = at.direction.x * out;
|
|
854
|
+
const dy = at.direction.y * out;
|
|
855
|
+
if (dx === 0 && dy === 0) return;
|
|
856
|
+
const backX = point.x - dx * size;
|
|
857
|
+
const backY = point.y - dy * size;
|
|
858
|
+
const half = size * ARROW_SPREAD;
|
|
859
|
+
const path = [
|
|
860
|
+
{
|
|
861
|
+
kind: "move",
|
|
862
|
+
to: {
|
|
863
|
+
x: backX - dy * half,
|
|
864
|
+
y: backY + dx * half
|
|
865
|
+
}
|
|
866
|
+
},
|
|
867
|
+
{
|
|
868
|
+
kind: "line",
|
|
869
|
+
to: point
|
|
870
|
+
},
|
|
871
|
+
{
|
|
872
|
+
kind: "line",
|
|
873
|
+
to: {
|
|
874
|
+
x: backX + dy * half,
|
|
875
|
+
y: backY - dx * half
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
];
|
|
879
|
+
shapes.push({
|
|
880
|
+
type: "shape",
|
|
881
|
+
outline: {
|
|
882
|
+
kind: "path",
|
|
883
|
+
path
|
|
884
|
+
},
|
|
885
|
+
stroke,
|
|
886
|
+
lineWidth,
|
|
887
|
+
opacity
|
|
888
|
+
});
|
|
889
|
+
}
|
|
890
|
+
/** This function adds the shapes of a route: its line, and the signs at its ends. */
|
|
891
|
+
function addRoute(shapes, route, look, unit, share) {
|
|
892
|
+
const opacity = Math.min(1, (look.opacity ?? 1) * share);
|
|
893
|
+
if (!(opacity > 0)) return;
|
|
894
|
+
const lineWidth = (look.width ?? 1.25) * unit;
|
|
895
|
+
shapes.push({
|
|
896
|
+
type: "shape",
|
|
897
|
+
outline: {
|
|
898
|
+
kind: "path",
|
|
899
|
+
path: route.path
|
|
900
|
+
},
|
|
901
|
+
stroke: look.stroke,
|
|
902
|
+
lineWidth,
|
|
903
|
+
dash: dashOf(look.dash, unit),
|
|
904
|
+
opacity
|
|
905
|
+
});
|
|
906
|
+
if (look.start !== void 0) addEnd(shapes, pathStart(route.path), -1, look.start, look.stroke, lineWidth, unit, opacity);
|
|
907
|
+
if (look.end !== void 0) addEnd(shapes, pathEnd(route.path), 1, look.end, look.stroke, lineWidth, unit, opacity);
|
|
908
|
+
}
|
|
909
|
+
/**
|
|
910
|
+
* This function gives the paint list of a picture: the shapes that a renderer draws, in the order of the paint.
|
|
911
|
+
* The function is pure, so a test examines a picture with no browser. `drawShapes` draws the list on a canvas.
|
|
912
|
+
*
|
|
913
|
+
* The layers are, from the back:
|
|
914
|
+
*
|
|
915
|
+
* - The boxes with children, each with its labels.
|
|
916
|
+
* - The routes. The routes of the focus and those with a highlight are after the others.
|
|
917
|
+
* - The boxes with no child, each with its labels.
|
|
918
|
+
* - The labels of the routes, then the labels that name no record.
|
|
919
|
+
*
|
|
920
|
+
* In a layer, the records are in the order of the picture. A label has the state of the record that it names.
|
|
921
|
+
* The focus of the picture decreases the color of each record out of it (`FOCUS_OPACITY`).
|
|
922
|
+
*/
|
|
923
|
+
function paintPicture(index, looks, theme, state) {
|
|
924
|
+
const { picture } = index;
|
|
925
|
+
const unit = lookUnit(state.zoom);
|
|
926
|
+
const margin = 24 * unit;
|
|
927
|
+
const { visible, measure } = state;
|
|
928
|
+
const inView = (rect) => {
|
|
929
|
+
if (visible === void 0) return true;
|
|
930
|
+
const overlap = overlapOf(grow(rect, margin), visible);
|
|
931
|
+
return overlap.x >= 0 && overlap.y >= 0;
|
|
932
|
+
};
|
|
933
|
+
const shareOf = (key, level) => {
|
|
934
|
+
const goal = FOCUS_OPACITY[level];
|
|
935
|
+
return state.fade === void 0 ? goal : state.fade(key, goal);
|
|
936
|
+
};
|
|
937
|
+
const containers = [];
|
|
938
|
+
const leaves = [];
|
|
939
|
+
for (const box of picture.boxes) {
|
|
940
|
+
const labels = index.boxLabels.get(box.id) ?? [];
|
|
941
|
+
const shown = inView(box);
|
|
942
|
+
if (!shown && !labels.some(inView)) continue;
|
|
943
|
+
const boxIs = boxState(index, box, state);
|
|
944
|
+
const look = looks.box(box, boxIs, theme);
|
|
945
|
+
if (look === void 0) continue;
|
|
946
|
+
const share = shareOf(recordKey("box", box.id), boxIs.focus);
|
|
947
|
+
const shapes = boxIs.container ? containers : leaves;
|
|
948
|
+
if (shown) addBox(shapes, box, look, unit, share);
|
|
949
|
+
const record = {
|
|
950
|
+
type: "box",
|
|
951
|
+
id: box.id,
|
|
952
|
+
box
|
|
953
|
+
};
|
|
954
|
+
for (const label of labels) if (inView(label)) addLabel(shapes, label, looks.label(label, boxIs, theme, record), share, measure);
|
|
955
|
+
}
|
|
956
|
+
const back = [];
|
|
957
|
+
const front = [];
|
|
958
|
+
const routeLabels = [];
|
|
959
|
+
for (const route of picture.routes) {
|
|
960
|
+
const line = routeLine(route);
|
|
961
|
+
if (line === void 0) continue;
|
|
962
|
+
const labels = index.routeLabels.get(route.id) ?? [];
|
|
963
|
+
const shown = inView(line.bounds);
|
|
964
|
+
if (!shown && !labels.some(inView)) continue;
|
|
965
|
+
const routeIs = routeState(route, state);
|
|
966
|
+
const look = looks.route(route, routeIs, theme);
|
|
967
|
+
if (look === void 0) continue;
|
|
968
|
+
const share = shareOf(recordKey("route", route.id), routeIs.focus);
|
|
969
|
+
if (shown) addRoute(routeIs.focus === "in" || routeIs.highlight > 0 ? front : back, route, look, unit, share);
|
|
970
|
+
const record = {
|
|
971
|
+
type: "route",
|
|
972
|
+
id: route.id,
|
|
973
|
+
route
|
|
974
|
+
};
|
|
975
|
+
for (const label of labels) if (inView(label)) addLabel(routeLabels, label, looks.label(label, routeIs, theme, record), share, measure);
|
|
976
|
+
}
|
|
977
|
+
const free = [];
|
|
978
|
+
if (index.freeLabels.length > 0) {
|
|
979
|
+
const level = state.focus === void 0 ? "all" : "out";
|
|
980
|
+
const labelIs = {
|
|
981
|
+
highlight: 0,
|
|
982
|
+
focus: level,
|
|
983
|
+
zoom: state.zoom,
|
|
984
|
+
container: false
|
|
985
|
+
};
|
|
986
|
+
const share = shareOf(FREE_LABELS, level);
|
|
987
|
+
for (const label of index.freeLabels) if (inView(label)) addLabel(free, label, looks.label(label, labelIs, theme, void 0), share, measure);
|
|
988
|
+
}
|
|
989
|
+
return containers.concat(back, front, leaves, routeLabels, free);
|
|
990
|
+
}
|
|
991
|
+
// src/graph/content.ts
|
|
992
|
+
/** The time in which the color of a record goes most of the way to its goal after a change of the focus, in seconds. */
|
|
993
|
+
const FOCUS_FADE = .08;
|
|
994
|
+
/** The bounds of a picture with no record. */
|
|
995
|
+
const NO_BOUNDS = {
|
|
996
|
+
x: 0,
|
|
997
|
+
y: 0,
|
|
998
|
+
w: 0,
|
|
999
|
+
h: 0
|
|
1000
|
+
};
|
|
1001
|
+
/** The largest number of text widths that a content keeps. */
|
|
1002
|
+
const MOST_WIDTHS = 4e3;
|
|
1003
|
+
const NO_SEEDS = [];
|
|
1004
|
+
const boxesOnly = (record) => record.type === "box";
|
|
1005
|
+
/**
|
|
1006
|
+
* This function makes the content of a flat stage from a picture: its boxes, its routes and its labels.
|
|
1007
|
+
*
|
|
1008
|
+
* - The looks give the colors, the lines and the signs of each record from its state.
|
|
1009
|
+
* - A person points at the top box below the pointer, or at a route that the option `picks` permits.
|
|
1010
|
+
* - A change of the focus changes the colors in a short time.
|
|
1011
|
+
* - The content makes shapes only for the records in view.
|
|
1012
|
+
*
|
|
1013
|
+
* The picture must be good (`checkPicture` of `@mark1russell7/layout-contract`). The content does not change it.
|
|
1014
|
+
*/
|
|
1015
|
+
function createPictureContent(picture, options) {
|
|
1016
|
+
const { looks } = options;
|
|
1017
|
+
const permits = options.picks ?? boxesOnly;
|
|
1018
|
+
const bySelection = (options.focus ?? "selection") === "selection";
|
|
1019
|
+
let index = indexPicture(picture);
|
|
1020
|
+
let bounds = pictureBounds(picture) ?? NO_BOUNDS;
|
|
1021
|
+
/** The records of `setFocus`, or `undefined` for the focus of the options. */
|
|
1022
|
+
let asked;
|
|
1023
|
+
/** The names from which the focus of the last picture came. */
|
|
1024
|
+
let seeds = NO_SEEDS;
|
|
1025
|
+
let focus;
|
|
1026
|
+
/** This value is true until the first picture of a picture. The first focus has no change with time. */
|
|
1027
|
+
let fresh = true;
|
|
1028
|
+
/** The color share of each record whose share changes, by the key of the record. */
|
|
1029
|
+
const fades = /* @__PURE__ */ new Map();
|
|
1030
|
+
/** The theme and the state of the last picture. A person cannot point at a record that the last picture did not show. */
|
|
1031
|
+
let last;
|
|
1032
|
+
/** The width of each text that the content measured, by its font and its text. */
|
|
1033
|
+
const widths = /* @__PURE__ */ new Map();
|
|
1034
|
+
const widthOf = (context, font, text) => {
|
|
1035
|
+
const key = `${font}\n${text}`;
|
|
1036
|
+
const known = widths.get(key);
|
|
1037
|
+
if (known !== void 0) return known;
|
|
1038
|
+
const before = context.font;
|
|
1039
|
+
context.font = font;
|
|
1040
|
+
const width = context.measureText(text).width;
|
|
1041
|
+
context.font = before;
|
|
1042
|
+
if (widths.size >= MOST_WIDTHS) widths.clear();
|
|
1043
|
+
widths.set(key, width);
|
|
1044
|
+
return width;
|
|
1045
|
+
};
|
|
1046
|
+
/** This function gives the names from which the focus comes. */
|
|
1047
|
+
const seedsOf = (highlight) => {
|
|
1048
|
+
if (asked !== void 0) return asked;
|
|
1049
|
+
if (!bySelection) return NO_SEEDS;
|
|
1050
|
+
const out = [];
|
|
1051
|
+
for (const id of index.boxes.keys()) if (highlight(id) === 2) out.push(id);
|
|
1052
|
+
for (const id of index.routes.keys()) if (!index.boxes.has(id) && highlight(id) === 2) out.push(id);
|
|
1053
|
+
return out;
|
|
1054
|
+
};
|
|
1055
|
+
/** This function starts the change of the color of each record whose focus level changes. A change that continues starts from its value. */
|
|
1056
|
+
const fadeTo = (next) => {
|
|
1057
|
+
const add = (key, from, goal) => {
|
|
1058
|
+
const value = fades.get(key)?.value ?? from;
|
|
1059
|
+
if (value === goal) fades.delete(key);
|
|
1060
|
+
else fades.set(key, {
|
|
1061
|
+
value,
|
|
1062
|
+
goal
|
|
1063
|
+
});
|
|
1064
|
+
};
|
|
1065
|
+
for (const box of index.picture.boxes) add(recordKey("box", box.id), FOCUS_OPACITY[boxFocus(focus, box)], FOCUS_OPACITY[boxFocus(next, box)]);
|
|
1066
|
+
for (const route of index.picture.routes) add(recordKey("route", route.id), FOCUS_OPACITY[routeFocus(focus, route)], FOCUS_OPACITY[routeFocus(next, route)]);
|
|
1067
|
+
if (index.freeLabels.length > 0) add(FREE_LABELS, FOCUS_OPACITY[focus === void 0 ? "all" : "out"], FOCUS_OPACITY[next === void 0 ? "all" : "out"]);
|
|
1068
|
+
};
|
|
1069
|
+
return {
|
|
1070
|
+
get bounds() {
|
|
1071
|
+
return bounds;
|
|
1072
|
+
},
|
|
1073
|
+
has: (record) => index.boxes.has(record) || index.routes.has(record),
|
|
1074
|
+
pick(at) {
|
|
1075
|
+
const known = last;
|
|
1076
|
+
const state = known === void 0 ? void 0 : {
|
|
1077
|
+
...known.state,
|
|
1078
|
+
zoom: at.view.k
|
|
1079
|
+
};
|
|
1080
|
+
const shown = (record) => {
|
|
1081
|
+
if (!permits(record)) return false;
|
|
1082
|
+
if (known === void 0 || state === void 0) return true;
|
|
1083
|
+
if (record.type === "box") return looks.box(record.box, boxState(index, record.box, state), known.theme) !== void 0;
|
|
1084
|
+
return looks.route(record.route, routeState(record.route, state), known.theme) !== void 0;
|
|
1085
|
+
};
|
|
1086
|
+
const record = pickRecord(index, at.point, {
|
|
1087
|
+
slack: at.slack / at.view.k,
|
|
1088
|
+
picks: shown
|
|
1089
|
+
});
|
|
1090
|
+
return record === void 0 ? void 0 : {
|
|
1091
|
+
record: record.id,
|
|
1092
|
+
value: record
|
|
1093
|
+
};
|
|
1094
|
+
},
|
|
1095
|
+
draw(context, frame, theme, highlight) {
|
|
1096
|
+
const { view } = frame;
|
|
1097
|
+
const from = seedsOf(highlight);
|
|
1098
|
+
if (fresh || from.length !== seeds.length || from.some((id, i) => id !== seeds[i])) {
|
|
1099
|
+
const next = from.length === 0 ? void 0 : focusOf(index, from);
|
|
1100
|
+
if (fresh || frame.settle) fades.clear();
|
|
1101
|
+
else fadeTo(next);
|
|
1102
|
+
seeds = from;
|
|
1103
|
+
focus = next;
|
|
1104
|
+
fresh = false;
|
|
1105
|
+
}
|
|
1106
|
+
if (frame.settle) fades.clear();
|
|
1107
|
+
for (const [key, fade] of fades) {
|
|
1108
|
+
fade.value = approach(fade.value, fade.goal, frame.seconds, FOCUS_FADE);
|
|
1109
|
+
if (fade.value === fade.goal) fades.delete(key);
|
|
1110
|
+
}
|
|
1111
|
+
const state = {
|
|
1112
|
+
zoom: view.k,
|
|
1113
|
+
highlight,
|
|
1114
|
+
focus,
|
|
1115
|
+
visible: {
|
|
1116
|
+
x: (0 - view.x) / view.k,
|
|
1117
|
+
y: (0 - view.y) / view.k,
|
|
1118
|
+
w: frame.width / view.k,
|
|
1119
|
+
h: frame.height / view.k
|
|
1120
|
+
},
|
|
1121
|
+
measure: (font, text) => widthOf(context, font, text),
|
|
1122
|
+
fade: fades.size === 0 ? void 0 : (key, goal) => fades.get(key)?.value ?? goal
|
|
1123
|
+
};
|
|
1124
|
+
last = {
|
|
1125
|
+
theme,
|
|
1126
|
+
state
|
|
1127
|
+
};
|
|
1128
|
+
drawShapes(context, paintPicture(index, looks, theme, state), view);
|
|
1129
|
+
return fades.size > 0;
|
|
1130
|
+
},
|
|
1131
|
+
setPicture(next) {
|
|
1132
|
+
index = indexPicture(next);
|
|
1133
|
+
bounds = pictureBounds(next) ?? NO_BOUNDS;
|
|
1134
|
+
fades.clear();
|
|
1135
|
+
fresh = true;
|
|
1136
|
+
last = void 0;
|
|
1137
|
+
},
|
|
1138
|
+
setFocus(records) {
|
|
1139
|
+
asked = records === void 0 ? void 0 : [...records];
|
|
1140
|
+
},
|
|
1141
|
+
dispose() {
|
|
1142
|
+
fades.clear();
|
|
1143
|
+
widths.clear();
|
|
1144
|
+
last = void 0;
|
|
1145
|
+
}
|
|
1146
|
+
};
|
|
1147
|
+
}
|
|
220
1148
|
// src/graph/keys.ts
|
|
221
1149
|
/**
|
|
222
1150
|
* This function gives the action of a key on a flat picture, or `undefined` for a key with no action.
|
|
@@ -228,10 +1156,7 @@ function pictureMoveAt(move, now) {
|
|
|
228
1156
|
*/
|
|
229
1157
|
function pictureKeyAction(key, now, area, options) {
|
|
230
1158
|
const step = Math.min(area.w, area.h) * KEY_PART;
|
|
231
|
-
const middle =
|
|
232
|
-
x: area.x + area.w / 2,
|
|
233
|
-
y: area.y + area.h / 2
|
|
234
|
-
};
|
|
1159
|
+
const middle = rectCenter(area);
|
|
235
1160
|
switch (key) {
|
|
236
1161
|
case "ArrowLeft": return {
|
|
237
1162
|
type: "move",
|
|
@@ -364,6 +1289,434 @@ function pointerCancel(pointers, id) {
|
|
|
364
1289
|
at: rest
|
|
365
1290
|
} : NO_POINTERS;
|
|
366
1291
|
}
|
|
367
|
-
|
|
1292
|
+
// src/graph/stack.ts
|
|
1293
|
+
/**
|
|
1294
|
+
* This function makes one content from a list of contents, for a flat stage that shows them together.
|
|
1295
|
+
* An example is a layer of boxes below a layer of routes and a layer of labels.
|
|
1296
|
+
*
|
|
1297
|
+
* - The first content gives the bounds: the views and the limits of the camera come from it.
|
|
1298
|
+
* - The stack draws the contents in their sequence, so the last content is on top.
|
|
1299
|
+
* - A pick asks the last content first. The first record that a content gives is the record.
|
|
1300
|
+
* - A record that two contents have is one record: each of them gets its highlight.
|
|
1301
|
+
* - A content does not get the state of the canvas of an earlier content: the stack restores it.
|
|
1302
|
+
* - The stage disposes the stack, and the stack disposes each content.
|
|
1303
|
+
*/
|
|
1304
|
+
function stackPictureContents(contents) {
|
|
1305
|
+
const [first] = contents;
|
|
1306
|
+
return {
|
|
1307
|
+
get bounds() {
|
|
1308
|
+
return first.bounds;
|
|
1309
|
+
},
|
|
1310
|
+
has: (record) => contents.some((content) => content.has(record)),
|
|
1311
|
+
pick(at) {
|
|
1312
|
+
for (let index = contents.length - 1; index >= 0; index -= 1) {
|
|
1313
|
+
const pick = contents[index]?.pick(at);
|
|
1314
|
+
if (pick !== void 0) return pick;
|
|
1315
|
+
}
|
|
1316
|
+
},
|
|
1317
|
+
draw(context, frame, theme, highlight) {
|
|
1318
|
+
let active = false;
|
|
1319
|
+
for (const content of contents) {
|
|
1320
|
+
context.save();
|
|
1321
|
+
active = content.draw(context, frame, theme, highlight) || active;
|
|
1322
|
+
context.restore();
|
|
1323
|
+
}
|
|
1324
|
+
return active;
|
|
1325
|
+
},
|
|
1326
|
+
dispose() {
|
|
1327
|
+
for (const content of contents) content.dispose?.();
|
|
1328
|
+
}
|
|
1329
|
+
};
|
|
1330
|
+
}
|
|
1331
|
+
// src/graph/stage.ts
|
|
1332
|
+
/** The distance from a mouse at which a thin part is below the pointer, in CSS pixels. */
|
|
1333
|
+
const PICK_SLACK = 5;
|
|
1334
|
+
/** The distance from a finger or a pen at which a thin part is below the pointer, in CSS pixels. */
|
|
1335
|
+
const TOUCH_PICK_SLACK = 12;
|
|
1336
|
+
/** The factor of the zoom for a double click. */
|
|
1337
|
+
const DOUBLE_CLICK_ZOOM = 2;
|
|
1338
|
+
/** The largest number of pixels of the canvas for one CSS pixel. A larger number costs memory, and a person does not see it. */
|
|
1339
|
+
const LARGEST_PIXEL_RATIO = 3;
|
|
1340
|
+
/**
|
|
1341
|
+
* This function makes a flat stage in a container of a page. The stage fills the container, so give the container a size.
|
|
1342
|
+
*
|
|
1343
|
+
* A person moves the picture with a drag, and changes the zoom with the wheel, with two fingers or with a double click.
|
|
1344
|
+
* A click selects the record below the pointer. With the keyboard, the arrow keys move the camera, `+` and `-` change the zoom,
|
|
1345
|
+
* and `Home` shows the first view. The stage makes a picture only after a change.
|
|
1346
|
+
*
|
|
1347
|
+
* The function gives `undefined` when the browser gives no 2D context for a canvas. It does not throw.
|
|
1348
|
+
*/
|
|
1349
|
+
function createPictureStage(container, options) {
|
|
1350
|
+
const canvas = container.ownerDocument.createElement("canvas");
|
|
1351
|
+
const made = canvas.getContext("2d");
|
|
1352
|
+
if (made === null) return void 0;
|
|
1353
|
+
const context = made;
|
|
1354
|
+
canvas.style.display = "block";
|
|
1355
|
+
canvas.style.width = "100%";
|
|
1356
|
+
canvas.style.height = "100%";
|
|
1357
|
+
canvas.style.cursor = "grab";
|
|
1358
|
+
canvas.style.touchAction = "none";
|
|
1359
|
+
canvas.tabIndex = 0;
|
|
1360
|
+
canvas.setAttribute("role", "application");
|
|
1361
|
+
canvas.setAttribute("aria-label", options.label);
|
|
1362
|
+
container.append(canvas);
|
|
1363
|
+
const views = {
|
|
1364
|
+
all: aimAll(),
|
|
1365
|
+
...options.views
|
|
1366
|
+
};
|
|
1367
|
+
const home = options.home ?? "all";
|
|
1368
|
+
const baseLimits = options.limits ?? ZOOM_LIMITS;
|
|
1369
|
+
const wheel = options.wheel ?? "in-use";
|
|
1370
|
+
let content = options.content;
|
|
1371
|
+
let theme = options.theme;
|
|
1372
|
+
let insets = options.insets ?? {};
|
|
1373
|
+
let highlights = NO_HIGHLIGHTS;
|
|
1374
|
+
/** The size of the picture, in CSS pixels. */
|
|
1375
|
+
let width = 0;
|
|
1376
|
+
let height = 0;
|
|
1377
|
+
let pixelRatio = 1;
|
|
1378
|
+
/** This value is true after the container gave a size. A stage with no size makes no picture. */
|
|
1379
|
+
let sized = false;
|
|
1380
|
+
let area = freeArea({
|
|
1381
|
+
width,
|
|
1382
|
+
height
|
|
1383
|
+
}, insets);
|
|
1384
|
+
let limits = baseLimits;
|
|
1385
|
+
let view = UNIT_VIEW;
|
|
1386
|
+
/** The view that the camera has, or goes to: a name of the views, or an aim. */
|
|
1387
|
+
let aim = home;
|
|
1388
|
+
let move;
|
|
1389
|
+
/** This value is true after a person moved the view. Then a new size of the container does not aim the camera again. */
|
|
1390
|
+
let moved = false;
|
|
1391
|
+
/** This value is true until the content had a picture with no time. */
|
|
1392
|
+
let settle = true;
|
|
1393
|
+
/** This value is true while a person uses the picture. The wheel then changes the zoom. */
|
|
1394
|
+
let inUse = false;
|
|
1395
|
+
let pointers = NO_POINTERS;
|
|
1396
|
+
/** This value is true while a pointer that is down moves the picture. */
|
|
1397
|
+
let dragging = false;
|
|
1398
|
+
/** The view that `onView` got last. */
|
|
1399
|
+
let told;
|
|
1400
|
+
let frame = 0;
|
|
1401
|
+
let lastTime = 0;
|
|
1402
|
+
let disposed = false;
|
|
1403
|
+
const invalidate = () => {
|
|
1404
|
+
if (frame === 0 && !disposed) frame = requestAnimationFrame(tick);
|
|
1405
|
+
};
|
|
1406
|
+
const setCursor = () => {
|
|
1407
|
+
canvas.style.cursor = dragging ? "grabbing" : highlights.hovered === void 0 ? "grab" : "pointer";
|
|
1408
|
+
};
|
|
1409
|
+
/** The bounds of the content at the last `setArea`. `refresh` finds a change of the bounds with them. */
|
|
1410
|
+
let fitted = content.bounds;
|
|
1411
|
+
/** This function sets the free area and the limits of the zoom, from the size, the insets and the content. */
|
|
1412
|
+
const setArea = () => {
|
|
1413
|
+
area = freeArea({
|
|
1414
|
+
width,
|
|
1415
|
+
height
|
|
1416
|
+
}, insets);
|
|
1417
|
+
fitted = content.bounds;
|
|
1418
|
+
limits = zoomLimitsFor(fitted, area, baseLimits);
|
|
1419
|
+
};
|
|
1420
|
+
const aimFrame = () => ({
|
|
1421
|
+
bounds: content.bounds,
|
|
1422
|
+
area,
|
|
1423
|
+
view,
|
|
1424
|
+
limits
|
|
1425
|
+
});
|
|
1426
|
+
/** This function moves the camera to a view, with a move or immediately. A picture with no size has no move. */
|
|
1427
|
+
const glide = (to, animate) => {
|
|
1428
|
+
if (animate && sized && !lessMotion() && !sameView(view, to)) move = {
|
|
1429
|
+
from: view,
|
|
1430
|
+
to,
|
|
1431
|
+
start: performance.now(),
|
|
1432
|
+
duration: 380
|
|
1433
|
+
};
|
|
1434
|
+
else {
|
|
1435
|
+
move = void 0;
|
|
1436
|
+
view = to;
|
|
1437
|
+
}
|
|
1438
|
+
invalidate();
|
|
1439
|
+
};
|
|
1440
|
+
const goTo = (next, animate) => {
|
|
1441
|
+
const found = typeof next === "string" ? views[next] : next;
|
|
1442
|
+
if (found === void 0) return;
|
|
1443
|
+
aim = next;
|
|
1444
|
+
moved = false;
|
|
1445
|
+
glide(found(aimFrame()), animate);
|
|
1446
|
+
};
|
|
1447
|
+
/**
|
|
1448
|
+
* This function sets a view that a person made, immediately. The zoom stays in its limits, and a part of the picture stays in the free area.
|
|
1449
|
+
* A zoom that the limits change keeps the point of the picture at the middle of the free area.
|
|
1450
|
+
*/
|
|
1451
|
+
const hold = (base) => {
|
|
1452
|
+
const k = clampZoom(base.k, limits);
|
|
1453
|
+
const middle = rectCenter(area);
|
|
1454
|
+
move = void 0;
|
|
1455
|
+
view = keepRect(k === base.k ? base : viewAt(toPicture(base, middle), middle, k), content.bounds, area);
|
|
1456
|
+
invalidate();
|
|
1457
|
+
};
|
|
1458
|
+
/** This function sets a view that a person made with a pointer, the wheel or a key. */
|
|
1459
|
+
const steer = (next) => {
|
|
1460
|
+
moved = true;
|
|
1461
|
+
hold(next);
|
|
1462
|
+
};
|
|
1463
|
+
/**
|
|
1464
|
+
* This function sets the size of the canvas from the size of the container and the pixels of the screen.
|
|
1465
|
+
* It gives true after a change. A container with no size changes nothing.
|
|
1466
|
+
*/
|
|
1467
|
+
const setSize = () => {
|
|
1468
|
+
const wide = container.clientWidth;
|
|
1469
|
+
const tall = container.clientHeight;
|
|
1470
|
+
if (wide === 0 || tall === 0) return false;
|
|
1471
|
+
const ratio = Math.min(globalThis.devicePixelRatio > 0 ? globalThis.devicePixelRatio : 1, LARGEST_PIXEL_RATIO);
|
|
1472
|
+
if (wide === width && tall === height && ratio === pixelRatio) return false;
|
|
1473
|
+
width = wide;
|
|
1474
|
+
height = tall;
|
|
1475
|
+
pixelRatio = ratio;
|
|
1476
|
+
canvas.width = Math.round(wide * ratio);
|
|
1477
|
+
canvas.height = Math.round(tall * ratio);
|
|
1478
|
+
return true;
|
|
1479
|
+
};
|
|
1480
|
+
/** This function sets the camera after a change of the size of the picture. */
|
|
1481
|
+
const fitAgain = () => {
|
|
1482
|
+
const before = area;
|
|
1483
|
+
const first = !sized;
|
|
1484
|
+
sized = true;
|
|
1485
|
+
setArea();
|
|
1486
|
+
if (first || !moved) {
|
|
1487
|
+
goTo(aim, false);
|
|
1488
|
+
return;
|
|
1489
|
+
}
|
|
1490
|
+
const base = move?.to ?? view;
|
|
1491
|
+
hold(viewAt(toPicture(base, rectCenter(before)), rectCenter(area), base.k));
|
|
1492
|
+
};
|
|
1493
|
+
function tick(now) {
|
|
1494
|
+
frame = 0;
|
|
1495
|
+
if (disposed) return;
|
|
1496
|
+
if (setSize()) fitAgain();
|
|
1497
|
+
if (!sized) return;
|
|
1498
|
+
const seconds = lastTime === 0 ? 1 / 60 : Math.min((now - lastTime) / 1e3, .1);
|
|
1499
|
+
let active = false;
|
|
1500
|
+
if (move !== void 0) {
|
|
1501
|
+
const step = pictureMoveAt(move, now);
|
|
1502
|
+
view = step.view;
|
|
1503
|
+
if (step.done) move = void 0;
|
|
1504
|
+
active = !step.done;
|
|
1505
|
+
}
|
|
1506
|
+
context.setTransform(1, 0, 0, 1, 0, 0);
|
|
1507
|
+
context.clearRect(0, 0, canvas.width, canvas.height);
|
|
1508
|
+
context.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);
|
|
1509
|
+
const shown = highlights;
|
|
1510
|
+
const drawing = content.draw(context, {
|
|
1511
|
+
view,
|
|
1512
|
+
width,
|
|
1513
|
+
height,
|
|
1514
|
+
area,
|
|
1515
|
+
pixelRatio,
|
|
1516
|
+
seconds,
|
|
1517
|
+
settle: settle || lessMotion()
|
|
1518
|
+
}, theme, (record) => highlightOf(shown, record));
|
|
1519
|
+
settle = false;
|
|
1520
|
+
active = drawing || active;
|
|
1521
|
+
lastTime = active ? now : 0;
|
|
1522
|
+
if (told === void 0 || !sameView(told, view)) {
|
|
1523
|
+
told = view;
|
|
1524
|
+
options.onView?.(view);
|
|
1525
|
+
}
|
|
1526
|
+
if (active) invalidate();
|
|
1527
|
+
}
|
|
1528
|
+
/** This function gives the position of a pointer in the picture, in CSS pixels from its top left corner. */
|
|
1529
|
+
const positionOf = (event) => {
|
|
1530
|
+
const box = canvas.getBoundingClientRect();
|
|
1531
|
+
return {
|
|
1532
|
+
x: event.clientX - box.left,
|
|
1533
|
+
y: event.clientY - box.top
|
|
1534
|
+
};
|
|
1535
|
+
};
|
|
1536
|
+
const pickAt = (screen, slack) => content.pick({
|
|
1537
|
+
point: toPicture(view, screen),
|
|
1538
|
+
screen,
|
|
1539
|
+
view,
|
|
1540
|
+
slack
|
|
1541
|
+
});
|
|
1542
|
+
const setHover = (pick) => {
|
|
1543
|
+
const record = pick?.record;
|
|
1544
|
+
if (record === highlights.hovered) return;
|
|
1545
|
+
highlights = {
|
|
1546
|
+
...highlights,
|
|
1547
|
+
hovered: record
|
|
1548
|
+
};
|
|
1549
|
+
setCursor();
|
|
1550
|
+
invalidate();
|
|
1551
|
+
options.onHover?.(pick?.value);
|
|
1552
|
+
};
|
|
1553
|
+
const listeners = new AbortController();
|
|
1554
|
+
const signal = listeners.signal;
|
|
1555
|
+
canvas.addEventListener("pointerdown", (event) => {
|
|
1556
|
+
if (event.button !== 0) return;
|
|
1557
|
+
inUse = true;
|
|
1558
|
+
canvas.focus({ preventScroll: true });
|
|
1559
|
+
try {
|
|
1560
|
+
canvas.setPointerCapture(event.pointerId);
|
|
1561
|
+
} catch {}
|
|
1562
|
+
if (move !== void 0) {
|
|
1563
|
+
move = void 0;
|
|
1564
|
+
moved = true;
|
|
1565
|
+
}
|
|
1566
|
+
pointers = pointerDown(pointers, event.pointerId, positionOf(event));
|
|
1567
|
+
}, { signal });
|
|
1568
|
+
canvas.addEventListener("pointermove", (event) => {
|
|
1569
|
+
if (pointers.at.has(event.pointerId)) {
|
|
1570
|
+
const next = pointerMove(pointers, event.pointerId, positionOf(event), view, limits);
|
|
1571
|
+
pointers = next.pointers;
|
|
1572
|
+
if (sameView(next.view, view)) return;
|
|
1573
|
+
dragging = true;
|
|
1574
|
+
setCursor();
|
|
1575
|
+
steer(next.view);
|
|
1576
|
+
return;
|
|
1577
|
+
}
|
|
1578
|
+
if (event.pointerType !== "touch" && pointers.at.size === 0) setHover(pickAt(positionOf(event), 5));
|
|
1579
|
+
}, { signal });
|
|
1580
|
+
canvas.addEventListener("pointerup", (event) => {
|
|
1581
|
+
if (!pointers.at.has(event.pointerId)) return;
|
|
1582
|
+
const mouse = event.pointerType === "mouse";
|
|
1583
|
+
const position = positionOf(event);
|
|
1584
|
+
const up = pointerUp(pointers, event.pointerId, position, mouse ? 4 : 10);
|
|
1585
|
+
pointers = up.pointers;
|
|
1586
|
+
if (pointers.at.size === 0) {
|
|
1587
|
+
dragging = false;
|
|
1588
|
+
setCursor();
|
|
1589
|
+
}
|
|
1590
|
+
if (!up.click) return;
|
|
1591
|
+
const pick = pickAt(position, mouse ? 5 : 12);
|
|
1592
|
+
highlights = {
|
|
1593
|
+
...highlights,
|
|
1594
|
+
selected: pick?.record
|
|
1595
|
+
};
|
|
1596
|
+
invalidate();
|
|
1597
|
+
options.onSelect?.(pick?.value);
|
|
1598
|
+
}, { signal });
|
|
1599
|
+
canvas.addEventListener("pointercancel", (event) => {
|
|
1600
|
+
pointers = pointerCancel(pointers, event.pointerId);
|
|
1601
|
+
if (pointers.at.size > 0) return;
|
|
1602
|
+
dragging = false;
|
|
1603
|
+
setCursor();
|
|
1604
|
+
}, { signal });
|
|
1605
|
+
canvas.addEventListener("pointerleave", (event) => {
|
|
1606
|
+
setHover(void 0);
|
|
1607
|
+
if (event.pointerType === "mouse") inUse = false;
|
|
1608
|
+
}, { signal });
|
|
1609
|
+
canvas.addEventListener("blur", () => {
|
|
1610
|
+
inUse = false;
|
|
1611
|
+
}, { signal });
|
|
1612
|
+
canvas.addEventListener("wheel", (event) => {
|
|
1613
|
+
if (wheel === "in-use" && !inUse && !event.ctrlKey) return;
|
|
1614
|
+
event.preventDefault();
|
|
1615
|
+
steer(zoomView(view, positionOf(event), wheelFactor(event.deltaY, event.deltaMode, event.ctrlKey), limits));
|
|
1616
|
+
}, {
|
|
1617
|
+
signal,
|
|
1618
|
+
passive: false
|
|
1619
|
+
});
|
|
1620
|
+
canvas.addEventListener("dblclick", (event) => {
|
|
1621
|
+
event.preventDefault();
|
|
1622
|
+
moved = true;
|
|
1623
|
+
glide(keepRect(zoomView(view, positionOf(event), 2, limits), content.bounds, area), true);
|
|
1624
|
+
}, { signal });
|
|
1625
|
+
canvas.addEventListener("keydown", (event) => {
|
|
1626
|
+
if (event.altKey || event.ctrlKey || event.metaKey) return;
|
|
1627
|
+
const action = pictureKeyAction(event.key, view, area, {
|
|
1628
|
+
home,
|
|
1629
|
+
views: options.keys,
|
|
1630
|
+
limits
|
|
1631
|
+
});
|
|
1632
|
+
if (action === void 0) return;
|
|
1633
|
+
event.preventDefault();
|
|
1634
|
+
inUse = true;
|
|
1635
|
+
if (action.type === "view") goTo(action.view, true);
|
|
1636
|
+
else steer(action.to);
|
|
1637
|
+
}, { signal });
|
|
1638
|
+
const observer = new ResizeObserver(() => {
|
|
1639
|
+
if (!disposed && setSize()) fitAgain();
|
|
1640
|
+
});
|
|
1641
|
+
if (setSize()) fitAgain();
|
|
1642
|
+
observer.observe(container);
|
|
1643
|
+
invalidate();
|
|
1644
|
+
return {
|
|
1645
|
+
canvas,
|
|
1646
|
+
setContent(next) {
|
|
1647
|
+
if (disposed || next === content) return;
|
|
1648
|
+
content.dispose?.();
|
|
1649
|
+
content = next;
|
|
1650
|
+
const selected = highlights.selected !== void 0 && content.has(highlights.selected) ? highlights.selected : void 0;
|
|
1651
|
+
highlights = {
|
|
1652
|
+
...highlights,
|
|
1653
|
+
hovered: void 0,
|
|
1654
|
+
selected
|
|
1655
|
+
};
|
|
1656
|
+
settle = true;
|
|
1657
|
+
setCursor();
|
|
1658
|
+
setArea();
|
|
1659
|
+
if (moved) hold(move?.to ?? view);
|
|
1660
|
+
else goTo(aim, false);
|
|
1661
|
+
},
|
|
1662
|
+
setTheme(next) {
|
|
1663
|
+
theme = next;
|
|
1664
|
+
invalidate();
|
|
1665
|
+
},
|
|
1666
|
+
setView(next, animate = true) {
|
|
1667
|
+
goTo(next, animate);
|
|
1668
|
+
},
|
|
1669
|
+
setInsets(next) {
|
|
1670
|
+
insets = next;
|
|
1671
|
+
setArea();
|
|
1672
|
+
if (!sized) return;
|
|
1673
|
+
if (moved) hold(move?.to ?? view);
|
|
1674
|
+
else goTo(aim, true);
|
|
1675
|
+
},
|
|
1676
|
+
view: () => view,
|
|
1677
|
+
area: () => area,
|
|
1678
|
+
zoomBy(factor, animate = true) {
|
|
1679
|
+
moved = true;
|
|
1680
|
+
glide(keepRect(zoomView(move?.to ?? view, rectCenter(area), factor, limits), content.bounds, area), animate);
|
|
1681
|
+
},
|
|
1682
|
+
highlight(records) {
|
|
1683
|
+
highlights = {
|
|
1684
|
+
...highlights,
|
|
1685
|
+
highlighted: new Set(records)
|
|
1686
|
+
};
|
|
1687
|
+
invalidate();
|
|
1688
|
+
},
|
|
1689
|
+
select(record) {
|
|
1690
|
+
highlights = {
|
|
1691
|
+
...highlights,
|
|
1692
|
+
selected: record
|
|
1693
|
+
};
|
|
1694
|
+
invalidate();
|
|
1695
|
+
},
|
|
1696
|
+
refresh() {
|
|
1697
|
+
if (disposed) return;
|
|
1698
|
+
if (highlights.hovered !== void 0 && !content.has(highlights.hovered)) setHover(void 0);
|
|
1699
|
+
const bounds = content.bounds;
|
|
1700
|
+
if (bounds.x !== fitted.x || bounds.y !== fitted.y || bounds.w !== fitted.w || bounds.h !== fitted.h) {
|
|
1701
|
+
setArea();
|
|
1702
|
+
if (sized) {
|
|
1703
|
+
if (moved) hold(move?.to ?? view);
|
|
1704
|
+
else goTo(aim, true);
|
|
1705
|
+
}
|
|
1706
|
+
}
|
|
1707
|
+
invalidate();
|
|
1708
|
+
},
|
|
1709
|
+
dispose() {
|
|
1710
|
+
if (disposed) return;
|
|
1711
|
+
disposed = true;
|
|
1712
|
+
cancelAnimationFrame(frame);
|
|
1713
|
+
observer.disconnect();
|
|
1714
|
+
listeners.abort();
|
|
1715
|
+
content.dispose?.();
|
|
1716
|
+
canvas.remove();
|
|
1717
|
+
}
|
|
1718
|
+
};
|
|
1719
|
+
}
|
|
1720
|
+
export { ARROW_SPREAD, BAR_HEIGHT, DARK_PICTURE_THEME, DOUBLE_CLICK_ZOOM, FIT_PADDING, FOCUS_FADE, FOCUS_OPACITY, FREE_LABELS, HALO_WIDTH, KEEP_IN_VIEW, LIGHT_PICTURE_THEME, NO_POINTERS, PICK_SLACK, SMALLEST_AREA, SMALLEST_BAR_LINE, SMALLEST_TEXT_LINE, TOUCH_CLICK_DISTANCE, TOUCH_PICK_SLACK, UNIT_VIEW, VIEW_MARGIN, WHEEL_LINE, WHEEL_PINCH, WHEEL_SPEED, ZOOM_LIMITS, aimAll, aimRects, aimReveal, boxDistance, boxFocus, boxState, clampZoom, createPictureContent, createPictureStage, drawShapes, fitRect, focusOf, frameRects, freeArea, indexPicture, keepRect, lookUnit, paintPicture, panView, pickRecord, pictureKeyAction, pictureMoveAt, pinchView, plainLooks, pointerCancel, pointerDown, pointerMove, pointerUp, recordKey, rectToScreen, revealRects, routeFocus, routeLine, routeState, showsRect, stackPictureContents, toPicture, toScreen, viewAt, viewBetween, wheelFactor, zoomLimitsFor, zoomView };
|
|
368
1721
|
|
|
369
1722
|
//# sourceMappingURL=graph.js.map
|