@yaks/diagram 0.0.0-stage → 0.2.36

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.
Files changed (63) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +222 -2
  3. package/_dist/ink.d.ts +30 -0
  4. package/_dist/ink.d.ts.map +1 -0
  5. package/_dist/layout.d.ts +92 -0
  6. package/_dist/layout.d.ts.map +1 -0
  7. package/_dist/mod.d.ts +20 -0
  8. package/_dist/mod.d.ts.map +1 -0
  9. package/_dist/mount.d.ts +55 -0
  10. package/_dist/mount.d.ts.map +1 -0
  11. package/_dist/pen.d.ts +50 -0
  12. package/_dist/pen.d.ts.map +1 -0
  13. package/_dist/shape.d.ts +44 -0
  14. package/_dist/shape.d.ts.map +1 -0
  15. package/_dist/steer.d.ts +13 -0
  16. package/_dist/steer.d.ts.map +1 -0
  17. package/_dist/styles.d.ts +25 -0
  18. package/_dist/styles.d.ts.map +1 -0
  19. package/_dist/svg.d.ts +22 -0
  20. package/_dist/svg.d.ts.map +1 -0
  21. package/_dist/tangle.d.ts +25 -0
  22. package/_dist/tangle.d.ts.map +1 -0
  23. package/_dist/view.d.ts +20 -0
  24. package/_dist/view.d.ts.map +1 -0
  25. package/deno.json +1 -0
  26. package/geometry.js +42 -0
  27. package/geometry.js.map +1 -0
  28. package/geometry.ts +44 -0
  29. package/ink.js +43 -0
  30. package/ink.js.map +1 -0
  31. package/ink.ts +61 -0
  32. package/layout.js +1048 -0
  33. package/layout.js.map +1 -0
  34. package/layout.ts +981 -0
  35. package/layout_bench.ts +27 -0
  36. package/mod.js +20 -0
  37. package/mod.js.map +1 -0
  38. package/mod.ts +20 -0
  39. package/mount.js +177 -0
  40. package/mount.js.map +1 -0
  41. package/mount.ts +205 -0
  42. package/package.json +18 -4
  43. package/pen.js +106 -0
  44. package/pen.js.map +1 -0
  45. package/pen.ts +130 -0
  46. package/shape.js +233 -0
  47. package/shape.js.map +1 -0
  48. package/shape.ts +204 -0
  49. package/steer.js +132 -0
  50. package/steer.js.map +1 -0
  51. package/steer.ts +112 -0
  52. package/styles.js +110 -0
  53. package/styles.js.map +1 -0
  54. package/styles.ts +110 -0
  55. package/svg.js +119 -0
  56. package/svg.js.map +1 -0
  57. package/svg.ts +193 -0
  58. package/tangle.js +151 -0
  59. package/tangle.js.map +1 -0
  60. package/tangle.ts +153 -0
  61. package/view.js +63 -0
  62. package/view.js.map +1 -0
  63. package/view.ts +65 -0
package/tangle.js ADDED
@@ -0,0 +1,151 @@
1
+ // What makes a drawing hard to read, found in a layout as svg() draws it: two
2
+ // shapes too close, a line through a shape it does not end at, two lines that
3
+ // cross or run over each other, and names written over shapes or each other. Pure: a layout in, a list of
4
+ // what is tangled out, each said with the short ids a person reads.
5
+ import { cross } from "./geometry.js";
6
+ import { curves } from "./pen.js";
7
+ import { APART, half, on, tag } from "./shape.js";
8
+ // The points a curve is checked at: enough that a line through the corner of
9
+ // a shape is still seen.
10
+ const STEPS = 24;
11
+ let once = (make)=>{
12
+ let made;
13
+ return ()=>made ??= make();
14
+ };
15
+ /**
16
+ * Everything tangled in a layout: shapes closer than `APART`, a line through a
17
+ * shape that is not one of its ends, lines that cross, lines that run within
18
+ * a few units of each other, and a line's name on a shape or on another
19
+ * name. Each is said with the short ids it involves.
20
+ *
21
+ * ```ts
22
+ * import { layout, tangles } from '@yaks/diagram'
23
+ * import { equal } from '@yaks/testing'
24
+ *
25
+ * let a = { entity: { eid: '0a' }, doc: { title: 'Dune' } }
26
+ * let b = { entity: { eid: '0b' }, review: { of: '0a' } }
27
+ * equal(tangles(layout([a, b])), [])
28
+ * ```
29
+ */ export let tangles = (layout)=>knots(layout).map((k)=>k.said);
30
+ /** Everything tangled in a layout, as {@link tangles} finds it, with what
31
+ * each involves; with `only`, just the tangles that involve one of those
32
+ * keys. */ export let knots = (layout, only)=>{
33
+ let out = [];
34
+ let asked = (...keys)=>!only || keys.some((k)=>only.has(k));
35
+ let knot = (said, ...keys)=>out.push({
36
+ said,
37
+ keys
38
+ });
39
+ let at = new Map(layout.nodes.map((n)=>[
40
+ n.eid,
41
+ n
42
+ ]));
43
+ let size = new Map(layout.nodes.map((n)=>[
44
+ n.eid,
45
+ half(n.shape, !!n.state)
46
+ ]));
47
+ let name = (eid)=>at.get(eid)?.label ?? eid;
48
+ let nodes = layout.nodes;
49
+ for(let i = 0; i < nodes.length; i++){
50
+ for(let j = i + 1; j < nodes.length; j++){
51
+ let a = nodes[i], b = nodes[j];
52
+ if (!asked(a.eid, b.eid)) continue;
53
+ let [aw, ah] = size.get(a.eid), [bw, bh] = size.get(b.eid);
54
+ if (Math.abs(a.x - b.x) < aw + bw + APART && Math.abs(a.y - b.y) < ah + bh + APART) knot(`${a.label} is too close to ${b.label}`, a.eid, b.eid);
55
+ }
56
+ }
57
+ let lines = curves(layout);
58
+ // Each line's points and name are worked out only when something near
59
+ // could meet them.
60
+ let paths = layout.links.map((l)=>{
61
+ let c = lines.get(l.key);
62
+ let xs = [
63
+ c.x0,
64
+ c.cx,
65
+ c.x1
66
+ ], ys = [
67
+ c.y0,
68
+ c.cy,
69
+ c.y1
70
+ ];
71
+ // Its bounds (a curve stays within its control points), as far out as a
72
+ // line beside it still runs over it.
73
+ let box = [
74
+ Math.min(...xs) - 6,
75
+ Math.min(...ys) - 6,
76
+ Math.max(...xs) + 6,
77
+ Math.max(...ys) + 6
78
+ ];
79
+ return {
80
+ l,
81
+ box,
82
+ pts: once(()=>Array.from({
83
+ length: STEPS + 1
84
+ }, (_, i)=>on(c, i / STEPS))),
85
+ named: once(()=>tag(c, l.mid, l.name, l.eid && (l.label ?? '')).box),
86
+ said: `${name(l.from)} ${l.name} ${name(l.to)}`
87
+ };
88
+ });
89
+ let apart = (a, b)=>a[2] < b[0] || b[2] < a[0] || a[3] < b[1] || b[3] < a[1];
90
+ for (let { l, pts, box, said } of paths){
91
+ for (let n of nodes){
92
+ if (n.eid == l.from || n.eid == l.to || !asked(l.key, n.eid)) continue;
93
+ let [w, h] = size.get(n.eid);
94
+ if (apart(box, [
95
+ n.x - w,
96
+ n.y - h,
97
+ n.x + w,
98
+ n.y + h
99
+ ])) continue;
100
+ if (pts().some(([x, y])=>Math.abs(x - n.x) < w && Math.abs(y - n.y) < h)) {
101
+ knot(`${said} runs through ${n.label}`, l.key, n.eid);
102
+ }
103
+ }
104
+ }
105
+ for(let i = 0; i < paths.length; i++){
106
+ for(let j = i + 1; j < paths.length; j++){
107
+ let a = paths[i], b = paths[j];
108
+ if (!asked(a.l.key, b.l.key) || apart(a.box, b.box)) continue;
109
+ let shared = [
110
+ a.l.from,
111
+ a.l.to
112
+ ].filter((e)=>e == b.l.from || e == b.l.to);
113
+ let [ap, bp] = [
114
+ a.pts(),
115
+ b.pts()
116
+ ];
117
+ let crossed = !shared.length && ap.some((p, k)=>k > 0 && bp.some((q, m)=>m > 0 && cross(ap[k - 1], p, bp[m - 1], q)));
118
+ if (crossed) {
119
+ knot(`${a.said} crosses ${b.said}`, a.l.key, b.l.key);
120
+ continue;
121
+ }
122
+ // Lines that share an end meet there; past their first and last
123
+ // stretch they should be apart.
124
+ let inner = (pts)=>pts.slice(4, -4);
125
+ let near = inner(ap).some(([x, y])=>inner(bp).some(([u, v])=>Math.hypot(x - u, y - v) < 6));
126
+ if (near) {
127
+ knot(`${a.said} runs over ${b.said}`, a.l.key, b.l.key);
128
+ }
129
+ }
130
+ }
131
+ // Each line's name, as svg() writes it.
132
+ for (let [i, { said, l, named }] of paths.entries()){
133
+ for (let n of nodes){
134
+ if (!asked(l.key, n.eid)) continue;
135
+ let [x0, y0, x1, y1] = named();
136
+ let [w, h] = size.get(n.eid);
137
+ if (x0 < n.x + w && x1 > n.x - w && y0 < n.y + h && y1 > n.y - h) {
138
+ knot(`the name of ${said} sits on ${n.label}`, l.key, n.eid);
139
+ }
140
+ }
141
+ for (let o of paths.slice(i + 1)){
142
+ if (!asked(l.key, o.l.key)) continue;
143
+ let [x0, y0, x1, y1] = named(), [u0, v0, u1, v1] = o.named();
144
+ if (x0 < u1 && u0 < x1 && y0 < v1 && v0 < y1) {
145
+ knot(`the names of ${said} and ${o.said} sit on each other`, l.key, o.l.key);
146
+ }
147
+ }
148
+ }
149
+ return out;
150
+ };
151
+ //# sourceMappingURL=tangle.js.map
package/tangle.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tangle.js","sources":["./tangle.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,8EAA8E;AAC9E,0GAA0G;AAC1G,oEAAoE;AAEpE,SAAS,KAAK,wBAAkC;AAChD,SAAS,MAAM,mBAAkB;AACjC,SAAS,KAAK,EAAE,IAAI,EAAE,EAAE,EAAE,GAAG,qBAAoB;AAEjD,6EAA6E;AAC7E,yBAAyB;AACzB,MAAM,QAAQ;AAEd,IAAI,OAAO,CAAI;EACb,IAAI;EACJ,OAAO,IAAM,SAAS;AACxB;AAEA;;;;;;;;;;;;;;CAcC,GACD,OAAO,IAAI,UAAU,CAAC,SACpB,MAAM,QAAQ,GAAG,CAAC,CAAC,IAAM,EAAE,IAAI,EAAC;AAMlC;;SAES,GACT,OAAO,IAAI,QAAQ,CAAC,QAAgB;EAClC,IAAI,MAAc,EAAE;EACpB,IAAI,QAAQ,CAAC,GAAG,OAAmB,CAAC,QAAQ,KAAK,IAAI,CAAC,CAAC,IAAM,KAAK,GAAG,CAAC;EACtE,IAAI,OAAO,CAAC,MAAc,GAAG,OAAmB,IAAI,IAAI,CAAC;MAAE;MAAM;IAAK;EACtE,IAAI,KAAK,IAAI,IAAI,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAM;MAAC,EAAE,GAAG;MAAE;KAAE;EACnD,IAAI,OAAO,IAAI,IACb,OAAO,KAAK,CAAC,GAAG,CAAC,CAAC,IAAM;MAAC,EAAE,GAAG;MAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC,EAAE,KAAK;KAAE;EAE3D,IAAI,OAAO,CAAC,MAAgB,GAAG,GAAG,CAAC,MAAM,SAAS;EAClD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two
2
+ // shapes too close, a line through a shape it does not end at, two lines that
3
+ // cross or run over each other, and names written over shapes or each other. Pure: a layout in, a list of
4
+ // what is tangled out, each said with the short ids a person reads.
5
+ import type { Layout } from './layout.ts'
6
+ import { cross, type Spot } from './geometry.js'
7
+ import { curves } from './pen.js'
8
+ import { APART, half, on, tag } from './shape.js'
9
+
10
+ // The points a curve is checked at: enough that a line through the corner of
11
+ // a shape is still seen.
12
+ const STEPS = 24
13
+
14
+ let once = <T>(make: () => T) => {
15
+ let made: T | undefined
16
+ return () => made ??= make()
17
+ }
18
+
19
+ /**
20
+ * Everything tangled in a layout: shapes closer than `APART`, a line through a
21
+ * shape that is not one of its ends, lines that cross, lines that run within
22
+ * a few units of each other, and a line's name on a shape or on another
23
+ * name. Each is said with the short ids it involves.
24
+ *
25
+ * ```ts
26
+ * import { layout, tangles } from '@yaks/diagram'
27
+ * import { equal } from '@yaks/testing'
28
+ *
29
+ * let a = { entity: { eid: '0a' }, doc: { title: 'Dune' } }
30
+ * let b = { entity: { eid: '0b' }, review: { of: '0a' } }
31
+ * equal(tangles(layout([a, b])), [])
32
+ * ```
33
+ */
34
+ export let tangles = (layout: Layout): string[] =>
35
+ knots(layout).map((k) => k.said)
36
+
37
+ /** A tangle, said as {@link tangles} says it, and the shapes and lines it
38
+ * involves, by their keys: a node's eid, a link's key. */
39
+ export type Knot = { said: string; keys: string[] }
40
+
41
+ /** Everything tangled in a layout, as {@link tangles} finds it, with what
42
+ * each involves; with `only`, just the tangles that involve one of those
43
+ * keys. */
44
+ export let knots = (layout: Layout, only?: ReadonlySet<string>): Knot[] => {
45
+ let out: Knot[] = []
46
+ let asked = (...keys: string[]) => !only || keys.some((k) => only.has(k))
47
+ let knot = (said: string, ...keys: string[]) => out.push({ said, keys })
48
+ let at = new Map(layout.nodes.map((n) => [n.eid, n]))
49
+ let size = new Map(
50
+ layout.nodes.map((n) => [n.eid, half(n.shape, !!n.state)]),
51
+ )
52
+ let name = (eid: string) => at.get(eid)?.label ?? eid
53
+ let nodes = layout.nodes
54
+ for (let i = 0; i < nodes.length; i++) {
55
+ for (let j = i + 1; j < nodes.length; j++) {
56
+ let a = nodes[i], b = nodes[j]
57
+ if (!asked(a.eid, b.eid)) continue
58
+ let [aw, ah] = size.get(a.eid)!, [bw, bh] = size.get(b.eid)!
59
+ if (
60
+ Math.abs(a.x - b.x) < aw + bw + APART &&
61
+ Math.abs(a.y - b.y) < ah + bh + APART
62
+ ) knot(`${a.label} is too close to ${b.label}`, a.eid, b.eid)
63
+ }
64
+ }
65
+ let lines = curves(layout)
66
+ // Each line's points and name are worked out only when something near
67
+ // could meet them.
68
+ let paths = layout.links.map((l) => {
69
+ let c = lines.get(l.key)!
70
+ let xs = [c.x0, c.cx, c.x1], ys = [c.y0, c.cy, c.y1]
71
+ // Its bounds (a curve stays within its control points), as far out as a
72
+ // line beside it still runs over it.
73
+ let box = [
74
+ Math.min(...xs) - 6,
75
+ Math.min(...ys) - 6,
76
+ Math.max(...xs) + 6,
77
+ Math.max(...ys) + 6,
78
+ ]
79
+ return {
80
+ l,
81
+ box,
82
+ pts: once(() =>
83
+ Array.from({ length: STEPS + 1 }, (_, i) => on(c, i / STEPS))
84
+ ),
85
+ named: once(() => tag(c, l.mid, l.name, l.eid && (l.label ?? '')).box),
86
+ said: `${name(l.from)} ${l.name} ${name(l.to)}`,
87
+ }
88
+ })
89
+ let apart = (a: number[], b: number[]) =>
90
+ a[2] < b[0] || b[2] < a[0] || a[3] < b[1] || b[3] < a[1]
91
+ for (let { l, pts, box, said } of paths) {
92
+ for (let n of nodes) {
93
+ if (n.eid == l.from || n.eid == l.to || !asked(l.key, n.eid)) continue
94
+ let [w, h] = size.get(n.eid)!
95
+ if (apart(box, [n.x - w, n.y - h, n.x + w, n.y + h])) continue
96
+ if (
97
+ pts().some(([x, y]) => Math.abs(x - n.x) < w && Math.abs(y - n.y) < h)
98
+ ) {
99
+ knot(`${said} runs through ${n.label}`, l.key, n.eid)
100
+ }
101
+ }
102
+ }
103
+ for (let i = 0; i < paths.length; i++) {
104
+ for (let j = i + 1; j < paths.length; j++) {
105
+ let a = paths[i], b = paths[j]
106
+ if (!asked(a.l.key, b.l.key) || apart(a.box, b.box)) continue
107
+ let shared = [a.l.from, a.l.to].filter((e) =>
108
+ e == b.l.from || e == b.l.to
109
+ )
110
+ let [ap, bp] = [a.pts(), b.pts()]
111
+ let crossed = !shared.length &&
112
+ ap.some((p, k) =>
113
+ k > 0 && bp.some((q, m) => m > 0 && cross(ap[k - 1], p, bp[m - 1], q))
114
+ )
115
+ if (crossed) {
116
+ knot(`${a.said} crosses ${b.said}`, a.l.key, b.l.key)
117
+ continue
118
+ }
119
+ // Lines that share an end meet there; past their first and last
120
+ // stretch they should be apart.
121
+ let inner = (pts: Spot[]) => pts.slice(4, -4)
122
+ let near = inner(ap).some(([x, y]) =>
123
+ inner(bp).some(([u, v]) => Math.hypot(x - u, y - v) < 6)
124
+ )
125
+ if (near) {
126
+ knot(`${a.said} runs over ${b.said}`, a.l.key, b.l.key)
127
+ }
128
+ }
129
+ }
130
+ // Each line's name, as svg() writes it.
131
+ for (let [i, { said, l, named }] of paths.entries()) {
132
+ for (let n of nodes) {
133
+ if (!asked(l.key, n.eid)) continue
134
+ let [x0, y0, x1, y1] = named()
135
+ let [w, h] = size.get(n.eid)!
136
+ if (x0 < n.x + w && x1 > n.x - w && y0 < n.y + h && y1 > n.y - h) {
137
+ knot(`the name of ${said} sits on ${n.label}`, l.key, n.eid)
138
+ }
139
+ }
140
+ for (let o of paths.slice(i + 1)) {
141
+ if (!asked(l.key, o.l.key)) continue
142
+ let [x0, y0, x1, y1] = named(), [u0, v0, u1, v1] = o.named()
143
+ if (x0 < u1 && u0 < x1 && y0 < v1 && v0 < y1) {
144
+ knot(
145
+ `the names of ${said} and ${o.said} sit on each other`,
146
+ l.key,
147
+ o.l.key,
148
+ )
149
+ }
150
+ }
151
+ }
152
+ return out
153
+ }
package/view.js ADDED
@@ -0,0 +1,63 @@
1
+ // The part of a drawing's sheet that is in view, as a reader zooms and pans
2
+ // it. Pure: a view and a gesture in, the next view out, never smaller than a
3
+ // sixth of the sheet nor larger than all of it, and never off its edge.
4
+ /** How far in a reader can zoom: a sixth of the sheet across. */ const DEEPEST = 6;
5
+ /** The whole sheet. */ export let whole = ({ width, height })=>[
6
+ 0,
7
+ 0,
8
+ width,
9
+ height
10
+ ];
11
+ // A view kept to the sheet: no larger than it, no smaller than DEEPEST lets,
12
+ // and wholly on it.
13
+ let kept = (layout, [x, y, w, h])=>{
14
+ let k = Math.min(1, Math.max(1 / DEEPEST, w / layout.width));
15
+ w = layout.width * k;
16
+ h = layout.height * k;
17
+ x = Math.max(0, Math.min(layout.width - w, x));
18
+ y = Math.max(0, Math.min(layout.height - h, y));
19
+ return [
20
+ x,
21
+ y,
22
+ w,
23
+ h
24
+ ];
25
+ };
26
+ /**
27
+ * Zoom by `k` (above 1 closer, below 1 farther) about a point given as
28
+ * fractions of the view, which stays where it is.
29
+ *
30
+ * ```ts
31
+ * import { layout, whole, zoom } from '@yaks/diagram'
32
+ * import { equal } from '@yaks/testing'
33
+ *
34
+ * let sheet = layout([], { width: 600, height: 400 })
35
+ * equal(zoom(sheet, whole(sheet), 2, [.5, .5]), [150, 100, 300, 200])
36
+ * equal(zoom(sheet, whole(sheet), .5, [.5, .5]), [0, 0, 600, 400])
37
+ * ```
38
+ */ export let zoom = (layout, [x, y, w, h], k, [fx, fy])=>{
39
+ let [, , nw, nh] = kept(layout, [
40
+ x,
41
+ y,
42
+ w / k,
43
+ h / k
44
+ ]);
45
+ return kept(layout, [
46
+ x + fx * (w - nw),
47
+ y + fy * (h - nh),
48
+ nw,
49
+ nh
50
+ ]);
51
+ };
52
+ /** Move the view by dx, dy in the sheet's units. */ export let pan = (layout, [x, y, w, h], dx, dy)=>kept(layout, [
53
+ x + dx,
54
+ y + dy,
55
+ w,
56
+ h
57
+ ]);
58
+ /** Whether the view shows less than the whole sheet. */ export let zoomed = (layout, [, , w])=>w < layout.width - .5;
59
+ /** Where a point of the sheet is, as fractions of the view. */ export let within = ([x, y, w, h], [px, py])=>[
60
+ (px - x) / w,
61
+ (py - y) / h
62
+ ];
63
+ //# sourceMappingURL=view.js.map
package/view.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"view.js","sources":["./view.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,6EAA6E;AAC7E,wEAAwE;AAMxE,+DAA+D,GAC/D,MAAM,UAAU;AAEhB,qBAAqB,GACrB,OAAO,IAAI,QAAQ,CAAC,EAAE,KAAK,EAAE,MAAM,EAAU,GAAW;IAAC;IAAG;IAAG;IAAO;GAAO,CAAA;AAE7E,6EAA6E;AAC7E,oBAAoB;AACpB,IAAI,OAAO,CAAC,QAAgB,CAAC,GAAG,GAAG,GAAG,EAAQ;EAC5C,IAAI,IAAI,KAAK,GAAG,CAAC,GAAG,KAAK,GAAG,CAAC,IAAI,SAAS,IAAI,OAAO,KAAK;EAC1D,IAAI,OAAO,KAAK,GAAG;EACnB,IAAI,OAAO,MAAM,GAAG;EACpB,IAAI,KAAK,GAAG,CAAC,GAAG,KAAK,GAAG,CAAC,OAAO,KAAK,GAAG,GAAG;EAC3C,IAAI,KAAK,GAAG,CAAC,GAAG,KAAK,GAAG,CAAC,OAAO,MAAM,GAAG,GAAG;EAC5C,OAAO;IAAC;IAAG;IAAG;IAAG;GAAE;AACrB;AAEA;;;;;;;;;;;;CAYC,GACD,OAAO,IAAI,OAAO,CAChB,QACA,CAAC,GAAG,GAAG,GAAG,EAAQ,EAClB,GACA,CAAC,IAAI,GAAqB;EAE1B,IAAI,KAAK,IAAI,GAAG,GAAG,KAAK,QAAQ;IAAC;IAAG;IAAG,IAAI;IAAG,IAAI;GAAE;EACpD,OAAO,KAAK,QAAQ;IAAC,IAAI,KAAK,CAAC,IAAI,EAAE;IAAG,IAAI,KAAK,CAAC,IAAI,EAAE;IAAG;IAAI;GAAG;AACpE,EAAC;AAED,kDAAkD,GAClD,OAAO,IAAI,MAAM,CACf,QACA,CAAC,GAAG,GAAG,GAAG,EAAQ,EAClB,IACA,KACS,KAAK,QAAQ;IAAC,IAAI;IAAI,IAAI;IAAI;IAAG;GAAE,EAAC;AAE/C,sDAAsD,GACtD,OAAO,IAAI,SAAS,CAAC,QAAgB,KAAK,EAAQ,GAChD,IAAI,OAAO,KAAK,GAAG,GAAE;AAEvB,6DAA6D,GAC7D,OAAO,IAAI,SAAS,CAClB,CAAC,GAAG,GAAG,GAAG,EAAQ,EAClB,CAAC,IAAI,GAAqB,GACL;IAAC,CAAC,KAAK,CAAC,IAAI;IAAG,CAAC,KAAK,CAAC,IAAI;GAAE,CAAA"}
package/view.ts ADDED
@@ -0,0 +1,65 @@
1
+ // The part of a drawing's sheet that is in view, as a reader zooms and pans
2
+ // it. Pure: a view and a gesture in, the next view out, never smaller than a
3
+ // sixth of the sheet nor larger than all of it, and never off its edge.
4
+ import type { Layout } from './layout.ts'
5
+
6
+ /** The part of the sheet in view: `[x, y, width, height]`, in its units. */
7
+ export type View = [number, number, number, number]
8
+
9
+ /** How far in a reader can zoom: a sixth of the sheet across. */
10
+ const DEEPEST = 6
11
+
12
+ /** The whole sheet. */
13
+ export let whole = ({ width, height }: Layout): View => [0, 0, width, height]
14
+
15
+ // A view kept to the sheet: no larger than it, no smaller than DEEPEST lets,
16
+ // and wholly on it.
17
+ let kept = (layout: Layout, [x, y, w, h]: View): View => {
18
+ let k = Math.min(1, Math.max(1 / DEEPEST, w / layout.width))
19
+ w = layout.width * k
20
+ h = layout.height * k
21
+ x = Math.max(0, Math.min(layout.width - w, x))
22
+ y = Math.max(0, Math.min(layout.height - h, y))
23
+ return [x, y, w, h]
24
+ }
25
+
26
+ /**
27
+ * Zoom by `k` (above 1 closer, below 1 farther) about a point given as
28
+ * fractions of the view, which stays where it is.
29
+ *
30
+ * ```ts
31
+ * import { layout, whole, zoom } from '@yaks/diagram'
32
+ * import { equal } from '@yaks/testing'
33
+ *
34
+ * let sheet = layout([], { width: 600, height: 400 })
35
+ * equal(zoom(sheet, whole(sheet), 2, [.5, .5]), [150, 100, 300, 200])
36
+ * equal(zoom(sheet, whole(sheet), .5, [.5, .5]), [0, 0, 600, 400])
37
+ * ```
38
+ */
39
+ export let zoom = (
40
+ layout: Layout,
41
+ [x, y, w, h]: View,
42
+ k: number,
43
+ [fx, fy]: [number, number],
44
+ ): View => {
45
+ let [, , nw, nh] = kept(layout, [x, y, w / k, h / k])
46
+ return kept(layout, [x + fx * (w - nw), y + fy * (h - nh), nw, nh])
47
+ }
48
+
49
+ /** Move the view by dx, dy in the sheet's units. */
50
+ export let pan = (
51
+ layout: Layout,
52
+ [x, y, w, h]: View,
53
+ dx: number,
54
+ dy: number,
55
+ ): View => kept(layout, [x + dx, y + dy, w, h])
56
+
57
+ /** Whether the view shows less than the whole sheet. */
58
+ export let zoomed = (layout: Layout, [, , w]: View): boolean =>
59
+ w < layout.width - .5
60
+
61
+ /** Where a point of the sheet is, as fractions of the view. */
62
+ export let within = (
63
+ [x, y, w, h]: View,
64
+ [px, py]: [number, number],
65
+ ): [number, number] => [(px - x) / w, (py - y) / h]