@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/pen.ts ADDED
@@ -0,0 +1,130 @@
1
+ // How a pen draws a layout in: each stage's shapes and lines one after
2
+ // another, each shape followed by the lines that reach it, so a reader who is
3
+ // part way through a stage sees part of it drawn and the pen where the next
4
+ // stroke is going. Pure: a layout and how far each stage has come in, how far
5
+ // each item is drawn and where the pen is out.
6
+ import type { Layout, Link, Node } from './layout.ts'
7
+ import { along, type Curve, curve, on } from './shape.js'
8
+
9
+ /**
10
+ * The curve each line is drawn along, by its key: from the rim of one shape
11
+ * to the rim of the other, bent as far as its `bend`.
12
+ *
13
+ * ```ts
14
+ * import { curves, layout } from '@yaks/diagram'
15
+ * import { equal } from '@yaks/testing'
16
+ *
17
+ * let a = { entity: { eid: '0a' }, doc: { title: 'Dune' } }
18
+ * let b = { entity: { eid: '0b' }, review: { of: '0a' } }
19
+ * equal([...curves(layout([a, b])).keys()], ['0b>review.of>0a'])
20
+ * ```
21
+ */
22
+ export let curves = (layout: Layout): Map<string, Curve> => {
23
+ let at = new Map(layout.nodes.map((d) => [d.eid, d]))
24
+ return new Map(
25
+ layout.links.map((l) => [
26
+ l.key,
27
+ curve(at.get(l.from)!, at.get(l.to)!, l.bend),
28
+ ]),
29
+ )
30
+ }
31
+
32
+ /** A shape or a line, by the key a drawing marks it with: a node's eid or a
33
+ * link's key. */
34
+ export type Item = Node | Link
35
+
36
+ export let keyOf = (item: Item): string => 'key' in item ? item.key : item.eid
37
+
38
+ /**
39
+ * The order a pen draws a layout's items in: stage by stage, and within a
40
+ * stage each shape in the order it joined, then the lines that now have both
41
+ * ends drawn.
42
+ */
43
+ export let strokes = (layout: Layout): Item[][] => {
44
+ let stages = [
45
+ ...new Set([...layout.nodes, ...layout.links].map((x) => x.stage)),
46
+ ].sort((a, b) => a - b)
47
+ let drawn = new Set<string>()
48
+ let stage = (s: number) => {
49
+ let out: Item[] = []
50
+ let lines = layout.links.filter((l) => l.stage == s)
51
+ .sort((a, b) => a.order - b.order)
52
+ let ready = () => {
53
+ for (let l of lines) {
54
+ if (out.includes(l) || !drawn.has(l.from) || !drawn.has(l.to)) continue
55
+ out.push(l)
56
+ }
57
+ }
58
+ // What earlier stages drew counts as drawn already.
59
+ for (let n of layout.nodes) if (n.stage < s) drawn.add(n.eid)
60
+ ready()
61
+ for (
62
+ let n of layout.nodes.filter((n) => n.stage == s)
63
+ .sort((a, b) => a.order - b.order)
64
+ ) {
65
+ out.push(n)
66
+ drawn.add(n.eid)
67
+ ready()
68
+ }
69
+ for (let l of lines) if (!out.includes(l)) out.push(l)
70
+ return out
71
+ }
72
+ return stages.map(stage)
73
+ }
74
+
75
+ /**
76
+ * How far each item is drawn, 0 to 1, by its key, when each stage has come
77
+ * `reached(stage)` of the way in (0 to 1). A stage's items share its way in:
78
+ * the first is drawn while the stage is barely reached and the last only
79
+ * once it nearly is.
80
+ *
81
+ * ```ts
82
+ * import { layout, progress } from '@yaks/diagram'
83
+ * import { equal } from '@yaks/testing'
84
+ *
85
+ * let a = { entity: { eid: '0a' }, doc: { title: 'Dune' } }
86
+ * let drawn = layout([a], { stage: 1 })
87
+ * equal(progress(drawn, () => 0).get('0a'), 0)
88
+ * equal(progress(drawn, () => 1).get('0a'), 1)
89
+ * ```
90
+ */
91
+ export let progress = (
92
+ layout: Layout,
93
+ reached: (stage: number) => number,
94
+ ): Map<string, number> => {
95
+ let out = new Map<string, number>()
96
+ for (let items of strokes(layout)) {
97
+ let p = Math.max(0, Math.min(1, reached(items[0].stage)))
98
+ items.forEach((x, i) =>
99
+ out.set(keyOf(x), Math.max(0, Math.min(1, p * items.length * 1.15 - i)))
100
+ )
101
+ }
102
+ return out
103
+ }
104
+
105
+ /** Where the pen is: on the item it is drawing, at that item's stage. */
106
+ export type Tip = { key: string; stage: number; x: number; y: number }
107
+
108
+ /**
109
+ * The pen's tip: on the latest item still being drawn, as far along its
110
+ * outline or line as it is drawn; none when nothing is part drawn.
111
+ */
112
+ export let tip = (
113
+ layout: Layout,
114
+ drawn: Map<string, number>,
115
+ ): Tip | undefined => {
116
+ let lines = curves(layout)
117
+ let busy = strokes(layout).flat()
118
+ .filter((x) => {
119
+ let t = drawn.get(keyOf(x)) ?? 0
120
+ return t > 0 && t < 1
121
+ }).at(-1)
122
+ if (!busy) return
123
+ let key = keyOf(busy), t = drawn.get(key)!
124
+ let [x, y] = 'key' in busy
125
+ ? on(lines.get(key)!, t)
126
+ : along(busy.shape, !!busy.state, t).map((v, i) =>
127
+ v + (i ? busy.y : busy.x)
128
+ ) as [number, number]
129
+ return { key, stage: busy.stage, x, y }
130
+ }
package/shape.js ADDED
@@ -0,0 +1,233 @@
1
+ // The outline each kind of entity is drawn as, and the geometry a pen needs:
2
+ // how far a box reaches from its centre, where a line toward it meets its
3
+ // rim, and the point a fraction of the way round its outline. Pure.
4
+ /** How an entity is outlined: a box, a circle (a person), a hexagon (an
5
+ * agent) or a note with a folded corner (a comment). */ /** The shape each component draws its entity as; any other is a box. */ export const SHAPES = {
6
+ person: 'round',
7
+ persona: 'hex',
8
+ session: 'hex',
9
+ comment: 'note'
10
+ };
11
+ /** A shape's half width and half height, with room for the title a round
12
+ * shape writes under itself. */ export let half = (shape, task = false)=>shape == 'round' || shape == 'hex' ? [
13
+ 60,
14
+ 54
15
+ ] : shape == 'note' ? [
16
+ 70,
17
+ 25
18
+ ] : task ? [
19
+ 79,
20
+ 28
21
+ ] : [
22
+ 72,
23
+ 25
24
+ ];
25
+ /** The room kept clear between two shapes. */ export const APART = 24;
26
+ /** A round shape's radius. */ export let radius = (shape)=>shape == 'round' ? 23 : 25;
27
+ // A shape's outline as the corners a pen walks, starting top left (or top)
28
+ // and going clockwise; a circle is walked as many short sides.
29
+ let corners = (shape, task)=>{
30
+ if (shape == 'round' || shape == 'hex') {
31
+ let r = radius(shape), n = shape == 'hex' ? 6 : 48;
32
+ return Array.from({
33
+ length: n
34
+ }, (_, i)=>{
35
+ let a = 2 * Math.PI * i / n - Math.PI / 2;
36
+ return [
37
+ Math.cos(a) * r,
38
+ Math.sin(a) * r
39
+ ];
40
+ });
41
+ }
42
+ let [w, h] = half(shape, task);
43
+ return shape == 'note' ? [
44
+ [
45
+ -w,
46
+ -h
47
+ ],
48
+ [
49
+ w - 9,
50
+ -h
51
+ ],
52
+ [
53
+ w,
54
+ -h + 9
55
+ ],
56
+ [
57
+ w,
58
+ h
59
+ ],
60
+ [
61
+ -w,
62
+ h
63
+ ]
64
+ ] : [
65
+ [
66
+ -w,
67
+ -h
68
+ ],
69
+ [
70
+ w,
71
+ -h
72
+ ],
73
+ [
74
+ w,
75
+ h
76
+ ],
77
+ [
78
+ -w,
79
+ h
80
+ ]
81
+ ];
82
+ };
83
+ let n = (x)=>+x.toFixed(1);
84
+ /** A shape's outline as an SVG path, centred on 0,0. */ export let outline = (shape, task = false)=>{
85
+ if (shape == 'round') {
86
+ let r = radius(shape);
87
+ return `M0,${-r}A${r},${r} 0 1 1 0,${r}A${r},${r} 0 1 1 0,${-r}Z`;
88
+ }
89
+ let [[x, y], ...rest] = corners(shape, task);
90
+ return `M${n(x)},${n(y)}` + rest.map(([a, b])=>`L${n(a)},${n(b)}`).join('') + 'Z';
91
+ };
92
+ /** Where a line from a shape's centre toward (dx, dy) leaves it, a little
93
+ * outside so an arrowhead rests on the rim. */ export let rim = (shape, task, dx, dy)=>{
94
+ if (shape == 'round' || shape == 'hex') {
95
+ let l = Math.hypot(dx, dy) || 1, r = radius(shape) + 3;
96
+ return [
97
+ dx / l * r,
98
+ dy / l * r
99
+ ];
100
+ }
101
+ let [w, h] = half(shape, task);
102
+ let t = Math.min((w + 3) / (Math.abs(dx) || 1e-9), (h + 3) / (Math.abs(dy) || 1e-9));
103
+ return [
104
+ dx * t,
105
+ dy * t
106
+ ];
107
+ };
108
+ /** The point a fraction `t` of the way round a shape's outline. */ export let along = (shape, task, t)=>{
109
+ let pts = corners(shape, task);
110
+ let sides = pts.map((p, i)=>{
111
+ let q = pts[(i + 1) % pts.length];
112
+ return [
113
+ p,
114
+ q,
115
+ Math.hypot(q[0] - p[0], q[1] - p[1])
116
+ ];
117
+ });
118
+ let left = Math.max(0, Math.min(1, t)) * sides.reduce((s, [, , l])=>s + l, 0);
119
+ for (let [[ax, ay], [bx, by], l] of sides){
120
+ if (left <= l) {
121
+ let u = l ? left / l : 0;
122
+ return [
123
+ ax + (bx - ax) * u,
124
+ ay + (by - ay) * u
125
+ ];
126
+ }
127
+ left -= l;
128
+ }
129
+ return pts[0];
130
+ };
131
+ /** The point a fraction `t` along a curve. */ export let on = (c, t)=>{
132
+ let u = 1 - t;
133
+ return [
134
+ u * u * c.x0 + 2 * u * t * c.cx + t * t * c.x1,
135
+ u * u * c.y0 + 2 * u * t * c.cy + t * t * c.y1
136
+ ];
137
+ };
138
+ /** The curve from the rim of `a` to the rim of `b`, its middle `bend` to the
139
+ * left of the straight line between them (to the right when negative): the
140
+ * control point sits twice that far out. */ export let curve = (a, b, bend)=>{
141
+ let dx = b.x - a.x, dy = b.y - a.y;
142
+ let [ox, oy] = rim(a.shape, !!a.state, dx, dy);
143
+ let [ix, iy] = rim(b.shape, !!b.state, -dx, -dy);
144
+ let x0 = a.x + ox, y0 = a.y + oy, x1 = b.x + ix, y1 = b.y + iy;
145
+ let len = Math.hypot(x1 - x0, y1 - y0) || 1;
146
+ let nx = -(y1 - y0) / len, ny = (x1 - x0) / len;
147
+ return {
148
+ x0,
149
+ y0,
150
+ cx: (x0 + x1) / 2 + nx * bend * 2,
151
+ cy: (y0 + y1) / 2 + ny * bend * 2,
152
+ x1,
153
+ y1
154
+ };
155
+ };
156
+ // How wide a word is written, by its font size, in the monospace a drawing
157
+ // writes names in.
158
+ let wide = (s, size)=>s.length * size * .6;
159
+ export let tag = (c, t, name, edge)=>{
160
+ let [x, y] = on(c, t);
161
+ if (edge == null) {
162
+ let w = wide(name, 8.5) / 2 + 2;
163
+ return {
164
+ x,
165
+ y,
166
+ name: [
167
+ x,
168
+ y + 3,
169
+ 'middle'
170
+ ],
171
+ sub: [
172
+ x,
173
+ y + 3,
174
+ 'middle'
175
+ ],
176
+ box: [
177
+ x - w,
178
+ y - 5,
179
+ x + w,
180
+ y + 6
181
+ ]
182
+ };
183
+ }
184
+ let u = 1 - t;
185
+ let dx = 2 * u * (c.cx - c.x0) + 2 * t * (c.x1 - c.cx);
186
+ let dy = 2 * u * (c.cy - c.y0) + 2 * t * (c.y1 - c.cy);
187
+ let w = Math.max(wide(name, 10), wide(edge, 8.5));
188
+ if (Math.abs(dy) > Math.abs(dx)) {
189
+ let side = Math.sign(-dy) || 1;
190
+ let x0 = x + side * 12, x1 = x0 + side * w;
191
+ return {
192
+ x,
193
+ y,
194
+ name: [
195
+ x0,
196
+ y - 1,
197
+ side > 0 ? 'start' : 'end'
198
+ ],
199
+ sub: [
200
+ x0,
201
+ y + 11,
202
+ side > 0 ? 'start' : 'end'
203
+ ],
204
+ box: [
205
+ Math.min(x - 8, x1),
206
+ y - 10,
207
+ Math.max(x + 8, x1),
208
+ y + 14
209
+ ]
210
+ };
211
+ }
212
+ return {
213
+ x,
214
+ y,
215
+ name: [
216
+ x,
217
+ y - 11,
218
+ 'middle'
219
+ ],
220
+ sub: [
221
+ x,
222
+ y + 19,
223
+ 'middle'
224
+ ],
225
+ box: [
226
+ x - w / 2,
227
+ y - 20,
228
+ x + w / 2,
229
+ y + 22
230
+ ]
231
+ };
232
+ };
233
+ //# sourceMappingURL=shape.js.map
package/shape.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shape.js","sources":["./shape.ts"],"names":[],"mappings":"AAAA,6EAA6E;AAC7E,0EAA0E;AAC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,KAAK,GAAG,CAAC,KAAK,MAAM,KAAK,KAAK,MAAM;EAC5C,IAAI,KAAK,GAAG,CAAC,MAAM,KAAK,GAAG,CAAC,KAAK;IAC/B,IAAI,OAAO,KAAK,IAAI,CAAC,CAAC,OAAO;IAC7B,IAAI,KAAK,IAAI,OAAO,IAAI,KAAK,KAAK,OAAO;IACzC,OAAO;MACL;MACA;MACA,MAAM;QAAC;QAAI,IAAI;QAAG,OAAO,IAAI,UAAU;OAAM;MAC7C,KAAK;QAAC;QAAI,IAAI;QAAI,OAAO,IAAI,UAAU;OAAM;MAC7C,KAAK;QAAC,KAAK,GAAG,CAAC,IAAI,GAAG;QAAK,IAAI;QAAI,KAAK,GAAG,CAAC,IAAI,GAAG;QAAK,IAAI;OAAG;IACjE;EACF;EACA,OAAO;IACL;IACA;IACA,MAAM;MAAC;MAAG,IAAI;MAAI;KAAS;IAC3B,KAAK;MAAC;MAAG,IAAI;MAAI;KAAS;IAC1B,KAAK;MAAC,IAAI,IAAI;MAAG,IAAI;MAAI,IAAI,IAAI;MAAG,IAAI;KAAG;EAC7C;AACF,EAAC"}
package/shape.ts ADDED
@@ -0,0 +1,204 @@
1
+ // The outline each kind of entity is drawn as, and the geometry a pen needs:
2
+ // how far a box reaches from its centre, where a line toward it meets its
3
+ // rim, and the point a fraction of the way round its outline. Pure.
4
+
5
+ /** How an entity is outlined: a box, a circle (a person), a hexagon (an
6
+ * agent) or a note with a folded corner (a comment). */
7
+ export type Shape = 'box' | 'round' | 'hex' | 'note'
8
+
9
+ /** The shape each component draws its entity as; any other is a box. */
10
+ export const SHAPES: Record<string, Shape> = {
11
+ person: 'round',
12
+ persona: 'hex',
13
+ session: 'hex',
14
+ comment: 'note',
15
+ }
16
+
17
+ /** A shape's half width and half height, with room for the title a round
18
+ * shape writes under itself. */
19
+ export let half = (shape: Shape, task = false): [number, number] =>
20
+ shape == 'round' || shape == 'hex'
21
+ ? [60, 54]
22
+ : shape == 'note'
23
+ ? [70, 25]
24
+ : task
25
+ ? [79, 28]
26
+ : [72, 25]
27
+
28
+ /** The room kept clear between two shapes. */
29
+ export const APART = 24
30
+
31
+ /** A round shape's radius. */
32
+ export let radius = (shape: Shape) => shape == 'round' ? 23 : 25
33
+
34
+ type Spot = [number, number]
35
+
36
+ // A shape's outline as the corners a pen walks, starting top left (or top)
37
+ // and going clockwise; a circle is walked as many short sides.
38
+ let corners = (shape: Shape, task: boolean): Spot[] => {
39
+ if (shape == 'round' || shape == 'hex') {
40
+ let r = radius(shape), n = shape == 'hex' ? 6 : 48
41
+ return Array.from({ length: n }, (_, i) => {
42
+ let a = 2 * Math.PI * i / n - Math.PI / 2
43
+ return [Math.cos(a) * r, Math.sin(a) * r]
44
+ })
45
+ }
46
+ let [w, h] = half(shape, task)
47
+ return shape == 'note'
48
+ ? [[-w, -h], [w - 9, -h], [w, -h + 9], [w, h], [-w, h]]
49
+ : [[-w, -h], [w, -h], [w, h], [-w, h]]
50
+ }
51
+
52
+ let n = (x: number) => +x.toFixed(1)
53
+
54
+ /** A shape's outline as an SVG path, centred on 0,0. */
55
+ export let outline = (shape: Shape, task = false): string => {
56
+ if (shape == 'round') {
57
+ let r = radius(shape)
58
+ return `M0,${-r}A${r},${r} 0 1 1 0,${r}A${r},${r} 0 1 1 0,${-r}Z`
59
+ }
60
+ let [[x, y], ...rest] = corners(shape, task)
61
+ return `M${n(x)},${n(y)}` +
62
+ rest.map(([a, b]) => `L${n(a)},${n(b)}`).join('') +
63
+ 'Z'
64
+ }
65
+
66
+ /** Where a line from a shape's centre toward (dx, dy) leaves it, a little
67
+ * outside so an arrowhead rests on the rim. */
68
+ export let rim = (
69
+ shape: Shape,
70
+ task: boolean,
71
+ dx: number,
72
+ dy: number,
73
+ ): Spot => {
74
+ if (shape == 'round' || shape == 'hex') {
75
+ let l = Math.hypot(dx, dy) || 1, r = radius(shape) + 3
76
+ return [dx / l * r, dy / l * r]
77
+ }
78
+ let [w, h] = half(shape, task)
79
+ let t = Math.min(
80
+ (w + 3) / (Math.abs(dx) || 1e-9),
81
+ (h + 3) / (Math.abs(dy) || 1e-9),
82
+ )
83
+ return [dx * t, dy * t]
84
+ }
85
+
86
+ /** The point a fraction `t` of the way round a shape's outline. */
87
+ export let along = (shape: Shape, task: boolean, t: number): Spot => {
88
+ let pts = corners(shape, task)
89
+ let sides = pts.map((p, i): [Spot, Spot, number] => {
90
+ let q = pts[(i + 1) % pts.length]
91
+ return [p, q, Math.hypot(q[0] - p[0], q[1] - p[1])]
92
+ })
93
+ let left = Math.max(0, Math.min(1, t)) *
94
+ sides.reduce((s, [, , l]) => s + l, 0)
95
+ for (let [[ax, ay], [bx, by], l] of sides) {
96
+ if (left <= l) {
97
+ let u = l ? left / l : 0
98
+ return [ax + (bx - ax) * u, ay + (by - ay) * u]
99
+ }
100
+ left -= l
101
+ }
102
+ return pts[0]
103
+ }
104
+
105
+ /** A line's curve: from one rim, bending through a control point, to the
106
+ * other rim. */
107
+ export type Curve = {
108
+ x0: number
109
+ y0: number
110
+ cx: number
111
+ cy: number
112
+ x1: number
113
+ y1: number
114
+ }
115
+
116
+ /** The point a fraction `t` along a curve. */
117
+ export let on = (c: Curve, t: number): Spot => {
118
+ let u = 1 - t
119
+ return [
120
+ u * u * c.x0 + 2 * u * t * c.cx + t * t * c.x1,
121
+ u * u * c.y0 + 2 * u * t * c.cy + t * t * c.y1,
122
+ ]
123
+ }
124
+
125
+ /** A shape where it sits: its centre, outline and whether it has a state. */
126
+ export type Seat = { x: number; y: number; shape: Shape; state?: string }
127
+
128
+ /** The curve from the rim of `a` to the rim of `b`, its middle `bend` to the
129
+ * left of the straight line between them (to the right when negative): the
130
+ * control point sits twice that far out. */
131
+ export let curve = (a: Seat, b: Seat, bend: number): Curve => {
132
+ let dx = b.x - a.x, dy = b.y - a.y
133
+ let [ox, oy] = rim(a.shape, !!a.state, dx, dy)
134
+ let [ix, iy] = rim(b.shape, !!b.state, -dx, -dy)
135
+ let x0 = a.x + ox, y0 = a.y + oy, x1 = b.x + ix, y1 = b.y + iy
136
+ let len = Math.hypot(x1 - x0, y1 - y0) || 1
137
+ let nx = -(y1 - y0) / len, ny = (x1 - x0) / len
138
+ return {
139
+ x0,
140
+ y0,
141
+ cx: (x0 + x1) / 2 + nx * bend * 2,
142
+ cy: (y0 + y1) / 2 + ny * bend * 2,
143
+ x1,
144
+ y1,
145
+ }
146
+ }
147
+
148
+ /** Where a line's name is written, a fraction along its curve: the point
149
+ * there, where its name and (for an edge) its short id go and how they are
150
+ * anchored, and the box the words cover. A line that is an edge entity has a
151
+ * diamond at the point, with its words beside it, or above and below when
152
+ * the line runs across. */
153
+ export type Tag = {
154
+ x: number
155
+ y: number
156
+ name: [number, number, 'start' | 'middle' | 'end']
157
+ sub: [number, number, 'start' | 'middle' | 'end']
158
+ box: [number, number, number, number]
159
+ }
160
+
161
+ // How wide a word is written, by its font size, in the monospace a drawing
162
+ // writes names in.
163
+ let wide = (s: string, size: number) => s.length * size * .6
164
+
165
+ export let tag = (
166
+ c: Curve,
167
+ t: number,
168
+ name: string,
169
+ edge?: string,
170
+ ): Tag => {
171
+ let [x, y] = on(c, t)
172
+ if (edge == null) {
173
+ let w = wide(name, 8.5) / 2 + 2
174
+ return {
175
+ x,
176
+ y,
177
+ name: [x, y + 3, 'middle'],
178
+ sub: [x, y + 3, 'middle'],
179
+ box: [x - w, y - 5, x + w, y + 6],
180
+ }
181
+ }
182
+ let u = 1 - t
183
+ let dx = 2 * u * (c.cx - c.x0) + 2 * t * (c.x1 - c.cx)
184
+ let dy = 2 * u * (c.cy - c.y0) + 2 * t * (c.y1 - c.cy)
185
+ let w = Math.max(wide(name, 10), wide(edge, 8.5))
186
+ if (Math.abs(dy) > Math.abs(dx)) {
187
+ let side = Math.sign(-dy) || 1
188
+ let x0 = x + side * 12, x1 = x0 + side * w
189
+ return {
190
+ x,
191
+ y,
192
+ name: [x0, y - 1, side > 0 ? 'start' : 'end'],
193
+ sub: [x0, y + 11, side > 0 ? 'start' : 'end'],
194
+ box: [Math.min(x - 8, x1), y - 10, Math.max(x + 8, x1), y + 14],
195
+ }
196
+ }
197
+ return {
198
+ x,
199
+ y,
200
+ name: [x, y - 11, 'middle'],
201
+ sub: [x, y + 19, 'middle'],
202
+ box: [x - w / 2, y - 20, x + w / 2, y + 22],
203
+ }
204
+ }
package/steer.js ADDED
@@ -0,0 +1,132 @@
1
+ // A reader steering a drawing in a page: a pinch (two fingers, or a trackpad,
2
+ // which a browser reports as a wheel with ctrl held) or ctrl-scroll zooms
3
+ // about where it happens, and a drag pans once zoomed in. Plain scrolling
4
+ // still scrolls the page, one finger on a drawing seen whole scrolls it too,
5
+ // and the click that ends a drag is not a click. The views come from view.ts;
6
+ // this only reads the gestures.
7
+ import { pan, zoom, zoomed } from "./view.js";
8
+ // How far a pointer moves before it is a drag, in pixels.
9
+ const SLOP = 4;
10
+ /** Follow a reader's gestures on `el`, which holds the drawing's `<svg>`;
11
+ * update its touch mode when the view changes and stop when it is removed. */ export let steer = (el, s)=>{
12
+ let down = new Map();
13
+ let moved = 0;
14
+ // Where a point on the screen is, as fractions of the view, and how many
15
+ // sheet units a pixel is: the drawing is fitted into its box, centred. A
16
+ // box with no size, as outside a laid out page, is a unit a pixel.
17
+ let frame = ()=>{
18
+ let box = (el.querySelector('svg') ?? el).getBoundingClientRect();
19
+ let [, , w, h] = s.view();
20
+ let bw = box.width || w, bh = box.height || h;
21
+ let k = Math.max(w / bw, h / bh);
22
+ return {
23
+ k,
24
+ at: (cx, cy)=>[
25
+ (cx - box.left - (bw - w / k) / 2) * k / w,
26
+ (cy - box.top - (bh - h / k) / 2) * k / h
27
+ ]
28
+ };
29
+ };
30
+ let touch = ()=>{
31
+ let l = s.layout();
32
+ let deep = !!l && zoomed(l, s.view());
33
+ el.style.touchAction = deep ? 'none' : 'pan-x pan-y';
34
+ el.classList.toggle('is-zoomed', deep);
35
+ };
36
+ let wheel = (e)=>{
37
+ let l = s.layout();
38
+ if (!e.ctrlKey || !l) return;
39
+ e.preventDefault();
40
+ let k = Math.exp(-e.deltaY * .01);
41
+ s.set(zoom(l, s.view(), k, frame().at(e.clientX, e.clientY)));
42
+ };
43
+ let pressed = (e)=>{
44
+ down.set(e.pointerId, [
45
+ e.clientX,
46
+ e.clientY
47
+ ]);
48
+ if (down.size == 1) moved = 0;
49
+ };
50
+ let move = (e)=>{
51
+ let l = s.layout(), was = down.get(e.pointerId);
52
+ if (!l || !was) return;
53
+ let pts = [
54
+ ...down.values()
55
+ ];
56
+ let mid = (ps)=>ps.reduce(([x, y], [a, b])=>[
57
+ x + a / ps.length,
58
+ y + b / ps.length
59
+ ], [
60
+ 0,
61
+ 0
62
+ ]);
63
+ let [ox, oy] = mid(pts);
64
+ let spread = pts.length == 2 ? Math.hypot(pts[0][0] - pts[1][0], pts[0][1] - pts[1][1]) : 0;
65
+ down.set(e.pointerId, [
66
+ e.clientX,
67
+ e.clientY
68
+ ]);
69
+ pts = [
70
+ ...down.values()
71
+ ];
72
+ let [nx, ny] = mid(pts);
73
+ moved += Math.hypot(e.clientX - was[0], e.clientY - was[1]);
74
+ let { k, at } = frame();
75
+ let v = s.view();
76
+ if (pts.length == 2) {
77
+ let now = Math.hypot(pts[0][0] - pts[1][0], pts[0][1] - pts[1][1]);
78
+ v = zoom(l, v, now / (spread || now), at(nx, ny));
79
+ } else if (!zoomed(l, v)) return;
80
+ s.set(pan(l, v, (ox - nx) * k, (oy - ny) * k));
81
+ };
82
+ let up = (e)=>void down.delete(e.pointerId);
83
+ // A click that ends a drag or a pinch moved the drawing; it opens nothing.
84
+ let click = (e)=>{
85
+ if (moved <= SLOP) return;
86
+ e.stopImmediatePropagation();
87
+ e.preventDefault();
88
+ moved = 0;
89
+ };
90
+ let on = [
91
+ [
92
+ 'wheel',
93
+ wheel,
94
+ {
95
+ passive: false
96
+ }
97
+ ],
98
+ [
99
+ 'pointerdown',
100
+ pressed
101
+ ],
102
+ [
103
+ 'pointermove',
104
+ move
105
+ ],
106
+ [
107
+ 'pointerup',
108
+ up
109
+ ],
110
+ [
111
+ 'pointercancel',
112
+ up
113
+ ],
114
+ [
115
+ 'click',
116
+ click,
117
+ {
118
+ capture: true
119
+ }
120
+ ]
121
+ ];
122
+ for (let [type, fn, opts] of on)el.addEventListener(type, fn, opts);
123
+ touch();
124
+ let stop = ()=>{
125
+ for (let [type, fn, opts] of on)el.removeEventListener(type, fn, opts);
126
+ };
127
+ return {
128
+ touch,
129
+ stop
130
+ };
131
+ };
132
+ //# sourceMappingURL=steer.js.map