@margoengine/labels 0.0.0-stage → 0.1.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/LICENSE +201 -0
- package/NOTICE +4 -0
- package/README.md +507 -3
- package/dist/callouts-route.d.ts +58 -0
- package/dist/callouts-route.js +150 -0
- package/dist/callouts.d.ts +51 -0
- package/dist/callouts.js +74 -0
- package/dist/cluster.d.ts +53 -0
- package/dist/cluster.js +104 -0
- package/dist/declutter.d.ts +61 -0
- package/dist/declutter.js +74 -0
- package/dist/distance.d.ts +56 -0
- package/dist/distance.js +91 -0
- package/dist/errors-52P7hKm0.js +9 -0
- package/dist/gpu-gl.d.ts +19 -0
- package/dist/gpu-shader.d.ts +3 -0
- package/dist/gpu.d.ts +97 -0
- package/dist/gpu.js +446 -0
- package/dist/hotspot.d.ts +54 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +566 -0
- package/dist/projection.d.ts +103 -0
- package/dist/renderer.d.ts +274 -0
- package/package.json +74 -6
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
import { n as e, t } from "./errors-52P7hKm0.js";
|
|
2
|
+
//#region src/callouts-route.ts
|
|
3
|
+
function n() {
|
|
4
|
+
let e = /* @__PURE__ */ new DataView(/* @__PURE__ */ new ArrayBuffer(8)), t = /* @__PURE__ */ new Map(), n = (n) => {
|
|
5
|
+
let r = t.get(n);
|
|
6
|
+
if (r !== void 0) return r;
|
|
7
|
+
e.setFloat64(0, n);
|
|
8
|
+
let i = e.getBigUint64(0), a = Number(i >> 52n & 2047n), o = i & (1n << 52n) - 1n | (a ? 1n << 52n : 0n);
|
|
9
|
+
return r = (i >> 63n ? -o : o) << BigInt(Math.max(0, a - 1)), t.set(n, r), r;
|
|
10
|
+
};
|
|
11
|
+
return (e, t, r) => {
|
|
12
|
+
let i = (t.x - e.x) * (r.y - e.y), a = (t.y - e.y) * (r.x - e.x), o = i - a, s = Math.abs(i) + Math.abs(a);
|
|
13
|
+
if (Number.isFinite(o) && s > 1e-300 && Math.abs(o) > 16 * 2 ** -52 * s) return Math.sign(o);
|
|
14
|
+
let c = n(e.x), l = n(e.y), u = (n(t.x) - c) * (n(r.y) - l) - (n(t.y) - l) * (n(r.x) - c);
|
|
15
|
+
return u < 0n ? -1 : +(u > 0n);
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function r(r, i, a) {
|
|
19
|
+
if (r.length !== i.length || !Number.isFinite(a) || [...r, ...i].some((e) => e.length !== 2 || !e.every(Number.isFinite))) throw e("provide equal counts of finite 2D sources/ports and a finite bendX", "labels");
|
|
20
|
+
if (!r.length) return [];
|
|
21
|
+
let o = i[0][0], s = o > a ? 1 : -1;
|
|
22
|
+
if (o === a || i.some((e) => e[0] !== o) || new Set(i.map((e) => e[1])).size !== i.length || r.some((e) => s * (a - e[0]) <= 0)) throw e("use distinct port heights in one column and place bendX strictly between all sources and ports", "labels");
|
|
23
|
+
let c = [[...r.map((e, t) => ({
|
|
24
|
+
id: t,
|
|
25
|
+
blue: !1,
|
|
26
|
+
x: e[0] * s,
|
|
27
|
+
y: e[1]
|
|
28
|
+
})), ...i.map((e, t) => ({
|
|
29
|
+
id: t,
|
|
30
|
+
blue: !0,
|
|
31
|
+
x: a * s,
|
|
32
|
+
y: e[1]
|
|
33
|
+
}))]], l = Array(r.length), u = n();
|
|
34
|
+
for (; c.length;) {
|
|
35
|
+
let e = c.pop();
|
|
36
|
+
if (!e.length) continue;
|
|
37
|
+
let n;
|
|
38
|
+
for (let t of e) !t.blue && (!n || t.x < n.x || t.x === n.x && (t.y < n.y || t.y === n.y && t.id < n.id)) && (n = t);
|
|
39
|
+
if (!n) throw t("state", "unbalanced callout partition; check source/port counts", "labels");
|
|
40
|
+
let o = n, s = e.filter((e) => e !== o);
|
|
41
|
+
s.sort((e, t) => {
|
|
42
|
+
let n = e.x === o.x && e.y === o.y;
|
|
43
|
+
if (n !== (t.x === o.x && t.y === o.y)) return n ? -1 : 1;
|
|
44
|
+
let r = u(o, e, t);
|
|
45
|
+
return r ? -r : e.x - t.x || e.y - t.y || Number(e.blue) - Number(t.blue) || e.id - t.id;
|
|
46
|
+
});
|
|
47
|
+
let d = 1, f = -1;
|
|
48
|
+
for (let e = 0; e < s.length; e++) if (d += s[e].blue ? -1 : 1, d === 0) {
|
|
49
|
+
f = e;
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
if (f < 0 || !s[f].blue) throw t("state", "unbalanced callout partition; check source/port counts", "labels");
|
|
53
|
+
let p = s[f];
|
|
54
|
+
l[o.id] = {
|
|
55
|
+
source: o.id,
|
|
56
|
+
port: p.id,
|
|
57
|
+
points: [
|
|
58
|
+
[...r[o.id]],
|
|
59
|
+
[a, i[p.id][1]],
|
|
60
|
+
[...i[p.id]]
|
|
61
|
+
]
|
|
62
|
+
}, c.push(s.slice(0, f), s.slice(f + 1));
|
|
63
|
+
}
|
|
64
|
+
return l;
|
|
65
|
+
}
|
|
66
|
+
var i = class {
|
|
67
|
+
constructor(t = {}) {
|
|
68
|
+
this.records = /* @__PURE__ */ new Map(), this.originalOverflow = "", this.svg = null, this.routes = [], this.writes = 0, this.options = {
|
|
69
|
+
side: t.side ?? "auto",
|
|
70
|
+
gap: t.gap ?? 16,
|
|
71
|
+
padding: t.padding ?? 4,
|
|
72
|
+
margin: t.margin ?? 8,
|
|
73
|
+
color: t.color ?? "currentColor",
|
|
74
|
+
width: t.width ?? 1,
|
|
75
|
+
overflow: t.overflow ?? !0
|
|
76
|
+
};
|
|
77
|
+
let n = this.options;
|
|
78
|
+
if (![
|
|
79
|
+
"left",
|
|
80
|
+
"right",
|
|
81
|
+
"auto"
|
|
82
|
+
].includes(n.side) || ![
|
|
83
|
+
n.gap,
|
|
84
|
+
n.padding,
|
|
85
|
+
n.margin,
|
|
86
|
+
n.width
|
|
87
|
+
].every(Number.isFinite) || n.gap <= 0 || n.width <= 0 || n.padding < 0 || n.margin < 0) throw e("use a valid side, positive finite gap/width and nonnegative finite padding/margin", "labels");
|
|
88
|
+
}
|
|
89
|
+
arrange(t, n) {
|
|
90
|
+
if (this.writes = 0, !t.length) {
|
|
91
|
+
this.routes = [];
|
|
92
|
+
for (let e of this.records.values()) e.element.remove();
|
|
93
|
+
this.records.clear();
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
let i = t.map((e) => {
|
|
97
|
+
let [t, r] = n.size(e);
|
|
98
|
+
return [t * e.scale, r * e.scale];
|
|
99
|
+
});
|
|
100
|
+
if (i.some((e) => !e.every((e) => Number.isFinite(e) && e > 0))) throw e("routed callouts require nonzero finite measured label boxes", "labels");
|
|
101
|
+
let a = Math.max(...i.map((e) => e[0])), o = Math.max(...i.map((e) => e[1])), s = Math.min(...t.map((e) => e.screen.x)), c = Math.max(...t.map((e) => e.screen.x)), { gap: l, margin: u, padding: d, overflow: f } = this.options, p = (this.options.side === "auto" ? s >= n.width - c ? "left" : "right" : this.options.side) === "right", m = p ? Math.max(n.width - u - a, c + 2 * l) : Math.min(u + a, s - 2 * l), h = m + (p ? -l : l), g = t.length * o + (t.length - 1) * d, _ = t.reduce((e, n) => e + n.screen.y / t.length, 0), v = Math.max(u, Math.min(n.height - u - g, _ - g / 2));
|
|
102
|
+
if (!f && (m - (p ? 0 : a) < 0 || m + (p ? a : 0) > n.width || v + g > n.height - u)) throw e("callout column does not fit the viewport; enable overflow or reduce label count/size", "labels");
|
|
103
|
+
let y = t.map((e, t) => [m, v + t * (o + d) + o / 2]);
|
|
104
|
+
this.routes = r(t.map((e) => [e.screen.x, e.screen.y]), y, h), this.mount(n.root);
|
|
105
|
+
let b = /* @__PURE__ */ new Set();
|
|
106
|
+
for (let e of this.routes) {
|
|
107
|
+
let r = t[e.source], [a, o] = i[e.source], s = e.points[2];
|
|
108
|
+
b.add(r);
|
|
109
|
+
let c = p ? m : m - a, l = s[1] - o / 2;
|
|
110
|
+
n.place(r, [(r.screen.x + r.screenOffset[0] - c) / a, (r.screen.y + r.screenOffset[1] - l) / o]);
|
|
111
|
+
let u = this.records.get(r);
|
|
112
|
+
if (!u) {
|
|
113
|
+
let e = n.root.ownerDocument.createElementNS("http://www.w3.org/2000/svg", "polyline");
|
|
114
|
+
e.setAttribute("fill", "none"), e.setAttribute("stroke", this.options.color), e.setAttribute("stroke-width", String(this.options.width)), e.setAttribute("stroke-linejoin", "round"), this.svg.appendChild(e), this.records.set(r, u = {
|
|
115
|
+
element: e,
|
|
116
|
+
points: ""
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
let d = e.points.map((e) => e.join(",")).join(" ");
|
|
120
|
+
u.points !== d && (u.element.setAttribute("points", d), u.points = d, this.writes++);
|
|
121
|
+
}
|
|
122
|
+
for (let [e, t] of this.records) b.has(e) || (t.element.remove(), this.records.delete(e));
|
|
123
|
+
}
|
|
124
|
+
mount(e) {
|
|
125
|
+
this.root !== e && (this.restoreOverflow(), this.root = e, this.originalOverflow = e.style.overflow ?? ""), this.options.overflow && e.style.overflow !== "visible" && (e.style.overflow = "visible"), this.svg || (this.svg = e.ownerDocument.createElementNS("http://www.w3.org/2000/svg", "svg"), this.svg.setAttribute("aria-hidden", "true"), Object.assign(this.svg.style, {
|
|
126
|
+
position: "absolute",
|
|
127
|
+
left: "0",
|
|
128
|
+
top: "0",
|
|
129
|
+
width: "100%",
|
|
130
|
+
height: "100%",
|
|
131
|
+
overflow: "visible",
|
|
132
|
+
pointerEvents: "none",
|
|
133
|
+
zIndex: "0"
|
|
134
|
+
})), this.svg.parentElement !== e && e.insertBefore(this.svg, e.firstChild);
|
|
135
|
+
}
|
|
136
|
+
restoreOverflow() {
|
|
137
|
+
this.root && this.options.overflow && this.root.style.overflow === "visible" && (this.root.style.overflow = this.originalOverflow);
|
|
138
|
+
}
|
|
139
|
+
clear() {
|
|
140
|
+
this.svg?.remove(), this.svg = null, this.records.clear(), this.routes = [], this.restoreOverflow(), this.root = void 0;
|
|
141
|
+
}
|
|
142
|
+
dispose() {
|
|
143
|
+
this.clear();
|
|
144
|
+
}
|
|
145
|
+
[Symbol.dispose]() {
|
|
146
|
+
this.clear();
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
//#endregion
|
|
150
|
+
export { i as RoutedCallouts, r as routeCalloutLeaders };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { Label, LabelLayout, LayoutContext } from './renderer.js';
|
|
2
|
+
/** Options of Callouts: the layout run first and how the leader lines look. */
|
|
3
|
+
export interface CalloutsOptions {
|
|
4
|
+
/** Layout run first, for example `new Declutter()`; its hide and place decisions are kept. Default none. */
|
|
5
|
+
layout?: LabelLayout | null;
|
|
6
|
+
/** Line and dot colour, any CSS colour. Default `'currentColor'`, the overlay's text colour. */
|
|
7
|
+
color?: string;
|
|
8
|
+
/** Line width in CSS pixels. Default 1. */
|
|
9
|
+
width?: number;
|
|
10
|
+
/** Radius of the dot drawn on the point, in CSS pixels; 0 draws none. Default 2. */
|
|
11
|
+
dot?: number;
|
|
12
|
+
/** Lines shorter than this, in CSS pixels, are not drawn (the label is next to its point). Default 2. */
|
|
13
|
+
minLength?: number;
|
|
14
|
+
/** Optional class name of the SVG element that holds the lines, for styling them with CSS. */
|
|
15
|
+
className?: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* A label layout that draws leader lines: for every shown label whose box does not cover its projected point (one
|
|
19
|
+
* moved off it with `screenOffset`, or placed away by another layout), a line from the point to the nearest point of
|
|
20
|
+
* the label's box, with a dot on the point. Lines sit in one SVG under the labels. Pass it as the renderer's
|
|
21
|
+
* `layout`; wrap another layout with the `layout` option.
|
|
22
|
+
*/
|
|
23
|
+
export declare class Callouts implements LabelLayout {
|
|
24
|
+
/** Layout run first; its hide and place decisions are kept. */
|
|
25
|
+
layout: LabelLayout | null;
|
|
26
|
+
readonly color: string;
|
|
27
|
+
readonly width: number;
|
|
28
|
+
readonly dot: number;
|
|
29
|
+
readonly minLength: number;
|
|
30
|
+
/** The SVG element holding the lines, made at the first update; null after `clear()`. */
|
|
31
|
+
svg: Element | null;
|
|
32
|
+
/** Lines drawn at the last update. */
|
|
33
|
+
lines: number;
|
|
34
|
+
/** SVG attribute writes made during the last update (0 on a still frame). */
|
|
35
|
+
writes: number;
|
|
36
|
+
private readonly className;
|
|
37
|
+
private readonly records;
|
|
38
|
+
private readonly hidden;
|
|
39
|
+
private readonly anchors;
|
|
40
|
+
private frame;
|
|
41
|
+
private outer;
|
|
42
|
+
/** The context the wrapped layout gets: the renderer's, with each hide and place recorded on the way through. */
|
|
43
|
+
private readonly recorder;
|
|
44
|
+
constructor(options?: CalloutsOptions);
|
|
45
|
+
arrange(labels: readonly Label[], context: LayoutContext): void;
|
|
46
|
+
/** Removes the SVG and every line; the next update starts again. */
|
|
47
|
+
clear(): void;
|
|
48
|
+
private line;
|
|
49
|
+
private mount;
|
|
50
|
+
private set;
|
|
51
|
+
}
|
package/dist/callouts.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { n as e } from "./errors-52P7hKm0.js";
|
|
2
|
+
//#region src/callouts.ts
|
|
3
|
+
var t = "http://www.w3.org/2000/svg", n = (t, n) => {
|
|
4
|
+
if (typeof t != "number" || !Number.isFinite(t) || t < 0) throw e(n, "labels");
|
|
5
|
+
return t;
|
|
6
|
+
}, r = (e) => String(Math.round(e * 100) / 100 || 0), i = class {
|
|
7
|
+
constructor(e = {}) {
|
|
8
|
+
this.svg = null, this.lines = 0, this.writes = 0, this.records = /* @__PURE__ */ new Map(), this.hidden = /* @__PURE__ */ new Set(), this.anchors = /* @__PURE__ */ new Map(), this.frame = 0, this.outer = null, this.layout = e.layout ?? null, this.color = e.color ?? "currentColor", this.width = n(e.width ?? 1, "width must be a finite number of CSS pixels, 0 or more"), this.dot = n(e.dot ?? 2, "dot must be a finite number of CSS pixels, 0 or more"), this.minLength = n(e.minLength ?? 2, "minLength must be a finite number of CSS pixels, 0 or more"), this.className = e.className ?? "";
|
|
9
|
+
let t = this;
|
|
10
|
+
this.recorder = {
|
|
11
|
+
get width() {
|
|
12
|
+
return t.outer.width;
|
|
13
|
+
},
|
|
14
|
+
get height() {
|
|
15
|
+
return t.outer.height;
|
|
16
|
+
},
|
|
17
|
+
get root() {
|
|
18
|
+
return t.outer.root;
|
|
19
|
+
},
|
|
20
|
+
size: (e) => this.outer.size(e),
|
|
21
|
+
hide: (e) => {
|
|
22
|
+
this.outer.hide(e), this.hidden.add(e);
|
|
23
|
+
},
|
|
24
|
+
place: (e, t) => {
|
|
25
|
+
this.outer.place(e, t), this.anchors.set(e, [t[0], t[1]]);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
arrange(e, t) {
|
|
30
|
+
this.outer = t, this.hidden.clear(), this.anchors.clear(), this.layout?.arrange(e, this.recorder), this.writes = 0, this.lines = 0;
|
|
31
|
+
let n = ++this.frame;
|
|
32
|
+
for (let i of e) {
|
|
33
|
+
if (this.hidden.has(i)) continue;
|
|
34
|
+
let e = this.anchors.get(i) ?? i.anchor, [a, o] = i.screenOffset;
|
|
35
|
+
if (a === 0 && o === 0 && e[0] >= 0 && e[0] <= 1 && e[1] >= 0 && e[1] <= 1) continue;
|
|
36
|
+
let [s, c] = t.size(i), l = s * i.scale, u = c * i.scale, d = i.screen.x, f = i.screen.y, p = d + a - e[0] * l, m = f + o - e[1] * u, h = Math.min(Math.max(d, p), p + l), g = Math.min(Math.max(f, m), m + u);
|
|
37
|
+
if (Math.hypot(h - d, g - f) < this.minLength) continue;
|
|
38
|
+
let _ = this.line(i, t.root);
|
|
39
|
+
_.frame = n, this.set(_.line, _.cache, "x1", r(d)), this.set(_.line, _.cache, "y1", r(f)), this.set(_.line, _.cache, "x2", r(h)), this.set(_.line, _.cache, "y2", r(g)), this.set(_.line, _.cache, "display", "inline"), _.dot && (this.set(_.dot, _.dotCache, "cx", r(d)), this.set(_.dot, _.dotCache, "cy", r(f)), this.set(_.dot, _.dotCache, "display", "inline")), this.lines++;
|
|
40
|
+
}
|
|
41
|
+
for (let [e, t] of this.records) t.frame !== n && (e.owner === null ? (t.line.remove(), t.dot?.remove(), this.records.delete(e)) : (this.set(t.line, t.cache, "display", "none"), t.dot && this.set(t.dot, t.dotCache, "display", "none")));
|
|
42
|
+
}
|
|
43
|
+
clear() {
|
|
44
|
+
this.svg?.remove(), this.svg = null, this.records.clear();
|
|
45
|
+
}
|
|
46
|
+
line(e, n) {
|
|
47
|
+
let r = this.mount(n), i = this.records.get(e);
|
|
48
|
+
if (!i) {
|
|
49
|
+
let n = r.ownerDocument, a = n.createElementNS(t, "line"), o = this.dot > 0 ? n.createElementNS(t, "circle") : null;
|
|
50
|
+
a.setAttribute("stroke", this.color), a.setAttribute("stroke-width", String(this.width)), a.setAttribute("stroke-linecap", "round"), r.appendChild(a), o && (o.setAttribute("r", String(this.dot)), o.setAttribute("fill", this.color), r.appendChild(o)), this.records.set(e, i = {
|
|
51
|
+
line: a,
|
|
52
|
+
dot: o,
|
|
53
|
+
cache: {},
|
|
54
|
+
dotCache: {},
|
|
55
|
+
frame: 0
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return i;
|
|
59
|
+
}
|
|
60
|
+
mount(e) {
|
|
61
|
+
let n = this.svg;
|
|
62
|
+
if (!n) {
|
|
63
|
+
n = e.ownerDocument.createElementNS(t, "svg"), this.className && n.setAttribute("class", this.className), n.setAttribute("aria-hidden", "true");
|
|
64
|
+
let r = n.style;
|
|
65
|
+
r.position = "absolute", r.left = "0px", r.top = "0px", r.width = "100%", r.height = "100%", r.overflow = "visible", r.pointerEvents = "none", r.zIndex = "0", this.svg = n;
|
|
66
|
+
}
|
|
67
|
+
return n.parentElement !== e && e.insertBefore(n, e.firstChild), n;
|
|
68
|
+
}
|
|
69
|
+
set(e, t, n, r) {
|
|
70
|
+
t[n] !== r && (t[n] = r, e.setAttribute(n, r), this.writes++);
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
//#endregion
|
|
74
|
+
export { i as Callouts };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { Label, LabelLayout, LayoutContext } from './renderer.js';
|
|
3
|
+
/** A visible cluster, including its original label handles for picking and drill-down. */
|
|
4
|
+
export interface LabelClusterInfo {
|
|
5
|
+
readonly members: readonly Label[];
|
|
6
|
+
readonly count: number;
|
|
7
|
+
readonly x: number;
|
|
8
|
+
readonly y: number;
|
|
9
|
+
readonly zoom: number;
|
|
10
|
+
readonly element: HTMLElement;
|
|
11
|
+
}
|
|
12
|
+
/** Pixel clustering configuration. Recomputed from projected anchors on every layout update. */
|
|
13
|
+
export interface ClusterLabelsOptions {
|
|
14
|
+
/** Maximum neighbor distance in CSS pixels; connected neighbors form a cluster. Default 40. */
|
|
15
|
+
radius?: number;
|
|
16
|
+
/** Minimum group count shown as a bubble. Default 2. */
|
|
17
|
+
minCount?: number;
|
|
18
|
+
/** Logical zoom level supplied by the application. Default 0. Projection changes always recluster. */
|
|
19
|
+
zoom?: number;
|
|
20
|
+
/** Disable clusters above this logical zoom. Default Infinity. */
|
|
21
|
+
maxZoom?: number;
|
|
22
|
+
/** CSS class on count bubbles. Default 'margo-label-cluster'. */
|
|
23
|
+
className?: string;
|
|
24
|
+
/** Optional layout for the unclustered labels. */
|
|
25
|
+
layout?: LabelLayout;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Optional screen-space label clustering. A radius graph is grouped into connected components,
|
|
29
|
+
* with a count bubble at each component's centroid. Camera zoom/motion recomputes the groups.
|
|
30
|
+
*/
|
|
31
|
+
export declare class ClusterLabels implements LabelLayout {
|
|
32
|
+
readonly radius: number;
|
|
33
|
+
readonly minCount: number;
|
|
34
|
+
readonly maxZoom: number;
|
|
35
|
+
readonly layout?: LabelLayout;
|
|
36
|
+
private level;
|
|
37
|
+
private readonly className;
|
|
38
|
+
private readonly bubbles;
|
|
39
|
+
private result;
|
|
40
|
+
/** Exact distance tests in the latest update; no elapsed-time measurement. */
|
|
41
|
+
comparisons: number;
|
|
42
|
+
constructor(options?: ClusterLabelsOptions);
|
|
43
|
+
/** Count bubbles from the latest update. Members preserve input order. */
|
|
44
|
+
get clusters(): readonly LabelClusterInfo[];
|
|
45
|
+
/** Updates the optional logical zoom limit; screen projection still controls spatial grouping. */
|
|
46
|
+
setZoom(zoom: number): this;
|
|
47
|
+
arrange(labels: readonly Label[], context: LayoutContext): void;
|
|
48
|
+
private removeUnused;
|
|
49
|
+
/** Removes bubbles immediately. The next arrange may build them again. */
|
|
50
|
+
clear(): void;
|
|
51
|
+
dispose(): void;
|
|
52
|
+
[Symbol.dispose](): void;
|
|
53
|
+
}
|
package/dist/cluster.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { n as e } from "./errors-52P7hKm0.js";
|
|
2
|
+
//#region src/cluster.ts
|
|
3
|
+
var t = class {
|
|
4
|
+
constructor(t = {}) {
|
|
5
|
+
if (this.bubbles = /* @__PURE__ */ new Map(), this.result = [], this.comparisons = 0, this.radius = t.radius ?? 40, this.minCount = t.minCount ?? 2, this.maxZoom = t.maxZoom ?? Infinity, this.level = t.zoom ?? 0, this.className = t.className ?? "margo-label-cluster", this.layout = t.layout, !Number.isFinite(this.radius) || this.radius < 1 || !Number.isInteger(this.minCount) || this.minCount < 2 || !Number.isFinite(this.level) || !(this.maxZoom >= 0)) throw e("use radius >= 1, integer minCount >= 2, finite zoom and maxZoom >= 0", "labels");
|
|
6
|
+
}
|
|
7
|
+
get clusters() {
|
|
8
|
+
return this.result;
|
|
9
|
+
}
|
|
10
|
+
setZoom(t) {
|
|
11
|
+
if (!Number.isFinite(t)) throw e("zoom must be finite", "labels");
|
|
12
|
+
return this.level = t, this;
|
|
13
|
+
}
|
|
14
|
+
arrange(t, n) {
|
|
15
|
+
if (this.comparisons = 0, this.result = [], this.level > this.maxZoom) {
|
|
16
|
+
this.removeUnused(/* @__PURE__ */ new Set()), this.layout?.arrange(t, n);
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
let r = t.length, i = Array.from({ length: r }, (e, t) => t), a = new Uint8Array(r), o = (e) => {
|
|
20
|
+
for (; i[e] !== e;) i[e] = i[i[e]], e = i[e];
|
|
21
|
+
return e;
|
|
22
|
+
}, s = (e, t) => {
|
|
23
|
+
e = o(e), t = o(t), e !== t && (a[e] < a[t] ? i[e] = t : (i[t] = e, a[e] === a[t] && a[e]++));
|
|
24
|
+
}, c = /* @__PURE__ */ new Map(), l = this.radius / Math.SQRT2;
|
|
25
|
+
for (let n = 0; n < r; n++) {
|
|
26
|
+
let { x: r, y: i } = t[n].screen;
|
|
27
|
+
if (!Number.isFinite(r) || !Number.isFinite(i)) throw e("cluster anchors must have finite projected coordinates", "labels");
|
|
28
|
+
let a = Math.floor(r / l), u = Math.floor(i / l), d = `${a},${u}`, f = c.get(d);
|
|
29
|
+
f && s(n, f.first);
|
|
30
|
+
for (let e = -2; e <= 2; e++) for (let l = -2; l <= 2; l++) {
|
|
31
|
+
if (l === 0 && e === 0) continue;
|
|
32
|
+
let d = c.get(`${a + l},${u + e}`);
|
|
33
|
+
if (!d || o(n) === o(d.first)) continue;
|
|
34
|
+
let f = Math.max(d.minX, Math.min(r, d.maxX)), p = Math.max(d.minY, Math.min(i, d.maxY));
|
|
35
|
+
if (!(Math.hypot(f - r, p - i) > this.radius)) {
|
|
36
|
+
for (let e of d.indices) if (this.comparisons++, Math.hypot(t[e].screen.x - r, t[e].screen.y - i) <= this.radius) {
|
|
37
|
+
s(n, d.first);
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
f ? (f.indices.push(n), f.minX = Math.min(f.minX, r), f.maxX = Math.max(f.maxX, r), f.minY = Math.min(f.minY, i), f.maxY = Math.max(f.maxY, i)) : c.set(d, {
|
|
43
|
+
indices: [n],
|
|
44
|
+
first: n,
|
|
45
|
+
minX: r,
|
|
46
|
+
maxX: r,
|
|
47
|
+
minY: i,
|
|
48
|
+
maxY: i
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
let u = /* @__PURE__ */ new Map();
|
|
52
|
+
for (let e = 0; e < r; e++) {
|
|
53
|
+
let n = o(e), r = u.get(n);
|
|
54
|
+
r || u.set(n, r = []), r.push(t[e]);
|
|
55
|
+
}
|
|
56
|
+
let d = [], f = /* @__PURE__ */ new Set();
|
|
57
|
+
for (let e of u.values()) {
|
|
58
|
+
if (e.length < this.minCount) {
|
|
59
|
+
for (let t of e) d.push(t);
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
let t = 0, r = 0;
|
|
63
|
+
for (let i of e) n.hide(i), t += i.screen.x / e.length, r += i.screen.y / e.length;
|
|
64
|
+
let i = e[0];
|
|
65
|
+
f.add(i);
|
|
66
|
+
let a = this.bubbles.get(i);
|
|
67
|
+
a || (a = n.root.ownerDocument.createElement("div"), a.className = this.className, a.setAttribute("data-margoengine-cluster", ""), Object.assign(a.style, {
|
|
68
|
+
position: "absolute",
|
|
69
|
+
transform: "translate(-50%, -50%)",
|
|
70
|
+
borderRadius: "999px",
|
|
71
|
+
background: "#2457d6",
|
|
72
|
+
color: "#fff",
|
|
73
|
+
padding: "6px 9px",
|
|
74
|
+
font: "600 12px system-ui",
|
|
75
|
+
pointerEvents: "auto",
|
|
76
|
+
zIndex: "1"
|
|
77
|
+
}), this.bubbles.set(i, a));
|
|
78
|
+
let o = String(e.length);
|
|
79
|
+
a.textContent !== o && (a.textContent = o), a.style.left !== `${t}px` && (a.style.left = `${t}px`), a.style.top !== `${r}px` && (a.style.top = `${r}px`), a.parentElement !== n.root && n.root.appendChild(a), this.result.push({
|
|
80
|
+
members: e,
|
|
81
|
+
count: e.length,
|
|
82
|
+
x: t,
|
|
83
|
+
y: r,
|
|
84
|
+
zoom: this.level,
|
|
85
|
+
element: a
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
this.removeUnused(f), this.layout?.arrange(d, n);
|
|
89
|
+
}
|
|
90
|
+
removeUnused(e) {
|
|
91
|
+
for (let [t, n] of this.bubbles) e.has(t) || (n.remove(), this.bubbles.delete(t));
|
|
92
|
+
}
|
|
93
|
+
clear() {
|
|
94
|
+
this.removeUnused(/* @__PURE__ */ new Set()), this.result = [];
|
|
95
|
+
}
|
|
96
|
+
dispose() {
|
|
97
|
+
this.clear();
|
|
98
|
+
}
|
|
99
|
+
[Symbol.dispose]() {
|
|
100
|
+
this.clear();
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
//#endregion
|
|
104
|
+
export { t as ClusterLabels };
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { Label, LabelLayout, LayoutContext } from './renderer.js';
|
|
2
|
+
/** An anchor a label can be placed with: which point of the element sits on its projected point, as fractions. */
|
|
3
|
+
export type Anchor = readonly [number, number];
|
|
4
|
+
/**
|
|
5
|
+
* The eight positions around a point, most preferred first, in the standard ranking of Christensen, Marks and Shieber
|
|
6
|
+
* (ACM TOG 1995, Figure 1): above right, above left, below left, below right, right, above, left, below (each an
|
|
7
|
+
* anchor of the element, so `[0, 1]` puts the element's lower left corner on the point).
|
|
8
|
+
*/
|
|
9
|
+
export declare const eightPositions: readonly Anchor[];
|
|
10
|
+
/** The four corner positions of {@link eightPositions}. */
|
|
11
|
+
export declare const fourPositions: readonly Anchor[];
|
|
12
|
+
/** Options of Declutter: the gap kept between labels, the positions tried, and how ties are kept steady. */
|
|
13
|
+
export interface DeclutterOptions {
|
|
14
|
+
/** Gap kept free between two labels, in CSS pixels. Default 2. */
|
|
15
|
+
padding?: number;
|
|
16
|
+
/**
|
|
17
|
+
* Anchors tried, in order, after a label's own anchor overlaps another label. Default none: an overlapping label is
|
|
18
|
+
* hidden. `eightPositions` or `fourPositions` move labels around their point first.
|
|
19
|
+
*/
|
|
20
|
+
positions?: readonly Anchor[];
|
|
21
|
+
/** Keep labels whole inside the view rectangle: a position that sticks out counts as an overlap. Default false. */
|
|
22
|
+
inside?: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Among labels of equal priority, the ones shown at the last update are kept first, so labels do not swap places
|
|
25
|
+
* as the camera moves. Default true.
|
|
26
|
+
*/
|
|
27
|
+
sticky?: boolean;
|
|
28
|
+
/** Side of the grid cells the overlap test buckets labels into, in CSS pixels. Default 64. */
|
|
29
|
+
cell?: number;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* A label layout that removes overlaps: labels are taken by priority (higher first), then, among equal priorities,
|
|
33
|
+
* those shown at the last update, then nearer ones. Each is placed with its own anchor or the first of `positions`
|
|
34
|
+
* that overlaps no label placed before it, and hidden when none is free. Pass it as the renderer's `layout`.
|
|
35
|
+
*/
|
|
36
|
+
export declare class Declutter implements LabelLayout {
|
|
37
|
+
readonly padding: number;
|
|
38
|
+
readonly positions: readonly Anchor[];
|
|
39
|
+
readonly inside: boolean;
|
|
40
|
+
readonly sticky: boolean;
|
|
41
|
+
readonly cell: number;
|
|
42
|
+
/** Labels hidden at the last update because every position overlapped. */
|
|
43
|
+
hidden: number;
|
|
44
|
+
/** Labels placed at one of `positions` instead of their own anchor at the last update. */
|
|
45
|
+
moved: number;
|
|
46
|
+
/** The update each label was last shown at. */
|
|
47
|
+
private readonly shownAt;
|
|
48
|
+
private frame;
|
|
49
|
+
private readonly order;
|
|
50
|
+
private readonly keys;
|
|
51
|
+
/** Accepted rectangles: left, top, right, bottom per label. */
|
|
52
|
+
private rects;
|
|
53
|
+
private count;
|
|
54
|
+
private readonly grid;
|
|
55
|
+
constructor(options?: DeclutterOptions);
|
|
56
|
+
arrange(labels: readonly Label[], context: LayoutContext): void;
|
|
57
|
+
/** Forgets which labels were shown, so the next update ranks by priority and distance alone. */
|
|
58
|
+
reset(): void;
|
|
59
|
+
private overlaps;
|
|
60
|
+
private accept;
|
|
61
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { n as e } from "./errors-52P7hKm0.js";
|
|
2
|
+
//#region src/declutter.ts
|
|
3
|
+
var t = [
|
|
4
|
+
[0, 1],
|
|
5
|
+
[1, 1],
|
|
6
|
+
[1, 0],
|
|
7
|
+
[0, 0],
|
|
8
|
+
[0, .5],
|
|
9
|
+
[.5, 1],
|
|
10
|
+
[1, .5],
|
|
11
|
+
[.5, 0]
|
|
12
|
+
], n = t.slice(0, 4), r = (t) => {
|
|
13
|
+
if (!t || t.length !== 2 || !Number.isFinite(t[0]) || !Number.isFinite(t[1])) throw e("positions must be [x, y] anchors of two finite numbers", "labels");
|
|
14
|
+
return [t[0], t[1]];
|
|
15
|
+
}, i = class {
|
|
16
|
+
constructor(t = {}) {
|
|
17
|
+
this.hidden = 0, this.moved = 0, this.shownAt = /* @__PURE__ */ new WeakMap(), this.frame = 0, this.order = [], this.keys = [], this.rects = /* @__PURE__ */ new Float64Array(64), this.count = 0, this.grid = /* @__PURE__ */ new Map();
|
|
18
|
+
let n = t.padding ?? 2, i = t.cell ?? 64;
|
|
19
|
+
if (!Number.isFinite(n) || n < 0) throw e("padding must be a finite number of CSS pixels, 0 or more", "labels");
|
|
20
|
+
if (!Number.isFinite(i) || i < 1) throw e("cell must be a finite number of CSS pixels, 1 or more", "labels");
|
|
21
|
+
if (t.positions !== void 0 && !Array.isArray(t.positions)) throw e("positions must be [x, y] anchors of two finite numbers", "labels");
|
|
22
|
+
this.padding = n, this.cell = i, this.positions = (t.positions ?? []).map(r), this.inside = t.inside ?? !1, this.sticky = t.sticky ?? !0;
|
|
23
|
+
}
|
|
24
|
+
arrange(e, t) {
|
|
25
|
+
let n = this.order, r = this.keys, i = this.shownAt, a = this.frame++, o = e.length;
|
|
26
|
+
n.length = o, r.length = o;
|
|
27
|
+
for (let t = 0; t < o; t++) n[t] = t, r[t] = this.sticky && i.get(e[t]) === a ? 1 : 0;
|
|
28
|
+
if (n.sort((t, n) => {
|
|
29
|
+
let i = e[t], a = e[n];
|
|
30
|
+
return a.priority - i.priority || r[n] - r[t] || i.distance - a.distance || t - n;
|
|
31
|
+
}), this.grid.size > 4096) this.grid.clear();
|
|
32
|
+
else for (let e of this.grid.values()) e.length = 0;
|
|
33
|
+
this.count = 0, this.hidden = 0, this.moved = 0;
|
|
34
|
+
let s = this.frame, { width: c, height: l } = t;
|
|
35
|
+
for (let r = 0; r < o; r++) {
|
|
36
|
+
let a = e[n[r]], [o, u] = t.size(a), d = o * a.scale, f = u * a.scale, p = a.screen.x + a.screenOffset[0], m = a.screen.y + a.screenOffset[1], h = !1;
|
|
37
|
+
for (let e = -1; e < this.positions.length && !h; e++) {
|
|
38
|
+
let [n, r] = e < 0 ? a.anchor : this.positions[e], i = p - n * d, o = m - r * f, s = i + d, u = o + f;
|
|
39
|
+
this.inside && (i < 0 || o < 0 || s > c || u > l) || this.overlaps(i, o, s, u) || (this.accept(i, o, s, u), e >= 0 && (t.place(a, this.positions[e]), this.moved++), h = !0);
|
|
40
|
+
}
|
|
41
|
+
h ? i.set(a, s) : (t.hide(a), this.hidden++);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
reset() {
|
|
45
|
+
this.frame++;
|
|
46
|
+
}
|
|
47
|
+
overlaps(e, t, n, r) {
|
|
48
|
+
let i = this.padding, o = this.rects, s = Math.floor((e - i) / this.cell), c = Math.floor((n + i) / this.cell), l = Math.floor((t - i) / this.cell), u = Math.floor((r + i) / this.cell);
|
|
49
|
+
for (let d = l; d <= u; d++) for (let l = s; l <= c; l++) {
|
|
50
|
+
let s = this.grid.get(a(l, d));
|
|
51
|
+
if (s) for (let a of s) {
|
|
52
|
+
let s = a * 4;
|
|
53
|
+
if (e < o[s + 2] + i && o[s] < n + i && t < o[s + 3] + i && o[s + 1] < r + i) return !0;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return !1;
|
|
57
|
+
}
|
|
58
|
+
accept(e, t, n, r) {
|
|
59
|
+
let i = this.count++;
|
|
60
|
+
if (this.rects.length < this.count * 4) {
|
|
61
|
+
let e = new Float64Array(this.rects.length * 2);
|
|
62
|
+
e.set(this.rects), this.rects = e;
|
|
63
|
+
}
|
|
64
|
+
let o = i * 4;
|
|
65
|
+
this.rects[o] = e, this.rects[o + 1] = t, this.rects[o + 2] = n, this.rects[o + 3] = r;
|
|
66
|
+
let s = Math.floor(e / this.cell), c = Math.floor(n / this.cell), l = Math.floor(t / this.cell), u = Math.floor(r / this.cell);
|
|
67
|
+
for (let e = l; e <= u; e++) for (let t = s; t <= c; t++) {
|
|
68
|
+
let n = a(t, e), r = this.grid.get(n);
|
|
69
|
+
r || this.grid.set(n, r = []), r.push(i);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}, a = (e, t) => (e + 32768) * 65536 + (t + 32768);
|
|
73
|
+
//#endregion
|
|
74
|
+
export { i as Declutter, t as eightPositions, n as fourPositions };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" preserve="true" />
|
|
2
|
+
import type { Label, LabelLayout, LayoutContext } from './renderer.js';
|
|
3
|
+
/** Clamped linear interpolation between two distances in world units. */
|
|
4
|
+
export interface LabelDistanceCurve {
|
|
5
|
+
near: number;
|
|
6
|
+
nearValue: number;
|
|
7
|
+
far: number;
|
|
8
|
+
farValue: number;
|
|
9
|
+
}
|
|
10
|
+
/** Distance policy for one label. Omitted curves keep opacity and scale at 1. */
|
|
11
|
+
export interface LabelDistancePolicy {
|
|
12
|
+
/** Inclusive near show limit, default 0. */
|
|
13
|
+
near?: number;
|
|
14
|
+
/** Inclusive far show limit, default Infinity. */
|
|
15
|
+
far?: number;
|
|
16
|
+
/** Endpoint opacity values must be in [0, 1]. */
|
|
17
|
+
opacity?: LabelDistanceCurve;
|
|
18
|
+
/** Nonnegative scale, multiplied by the renderer's distanceScale. */
|
|
19
|
+
scale?: LabelDistanceCurve;
|
|
20
|
+
}
|
|
21
|
+
/** Configuration for a distance layout and an optional subsequent layout. */
|
|
22
|
+
export interface DistanceLabelsOptions {
|
|
23
|
+
/** Policy applied to labels without a per-label policy. Default no limits or fades. */
|
|
24
|
+
defaults?: LabelDistancePolicy;
|
|
25
|
+
/** Runs after distance culling/scaling, so collision and callout geometry uses the final scale. */
|
|
26
|
+
layout?: LabelLayout;
|
|
27
|
+
}
|
|
28
|
+
/** Pure evaluation for DOM or GPU label consumers. Curves clamp at both ends; show limits are inclusive. */
|
|
29
|
+
export declare function evaluateLabelDistance(distance: number, policy?: LabelDistancePolicy): {
|
|
30
|
+
opacity: number;
|
|
31
|
+
scale: number;
|
|
32
|
+
visible: boolean;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Optional distance fade/display layout. Policies never change label.visible or its original opacity.
|
|
36
|
+
* Pass as LabelRenderer's layout; set(label, policy) overrides the default policy for that label.
|
|
37
|
+
*/
|
|
38
|
+
export declare class DistanceLabels implements LabelLayout {
|
|
39
|
+
private readonly policies;
|
|
40
|
+
private readonly styles;
|
|
41
|
+
private readonly defaults;
|
|
42
|
+
readonly layout?: LabelLayout;
|
|
43
|
+
/** Labels excluded by the last distance update. */
|
|
44
|
+
hidden: number;
|
|
45
|
+
constructor(options?: DistanceLabelsOptions);
|
|
46
|
+
/** Validates and copies a per-label policy. */
|
|
47
|
+
set(label: Label, policy: LabelDistancePolicy): this;
|
|
48
|
+
/** Drops a policy, restores inline opacity, and returns whether the label had a policy. */
|
|
49
|
+
delete(label: Label): boolean;
|
|
50
|
+
arrange(labels: readonly Label[], context: LayoutContext): void;
|
|
51
|
+
private restore;
|
|
52
|
+
/** Restores every managed opacity and drops per-label policies. */
|
|
53
|
+
clear(): void;
|
|
54
|
+
dispose(): void;
|
|
55
|
+
[Symbol.dispose](): void;
|
|
56
|
+
}
|