@phun-ky/arclace 0.0.0-stage → 0.2.0
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/CHANGELOG.md +44 -0
- package/LICENSE +21 -0
- package/README.md +315 -2
- package/dist/arclace.d.ts +411 -0
- package/dist/arclace.js +640 -0
- package/dist/arclace.js.map +1 -0
- package/dist/arclace.umd.cjs +2 -0
- package/dist/arclace.umd.cjs.map +1 -0
- package/dist/style.css +52 -0
- package/package.json +116 -4
- package/src/Arclace.ts +488 -0
- package/src/Connection.ts +407 -0
- package/src/index.ts +30 -0
- package/src/style.css +52 -0
- package/src/types.ts +200 -0
- package/src/utils/geometry.ts +58 -0
- package/src/utils/id.ts +14 -0
- package/src/utils/measure.ts +64 -0
- package/src/utils/paths.ts +310 -0
- package/src/utils/resolve-element.ts +29 -0
- package/src/utils/sides.ts +108 -0
- package/src/utils/svg.ts +34 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ArcPointInterface,
|
|
3
|
+
ArcRectInterface,
|
|
4
|
+
ArcSideType
|
|
5
|
+
} from '../types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Unit vectors pointing away from each side of a box.
|
|
9
|
+
*/
|
|
10
|
+
export const NORMALS: Record<ArcSideType, ArcPointInterface> = {
|
|
11
|
+
top: { x: 0, y: -1 },
|
|
12
|
+
right: { x: 1, y: 0 },
|
|
13
|
+
bottom: { x: 0, y: 1 },
|
|
14
|
+
left: { x: -1, y: 0 }
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const OPPOSITE: Record<ArcSideType, ArcSideType> = {
|
|
18
|
+
top: 'bottom',
|
|
19
|
+
right: 'left',
|
|
20
|
+
bottom: 'top',
|
|
21
|
+
left: 'right'
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const isHorizontalSide = (side: ArcSideType): boolean =>
|
|
25
|
+
side === 'left' || side === 'right';
|
|
26
|
+
|
|
27
|
+
export const center = (rect: ArcRectInterface): ArcPointInterface => ({
|
|
28
|
+
x: rect.left + rect.width / 2,
|
|
29
|
+
y: rect.top + rect.height / 2
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
export const add = (
|
|
33
|
+
a: ArcPointInterface,
|
|
34
|
+
b: ArcPointInterface,
|
|
35
|
+
factor = 1
|
|
36
|
+
): ArcPointInterface => ({ x: a.x + b.x * factor, y: a.y + b.y * factor });
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Euclidean distance between two points.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* distance({ x: 0, y: 0 }, { x: 3, y: 4 }); // 5
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export const distance = (a: ArcPointInterface, b: ArcPointInterface): number =>
|
|
47
|
+
Math.hypot(b.x - a.x, b.y - a.y);
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Rounds to two decimals to keep path data short.
|
|
51
|
+
*/
|
|
52
|
+
export const round = (value: number): number => Math.round(value * 100) / 100;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Formats a point for use in SVG path data.
|
|
56
|
+
*/
|
|
57
|
+
export const fmt = (point: ArcPointInterface): string =>
|
|
58
|
+
`${round(point.x)} ${round(point.y)}`;
|
package/src/utils/id.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generates a unique ID consisting of an underscore followed by a random alphanumeric string.
|
|
3
|
+
*
|
|
4
|
+
* @returns {string} - A unique ID.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* // Generate a unique ID
|
|
9
|
+
* const id = uniqueID();
|
|
10
|
+
* console.log(id); // Example output: "_abc123def"
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export const uniqueID = (): string =>
|
|
14
|
+
'_' + Math.random().toString(36).substring(2, 11);
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { ArcRectInterface } from '../types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Where the SVG overlay is on screen, and how much it is scaled by CSS
|
|
5
|
+
* transforms on its ancestors.
|
|
6
|
+
*/
|
|
7
|
+
export type ArcOriginType = {
|
|
8
|
+
left: number;
|
|
9
|
+
top: number;
|
|
10
|
+
scaleX: number;
|
|
11
|
+
scaleY: number;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Measures the overlay so screen coordinates can be converted to coordinates
|
|
16
|
+
* inside it. Because the overlay is measured directly, this works no matter
|
|
17
|
+
* how the container is positioned, scrolled or scaled.
|
|
18
|
+
*/
|
|
19
|
+
export const getOrigin = (svg: SVGSVGElement): ArcOriginType => {
|
|
20
|
+
const rect = svg.getBoundingClientRect();
|
|
21
|
+
const { clientWidth, clientHeight } = svg;
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
left: rect.left,
|
|
25
|
+
top: rect.top,
|
|
26
|
+
scaleX: clientWidth > 0 && rect.width > 0 ? rect.width / clientWidth : 1,
|
|
27
|
+
scaleY: clientHeight > 0 && rect.height > 0 ? rect.height / clientHeight : 1
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Measures an element in the overlay's coordinate system.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* const rect = getLocalRect(element, getOrigin(svg));
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export const getLocalRect = (
|
|
40
|
+
el: Element,
|
|
41
|
+
origin: ArcOriginType
|
|
42
|
+
): ArcRectInterface => {
|
|
43
|
+
const rect = el.getBoundingClientRect();
|
|
44
|
+
const left = (rect.left - origin.left) / origin.scaleX;
|
|
45
|
+
const top = (rect.top - origin.top) / origin.scaleY;
|
|
46
|
+
const width = rect.width / origin.scaleX;
|
|
47
|
+
const height = rect.height / origin.scaleY;
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
left,
|
|
51
|
+
top,
|
|
52
|
+
width,
|
|
53
|
+
height,
|
|
54
|
+
right: left + width,
|
|
55
|
+
bottom: top + height
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Whether an element is in the document and has a layout box, i.e. is not
|
|
61
|
+
* `display: none`.
|
|
62
|
+
*/
|
|
63
|
+
export const isRendered = (el: Element): boolean =>
|
|
64
|
+
el.isConnected && el.getClientRects().length > 0;
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import type { ArcPointInterface, ArcSideType } from '../types';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
add,
|
|
5
|
+
distance,
|
|
6
|
+
fmt,
|
|
7
|
+
isHorizontalSide,
|
|
8
|
+
NORMALS,
|
|
9
|
+
OPPOSITE
|
|
10
|
+
} from './geometry';
|
|
11
|
+
|
|
12
|
+
export type ArcPathResultType = {
|
|
13
|
+
/**
|
|
14
|
+
* SVG path data.
|
|
15
|
+
*/
|
|
16
|
+
d: string;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The middle of the path, used to place labels.
|
|
20
|
+
*/
|
|
21
|
+
labelPoint: ArcPointInterface;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Normalizes a distance to a range from 0 to 1, based on max distance.
|
|
26
|
+
*
|
|
27
|
+
* @param distance - The actual distance.
|
|
28
|
+
* @param maxDistance - The distance treated as 100% (1.0).
|
|
29
|
+
* @returns Normalized value between 0 and 1.
|
|
30
|
+
*/
|
|
31
|
+
export const normalizeDistance = (
|
|
32
|
+
distance: number,
|
|
33
|
+
maxDistance: number = 1000
|
|
34
|
+
): number => Math.min(distance / maxDistance, 1);
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Maps a normalized value [0, 1] to a value between min and max.
|
|
38
|
+
*
|
|
39
|
+
* @param norm - Normalized value (0 to 1).
|
|
40
|
+
* @param min - Minimum output value.
|
|
41
|
+
* @param max - Maximum output value.
|
|
42
|
+
* @returns Value between min and max.
|
|
43
|
+
*/
|
|
44
|
+
export const lerp = (norm: number, min: number, max: number): number =>
|
|
45
|
+
min + (max - min) * norm;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Calculates a curve multiplier based on distance. Short lines get a
|
|
49
|
+
* relatively larger bulge than long lines.
|
|
50
|
+
*
|
|
51
|
+
* @param distance - Distance between points.
|
|
52
|
+
* @param min - Minimum multiplier.
|
|
53
|
+
* @param max - Maximum multiplier.
|
|
54
|
+
* @param maxDistance - Distance at which max multiplier is used.
|
|
55
|
+
* @returns A multiplier between min and max.
|
|
56
|
+
*/
|
|
57
|
+
export const curveMultiplierFromDistance = (
|
|
58
|
+
distance: number,
|
|
59
|
+
min: number = 1.1,
|
|
60
|
+
max: number = 3.5,
|
|
61
|
+
maxDistance: number = 1000
|
|
62
|
+
): number => lerp(normalizeDistance(distance, maxDistance), min, max);
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* A straight line between two points.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* straightPath({ x: 0, y: 0 }, { x: 100, y: 0 }).d; // 'M 0 0 L 100 0'
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export const straightPath = (
|
|
73
|
+
start: ArcPointInterface,
|
|
74
|
+
stop: ArcPointInterface
|
|
75
|
+
): ArcPathResultType => ({
|
|
76
|
+
d: `M ${fmt(start)} L ${fmt(stop)}`,
|
|
77
|
+
labelPoint: { x: (start.x + stop.x) / 2, y: (start.y + stop.y) / 2 }
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* A cubic bezier curve that leaves `start` and arrives at `stop`
|
|
82
|
+
* perpendicular to their sides.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```ts
|
|
86
|
+
* bezierPath({ x: 0, y: 0 }, 'right', { x: 200, y: 100 }, 'left').d;
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export const bezierPath = (
|
|
90
|
+
start: ArcPointInterface,
|
|
91
|
+
startSide: ArcSideType,
|
|
92
|
+
stop: ArcPointInterface,
|
|
93
|
+
stopSide: ArcSideType,
|
|
94
|
+
curvature = 1
|
|
95
|
+
): ArcPathResultType => {
|
|
96
|
+
const length = distance(start, stop);
|
|
97
|
+
const bulge = (length / curveMultiplierFromDistance(length)) * curvature;
|
|
98
|
+
const c1 = add(start, NORMALS[startSide], bulge);
|
|
99
|
+
const c2 = add(stop, NORMALS[stopSide], bulge);
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
d: `M ${fmt(start)} C ${fmt(c1)}, ${fmt(c2)}, ${fmt(stop)}`,
|
|
103
|
+
// Point on the curve at t = 0.5
|
|
104
|
+
labelPoint: {
|
|
105
|
+
x: (start.x + 3 * c1.x + 3 * c2.x + stop.x) / 8,
|
|
106
|
+
y: (start.y + 3 * c1.y + 3 * c2.y + stop.y) / 8
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Removes repeated points and points in the middle of a straight segment.
|
|
113
|
+
*/
|
|
114
|
+
export const simplifyPolyline = (
|
|
115
|
+
points: ArcPointInterface[]
|
|
116
|
+
): ArcPointInterface[] => {
|
|
117
|
+
const unique = points.filter(
|
|
118
|
+
(point, i) => i === 0 || distance(point, points[i - 1]) > 0.01
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
return unique.filter((point, i) => {
|
|
122
|
+
if (i === 0 || i === unique.length - 1) return true;
|
|
123
|
+
|
|
124
|
+
const prev = unique[i - 1];
|
|
125
|
+
const next = unique[i + 1];
|
|
126
|
+
const cross =
|
|
127
|
+
(point.x - prev.x) * (next.y - prev.y) -
|
|
128
|
+
(point.y - prev.y) * (next.x - prev.x);
|
|
129
|
+
|
|
130
|
+
return Math.abs(cross) > 0.01;
|
|
131
|
+
});
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Turns a polyline into path data, rounding each corner with a quadratic
|
|
136
|
+
* curve. The radius shrinks on short segments so corners never overlap.
|
|
137
|
+
*/
|
|
138
|
+
export const roundedPolyline = (
|
|
139
|
+
points: ArcPointInterface[],
|
|
140
|
+
radius: number
|
|
141
|
+
): string => {
|
|
142
|
+
if (points.length === 0) return '';
|
|
143
|
+
|
|
144
|
+
const towards = (
|
|
145
|
+
from: ArcPointInterface,
|
|
146
|
+
to: ArcPointInterface,
|
|
147
|
+
length: number
|
|
148
|
+
): ArcPointInterface => {
|
|
149
|
+
const total = distance(from, to);
|
|
150
|
+
|
|
151
|
+
return {
|
|
152
|
+
x: from.x + ((to.x - from.x) / total) * length,
|
|
153
|
+
y: from.y + ((to.y - from.y) / total) * length
|
|
154
|
+
};
|
|
155
|
+
};
|
|
156
|
+
const segments = [`M ${fmt(points[0])}`];
|
|
157
|
+
|
|
158
|
+
for (let i = 1; i < points.length - 1; i++) {
|
|
159
|
+
const prev = points[i - 1];
|
|
160
|
+
const corner = points[i];
|
|
161
|
+
const next = points[i + 1];
|
|
162
|
+
const r = Math.min(
|
|
163
|
+
radius,
|
|
164
|
+
distance(prev, corner) / 2,
|
|
165
|
+
distance(corner, next) / 2
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
if (r <= 0) {
|
|
169
|
+
segments.push(`L ${fmt(corner)}`);
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
segments.push(
|
|
174
|
+
`L ${fmt(towards(corner, prev, r))}`,
|
|
175
|
+
`Q ${fmt(corner)}, ${fmt(towards(corner, next, r))}`
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
if (points.length > 1) segments.push(`L ${fmt(points[points.length - 1])}`);
|
|
180
|
+
|
|
181
|
+
return segments.join(' ');
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* The point halfway along a polyline.
|
|
186
|
+
*/
|
|
187
|
+
export const polylineMidpoint = (
|
|
188
|
+
points: ArcPointInterface[]
|
|
189
|
+
): ArcPointInterface => {
|
|
190
|
+
const lengths = points.slice(1).map((point, i) => distance(points[i], point));
|
|
191
|
+
|
|
192
|
+
let remaining = lengths.reduce((sum, length) => sum + length, 0) / 2;
|
|
193
|
+
|
|
194
|
+
for (let i = 0; i < lengths.length; i++) {
|
|
195
|
+
if (remaining <= lengths[i] && lengths[i] > 0) {
|
|
196
|
+
const t = remaining / lengths[i];
|
|
197
|
+
|
|
198
|
+
return {
|
|
199
|
+
x: points[i].x + (points[i + 1].x - points[i].x) * t,
|
|
200
|
+
y: points[i].y + (points[i + 1].y - points[i].y) * t
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
remaining -= lengths[i];
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return { ...points[0] };
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
// Swapping x and y turns vertical sides into horizontal ones, so only the
|
|
211
|
+
// horizontal case has to be routed by hand.
|
|
212
|
+
const SWAPPED_SIDE: Record<ArcSideType, ArcSideType> = {
|
|
213
|
+
top: 'left',
|
|
214
|
+
bottom: 'right',
|
|
215
|
+
left: 'top',
|
|
216
|
+
right: 'bottom'
|
|
217
|
+
};
|
|
218
|
+
const swap = (point: ArcPointInterface): ArcPointInterface => ({
|
|
219
|
+
x: point.y,
|
|
220
|
+
y: point.x
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Routes a line from `start` to `stop` using only horizontal and vertical
|
|
225
|
+
* segments. The line leaves and arrives perpendicular to the given sides,
|
|
226
|
+
* travelling at least `offset` pixels straight out before turning back.
|
|
227
|
+
*
|
|
228
|
+
* @returns The corner points of the route, including `start` and `stop`.
|
|
229
|
+
*/
|
|
230
|
+
export const routeOrthogonal = (
|
|
231
|
+
start: ArcPointInterface,
|
|
232
|
+
startSide: ArcSideType,
|
|
233
|
+
stop: ArcPointInterface,
|
|
234
|
+
stopSide: ArcSideType,
|
|
235
|
+
offset = 20
|
|
236
|
+
): ArcPointInterface[] => {
|
|
237
|
+
if (!isHorizontalSide(startSide)) {
|
|
238
|
+
return routeOrthogonal(
|
|
239
|
+
swap(start),
|
|
240
|
+
SWAPPED_SIDE[startSide],
|
|
241
|
+
swap(stop),
|
|
242
|
+
SWAPPED_SIDE[stopSide],
|
|
243
|
+
offset
|
|
244
|
+
).map(swap);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
const n1 = NORMALS[startSide];
|
|
248
|
+
const n2 = NORMALS[stopSide];
|
|
249
|
+
const s1 = add(start, n1, offset);
|
|
250
|
+
const s2 = add(stop, n2, offset);
|
|
251
|
+
|
|
252
|
+
if (isHorizontalSide(stopSide)) {
|
|
253
|
+
if (stopSide === OPPOSITE[startSide]) {
|
|
254
|
+
// Facing each other with room in between: turn halfway
|
|
255
|
+
if ((s2.x - s1.x) * n1.x >= 0) {
|
|
256
|
+
const midX = (start.x + stop.x) / 2;
|
|
257
|
+
|
|
258
|
+
return [start, { x: midX, y: start.y }, { x: midX, y: stop.y }, stop];
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// Facing away from each other: step out, cross over halfway, step in
|
|
262
|
+
const midY = (s1.y + s2.y) / 2;
|
|
263
|
+
|
|
264
|
+
return [start, s1, { x: s1.x, y: midY }, { x: s2.x, y: midY }, s2, stop];
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// Same side: go around the outside of both
|
|
268
|
+
const x = n1.x > 0 ? Math.max(s1.x, s2.x) : Math.min(s1.x, s2.x);
|
|
269
|
+
|
|
270
|
+
return [start, { x, y: start.y }, { x, y: stop.y }, stop];
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
// One horizontal and one vertical side: a single corner if it lies in
|
|
274
|
+
// front of both sides, otherwise step out of both and meet
|
|
275
|
+
const corner = { x: stop.x, y: start.y };
|
|
276
|
+
|
|
277
|
+
if ((corner.x - start.x) * n1.x > 0 && (corner.y - stop.y) * n2.y > 0) {
|
|
278
|
+
return [start, corner, stop];
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
return [start, s1, { x: s1.x, y: s2.y }, s2, stop];
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* An orthogonal line with rounded corners.
|
|
286
|
+
*
|
|
287
|
+
* @example
|
|
288
|
+
* ```ts
|
|
289
|
+
* orthogonalPath({ x: 0, y: 0 }, 'right', { x: 200, y: 100 }, 'left', {
|
|
290
|
+
* radius: 8,
|
|
291
|
+
* offset: 20
|
|
292
|
+
* }).d;
|
|
293
|
+
* ```
|
|
294
|
+
*/
|
|
295
|
+
export const orthogonalPath = (
|
|
296
|
+
start: ArcPointInterface,
|
|
297
|
+
startSide: ArcSideType,
|
|
298
|
+
stop: ArcPointInterface,
|
|
299
|
+
stopSide: ArcSideType,
|
|
300
|
+
{ radius = 8, offset = 20 }: { radius?: number; offset?: number } = {}
|
|
301
|
+
): ArcPathResultType => {
|
|
302
|
+
const points = simplifyPolyline(
|
|
303
|
+
routeOrthogonal(start, startSide, stop, stopSide, offset)
|
|
304
|
+
);
|
|
305
|
+
|
|
306
|
+
return {
|
|
307
|
+
d: roundedPolyline(points, radius),
|
|
308
|
+
labelPoint: polylineMidpoint(points)
|
|
309
|
+
};
|
|
310
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { ArcElementTargetType } from '../types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Resolves an element or a CSS selector to an element.
|
|
5
|
+
*
|
|
6
|
+
* @throws {Error} When the selector matches nothing or the target is not an element.
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* const el = resolveElement('#box-1');
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export const resolveElement = (
|
|
13
|
+
target: ArcElementTargetType,
|
|
14
|
+
name = 'element'
|
|
15
|
+
): Element => {
|
|
16
|
+
if (typeof target === 'string') {
|
|
17
|
+
const el = document.querySelector(target);
|
|
18
|
+
|
|
19
|
+
if (!el) throw Error(`arclace: no ${name} matches "${target}"`);
|
|
20
|
+
|
|
21
|
+
return el;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (!(target instanceof Element)) {
|
|
25
|
+
throw Error(`arclace: ${name} must be an Element or a selector string`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return target;
|
|
29
|
+
};
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ArcPointInterface,
|
|
3
|
+
ArcRectInterface,
|
|
4
|
+
ArcSideOptionType,
|
|
5
|
+
ArcSideType
|
|
6
|
+
} from '../types';
|
|
7
|
+
|
|
8
|
+
import { add, center, NORMALS, OPPOSITE } from './geometry';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Picks the side of `from` that faces `to`.
|
|
12
|
+
*
|
|
13
|
+
* Boxes that are further apart horizontally than vertically connect
|
|
14
|
+
* left/right, otherwise top/bottom.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```ts
|
|
18
|
+
* facingSide(
|
|
19
|
+
* { left: 0, top: 0, right: 100, bottom: 50, width: 100, height: 50 },
|
|
20
|
+
* { left: 300, top: 0, right: 400, bottom: 50, width: 100, height: 50 }
|
|
21
|
+
* ); // 'right'
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export const facingSide = (
|
|
25
|
+
from: ArcRectInterface,
|
|
26
|
+
to: ArcRectInterface
|
|
27
|
+
): ArcSideType => {
|
|
28
|
+
const a = center(from);
|
|
29
|
+
const b = center(to);
|
|
30
|
+
const dx = b.x - a.x;
|
|
31
|
+
const dy = b.y - a.y;
|
|
32
|
+
// Positive when the boxes are separated on that axis, negative when they overlap
|
|
33
|
+
const gapX = Math.max(to.left - from.right, from.left - to.right);
|
|
34
|
+
const gapY = Math.max(to.top - from.bottom, from.top - to.bottom);
|
|
35
|
+
const horizontal = gapX === gapY ? Math.abs(dx) >= Math.abs(dy) : gapX > gapY;
|
|
36
|
+
|
|
37
|
+
if (horizontal) return dx >= 0 ? 'right' : 'left';
|
|
38
|
+
|
|
39
|
+
return dy >= 0 ? 'bottom' : 'top';
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Resolves `'auto'` sides for a connection between two boxes.
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* ```ts
|
|
47
|
+
* resolveSides(startRect, stopRect, 'auto', 'auto');
|
|
48
|
+
* // { startSide: 'right', stopSide: 'left' }
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
51
|
+
export const resolveSides = (
|
|
52
|
+
startRect: ArcRectInterface,
|
|
53
|
+
stopRect: ArcRectInterface,
|
|
54
|
+
startSide: ArcSideOptionType = 'auto',
|
|
55
|
+
stopSide: ArcSideOptionType = 'auto'
|
|
56
|
+
): { startSide: ArcSideType; stopSide: ArcSideType } => ({
|
|
57
|
+
startSide: startSide === 'auto' ? facingSide(startRect, stopRect) : startSide,
|
|
58
|
+
stopSide:
|
|
59
|
+
stopSide === 'auto' ? OPPOSITE[facingSide(startRect, stopRect)] : stopSide
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Returns the point on a side of a box where a line attaches.
|
|
64
|
+
*
|
|
65
|
+
* When `count` lines share the side, they are spread around the middle of the
|
|
66
|
+
* side, `spacing` pixels apart, shrinking to fit the side if needed. `gap`
|
|
67
|
+
* moves the point away from the box.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* // The second of three lines on the top side
|
|
72
|
+
* getAnchorPoint(rect, 'top', { index: 1, count: 3 });
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
75
|
+
export const getAnchorPoint = (
|
|
76
|
+
rect: ArcRectInterface,
|
|
77
|
+
side: ArcSideType,
|
|
78
|
+
{
|
|
79
|
+
index = 0,
|
|
80
|
+
count = 1,
|
|
81
|
+
spacing = 16,
|
|
82
|
+
gap = 0
|
|
83
|
+
}: { index?: number; count?: number; spacing?: number; gap?: number } = {}
|
|
84
|
+
): ArcPointInterface => {
|
|
85
|
+
const length = side === 'top' || side === 'bottom' ? rect.width : rect.height;
|
|
86
|
+
const step = count > 1 ? Math.min(spacing, length / count) : 0;
|
|
87
|
+
const shift = (index - (count - 1) / 2) * step;
|
|
88
|
+
const { x: cx, y: cy } = center(rect);
|
|
89
|
+
|
|
90
|
+
let point: ArcPointInterface;
|
|
91
|
+
|
|
92
|
+
switch (side) {
|
|
93
|
+
case 'top':
|
|
94
|
+
point = { x: cx + shift, y: rect.top };
|
|
95
|
+
break;
|
|
96
|
+
case 'bottom':
|
|
97
|
+
point = { x: cx + shift, y: rect.bottom };
|
|
98
|
+
break;
|
|
99
|
+
case 'left':
|
|
100
|
+
point = { x: rect.left, y: cy + shift };
|
|
101
|
+
break;
|
|
102
|
+
case 'right':
|
|
103
|
+
point = { x: rect.right, y: cy + shift };
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
return add(point, NORMALS[side], gap);
|
|
108
|
+
};
|
package/src/utils/svg.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export const SVG_NS = 'http://www.w3.org/2000/svg';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Creates an SVG element with the given attributes.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* const path = createSVGElement('path', { fill: 'none' });
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
export const createSVGElement = <K extends keyof SVGElementTagNameMap>(
|
|
12
|
+
tagName: K,
|
|
13
|
+
attributes: Record<string, string | number> = {}
|
|
14
|
+
): SVGElementTagNameMap[K] => {
|
|
15
|
+
const el = document.createElementNS(SVG_NS, tagName);
|
|
16
|
+
|
|
17
|
+
setAttributes(el, attributes);
|
|
18
|
+
|
|
19
|
+
return el;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Sets attributes on an element. `null` and `undefined` values remove the
|
|
24
|
+
* attribute.
|
|
25
|
+
*/
|
|
26
|
+
export const setAttributes = (
|
|
27
|
+
el: Element,
|
|
28
|
+
attributes: Record<string, string | number | null | undefined>
|
|
29
|
+
): void => {
|
|
30
|
+
for (const [name, value] of Object.entries(attributes)) {
|
|
31
|
+
if (value === null || value === undefined) el.removeAttribute(name);
|
|
32
|
+
else el.setAttribute(name, String(value));
|
|
33
|
+
}
|
|
34
|
+
};
|