@vitessce/gl 4.0.8 → 4.0.10
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/dist/{deflate-DNngSh3q.js → deflate-DD4d--xe.js} +1 -1
- package/dist/{index-_UASQg2S.js → index-D4C8DA1x.js} +1324 -109
- package/dist/index.js +18 -13
- package/dist/{jpeg-BrGNk507.js → jpeg-CZeZBc_8.js} +1 -1
- package/dist/{lerc-A9bnCzoN.js → lerc-Ct7C32yr.js} +1 -1
- package/dist/{lzw-_IYIPfiq.js → lzw-BgdxJ7Qg.js} +1 -1
- package/dist/{packbits-CxQQOChS.js → packbits-BhOBFsGy.js} +1 -1
- package/dist/{raw-ti2YIR09.js → raw-B5_2TWMK.js} +1 -1
- package/dist/{webimage-DgRcFzf_.js → webimage-DcEapRkY.js} +1 -1
- package/dist-tsc/AnnotationLayer.d.ts +91 -0
- package/dist-tsc/AnnotationLayer.d.ts.map +1 -0
- package/dist-tsc/AnnotationLayer.js +71 -0
- package/dist-tsc/AnnotationLayer.js.map +1 -0
- package/dist-tsc/AnnotationLayer.test.d.ts +2 -0
- package/dist-tsc/AnnotationLayer.test.d.ts.map +1 -0
- package/dist-tsc/AnnotationLayer.test.js +59 -0
- package/dist-tsc/AnnotationLayer.test.js.map +1 -0
- package/dist-tsc/annotation-layer-utils.d.ts +25 -0
- package/dist-tsc/annotation-layer-utils.d.ts.map +1 -0
- package/dist-tsc/annotation-layer-utils.js +1110 -0
- package/dist-tsc/annotation-layer-utils.js.map +1 -0
- package/dist-tsc/annotation-layer-utils.test.d.ts +2 -0
- package/dist-tsc/annotation-layer-utils.test.d.ts.map +1 -0
- package/dist-tsc/annotation-layer-utils.test.js +153 -0
- package/dist-tsc/annotation-layer-utils.test.js.map +1 -0
- package/dist-tsc/deck.js +2 -2
- package/dist-tsc/deck.js.map +1 -1
- package/dist-tsc/index.d.ts +2 -0
- package/dist-tsc/index.js +2 -0
- package/dist-tsc/index.js.map +1 -1
- package/package.json +4 -3
- package/src/AnnotationLayer.js +91 -0
- package/src/AnnotationLayer.test.js +67 -0
- package/src/annotation-layer-utils.js +1137 -0
- package/src/annotation-layer-utils.test.js +180 -0
- package/src/deck.js +2 -1
- package/src/index.js +2 -0
|
@@ -0,0 +1,1110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Attribution: Originally written by @RPSeaman
|
|
3
|
+
* Reference: https://github.com/vitessce/vitessce/pull/2528
|
|
4
|
+
*/
|
|
5
|
+
import { ViewType } from '@vitessce/constants-internal';
|
|
6
|
+
import { PolygonLayer, PathLayer, TextLayer, ScatterplotLayer, COORDINATE_SYSTEM, PathStyleExtension } from './deck.js';
|
|
7
|
+
// Target arrowhead size in screen pixels, converted to data-space at render time.
|
|
8
|
+
const ARROW_SCREEN_PX = 14;
|
|
9
|
+
// Text label styling — matches the annotation controller panel's MUI font stack.
|
|
10
|
+
const LABEL_FONT_FAMILY = '"Roboto", "Helvetica", "Arial", sans-serif';
|
|
11
|
+
const LABEL_FONT_WEIGHT = 'bold';
|
|
12
|
+
const LABEL_FONT_SIZE = 14;
|
|
13
|
+
// Ratio-based thresholds relative to authored zoom: zoomRatio = 2^(currentZoom - authoredZoom).
|
|
14
|
+
// ratio=1 → exactly at authored zoom; ratio=0.5 → one stop zoomed out; etc.
|
|
15
|
+
const RATIO_CLUSTER = 0.10; // below → clustered dots (~3.3 stops out)
|
|
16
|
+
const RATIO_DOT = 0.30; // below → individual dots (~1.74 stops out)
|
|
17
|
+
const RATIO_GEO_FADE = 0.55; // dot↔geometry crossfade zone top (~0.86 stops out)
|
|
18
|
+
const RATIO_TIPS = 0.70; // above → arrow/tick caps visible (fades to 0 at RATIO_GEO_FADE)
|
|
19
|
+
const RATIO_LABEL = 0.55; // labels start appearing (~0.86 stops out)
|
|
20
|
+
const RATIO_LABEL_FULL = 0.75; // labels reach full opacity (~0.42 stops out)
|
|
21
|
+
// Pick a background color that contrasts with the stroke color.
|
|
22
|
+
// Dark stroke (H&E / light images) → light bg; light stroke (fluorescence) → dark bg.
|
|
23
|
+
function textBgColor(strokeColor) {
|
|
24
|
+
const [r, g, b] = strokeColor;
|
|
25
|
+
const lum = (0.299 * r + 0.587 * g + 0.114 * b) / 255;
|
|
26
|
+
return lum > 0.45 ? [20, 20, 20, 210] : [245, 245, 245, 210];
|
|
27
|
+
}
|
|
28
|
+
// Parse an SVG-style dash string ("10 5", "10 20 30 10") into a [dash, gap] pair for
|
|
29
|
+
// deck.gl's PathStyleExtension. Returns null for "none" or invalid input.
|
|
30
|
+
// deck.gl only supports a 2-element array; we use the first two values of longer patterns.
|
|
31
|
+
function parseDashArray(strokeDashArray) {
|
|
32
|
+
if (!strokeDashArray || strokeDashArray === 'none')
|
|
33
|
+
return null;
|
|
34
|
+
const nums = strokeDashArray.trim().split(/\s+/).map(Number).filter(n => !Number.isNaN(n) && n >= 0);
|
|
35
|
+
return nums.length >= 2 ? [nums[0], nums[1]] : null;
|
|
36
|
+
}
|
|
37
|
+
function computeEllipsePolygon(cx, cy, rx, ry, numSegments = 64) {
|
|
38
|
+
const pts = [];
|
|
39
|
+
for (let i = 0; i < numSegments; i++) {
|
|
40
|
+
const angle = (2 * Math.PI * i) / numSegments;
|
|
41
|
+
pts.push([cx + rx * Math.cos(angle), cy + ry * Math.sin(angle)]);
|
|
42
|
+
}
|
|
43
|
+
return pts;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Build a deck.gl layer that previews the in-progress annotation shape while drawing.
|
|
47
|
+
* @param {{ type: string, vertices: [number, number][] }} inProgress
|
|
48
|
+
* @param {[number, number] | null} hoverCoord - current mouse position in data coords
|
|
49
|
+
* @param {number[]} strokeColor - [r, g, b]
|
|
50
|
+
*/
|
|
51
|
+
export function createPreviewLayer(inProgress, hoverCoord, strokeColor = [255, 255, 0]) {
|
|
52
|
+
if (!inProgress || inProgress.vertices.length === 0)
|
|
53
|
+
return null;
|
|
54
|
+
const { type, vertices } = inProgress;
|
|
55
|
+
const base = { coordinateSystem: COORDINATE_SYSTEM.CARTESIAN };
|
|
56
|
+
const previewColor = [...strokeColor, 160];
|
|
57
|
+
const cursor = hoverCoord ?? vertices[vertices.length - 1];
|
|
58
|
+
if (type === 'rectangle' && vertices.length === 1) {
|
|
59
|
+
const [ax, ay] = vertices[0];
|
|
60
|
+
const [bx, by] = cursor;
|
|
61
|
+
const path = [[ax, ay], [bx, ay], [bx, by], [ax, by], [ax, ay]];
|
|
62
|
+
return new PathLayer({
|
|
63
|
+
...base,
|
|
64
|
+
id: 'annotation-preview-rectangle',
|
|
65
|
+
data: [{ path }],
|
|
66
|
+
getPath: d => d.path,
|
|
67
|
+
getColor: previewColor,
|
|
68
|
+
getWidth: 1,
|
|
69
|
+
widthUnits: 'pixels',
|
|
70
|
+
extensions: [new PathStyleExtension({ dash: true })],
|
|
71
|
+
getDashArray: [8, 4],
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
if (type === 'line' && vertices.length === 1) {
|
|
75
|
+
const path = [vertices[0], cursor];
|
|
76
|
+
return new PathLayer({
|
|
77
|
+
...base,
|
|
78
|
+
id: 'annotation-preview-line',
|
|
79
|
+
data: [{ path }],
|
|
80
|
+
getPath: d => d.path,
|
|
81
|
+
getColor: previewColor,
|
|
82
|
+
getWidth: 1,
|
|
83
|
+
widthUnits: 'pixels',
|
|
84
|
+
extensions: [new PathStyleExtension({ dash: true })],
|
|
85
|
+
getDashArray: [8, 4],
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
if (type === 'ellipse' && vertices.length === 1) {
|
|
89
|
+
const [cx, cy] = vertices[0];
|
|
90
|
+
const [ex, ey] = cursor;
|
|
91
|
+
const rx = Math.abs(ex - cx);
|
|
92
|
+
const ry = Math.abs(ey - cy);
|
|
93
|
+
if (rx < 1 && ry < 1)
|
|
94
|
+
return null;
|
|
95
|
+
const pts = computeEllipsePolygon(cx, cy, Math.max(rx, 1), Math.max(ry, 1));
|
|
96
|
+
const path = [...pts, pts[0]];
|
|
97
|
+
return new PathLayer({
|
|
98
|
+
...base,
|
|
99
|
+
id: 'annotation-preview-ellipse',
|
|
100
|
+
data: [{ path }],
|
|
101
|
+
getPath: d => d.path,
|
|
102
|
+
getColor: previewColor,
|
|
103
|
+
getWidth: 1,
|
|
104
|
+
widthUnits: 'pixels',
|
|
105
|
+
extensions: [new PathStyleExtension({ dash: true })],
|
|
106
|
+
getDashArray: [8, 4],
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
if ((type === 'polygon' || type === 'polyline') && vertices.length >= 1) {
|
|
110
|
+
const path = [...vertices, cursor];
|
|
111
|
+
if (type === 'polygon' && vertices.length >= 2)
|
|
112
|
+
path.push(vertices[0]);
|
|
113
|
+
return new PathLayer({
|
|
114
|
+
...base,
|
|
115
|
+
id: `annotation-preview-${type}`,
|
|
116
|
+
data: [{ path }],
|
|
117
|
+
getPath: d => d.path,
|
|
118
|
+
getColor: previewColor,
|
|
119
|
+
getWidth: 1,
|
|
120
|
+
widthUnits: 'pixels',
|
|
121
|
+
jointRounded: true,
|
|
122
|
+
capRounded: true,
|
|
123
|
+
extensions: [new PathStyleExtension({ dash: true })],
|
|
124
|
+
getDashArray: [8, 4],
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
return null;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Compute the two endpoints of a tick-cap (perpendicular crossbar) at (tipX, tipY).
|
|
131
|
+
* dirX/dirY is the line direction (the tick is perpendicular to it).
|
|
132
|
+
* Returns null if the direction vector is zero-length.
|
|
133
|
+
*/
|
|
134
|
+
export function computeTickCap(tipX, tipY, dirX, dirY, dataSize) {
|
|
135
|
+
const len = Math.sqrt(dirX * dirX + dirY * dirY);
|
|
136
|
+
if (len === 0)
|
|
137
|
+
return null;
|
|
138
|
+
const ux = dirX / len;
|
|
139
|
+
const uy = dirY / len;
|
|
140
|
+
// Perpendicular unit vector
|
|
141
|
+
const px = -uy;
|
|
142
|
+
const py = ux;
|
|
143
|
+
const half = dataSize * 0.55;
|
|
144
|
+
return [
|
|
145
|
+
[tipX + px * half, tipY + py * half],
|
|
146
|
+
[tipX - px * half, tipY - py * half],
|
|
147
|
+
];
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Compute the three vertices of an arrowhead triangle pointing in the direction (dirX, dirY).
|
|
151
|
+
* The tip is at (tipX, tipY). dataSize controls the triangle's height in data units.
|
|
152
|
+
* Returns null if the direction vector is zero-length.
|
|
153
|
+
*/
|
|
154
|
+
export function computeArrowhead(tipX, tipY, dirX, dirY, dataSize) {
|
|
155
|
+
const len = Math.sqrt(dirX * dirX + dirY * dirY);
|
|
156
|
+
if (len === 0)
|
|
157
|
+
return null;
|
|
158
|
+
const ux = dirX / len;
|
|
159
|
+
const uy = dirY / len;
|
|
160
|
+
const px = -uy;
|
|
161
|
+
const py = ux;
|
|
162
|
+
const halfBase = dataSize * 0.4;
|
|
163
|
+
const bx = tipX - ux * dataSize;
|
|
164
|
+
const by = tipY - uy * dataSize;
|
|
165
|
+
return [
|
|
166
|
+
[tipX, tipY],
|
|
167
|
+
[bx + px * halfBase, by + py * halfBase],
|
|
168
|
+
[bx - px * halfBase, by - py * halfBase],
|
|
169
|
+
];
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Convert an array of annotation shape objects into deck.gl layers.
|
|
173
|
+
* Works for any CARTESIAN coordinate system view (spatial images, embedding scatterplots).
|
|
174
|
+
*
|
|
175
|
+
* @param {object[]} shapes - Filtered shapes for this view (already targetView-matched).
|
|
176
|
+
* @param {number} zoom - Current view zoom level, keeps arrowheads a fixed screen size.
|
|
177
|
+
* @param {string|null} selectedShapeUid - UID of the shape to highlight (from editor selection).
|
|
178
|
+
* @returns {object[]} Array of deck.gl layer instances.
|
|
179
|
+
*/
|
|
180
|
+
// Insert thousand-separator commas for large integers (no locale API needed).
|
|
181
|
+
function fmtCommas(n) {
|
|
182
|
+
return Math.round(n).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
183
|
+
}
|
|
184
|
+
export function getMeasurementLabel(shape, physicalPixelSize, viewType = ViewType.SPATIAL) {
|
|
185
|
+
const pps = physicalPixelSize;
|
|
186
|
+
const px = pps?.x ?? 1;
|
|
187
|
+
const py = pps?.y ?? 1;
|
|
188
|
+
const unit = pps ? (pps.unit ?? '') : null;
|
|
189
|
+
const hasPhys = !!pps;
|
|
190
|
+
// Embedding scatterplots (t-SNE/UMAP/PCA) have no pixel grid — their axes are
|
|
191
|
+
// continuous, arbitrary-scale coordinates, so decimals are real signal, not noise.
|
|
192
|
+
const isEmbedding = viewType === ViewType.SCATTERPLOT;
|
|
193
|
+
const fmtDist = (d) => {
|
|
194
|
+
if (hasPhys)
|
|
195
|
+
return `${d.toFixed(2)} ${unit}`;
|
|
196
|
+
if (isEmbedding)
|
|
197
|
+
return d.toFixed(2);
|
|
198
|
+
return `${fmtCommas(d)} px`;
|
|
199
|
+
};
|
|
200
|
+
const fmtArea = (a) => {
|
|
201
|
+
if (hasPhys)
|
|
202
|
+
return `${a.toFixed(2)} ${unit}²`;
|
|
203
|
+
if (isEmbedding)
|
|
204
|
+
return a.toFixed(2);
|
|
205
|
+
return `${fmtCommas(a)} px²`;
|
|
206
|
+
};
|
|
207
|
+
if (shape.type === 'line') {
|
|
208
|
+
const dx = (shape.x2 ?? 0) - (shape.x1 ?? 0);
|
|
209
|
+
const dy = (shape.y2 ?? 0) - (shape.y1 ?? 0);
|
|
210
|
+
const dist = hasPhys
|
|
211
|
+
? Math.sqrt(((dx * px) ** 2) + ((dy * py) ** 2))
|
|
212
|
+
: Math.sqrt(dx * dx + dy * dy);
|
|
213
|
+
return fmtDist(dist);
|
|
214
|
+
}
|
|
215
|
+
if (shape.type === 'rectangle') {
|
|
216
|
+
return fmtArea(hasPhys
|
|
217
|
+
? Math.abs(shape.width ?? 0) * px * Math.abs(shape.height ?? 0) * py
|
|
218
|
+
: Math.abs((shape.width ?? 0) * (shape.height ?? 0)));
|
|
219
|
+
}
|
|
220
|
+
if (shape.type === 'ellipse') {
|
|
221
|
+
const rx = Math.abs(shape.radiusX ?? 0);
|
|
222
|
+
const ry = Math.abs(shape.radiusY ?? 0);
|
|
223
|
+
return fmtArea(hasPhys ? Math.PI * rx * px * ry * py : Math.PI * rx * ry);
|
|
224
|
+
}
|
|
225
|
+
if (shape.type === 'polygon' && shape.points?.length >= 3) {
|
|
226
|
+
const pts = shape.points;
|
|
227
|
+
let shoelace = 0;
|
|
228
|
+
for (let i = 0; i < pts.length; i++) {
|
|
229
|
+
const [ax, ay] = pts[i];
|
|
230
|
+
const [bx, by] = pts[(i + 1) % pts.length];
|
|
231
|
+
shoelace += (hasPhys ? ax * px : ax) * (hasPhys ? by * py : by)
|
|
232
|
+
- (hasPhys ? bx * px : bx) * (hasPhys ? ay * py : ay);
|
|
233
|
+
}
|
|
234
|
+
return fmtArea(Math.abs(shoelace / 2));
|
|
235
|
+
}
|
|
236
|
+
if (shape.type === 'polyline' && shape.points?.length >= 2) {
|
|
237
|
+
let total = 0;
|
|
238
|
+
for (let i = 0; i < shape.points.length - 1; i++) {
|
|
239
|
+
const [ax, ay] = shape.points[i];
|
|
240
|
+
const [bx, by] = shape.points[i + 1];
|
|
241
|
+
total += hasPhys
|
|
242
|
+
? Math.sqrt((((bx - ax) * px) ** 2) + (((by - ay) * py) ** 2))
|
|
243
|
+
: Math.sqrt(((bx - ax) ** 2) + ((by - ay) ** 2));
|
|
244
|
+
}
|
|
245
|
+
return fmtDist(total);
|
|
246
|
+
}
|
|
247
|
+
return null;
|
|
248
|
+
}
|
|
249
|
+
// Pick the perpendicular direction (unit vec) that points "below" the line
|
|
250
|
+
// (more +y in image coords).
|
|
251
|
+
function pickBelowPerp(ux, uy) {
|
|
252
|
+
const rp = [uy, -ux];
|
|
253
|
+
const lp = [-uy, ux];
|
|
254
|
+
return (rp[1] > lp[1] || (rp[1] === lp[1] && rp[0] > lp[0])) ? rp : lp;
|
|
255
|
+
}
|
|
256
|
+
// Normalize angle (degrees) so text reads left-to-right, not upside-down.
|
|
257
|
+
function readableAngle(deg) {
|
|
258
|
+
let a = deg;
|
|
259
|
+
if (a > 90)
|
|
260
|
+
a -= 180;
|
|
261
|
+
if (a < -90)
|
|
262
|
+
a += 180;
|
|
263
|
+
return a;
|
|
264
|
+
}
|
|
265
|
+
function getShapeCentroid(shape) {
|
|
266
|
+
if (shape.type === 'rectangle') {
|
|
267
|
+
return [(shape.x ?? 0) + (shape.width ?? 0) / 2, (shape.y ?? 0) + (shape.height ?? 0) / 2];
|
|
268
|
+
}
|
|
269
|
+
if (shape.type === 'line') {
|
|
270
|
+
return [((shape.x1 ?? 0) + (shape.x2 ?? 0)) / 2, ((shape.y1 ?? 0) + (shape.y2 ?? 0)) / 2];
|
|
271
|
+
}
|
|
272
|
+
if (shape.type === 'ellipse') {
|
|
273
|
+
return [shape.x1 ?? 0, shape.y1 ?? 0];
|
|
274
|
+
}
|
|
275
|
+
if ((shape.type === 'polygon' || shape.type === 'polyline') && shape.points?.length >= 1) {
|
|
276
|
+
const mx = shape.points.reduce((s, p) => s + p[0], 0) / shape.points.length;
|
|
277
|
+
const my = shape.points.reduce((s, p) => s + p[1], 0) / shape.points.length;
|
|
278
|
+
return [mx, my];
|
|
279
|
+
}
|
|
280
|
+
return [0, 0];
|
|
281
|
+
}
|
|
282
|
+
function computeClusters(shapeList, zoom, cellPx = 50) {
|
|
283
|
+
// Snap to integer zoom stops so cluster membership is stable during continuous
|
|
284
|
+
// scrolling — groups only change at discrete zoom-level crossings, not every frame.
|
|
285
|
+
const cellSize = cellPx / (2 ** Math.floor(zoom));
|
|
286
|
+
const cells = new Map();
|
|
287
|
+
shapeList.forEach(({ shape }) => {
|
|
288
|
+
const [cx, cy] = getShapeCentroid(shape);
|
|
289
|
+
const key = `${Math.floor(cx / cellSize)},${Math.floor(cy / cellSize)}`;
|
|
290
|
+
if (!cells.has(key)) {
|
|
291
|
+
cells.set(key, { sumX: 0, sumY: 0, count: 0, color: shape.strokeColor ?? [255, 255, 255] });
|
|
292
|
+
}
|
|
293
|
+
const c = cells.get(key);
|
|
294
|
+
c.sumX += cx;
|
|
295
|
+
c.sumY += cy;
|
|
296
|
+
c.count++;
|
|
297
|
+
});
|
|
298
|
+
return Array.from(cells.values()).map(c => ({
|
|
299
|
+
position: [c.sumX / c.count, c.sumY / c.count, 0],
|
|
300
|
+
count: c.count,
|
|
301
|
+
color: c.color,
|
|
302
|
+
}));
|
|
303
|
+
}
|
|
304
|
+
export function createAnnotationLayers(shapes = [], zoom = 0, selectedShapeUid = null, physicalPixelSize = null, authoredZoom = null, semanticZoom = true, viewType = ViewType.SPATIAL) {
|
|
305
|
+
// Selection halo — rendered first so the original shape draws on top of it.
|
|
306
|
+
// Only the extra pixels outside the original stroke are visible, creating a subtle glow ring.
|
|
307
|
+
const selectionLayers = [];
|
|
308
|
+
if (selectedShapeUid) {
|
|
309
|
+
const sel = shapes.find(s => s.uid === selectedShapeUid);
|
|
310
|
+
if (sel) {
|
|
311
|
+
const { uid: sUid, type: sType, strokeWidth: sStrokeWidth = 3 } = sel;
|
|
312
|
+
const haloColor = [255, 220, 50, 70];
|
|
313
|
+
const haloWidth = Math.max(sStrokeWidth + 8, 10);
|
|
314
|
+
const base = { coordinateSystem: COORDINATE_SYSTEM.CARTESIAN };
|
|
315
|
+
if (sType === 'rectangle') {
|
|
316
|
+
const { x, y, width, height } = sel;
|
|
317
|
+
selectionLayers.push(new PathLayer({
|
|
318
|
+
...base,
|
|
319
|
+
id: `annotation-select-${sUid}`,
|
|
320
|
+
data: [{
|
|
321
|
+
path: [
|
|
322
|
+
[x, y], [x + width, y],
|
|
323
|
+
[x + width, y + height], [x, y + height], [x, y],
|
|
324
|
+
],
|
|
325
|
+
}],
|
|
326
|
+
getPath: d => d.path,
|
|
327
|
+
getColor: haloColor,
|
|
328
|
+
getWidth: haloWidth,
|
|
329
|
+
widthUnits: 'pixels',
|
|
330
|
+
jointRounded: true,
|
|
331
|
+
capRounded: true,
|
|
332
|
+
}));
|
|
333
|
+
}
|
|
334
|
+
if (sType === 'line') {
|
|
335
|
+
const { x1, y1, x2, y2 } = sel;
|
|
336
|
+
selectionLayers.push(new PathLayer({
|
|
337
|
+
...base,
|
|
338
|
+
id: `annotation-select-${sUid}`,
|
|
339
|
+
data: [{ path: [[x1, y1], [x2, y2]] }],
|
|
340
|
+
getPath: d => d.path,
|
|
341
|
+
getColor: haloColor,
|
|
342
|
+
getWidth: haloWidth,
|
|
343
|
+
widthUnits: 'pixels',
|
|
344
|
+
capRounded: true,
|
|
345
|
+
}));
|
|
346
|
+
}
|
|
347
|
+
if (sType === 'ellipse') {
|
|
348
|
+
const polygon = computeEllipsePolygon(sel.x1, sel.y1, sel.radiusX, sel.radiusY);
|
|
349
|
+
selectionLayers.push(new PathLayer({
|
|
350
|
+
...base,
|
|
351
|
+
id: `annotation-select-${sUid}`,
|
|
352
|
+
data: [{ path: [...polygon, polygon[0]] }],
|
|
353
|
+
getPath: d => d.path,
|
|
354
|
+
getColor: haloColor,
|
|
355
|
+
getWidth: haloWidth,
|
|
356
|
+
widthUnits: 'pixels',
|
|
357
|
+
jointRounded: true,
|
|
358
|
+
}));
|
|
359
|
+
}
|
|
360
|
+
if (sType === 'polygon' && sel.points?.length >= 3) {
|
|
361
|
+
selectionLayers.push(new PathLayer({
|
|
362
|
+
...base,
|
|
363
|
+
id: `annotation-select-${sUid}`,
|
|
364
|
+
data: [{ path: [...sel.points, sel.points[0]] }],
|
|
365
|
+
getPath: d => d.path,
|
|
366
|
+
getColor: haloColor,
|
|
367
|
+
getWidth: haloWidth,
|
|
368
|
+
widthUnits: 'pixels',
|
|
369
|
+
jointRounded: true,
|
|
370
|
+
}));
|
|
371
|
+
}
|
|
372
|
+
if (sType === 'polyline' && sel.points?.length >= 2) {
|
|
373
|
+
selectionLayers.push(new PathLayer({
|
|
374
|
+
...base,
|
|
375
|
+
id: `annotation-select-${sUid}`,
|
|
376
|
+
data: [{ path: sel.points }],
|
|
377
|
+
getPath: d => d.path,
|
|
378
|
+
getColor: haloColor,
|
|
379
|
+
getWidth: haloWidth,
|
|
380
|
+
widthUnits: 'pixels',
|
|
381
|
+
jointRounded: true,
|
|
382
|
+
capRounded: true,
|
|
383
|
+
}));
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
const layers = [];
|
|
388
|
+
const dotShapes = [];
|
|
389
|
+
const clusterDotShapes = [];
|
|
390
|
+
shapes.forEach((shape, i) => {
|
|
391
|
+
const { uid, type, strokeColor = [255, 255, 255], strokeWidth = 3, strokeDashArray, } = shape;
|
|
392
|
+
// LOD: compute geometry + label opacity, demote tiny shapes to dots.
|
|
393
|
+
// Selected shapes always render at full detail regardless of zoom.
|
|
394
|
+
const isSelected = uid === selectedShapeUid;
|
|
395
|
+
let shapeOpacity = 1;
|
|
396
|
+
let tipsOpacity = 1;
|
|
397
|
+
let labelOpacity = 0;
|
|
398
|
+
if (isSelected || !semanticZoom || authoredZoom === null) {
|
|
399
|
+
// No LOD: selected shapes always render fully; LOD disabled; or no reference zoom available.
|
|
400
|
+
tipsOpacity = 1;
|
|
401
|
+
labelOpacity = 1;
|
|
402
|
+
}
|
|
403
|
+
else {
|
|
404
|
+
// Ratio-based LOD relative to the frame's captured zoom.
|
|
405
|
+
// ratio=1 → at authored zoom; <1 → zoomed out; >1 → zoomed in (always full detail).
|
|
406
|
+
const zoomRatio = 2 ** (zoom - authoredZoom);
|
|
407
|
+
if (zoomRatio < RATIO_CLUSTER) {
|
|
408
|
+
clusterDotShapes.push({ shape });
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
if (zoomRatio < RATIO_DOT) {
|
|
412
|
+
dotShapes.push({ shape, alpha: 200 });
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
if (zoomRatio < RATIO_GEO_FADE) {
|
|
416
|
+
const t = (zoomRatio - RATIO_DOT) / (RATIO_GEO_FADE - RATIO_DOT);
|
|
417
|
+
shapeOpacity = t;
|
|
418
|
+
dotShapes.push({ shape, alpha: Math.round((1 - t) * 200) });
|
|
419
|
+
}
|
|
420
|
+
tipsOpacity = zoomRatio >= RATIO_TIPS
|
|
421
|
+
? shapeOpacity
|
|
422
|
+
: Math.max(0, ((zoomRatio - RATIO_GEO_FADE) / (RATIO_TIPS - RATIO_GEO_FADE)) * shapeOpacity);
|
|
423
|
+
if (zoomRatio >= RATIO_LABEL_FULL) {
|
|
424
|
+
labelOpacity = shapeOpacity;
|
|
425
|
+
}
|
|
426
|
+
else if (zoomRatio >= RATIO_LABEL) {
|
|
427
|
+
labelOpacity = ((zoomRatio - RATIO_LABEL) / (RATIO_LABEL_FULL - RATIO_LABEL))
|
|
428
|
+
* shapeOpacity;
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
// Arrowhead scales with stroke width: 4px per unit, floor at ARROW_SCREEN_PX.
|
|
432
|
+
const arrowDataSize = Math.max(ARROW_SCREEN_PX, strokeWidth * 4) / (2 ** zoom);
|
|
433
|
+
const base = { coordinateSystem: COORDINATE_SYSTEM.CARTESIAN, opacity: shapeOpacity };
|
|
434
|
+
const tipsBase = { coordinateSystem: COORDINATE_SYSTEM.CARTESIAN, opacity: tipsOpacity };
|
|
435
|
+
const labelBase = { coordinateSystem: COORDINATE_SYSTEM.CARTESIAN, opacity: labelOpacity };
|
|
436
|
+
const labelBgProps = shape.labelBackground
|
|
437
|
+
? {
|
|
438
|
+
background: true,
|
|
439
|
+
getBackgroundColor: textBgColor(strokeColor),
|
|
440
|
+
backgroundPadding: [3, 1, 3, 2],
|
|
441
|
+
}
|
|
442
|
+
: {};
|
|
443
|
+
const labelProps = { ...labelBgProps };
|
|
444
|
+
const dashArray = parseDashArray(strokeDashArray);
|
|
445
|
+
// Always include PathStyleExtension on outline PathLayers — adding/removing extensions
|
|
446
|
+
// on a stable layer ID confuses deck.gl's reconciler. Use [0,0] for solid instead.
|
|
447
|
+
const dashExt = [new PathStyleExtension({ dash: true })];
|
|
448
|
+
const getDash = dashArray ?? [0, 0];
|
|
449
|
+
if (type === 'rectangle') {
|
|
450
|
+
const { x, y, width, height, fillColor = strokeColor, fillOpacity = 0 } = shape;
|
|
451
|
+
const filled = fillOpacity > 0;
|
|
452
|
+
const rectPolygon = [[x, y], [x + width, y], [x + width, y + height], [x, y + height]];
|
|
453
|
+
layers.push(new PolygonLayer({
|
|
454
|
+
...base,
|
|
455
|
+
id: `annotation-rect-${uid}-${i}`,
|
|
456
|
+
data: [{ polygon: rectPolygon }],
|
|
457
|
+
getPolygon: d => d.polygon,
|
|
458
|
+
filled,
|
|
459
|
+
stroked: true,
|
|
460
|
+
getFillColor: filled ? [...fillColor, Math.round(fillOpacity * 255)] : [0, 0, 0, 0],
|
|
461
|
+
getLineColor: strokeColor,
|
|
462
|
+
getLineWidth: strokeWidth,
|
|
463
|
+
lineWidthUnits: 'pixels',
|
|
464
|
+
...(dashArray ? {
|
|
465
|
+
extensions: [new PathStyleExtension({ dash: true })],
|
|
466
|
+
getDashArray: dashArray,
|
|
467
|
+
} : {}),
|
|
468
|
+
}));
|
|
469
|
+
if (shape.text && labelOpacity > 0) {
|
|
470
|
+
layers.push(new TextLayer({
|
|
471
|
+
...labelBase,
|
|
472
|
+
...labelProps,
|
|
473
|
+
id: `annotation-rect-text-${uid}-${i}`,
|
|
474
|
+
data: [{ position: [x, y], text: shape.text }],
|
|
475
|
+
getPosition: d => d.position,
|
|
476
|
+
getText: d => d.text,
|
|
477
|
+
getColor: strokeColor,
|
|
478
|
+
getSize: LABEL_FONT_SIZE,
|
|
479
|
+
sizeUnits: 'pixels',
|
|
480
|
+
fontFamily: LABEL_FONT_FAMILY,
|
|
481
|
+
fontWeight: LABEL_FONT_WEIGHT,
|
|
482
|
+
getPixelOffset: [3, -7],
|
|
483
|
+
getTextAnchor: 'start',
|
|
484
|
+
getAlignmentBaseline: 'bottom',
|
|
485
|
+
}));
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
if (type === 'line') {
|
|
489
|
+
const { x1, y1, x2, y2, markerStart, markerEnd } = shape;
|
|
490
|
+
const dx = x2 - x1;
|
|
491
|
+
const dy = y2 - y1;
|
|
492
|
+
const len = Math.sqrt(dx * dx + dy * dy);
|
|
493
|
+
const ux = len > 0 ? dx / len : 0;
|
|
494
|
+
const uy = len > 0 ? dy / len : 0;
|
|
495
|
+
// Shorten the shaft so it meets the arrowhead base, not the tip,
|
|
496
|
+
// preventing the line width from poking through the pointed arrowhead.
|
|
497
|
+
// Tick caps sit perpendicular at the endpoint so no shortening is needed.
|
|
498
|
+
const srcX = markerStart === 'Arrow' ? x1 + ux * arrowDataSize : x1;
|
|
499
|
+
const srcY = markerStart === 'Arrow' ? y1 + uy * arrowDataSize : y1;
|
|
500
|
+
const tgtX = markerEnd === 'Arrow' ? x2 - ux * arrowDataSize : x2;
|
|
501
|
+
const tgtY = markerEnd === 'Arrow' ? y2 - uy * arrowDataSize : y2;
|
|
502
|
+
layers.push(new PathLayer({
|
|
503
|
+
...base,
|
|
504
|
+
id: `annotation-line-${uid}-${i}`,
|
|
505
|
+
extensions: [new PathStyleExtension({ dash: true })],
|
|
506
|
+
getDashArray: dashArray ?? [0, 0],
|
|
507
|
+
data: [{ path: [[srcX, srcY], [tgtX, tgtY]] }],
|
|
508
|
+
getPath: d => d.path,
|
|
509
|
+
getColor: strokeColor,
|
|
510
|
+
getWidth: strokeWidth,
|
|
511
|
+
widthUnits: 'pixels',
|
|
512
|
+
capRounded: true,
|
|
513
|
+
}));
|
|
514
|
+
if (markerEnd === 'Arrow' && tipsOpacity > 0) {
|
|
515
|
+
const head = computeArrowhead(x2, y2, x2 - x1, y2 - y1, arrowDataSize);
|
|
516
|
+
if (head) {
|
|
517
|
+
layers.push(new PolygonLayer({
|
|
518
|
+
...tipsBase,
|
|
519
|
+
id: `annotation-end-arrow-${uid}-${i}`,
|
|
520
|
+
data: [{ polygon: head }],
|
|
521
|
+
getPolygon: d => d.polygon,
|
|
522
|
+
filled: true,
|
|
523
|
+
stroked: false,
|
|
524
|
+
getFillColor: strokeColor,
|
|
525
|
+
}));
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
if (markerStart === 'Arrow' && tipsOpacity > 0) {
|
|
529
|
+
const head = computeArrowhead(x1, y1, x1 - x2, y1 - y2, arrowDataSize);
|
|
530
|
+
if (head) {
|
|
531
|
+
layers.push(new PolygonLayer({
|
|
532
|
+
...tipsBase,
|
|
533
|
+
id: `annotation-start-arrow-${uid}-${i}`,
|
|
534
|
+
data: [{ polygon: head }],
|
|
535
|
+
getPolygon: d => d.polygon,
|
|
536
|
+
filled: true,
|
|
537
|
+
stroked: false,
|
|
538
|
+
getFillColor: strokeColor,
|
|
539
|
+
}));
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
if (markerEnd === 'Tick' && tipsOpacity > 0) {
|
|
543
|
+
const tick = computeTickCap(x2, y2, x2 - x1, y2 - y1, arrowDataSize);
|
|
544
|
+
if (tick) {
|
|
545
|
+
layers.push(new PathLayer({
|
|
546
|
+
...tipsBase,
|
|
547
|
+
id: `annotation-end-tick-${uid}-${i}`,
|
|
548
|
+
data: [{ path: tick }],
|
|
549
|
+
getPath: d => d.path,
|
|
550
|
+
getColor: strokeColor,
|
|
551
|
+
getWidth: strokeWidth,
|
|
552
|
+
widthUnits: 'pixels',
|
|
553
|
+
capRounded: true,
|
|
554
|
+
}));
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
if (markerStart === 'Tick' && tipsOpacity > 0) {
|
|
558
|
+
const tick = computeTickCap(x1, y1, x1 - x2, y1 - y2, arrowDataSize);
|
|
559
|
+
if (tick) {
|
|
560
|
+
layers.push(new PathLayer({
|
|
561
|
+
...tipsBase,
|
|
562
|
+
id: `annotation-start-tick-${uid}-${i}`,
|
|
563
|
+
data: [{ path: tick }],
|
|
564
|
+
getPath: d => d.path,
|
|
565
|
+
getColor: strokeColor,
|
|
566
|
+
getWidth: strokeWidth,
|
|
567
|
+
widthUnits: 'pixels',
|
|
568
|
+
capRounded: true,
|
|
569
|
+
}));
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
if (shape.text) {
|
|
573
|
+
const pos = shape.textPosition ?? 'start';
|
|
574
|
+
let textX;
|
|
575
|
+
let textY;
|
|
576
|
+
if (pos === 'start') {
|
|
577
|
+
textX = x1;
|
|
578
|
+
textY = y1;
|
|
579
|
+
}
|
|
580
|
+
else if (pos === 'end') {
|
|
581
|
+
textX = x2;
|
|
582
|
+
textY = y2;
|
|
583
|
+
}
|
|
584
|
+
else {
|
|
585
|
+
textX = (x1 + x2) / 2;
|
|
586
|
+
textY = (y1 + y2) / 2;
|
|
587
|
+
}
|
|
588
|
+
let pixelOffset = [0, 0];
|
|
589
|
+
let textAnchor = 'middle';
|
|
590
|
+
let alignmentBaseline = 'center';
|
|
591
|
+
if (pos !== 'middle') {
|
|
592
|
+
const dxT = x2 - x1;
|
|
593
|
+
const dyT = y2 - y1;
|
|
594
|
+
const lenT = Math.sqrt(dxT * dxT + dyT * dyT);
|
|
595
|
+
if (lenT > 0) {
|
|
596
|
+
const uxT = dxT / lenT;
|
|
597
|
+
const uyT = dyT / lenT;
|
|
598
|
+
const sign = pos === 'start' ? -1 : 1;
|
|
599
|
+
const bufferPx = shape.textBufferPx ?? 8;
|
|
600
|
+
const offsetX = sign * uxT * bufferPx;
|
|
601
|
+
const offsetY = sign * uyT * bufferPx;
|
|
602
|
+
pixelOffset = [offsetX, offsetY];
|
|
603
|
+
// Derive text anchor from the dominant offset direction so the label
|
|
604
|
+
// extends AWAY from the line rather than back toward it.
|
|
605
|
+
// Horizontal dominant → anchor by X edge; vertical dominant → anchor by Y baseline.
|
|
606
|
+
const absX = Math.abs(offsetX);
|
|
607
|
+
const absY = Math.abs(offsetY);
|
|
608
|
+
if (absX >= absY) {
|
|
609
|
+
textAnchor = offsetX > 0 ? 'start' : 'end';
|
|
610
|
+
}
|
|
611
|
+
if (absY >= absX) {
|
|
612
|
+
alignmentBaseline = offsetY > 0 ? 'top' : 'bottom';
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
}
|
|
616
|
+
else if (len > 0) {
|
|
617
|
+
// Middle: float on the TOP side of the line, parallel to it (avoids conflicting with
|
|
618
|
+
// the measurement label which sits on the bottom side).
|
|
619
|
+
const belowPerp = pickBelowPerp(ux, uy);
|
|
620
|
+
const bufferPx = shape.textBufferPx ?? 14;
|
|
621
|
+
pixelOffset = [-belowPerp[0] * bufferPx, -belowPerp[1] * bufferPx];
|
|
622
|
+
textAnchor = 'middle';
|
|
623
|
+
alignmentBaseline = 'center';
|
|
624
|
+
}
|
|
625
|
+
const middleAngle = pos === 'middle' && len > 0
|
|
626
|
+
? -readableAngle(Math.atan2(dy, dx) * 180 / Math.PI)
|
|
627
|
+
: null;
|
|
628
|
+
if (labelOpacity <= 0)
|
|
629
|
+
return;
|
|
630
|
+
layers.push(new TextLayer({
|
|
631
|
+
...labelBase,
|
|
632
|
+
...labelProps,
|
|
633
|
+
id: `annotation-line-text-${uid}-${i}`,
|
|
634
|
+
data: [{ position: [textX, textY], text: shape.text }],
|
|
635
|
+
getPosition: d => d.position,
|
|
636
|
+
getText: d => d.text,
|
|
637
|
+
getColor: strokeColor,
|
|
638
|
+
getSize: LABEL_FONT_SIZE,
|
|
639
|
+
sizeUnits: 'pixels',
|
|
640
|
+
fontFamily: LABEL_FONT_FAMILY,
|
|
641
|
+
fontWeight: LABEL_FONT_WEIGHT,
|
|
642
|
+
getPixelOffset: pixelOffset,
|
|
643
|
+
getTextAnchor: textAnchor,
|
|
644
|
+
getAlignmentBaseline: alignmentBaseline,
|
|
645
|
+
...(middleAngle !== null && { getAngle: middleAngle }),
|
|
646
|
+
}));
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
if (type === 'ellipse') {
|
|
650
|
+
const { x1, y1, radiusX, radiusY, fillColor = strokeColor, fillOpacity = 0 } = shape;
|
|
651
|
+
const polygon = computeEllipsePolygon(x1, y1, radiusX, radiusY);
|
|
652
|
+
const filled = fillOpacity > 0;
|
|
653
|
+
layers.push(new PolygonLayer({
|
|
654
|
+
...base,
|
|
655
|
+
id: `annotation-ellipse-${uid}-${i}`,
|
|
656
|
+
data: [{ polygon }],
|
|
657
|
+
getPolygon: d => d.polygon,
|
|
658
|
+
filled,
|
|
659
|
+
stroked: true,
|
|
660
|
+
getFillColor: filled ? [...fillColor, Math.round(fillOpacity * 255)] : [0, 0, 0, 0],
|
|
661
|
+
getLineColor: strokeColor,
|
|
662
|
+
getLineWidth: strokeWidth,
|
|
663
|
+
lineWidthUnits: 'pixels',
|
|
664
|
+
...(dashArray ? {
|
|
665
|
+
extensions: [new PathStyleExtension({ dash: true })],
|
|
666
|
+
getDashArray: dashArray,
|
|
667
|
+
} : {}),
|
|
668
|
+
}));
|
|
669
|
+
if (shape.text && labelOpacity > 0) {
|
|
670
|
+
layers.push(new TextLayer({
|
|
671
|
+
...labelBase,
|
|
672
|
+
...labelProps,
|
|
673
|
+
id: `annotation-ellipse-text-${uid}-${i}`,
|
|
674
|
+
data: [{ position: [x1, y1 - radiusY], text: shape.text }],
|
|
675
|
+
getPosition: d => d.position,
|
|
676
|
+
getText: d => d.text,
|
|
677
|
+
getColor: strokeColor,
|
|
678
|
+
getSize: LABEL_FONT_SIZE,
|
|
679
|
+
sizeUnits: 'pixels',
|
|
680
|
+
fontFamily: LABEL_FONT_FAMILY,
|
|
681
|
+
fontWeight: LABEL_FONT_WEIGHT,
|
|
682
|
+
getPixelOffset: [0, -4],
|
|
683
|
+
getTextAnchor: 'middle',
|
|
684
|
+
getAlignmentBaseline: 'bottom',
|
|
685
|
+
}));
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
if (type === 'polygon') {
|
|
689
|
+
const { points, fillColor = strokeColor, fillOpacity = 0 } = shape;
|
|
690
|
+
if (!points || points.length < 3)
|
|
691
|
+
return;
|
|
692
|
+
const closedPath = [...points, points[0]];
|
|
693
|
+
if (fillOpacity > 0) {
|
|
694
|
+
layers.push(new PolygonLayer({
|
|
695
|
+
...base,
|
|
696
|
+
id: `annotation-polygon-fill-${uid}-${i}`,
|
|
697
|
+
data: [{ polygon: points }],
|
|
698
|
+
getPolygon: d => d.polygon,
|
|
699
|
+
stroked: false,
|
|
700
|
+
filled: true,
|
|
701
|
+
getFillColor: [...fillColor, Math.round(fillOpacity * 255)],
|
|
702
|
+
}));
|
|
703
|
+
}
|
|
704
|
+
layers.push(new PathLayer({
|
|
705
|
+
...base,
|
|
706
|
+
id: `annotation-polygon-${uid}-${i}`,
|
|
707
|
+
extensions: dashExt,
|
|
708
|
+
getDashArray: getDash,
|
|
709
|
+
data: [{ path: closedPath }],
|
|
710
|
+
getPath: d => d.path,
|
|
711
|
+
getColor: strokeColor,
|
|
712
|
+
getWidth: strokeWidth,
|
|
713
|
+
widthUnits: 'pixels',
|
|
714
|
+
jointRounded: true,
|
|
715
|
+
}));
|
|
716
|
+
if (shape.text && labelOpacity > 0) {
|
|
717
|
+
const minX = Math.min(...points.map(p => p[0]));
|
|
718
|
+
const maxX = Math.max(...points.map(p => p[0]));
|
|
719
|
+
const minY = Math.min(...points.map(p => p[1]));
|
|
720
|
+
const topCenterX = (minX + maxX) / 2;
|
|
721
|
+
layers.push(new TextLayer({
|
|
722
|
+
...labelBase,
|
|
723
|
+
...labelProps,
|
|
724
|
+
id: `annotation-polygon-text-${uid}-${i}`,
|
|
725
|
+
data: [{ position: [topCenterX, minY], text: shape.text }],
|
|
726
|
+
getPosition: d => d.position,
|
|
727
|
+
getText: d => d.text,
|
|
728
|
+
getColor: strokeColor,
|
|
729
|
+
getSize: LABEL_FONT_SIZE,
|
|
730
|
+
sizeUnits: 'pixels',
|
|
731
|
+
fontFamily: LABEL_FONT_FAMILY,
|
|
732
|
+
fontWeight: LABEL_FONT_WEIGHT,
|
|
733
|
+
getPixelOffset: [0, -4],
|
|
734
|
+
getTextAnchor: 'middle',
|
|
735
|
+
getAlignmentBaseline: 'bottom',
|
|
736
|
+
}));
|
|
737
|
+
}
|
|
738
|
+
}
|
|
739
|
+
if (type === 'polyline') {
|
|
740
|
+
const { points, markerStart, markerEnd } = shape;
|
|
741
|
+
if (!points || points.length < 2)
|
|
742
|
+
return;
|
|
743
|
+
// Shorten the shaft at arrow endpoints so the stroke doesn't poke through the arrowhead tip.
|
|
744
|
+
// Tick caps are perpendicular so no shortening needed for those.
|
|
745
|
+
let pathPoints = points;
|
|
746
|
+
if (markerStart === 'Arrow') {
|
|
747
|
+
const [x0, y0] = points[0];
|
|
748
|
+
const [x1, y1] = points[1];
|
|
749
|
+
const dx = x0 - x1;
|
|
750
|
+
const dy = y0 - y1;
|
|
751
|
+
const len = Math.sqrt(dx * dx + dy * dy);
|
|
752
|
+
if (len > 0) {
|
|
753
|
+
const ux = dx / len;
|
|
754
|
+
const uy = dy / len;
|
|
755
|
+
pathPoints = [[x0 - ux * arrowDataSize, y0 - uy * arrowDataSize], ...pathPoints.slice(1)];
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
if (markerEnd === 'Arrow') {
|
|
759
|
+
const last = pathPoints[pathPoints.length - 1];
|
|
760
|
+
const prev = pathPoints[pathPoints.length - 2];
|
|
761
|
+
const dx = last[0] - prev[0];
|
|
762
|
+
const dy = last[1] - prev[1];
|
|
763
|
+
const len = Math.sqrt(dx * dx + dy * dy);
|
|
764
|
+
if (len > 0) {
|
|
765
|
+
const ux = dx / len;
|
|
766
|
+
const uy = dy / len;
|
|
767
|
+
const shortened = [last[0] - ux * arrowDataSize, last[1] - uy * arrowDataSize];
|
|
768
|
+
pathPoints = [...pathPoints.slice(0, -1), shortened];
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
layers.push(new PathLayer({
|
|
772
|
+
...base,
|
|
773
|
+
id: `annotation-polyline-${uid}-${i}`,
|
|
774
|
+
extensions: dashExt,
|
|
775
|
+
getDashArray: getDash,
|
|
776
|
+
data: [{ path: pathPoints }],
|
|
777
|
+
getPath: d => d.path,
|
|
778
|
+
getColor: strokeColor,
|
|
779
|
+
getWidth: strokeWidth,
|
|
780
|
+
widthUnits: 'pixels',
|
|
781
|
+
jointRounded: true,
|
|
782
|
+
capRounded: true,
|
|
783
|
+
}));
|
|
784
|
+
if (markerStart === 'Arrow' && tipsOpacity > 0) {
|
|
785
|
+
const [x0, y0] = points[0];
|
|
786
|
+
const [x1, y1] = points[1];
|
|
787
|
+
const head = computeArrowhead(x0, y0, x0 - x1, y0 - y1, arrowDataSize);
|
|
788
|
+
if (head) {
|
|
789
|
+
layers.push(new PolygonLayer({
|
|
790
|
+
...tipsBase,
|
|
791
|
+
id: `annotation-polyline-start-arrow-${uid}-${i}`,
|
|
792
|
+
data: [{ polygon: head }],
|
|
793
|
+
getPolygon: d => d.polygon,
|
|
794
|
+
filled: true,
|
|
795
|
+
stroked: false,
|
|
796
|
+
getFillColor: strokeColor,
|
|
797
|
+
}));
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
if (markerEnd === 'Arrow' && tipsOpacity > 0) {
|
|
801
|
+
const last = points[points.length - 1];
|
|
802
|
+
const prev = points[points.length - 2];
|
|
803
|
+
const dxArrow = last[0] - prev[0];
|
|
804
|
+
const dyArrow = last[1] - prev[1];
|
|
805
|
+
const head = computeArrowhead(last[0], last[1], dxArrow, dyArrow, arrowDataSize);
|
|
806
|
+
if (head) {
|
|
807
|
+
layers.push(new PolygonLayer({
|
|
808
|
+
...tipsBase,
|
|
809
|
+
id: `annotation-polyline-end-arrow-${uid}-${i}`,
|
|
810
|
+
data: [{ polygon: head }],
|
|
811
|
+
getPolygon: d => d.polygon,
|
|
812
|
+
filled: true,
|
|
813
|
+
stroked: false,
|
|
814
|
+
getFillColor: strokeColor,
|
|
815
|
+
}));
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
if (markerStart === 'Tick' && tipsOpacity > 0) {
|
|
819
|
+
const [x0, y0] = points[0];
|
|
820
|
+
const [x1, y1] = points[1];
|
|
821
|
+
const tick = computeTickCap(x0, y0, x0 - x1, y0 - y1, arrowDataSize);
|
|
822
|
+
if (tick) {
|
|
823
|
+
layers.push(new PathLayer({
|
|
824
|
+
...tipsBase,
|
|
825
|
+
id: `annotation-polyline-start-tick-${uid}-${i}`,
|
|
826
|
+
data: [{ path: tick }],
|
|
827
|
+
getPath: d => d.path,
|
|
828
|
+
getColor: strokeColor,
|
|
829
|
+
getWidth: strokeWidth,
|
|
830
|
+
widthUnits: 'pixels',
|
|
831
|
+
capRounded: true,
|
|
832
|
+
}));
|
|
833
|
+
}
|
|
834
|
+
}
|
|
835
|
+
if (markerEnd === 'Tick' && tipsOpacity > 0) {
|
|
836
|
+
const last = points[points.length - 1];
|
|
837
|
+
const prev = points[points.length - 2];
|
|
838
|
+
const dxTick = last[0] - prev[0];
|
|
839
|
+
const dyTick = last[1] - prev[1];
|
|
840
|
+
const tick = computeTickCap(last[0], last[1], dxTick, dyTick, arrowDataSize);
|
|
841
|
+
if (tick) {
|
|
842
|
+
layers.push(new PathLayer({
|
|
843
|
+
...tipsBase,
|
|
844
|
+
id: `annotation-polyline-end-tick-${uid}-${i}`,
|
|
845
|
+
data: [{ path: tick }],
|
|
846
|
+
getPath: d => d.path,
|
|
847
|
+
getColor: strokeColor,
|
|
848
|
+
getWidth: strokeWidth,
|
|
849
|
+
widthUnits: 'pixels',
|
|
850
|
+
capRounded: true,
|
|
851
|
+
}));
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
if (shape.text) {
|
|
855
|
+
const pos = shape.textPosition ?? 'start';
|
|
856
|
+
const bufferPx = shape.textBufferPx ?? 8;
|
|
857
|
+
let textPt;
|
|
858
|
+
let dirX;
|
|
859
|
+
let dirY;
|
|
860
|
+
if (pos === 'end') {
|
|
861
|
+
const last = points[points.length - 1];
|
|
862
|
+
const prev = points[points.length - 2];
|
|
863
|
+
textPt = last;
|
|
864
|
+
dirX = last[0] - prev[0];
|
|
865
|
+
dirY = last[1] - prev[1];
|
|
866
|
+
}
|
|
867
|
+
else if (pos === 'middle') {
|
|
868
|
+
const mx = points.reduce((s, p) => s + p[0], 0) / points.length;
|
|
869
|
+
const my = points.reduce((s, p) => s + p[1], 0) / points.length;
|
|
870
|
+
textPt = [mx, my];
|
|
871
|
+
dirX = 0;
|
|
872
|
+
dirY = 0;
|
|
873
|
+
}
|
|
874
|
+
else {
|
|
875
|
+
([textPt] = points);
|
|
876
|
+
dirX = points[0][0] - points[1][0];
|
|
877
|
+
dirY = points[0][1] - points[1][1];
|
|
878
|
+
}
|
|
879
|
+
const [textX, textY] = textPt;
|
|
880
|
+
const lenT = Math.sqrt(dirX * dirX + dirY * dirY);
|
|
881
|
+
let polylinePixelOffset = [0, 0];
|
|
882
|
+
let polylineTextAnchor = 'middle';
|
|
883
|
+
let polylineAlignmentBaseline = pos === 'middle' ? 'center' : 'bottom';
|
|
884
|
+
if (lenT > 0) {
|
|
885
|
+
const uxT = dirX / lenT;
|
|
886
|
+
const uyT = dirY / lenT;
|
|
887
|
+
const offX = uxT * bufferPx;
|
|
888
|
+
const offY = uyT * bufferPx;
|
|
889
|
+
polylinePixelOffset = [offX, offY];
|
|
890
|
+
if (Math.abs(offX) >= Math.abs(offY)) {
|
|
891
|
+
polylineTextAnchor = offX > 0 ? 'start' : 'end';
|
|
892
|
+
}
|
|
893
|
+
if (Math.abs(offY) >= Math.abs(offX)) {
|
|
894
|
+
polylineAlignmentBaseline = offY > 0 ? 'top' : 'bottom';
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
if (labelOpacity <= 0)
|
|
898
|
+
return;
|
|
899
|
+
layers.push(new TextLayer({
|
|
900
|
+
...labelBase,
|
|
901
|
+
...labelProps,
|
|
902
|
+
id: `annotation-polyline-text-${uid}-${i}`,
|
|
903
|
+
data: [{ position: [textX, textY], text: shape.text }],
|
|
904
|
+
getPosition: d => d.position,
|
|
905
|
+
getText: d => d.text,
|
|
906
|
+
getColor: strokeColor,
|
|
907
|
+
getSize: LABEL_FONT_SIZE,
|
|
908
|
+
sizeUnits: 'pixels',
|
|
909
|
+
fontFamily: LABEL_FONT_FAMILY,
|
|
910
|
+
fontWeight: LABEL_FONT_WEIGHT,
|
|
911
|
+
getPixelOffset: polylinePixelOffset,
|
|
912
|
+
getTextAnchor: polylineTextAnchor,
|
|
913
|
+
getAlignmentBaseline: polylineAlignmentBaseline,
|
|
914
|
+
}));
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
});
|
|
918
|
+
if (dotShapes.length > 0) {
|
|
919
|
+
layers.push(new ScatterplotLayer({
|
|
920
|
+
coordinateSystem: COORDINATE_SYSTEM.CARTESIAN,
|
|
921
|
+
id: 'annotation-dots',
|
|
922
|
+
data: dotShapes.map(({ shape: s, alpha }) => ({
|
|
923
|
+
position: getShapeCentroid(s),
|
|
924
|
+
color: [...(s.strokeColor ?? [255, 255, 255]), alpha],
|
|
925
|
+
})),
|
|
926
|
+
getPosition: d => d.position,
|
|
927
|
+
getFillColor: d => d.color,
|
|
928
|
+
getRadius: 4,
|
|
929
|
+
radiusUnits: 'pixels',
|
|
930
|
+
filled: true,
|
|
931
|
+
stroked: false,
|
|
932
|
+
}));
|
|
933
|
+
}
|
|
934
|
+
if (clusterDotShapes.length > 0) {
|
|
935
|
+
const clusters = computeClusters(clusterDotShapes, zoom);
|
|
936
|
+
layers.push(new ScatterplotLayer({
|
|
937
|
+
coordinateSystem: COORDINATE_SYSTEM.CARTESIAN,
|
|
938
|
+
id: 'annotation-clusters',
|
|
939
|
+
data: clusters,
|
|
940
|
+
getPosition: d => d.position,
|
|
941
|
+
getFillColor: d => [...d.color, 180],
|
|
942
|
+
getRadius: d => Math.max(6, 4 + Math.sqrt(d.count) * 2),
|
|
943
|
+
radiusUnits: 'pixels',
|
|
944
|
+
filled: true,
|
|
945
|
+
stroked: true,
|
|
946
|
+
getLineColor: d => [...d.color, 220],
|
|
947
|
+
lineWidthMinPixels: 1.5,
|
|
948
|
+
}));
|
|
949
|
+
const multiClusters = clusters.filter(c => c.count > 1);
|
|
950
|
+
if (multiClusters.length > 0) {
|
|
951
|
+
layers.push(new TextLayer({
|
|
952
|
+
coordinateSystem: COORDINATE_SYSTEM.CARTESIAN,
|
|
953
|
+
id: 'annotation-cluster-counts',
|
|
954
|
+
data: multiClusters,
|
|
955
|
+
getPosition: d => d.position,
|
|
956
|
+
getText: d => String(d.count),
|
|
957
|
+
getColor: [255, 255, 255],
|
|
958
|
+
getSize: 10,
|
|
959
|
+
sizeUnits: 'pixels',
|
|
960
|
+
fontWeight: 'bold',
|
|
961
|
+
getTextAnchor: 'middle',
|
|
962
|
+
getAlignmentBaseline: 'center',
|
|
963
|
+
}));
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
// Measurement labels for shapes with showMeasure: true
|
|
967
|
+
const measureLayers = [];
|
|
968
|
+
shapes.forEach((shape) => {
|
|
969
|
+
if (!shape.showMeasure)
|
|
970
|
+
return;
|
|
971
|
+
const mIsSelected = shape.uid === selectedShapeUid;
|
|
972
|
+
const label = getMeasurementLabel(shape, physicalPixelSize, viewType);
|
|
973
|
+
if (!label)
|
|
974
|
+
return;
|
|
975
|
+
const color = shape.strokeColor ?? [255, 255, 255];
|
|
976
|
+
let mShapeOpacity = 1;
|
|
977
|
+
let mLabelFactor = 0;
|
|
978
|
+
if (mIsSelected || !semanticZoom || authoredZoom === null) {
|
|
979
|
+
mLabelFactor = 1;
|
|
980
|
+
}
|
|
981
|
+
else {
|
|
982
|
+
const mZoomRatio = 2 ** (zoom - authoredZoom);
|
|
983
|
+
if (mZoomRatio < RATIO_DOT)
|
|
984
|
+
return;
|
|
985
|
+
if (mZoomRatio < RATIO_GEO_FADE) {
|
|
986
|
+
mShapeOpacity = (mZoomRatio - RATIO_DOT) / (RATIO_GEO_FADE - RATIO_DOT);
|
|
987
|
+
}
|
|
988
|
+
if (mZoomRatio >= RATIO_LABEL_FULL) {
|
|
989
|
+
mLabelFactor = 1;
|
|
990
|
+
}
|
|
991
|
+
else if (mZoomRatio >= RATIO_LABEL) {
|
|
992
|
+
mLabelFactor = (mZoomRatio - RATIO_LABEL) / (RATIO_LABEL_FULL - RATIO_LABEL);
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
const measureOpacity = mShapeOpacity * mLabelFactor;
|
|
996
|
+
if (measureOpacity <= 0)
|
|
997
|
+
return;
|
|
998
|
+
const base = {
|
|
999
|
+
coordinateSystem: COORDINATE_SYSTEM.CARTESIAN,
|
|
1000
|
+
opacity: measureOpacity,
|
|
1001
|
+
getText: d => d.text,
|
|
1002
|
+
getPosition: d => d.position,
|
|
1003
|
+
getColor: color,
|
|
1004
|
+
getSize: 13,
|
|
1005
|
+
sizeUnits: 'pixels',
|
|
1006
|
+
fontFamily: LABEL_FONT_FAMILY,
|
|
1007
|
+
fontWeight: 'bold',
|
|
1008
|
+
// 'auto' builds the font atlas from the actual rendered characters (e.g. µ, ²)
|
|
1009
|
+
// instead of deck.gl's default ASCII-only atlas.
|
|
1010
|
+
characterSet: 'auto',
|
|
1011
|
+
...(shape.measureBackground
|
|
1012
|
+
? {
|
|
1013
|
+
background: true,
|
|
1014
|
+
getBackgroundColor: textBgColor(color),
|
|
1015
|
+
backgroundPadding: [3, 1, 3, 2],
|
|
1016
|
+
}
|
|
1017
|
+
: {}),
|
|
1018
|
+
};
|
|
1019
|
+
if (shape.type === 'line') {
|
|
1020
|
+
const mx = ((shape.x1 ?? 0) + (shape.x2 ?? 0)) / 2;
|
|
1021
|
+
const my = ((shape.y1 ?? 0) + (shape.y2 ?? 0)) / 2;
|
|
1022
|
+
const dx = (shape.x2 ?? 0) - (shape.x1 ?? 0);
|
|
1023
|
+
const dy = (shape.y2 ?? 0) - (shape.y1 ?? 0);
|
|
1024
|
+
const len = Math.sqrt(dx * dx + dy * dy);
|
|
1025
|
+
if (len === 0)
|
|
1026
|
+
return;
|
|
1027
|
+
const perp = pickBelowPerp(dx / len, dy / len);
|
|
1028
|
+
const angle = readableAngle(Math.atan2(dy, dx) * 180 / Math.PI);
|
|
1029
|
+
measureLayers.push(new TextLayer({
|
|
1030
|
+
...base,
|
|
1031
|
+
id: `annotation-measure-${shape.uid}`,
|
|
1032
|
+
data: [{ position: [mx, my], text: label }],
|
|
1033
|
+
getAngle: -angle,
|
|
1034
|
+
getTextAnchor: 'middle',
|
|
1035
|
+
getAlignmentBaseline: 'center',
|
|
1036
|
+
getPixelOffset: [perp[0] * 16, perp[1] * 16],
|
|
1037
|
+
}));
|
|
1038
|
+
}
|
|
1039
|
+
else if (shape.type === 'polyline' && shape.points?.length >= 2) {
|
|
1040
|
+
const pts = shape.points;
|
|
1041
|
+
const mi = Math.floor((pts.length - 1) / 2);
|
|
1042
|
+
const [ax, ay] = pts[mi];
|
|
1043
|
+
const [bx, by] = pts[mi + 1];
|
|
1044
|
+
const dx = bx - ax;
|
|
1045
|
+
const dy = by - ay;
|
|
1046
|
+
const len = Math.sqrt(dx * dx + dy * dy);
|
|
1047
|
+
if (len === 0)
|
|
1048
|
+
return;
|
|
1049
|
+
const perp = pickBelowPerp(dx / len, dy / len);
|
|
1050
|
+
const angle = readableAngle(Math.atan2(dy, dx) * 180 / Math.PI);
|
|
1051
|
+
measureLayers.push(new TextLayer({
|
|
1052
|
+
...base,
|
|
1053
|
+
id: `annotation-measure-${shape.uid}`,
|
|
1054
|
+
data: [{ position: [(ax + bx) / 2, (ay + by) / 2], text: label }],
|
|
1055
|
+
getAngle: -angle,
|
|
1056
|
+
getTextAnchor: 'middle',
|
|
1057
|
+
getAlignmentBaseline: 'center',
|
|
1058
|
+
getPixelOffset: [perp[0] * 16, perp[1] * 16],
|
|
1059
|
+
}));
|
|
1060
|
+
}
|
|
1061
|
+
else if (shape.type === 'rectangle') {
|
|
1062
|
+
// Bottom-right of the actual drawn rect (handles negative width/height)
|
|
1063
|
+
const x0 = shape.x ?? 0;
|
|
1064
|
+
const y0 = shape.y ?? 0;
|
|
1065
|
+
const w = shape.width ?? 0;
|
|
1066
|
+
const h = shape.height ?? 0;
|
|
1067
|
+
const rx = x0 + (w >= 0 ? w : 0);
|
|
1068
|
+
const ry = y0 + (h >= 0 ? h : 0);
|
|
1069
|
+
measureLayers.push(new TextLayer({
|
|
1070
|
+
...base,
|
|
1071
|
+
id: `annotation-measure-${shape.uid}`,
|
|
1072
|
+
data: [{ position: [rx, ry], text: label }],
|
|
1073
|
+
getTextAnchor: 'end',
|
|
1074
|
+
getAlignmentBaseline: 'bottom',
|
|
1075
|
+
getPixelOffset: [-6, -6],
|
|
1076
|
+
}));
|
|
1077
|
+
}
|
|
1078
|
+
else if (shape.type === 'ellipse') {
|
|
1079
|
+
// Just outside the bottom of the ellipse
|
|
1080
|
+
const cx = shape.x1 ?? 0;
|
|
1081
|
+
const cy = shape.y1 ?? 0;
|
|
1082
|
+
const ry = Math.abs(shape.radiusY ?? 0);
|
|
1083
|
+
measureLayers.push(new TextLayer({
|
|
1084
|
+
...base,
|
|
1085
|
+
id: `annotation-measure-${shape.uid}`,
|
|
1086
|
+
data: [{ position: [cx, cy + ry], text: label }],
|
|
1087
|
+
getTextAnchor: 'middle',
|
|
1088
|
+
getAlignmentBaseline: 'top',
|
|
1089
|
+
getPixelOffset: [0, 8],
|
|
1090
|
+
}));
|
|
1091
|
+
}
|
|
1092
|
+
else if (shape.type === 'polygon' && shape.points?.length >= 3) {
|
|
1093
|
+
// Just outside the bottom edge of the bounding box
|
|
1094
|
+
const xs = shape.points.map(p => p[0]);
|
|
1095
|
+
const ys = shape.points.map(p => p[1]);
|
|
1096
|
+
const cx = (Math.min(...xs) + Math.max(...xs)) / 2;
|
|
1097
|
+
const maxY = Math.max(...ys);
|
|
1098
|
+
measureLayers.push(new TextLayer({
|
|
1099
|
+
...base,
|
|
1100
|
+
id: `annotation-measure-${shape.uid}`,
|
|
1101
|
+
data: [{ position: [cx, maxY], text: label }],
|
|
1102
|
+
getTextAnchor: 'middle',
|
|
1103
|
+
getAlignmentBaseline: 'top',
|
|
1104
|
+
getPixelOffset: [0, 8],
|
|
1105
|
+
}));
|
|
1106
|
+
}
|
|
1107
|
+
});
|
|
1108
|
+
return [...selectionLayers, ...layers, ...measureLayers];
|
|
1109
|
+
}
|
|
1110
|
+
//# sourceMappingURL=annotation-layer-utils.js.map
|