@mark1russell7/visuals 0.3.4 → 0.3.5

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/build/graph.js CHANGED
@@ -1,4 +1,4 @@
1
- import { c as isClick, d as KEY_ZOOM, i as partAt, l as KEY_PART, n as easeInOut } from "./motion-DViuJNyh.js";
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, u as KEY_PART } from "./motion-VS13e4Pw.js";
2
2
  // src/graph/camera.ts
3
3
  /** The default limits of the zoom. */
4
4
  const ZOOM_LIMITS = {
@@ -123,26 +123,74 @@ function freeArea(size, insets = {}) {
123
123
  }
124
124
  /** The default distance between a rectangle that a camera shows and each edge of the area, in CSS pixels. */
125
125
  const FIT_PADDING = 24;
126
- /** This function gives the middle of a rectangle. */
126
+ /**
127
+ * This function gives the middle of a rectangle.
128
+ * The home of this function is `@mark1russell7/layout-contract`, which does not have it. It moves there with the records of a picture.
129
+ */
127
130
  function middleOf(rect) {
128
131
  return {
129
132
  x: rect.x + rect.w / 2,
130
133
  y: rect.y + rect.h / 2
131
134
  };
132
135
  }
136
+ /** This function gives the zoom at which a rectangle of the picture fills an area of the screen, with a padding at each edge. */
137
+ function fillZoom(rect, area, padding) {
138
+ const width = Math.max(area.w - 2 * padding, 1);
139
+ const height = Math.max(area.h - 2 * padding, 1);
140
+ return Math.min(width / Math.max(rect.w, 0), height / Math.max(rect.h, 0));
141
+ }
133
142
  /**
134
143
  * This function gives the view that shows a rectangle of the picture in an area of the screen, as large as the area permits.
135
144
  * The middle of the rectangle is at the middle of the area. A rectangle with no size gets the largest zoom.
136
145
  */
137
146
  function fitRect(rect, area, options = {}) {
138
147
  const limits = options.limits ?? ZOOM_LIMITS;
139
- const padding = options.padding ?? 24;
140
- const width = Math.max(area.w - 2 * padding, 1);
141
- const height = Math.max(area.h - 2 * padding, 1);
142
- const k = clampZoom(Math.min(options.most ?? limits.max, width / Math.max(rect.w, 0), height / Math.max(rect.h, 0)), limits);
148
+ const k = clampZoom(Math.min(options.most ?? limits.max, fillZoom(rect, area, options.padding ?? 24)), limits);
143
149
  return viewAt(middleOf(rect), middleOf(area), k);
144
150
  }
145
151
  /**
152
+ * This function gives the limits of the zoom for a picture with the bounds `bounds`.
153
+ *
154
+ * The smallest zoom of the result shows all of the picture in the area, with a padding at each edge.
155
+ * A picture that fits at the smallest zoom of `limits` keeps `limits`. Thus a person can show all of a very large picture.
156
+ */
157
+ function zoomLimitsFor(bounds, area, limits = ZOOM_LIMITS, padding = 24) {
158
+ const all = fillZoom(bounds, area, padding);
159
+ return all > 0 && all < limits.min ? {
160
+ min: all,
161
+ max: limits.max
162
+ } : limits;
163
+ }
164
+ /** The length of a picture that stays in the free area when a person moves the picture, in CSS pixels. */
165
+ const KEEP_IN_VIEW = 64;
166
+ /** This function gives the start of a span of the screen that keeps the length `least` in an area, on one axis. */
167
+ function keepSpan(start, length, areaStart, areaLength, least) {
168
+ const keep = Math.min(Math.max(least, 0), length, areaLength);
169
+ return Math.min(Math.max(start, areaStart + keep - length), areaStart + areaLength - keep);
170
+ }
171
+ /**
172
+ * This function gives the view that keeps a part of a rectangle of the picture in an area of the screen.
173
+ * A person who moves the picture then cannot lose it.
174
+ *
175
+ * `least` is the length of the rectangle that stays in the area on each axis, in CSS pixels.
176
+ * A rectangle that is smaller on the screen stays in the area fully. The zoom does not change.
177
+ * The result is `view` itself when the view keeps the rectangle.
178
+ */
179
+ function keepRect(view, rect, area, least = 64) {
180
+ const shown = rectToScreen(view, rect);
181
+ const x = keepSpan(shown.x, shown.w, area.x, area.w, least);
182
+ const y = keepSpan(shown.y, shown.h, area.y, area.h, least);
183
+ return x === shown.x && y === shown.y ? view : {
184
+ x: view.x + x - shown.x,
185
+ y: view.y + y - shown.y,
186
+ k: view.k
187
+ };
188
+ }
189
+ /** This function tells if two views are equal. */
190
+ function sameView(a, b) {
191
+ return a.x === b.x && a.y === b.y && a.k === b.k;
192
+ }
193
+ /**
146
194
  * 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.
147
195
  *
148
196
  * Give the bounds of the largest group of boxes first, then the bounds of smaller groups.
@@ -217,6 +265,34 @@ function pictureMoveAt(move, now) {
217
265
  done: false
218
266
  };
219
267
  }
268
+ // src/graph/aim.ts
269
+ /** This function makes the aim that shows all of the picture, as large as the free area permits. */
270
+ function aimAll(options = {}) {
271
+ return (frame) => fitRect(frame.bounds, frame.area, {
272
+ ...options,
273
+ limits: options.limits ?? frame.limits
274
+ });
275
+ }
276
+ /**
277
+ * This function makes the aim that shows a group of boxes at a zoom that a person reads.
278
+ * The rectangles are the bounds of the groups, the largest group first: `frameRects` selects one. An empty list keeps the view.
279
+ */
280
+ function aimRects(rects, options) {
281
+ return (frame) => frameRects(rects, frame.area, {
282
+ ...options,
283
+ limits: options.limits ?? frame.limits
284
+ }) ?? frame.view;
285
+ }
286
+ /**
287
+ * This function makes the aim that shows a selection, and keeps the view when the selection is in view.
288
+ * The rectangles are the bounds of the groups of the selection, the largest group first: `revealRects` selects one.
289
+ */
290
+ function aimReveal(rects, options) {
291
+ return (frame) => revealRects(frame.view, rects, frame.area, {
292
+ ...options,
293
+ limits: options.limits ?? frame.limits
294
+ });
295
+ }
220
296
  // src/graph/keys.ts
221
297
  /**
222
298
  * This function gives the action of a key on a flat picture, or `undefined` for a key with no action.
@@ -228,10 +304,7 @@ function pictureMoveAt(move, now) {
228
304
  */
229
305
  function pictureKeyAction(key, now, area, options) {
230
306
  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
- };
307
+ const middle = middleOf(area);
235
308
  switch (key) {
236
309
  case "ArrowLeft": return {
237
310
  type: "move",
@@ -364,6 +437,419 @@ function pointerCancel(pointers, id) {
364
437
  at: rest
365
438
  } : NO_POINTERS;
366
439
  }
367
- export { FIT_PADDING, NO_POINTERS, SMALLEST_AREA, TOUCH_CLICK_DISTANCE, UNIT_VIEW, WHEEL_LINE, WHEEL_PINCH, WHEEL_SPEED, ZOOM_LIMITS, clampZoom, fitRect, frameRects, freeArea, panView, pictureKeyAction, pictureMoveAt, pinchView, pointerCancel, pointerDown, pointerMove, pointerUp, rectToScreen, revealRects, showsRect, toPicture, toScreen, viewAt, viewBetween, wheelFactor, zoomView };
440
+ // src/graph/stack.ts
441
+ /**
442
+ * This function makes one content from a list of contents, for a flat stage that shows them together.
443
+ * An example is a layer of boxes below a layer of routes and a layer of labels.
444
+ *
445
+ * - The first content gives the bounds: the views and the limits of the camera come from it.
446
+ * - The stack draws the contents in their sequence, so the last content is on top.
447
+ * - A pick asks the last content first. The first record that a content gives is the record.
448
+ * - A record that two contents have is one record: each of them gets its highlight.
449
+ * - A content does not get the state of the canvas of an earlier content: the stack restores it.
450
+ * - The stage disposes the stack, and the stack disposes each content.
451
+ */
452
+ function stackPictureContents(contents) {
453
+ const [first] = contents;
454
+ return {
455
+ get bounds() {
456
+ return first.bounds;
457
+ },
458
+ has: (record) => contents.some((content) => content.has(record)),
459
+ pick(at) {
460
+ for (let index = contents.length - 1; index >= 0; index -= 1) {
461
+ const pick = contents[index]?.pick(at);
462
+ if (pick !== void 0) return pick;
463
+ }
464
+ },
465
+ draw(context, frame, theme, highlight) {
466
+ let active = false;
467
+ for (const content of contents) {
468
+ context.save();
469
+ active = content.draw(context, frame, theme, highlight) || active;
470
+ context.restore();
471
+ }
472
+ return active;
473
+ },
474
+ dispose() {
475
+ for (const content of contents) content.dispose?.();
476
+ }
477
+ };
478
+ }
479
+ // src/graph/stage.ts
480
+ /** The distance from a mouse at which a thin part is below the pointer, in CSS pixels. */
481
+ const PICK_SLACK = 5;
482
+ /** The distance from a finger or a pen at which a thin part is below the pointer, in CSS pixels. */
483
+ const TOUCH_PICK_SLACK = 12;
484
+ /** The factor of the zoom for a double click. */
485
+ const DOUBLE_CLICK_ZOOM = 2;
486
+ /** The largest number of pixels of the canvas for one CSS pixel. A larger number costs memory, and a person does not see it. */
487
+ const LARGEST_PIXEL_RATIO = 3;
488
+ /**
489
+ * This function makes a flat stage in a container of a page. The stage fills the container, so give the container a size.
490
+ *
491
+ * A person moves the picture with a drag, and changes the zoom with the wheel, with two fingers or with a double click.
492
+ * A click selects the record below the pointer. With the keyboard, the arrow keys move the camera, `+` and `-` change the zoom,
493
+ * and `Home` shows the first view. The stage makes a picture only after a change.
494
+ *
495
+ * The function gives `undefined` when the browser gives no 2D context for a canvas. It does not throw.
496
+ */
497
+ function createPictureStage(container, options) {
498
+ const canvas = container.ownerDocument.createElement("canvas");
499
+ const made = canvas.getContext("2d");
500
+ if (made === null) return void 0;
501
+ const context = made;
502
+ canvas.style.display = "block";
503
+ canvas.style.width = "100%";
504
+ canvas.style.height = "100%";
505
+ canvas.style.cursor = "grab";
506
+ canvas.style.touchAction = "none";
507
+ canvas.tabIndex = 0;
508
+ canvas.setAttribute("role", "application");
509
+ canvas.setAttribute("aria-label", options.label);
510
+ container.append(canvas);
511
+ const views = {
512
+ all: aimAll(),
513
+ ...options.views
514
+ };
515
+ const home = options.home ?? "all";
516
+ const baseLimits = options.limits ?? ZOOM_LIMITS;
517
+ const wheel = options.wheel ?? "in-use";
518
+ let content = options.content;
519
+ let theme = options.theme;
520
+ let insets = options.insets ?? {};
521
+ let highlights = NO_HIGHLIGHTS;
522
+ /** The size of the picture, in CSS pixels. */
523
+ let width = 0;
524
+ let height = 0;
525
+ let pixelRatio = 1;
526
+ /** This value is true after the container gave a size. A stage with no size makes no picture. */
527
+ let sized = false;
528
+ let area = freeArea({
529
+ width,
530
+ height
531
+ }, insets);
532
+ let limits = baseLimits;
533
+ let view = UNIT_VIEW;
534
+ /** The view that the camera has, or goes to: a name of the views, or an aim. */
535
+ let aim = home;
536
+ let move;
537
+ /** This value is true after a person moved the view. Then a new size of the container does not aim the camera again. */
538
+ let moved = false;
539
+ /** This value is true until the content had a picture with no time. */
540
+ let settle = true;
541
+ /** This value is true while a person uses the picture. The wheel then changes the zoom. */
542
+ let inUse = false;
543
+ let pointers = NO_POINTERS;
544
+ /** This value is true while a pointer that is down moves the picture. */
545
+ let dragging = false;
546
+ /** The view that `onView` got last. */
547
+ let told;
548
+ let frame = 0;
549
+ let lastTime = 0;
550
+ let disposed = false;
551
+ const invalidate = () => {
552
+ if (frame === 0 && !disposed) frame = requestAnimationFrame(tick);
553
+ };
554
+ const setCursor = () => {
555
+ canvas.style.cursor = dragging ? "grabbing" : highlights.hovered === void 0 ? "grab" : "pointer";
556
+ };
557
+ /** This function sets the free area and the limits of the zoom, from the size, the insets and the content. */
558
+ const setArea = () => {
559
+ area = freeArea({
560
+ width,
561
+ height
562
+ }, insets);
563
+ limits = zoomLimitsFor(content.bounds, area, baseLimits);
564
+ };
565
+ const aimFrame = () => ({
566
+ bounds: content.bounds,
567
+ area,
568
+ view,
569
+ limits
570
+ });
571
+ /** This function moves the camera to a view, with a move or immediately. A picture with no size has no move. */
572
+ const glide = (to, animate) => {
573
+ if (animate && sized && !lessMotion() && !sameView(view, to)) move = {
574
+ from: view,
575
+ to,
576
+ start: performance.now(),
577
+ duration: 380
578
+ };
579
+ else {
580
+ move = void 0;
581
+ view = to;
582
+ }
583
+ invalidate();
584
+ };
585
+ const goTo = (next, animate) => {
586
+ const found = typeof next === "string" ? views[next] : next;
587
+ if (found === void 0) return;
588
+ aim = next;
589
+ moved = false;
590
+ glide(found(aimFrame()), animate);
591
+ };
592
+ /**
593
+ * 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.
594
+ * A zoom that the limits change keeps the point of the picture at the middle of the free area.
595
+ */
596
+ const hold = (base) => {
597
+ const k = clampZoom(base.k, limits);
598
+ const middle = middleOf(area);
599
+ move = void 0;
600
+ view = keepRect(k === base.k ? base : viewAt(toPicture(base, middle), middle, k), content.bounds, area);
601
+ invalidate();
602
+ };
603
+ /** This function sets a view that a person made with a pointer, the wheel or a key. */
604
+ const steer = (next) => {
605
+ moved = true;
606
+ hold(next);
607
+ };
608
+ /**
609
+ * This function sets the size of the canvas from the size of the container and the pixels of the screen.
610
+ * It gives true after a change. A container with no size changes nothing.
611
+ */
612
+ const setSize = () => {
613
+ const wide = container.clientWidth;
614
+ const tall = container.clientHeight;
615
+ if (wide === 0 || tall === 0) return false;
616
+ const ratio = Math.min(globalThis.devicePixelRatio > 0 ? globalThis.devicePixelRatio : 1, LARGEST_PIXEL_RATIO);
617
+ if (wide === width && tall === height && ratio === pixelRatio) return false;
618
+ width = wide;
619
+ height = tall;
620
+ pixelRatio = ratio;
621
+ canvas.width = Math.round(wide * ratio);
622
+ canvas.height = Math.round(tall * ratio);
623
+ return true;
624
+ };
625
+ /** This function sets the camera after a change of the size of the picture. */
626
+ const fitAgain = () => {
627
+ const before = area;
628
+ const first = !sized;
629
+ sized = true;
630
+ setArea();
631
+ if (first || !moved) {
632
+ goTo(aim, false);
633
+ return;
634
+ }
635
+ const base = move?.to ?? view;
636
+ hold(viewAt(toPicture(base, middleOf(before)), middleOf(area), base.k));
637
+ };
638
+ function tick(now) {
639
+ frame = 0;
640
+ if (disposed) return;
641
+ if (setSize()) fitAgain();
642
+ if (!sized) return;
643
+ const seconds = lastTime === 0 ? 1 / 60 : Math.min((now - lastTime) / 1e3, .1);
644
+ let active = false;
645
+ if (move !== void 0) {
646
+ const step = pictureMoveAt(move, now);
647
+ view = step.view;
648
+ if (step.done) move = void 0;
649
+ active = !step.done;
650
+ }
651
+ context.setTransform(1, 0, 0, 1, 0, 0);
652
+ context.clearRect(0, 0, canvas.width, canvas.height);
653
+ context.setTransform(pixelRatio, 0, 0, pixelRatio, 0, 0);
654
+ const shown = highlights;
655
+ const drawing = content.draw(context, {
656
+ view,
657
+ width,
658
+ height,
659
+ area,
660
+ pixelRatio,
661
+ seconds,
662
+ settle: settle || lessMotion()
663
+ }, theme, (record) => highlightOf(shown, record));
664
+ settle = false;
665
+ active = drawing || active;
666
+ lastTime = active ? now : 0;
667
+ if (told === void 0 || !sameView(told, view)) {
668
+ told = view;
669
+ options.onView?.(view);
670
+ }
671
+ if (active) invalidate();
672
+ }
673
+ /** This function gives the position of a pointer in the picture, in CSS pixels from its top left corner. */
674
+ const positionOf = (event) => {
675
+ const box = canvas.getBoundingClientRect();
676
+ return {
677
+ x: event.clientX - box.left,
678
+ y: event.clientY - box.top
679
+ };
680
+ };
681
+ const pickAt = (screen, slack) => content.pick({
682
+ point: toPicture(view, screen),
683
+ screen,
684
+ view,
685
+ slack
686
+ });
687
+ const setHover = (pick) => {
688
+ const record = pick?.record;
689
+ if (record === highlights.hovered) return;
690
+ highlights = {
691
+ ...highlights,
692
+ hovered: record
693
+ };
694
+ setCursor();
695
+ invalidate();
696
+ options.onHover?.(pick?.value);
697
+ };
698
+ const listeners = new AbortController();
699
+ const signal = listeners.signal;
700
+ canvas.addEventListener("pointerdown", (event) => {
701
+ if (event.button !== 0) return;
702
+ inUse = true;
703
+ canvas.focus({ preventScroll: true });
704
+ try {
705
+ canvas.setPointerCapture(event.pointerId);
706
+ } catch {}
707
+ if (move !== void 0) {
708
+ move = void 0;
709
+ moved = true;
710
+ }
711
+ pointers = pointerDown(pointers, event.pointerId, positionOf(event));
712
+ }, { signal });
713
+ canvas.addEventListener("pointermove", (event) => {
714
+ if (pointers.at.has(event.pointerId)) {
715
+ const next = pointerMove(pointers, event.pointerId, positionOf(event), view, limits);
716
+ pointers = next.pointers;
717
+ if (sameView(next.view, view)) return;
718
+ dragging = true;
719
+ setCursor();
720
+ steer(next.view);
721
+ return;
722
+ }
723
+ if (event.pointerType !== "touch" && pointers.at.size === 0) setHover(pickAt(positionOf(event), 5));
724
+ }, { signal });
725
+ canvas.addEventListener("pointerup", (event) => {
726
+ if (!pointers.at.has(event.pointerId)) return;
727
+ const mouse = event.pointerType === "mouse";
728
+ const position = positionOf(event);
729
+ const up = pointerUp(pointers, event.pointerId, position, mouse ? 4 : 10);
730
+ pointers = up.pointers;
731
+ if (pointers.at.size === 0) {
732
+ dragging = false;
733
+ setCursor();
734
+ }
735
+ if (!up.click) return;
736
+ const pick = pickAt(position, mouse ? 5 : 12);
737
+ highlights = {
738
+ ...highlights,
739
+ selected: pick?.record
740
+ };
741
+ invalidate();
742
+ options.onSelect?.(pick?.value);
743
+ }, { signal });
744
+ canvas.addEventListener("pointercancel", (event) => {
745
+ pointers = pointerCancel(pointers, event.pointerId);
746
+ if (pointers.at.size > 0) return;
747
+ dragging = false;
748
+ setCursor();
749
+ }, { signal });
750
+ canvas.addEventListener("pointerleave", (event) => {
751
+ setHover(void 0);
752
+ if (event.pointerType === "mouse") inUse = false;
753
+ }, { signal });
754
+ canvas.addEventListener("blur", () => {
755
+ inUse = false;
756
+ }, { signal });
757
+ canvas.addEventListener("wheel", (event) => {
758
+ if (wheel === "in-use" && !inUse && !event.ctrlKey) return;
759
+ event.preventDefault();
760
+ steer(zoomView(view, positionOf(event), wheelFactor(event.deltaY, event.deltaMode, event.ctrlKey), limits));
761
+ }, {
762
+ signal,
763
+ passive: false
764
+ });
765
+ canvas.addEventListener("dblclick", (event) => {
766
+ event.preventDefault();
767
+ moved = true;
768
+ glide(keepRect(zoomView(view, positionOf(event), 2, limits), content.bounds, area), true);
769
+ }, { signal });
770
+ canvas.addEventListener("keydown", (event) => {
771
+ if (event.altKey || event.ctrlKey || event.metaKey) return;
772
+ const action = pictureKeyAction(event.key, view, area, {
773
+ home,
774
+ views: options.keys,
775
+ limits
776
+ });
777
+ if (action === void 0) return;
778
+ event.preventDefault();
779
+ inUse = true;
780
+ if (action.type === "view") goTo(action.view, true);
781
+ else steer(action.to);
782
+ }, { signal });
783
+ const observer = new ResizeObserver(() => {
784
+ if (!disposed && setSize()) fitAgain();
785
+ });
786
+ if (setSize()) fitAgain();
787
+ observer.observe(container);
788
+ invalidate();
789
+ return {
790
+ canvas,
791
+ setContent(next) {
792
+ if (disposed || next === content) return;
793
+ content.dispose?.();
794
+ content = next;
795
+ const selected = highlights.selected !== void 0 && content.has(highlights.selected) ? highlights.selected : void 0;
796
+ highlights = {
797
+ ...highlights,
798
+ hovered: void 0,
799
+ selected
800
+ };
801
+ settle = true;
802
+ setCursor();
803
+ setArea();
804
+ if (moved) hold(move?.to ?? view);
805
+ else goTo(aim, false);
806
+ },
807
+ setTheme(next) {
808
+ theme = next;
809
+ invalidate();
810
+ },
811
+ setView(next, animate = true) {
812
+ goTo(next, animate);
813
+ },
814
+ setInsets(next) {
815
+ insets = next;
816
+ setArea();
817
+ if (!sized) return;
818
+ if (moved) hold(move?.to ?? view);
819
+ else goTo(aim, true);
820
+ },
821
+ view: () => view,
822
+ area: () => area,
823
+ zoomBy(factor, animate = true) {
824
+ moved = true;
825
+ glide(keepRect(zoomView(move?.to ?? view, middleOf(area), factor, limits), content.bounds, area), animate);
826
+ },
827
+ highlight(records) {
828
+ highlights = {
829
+ ...highlights,
830
+ highlighted: new Set(records)
831
+ };
832
+ invalidate();
833
+ },
834
+ select(record) {
835
+ highlights = {
836
+ ...highlights,
837
+ selected: record
838
+ };
839
+ invalidate();
840
+ },
841
+ refresh: invalidate,
842
+ dispose() {
843
+ if (disposed) return;
844
+ disposed = true;
845
+ cancelAnimationFrame(frame);
846
+ observer.disconnect();
847
+ listeners.abort();
848
+ content.dispose?.();
849
+ canvas.remove();
850
+ }
851
+ };
852
+ }
853
+ export { DOUBLE_CLICK_ZOOM, FIT_PADDING, KEEP_IN_VIEW, NO_POINTERS, PICK_SLACK, SMALLEST_AREA, TOUCH_CLICK_DISTANCE, TOUCH_PICK_SLACK, UNIT_VIEW, WHEEL_LINE, WHEEL_PINCH, WHEEL_SPEED, ZOOM_LIMITS, aimAll, aimRects, aimReveal, clampZoom, createPictureStage, fitRect, frameRects, freeArea, keepRect, panView, pictureKeyAction, pictureMoveAt, pinchView, pointerCancel, pointerDown, pointerMove, pointerUp, rectToScreen, revealRects, showsRect, stackPictureContents, toPicture, toScreen, viewAt, viewBetween, wheelFactor, zoomLimitsFor, zoomView };
368
854
 
369
855
  //# sourceMappingURL=graph.js.map