@sakuzu/maplibre-gl-draw 2.1.0 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (35) hide show
  1. package/CHANGELOG.md +49 -0
  2. package/dist/dataset/retained.js +19 -18
  3. package/dist/dataset/selection.d.ts +3 -3
  4. package/dist/dataset/selection.js +3 -7
  5. package/dist/dataset/table-source.js +13 -11
  6. package/dist/modes/select/click-handler.d.ts +4 -1
  7. package/dist/modes/select/click-handler.js +17 -9
  8. package/dist/view/layer/custom-layer.js +2 -1
  9. package/dist/view/layer/drape-planner.d.ts +3 -0
  10. package/dist/view/layer/drape-planner.js +9 -7
  11. package/dist/view/layer/frame-render.js +2 -3
  12. package/dist/view/layer/frame-state.d.ts +3 -0
  13. package/dist/view/layer/frame-state.js +2 -1
  14. package/dist/view/layer/gl-state.d.ts +2 -3
  15. package/dist/view/layer/gl-state.js +2 -3
  16. package/dist/view/layer/renderers.js +2 -1
  17. package/dist/view/renderers/line/dash.d.ts +27 -0
  18. package/dist/view/renderers/line/dash.js +23 -12
  19. package/dist/view/shaders/initializer.d.ts +3 -0
  20. package/dist/view/shaders/initializer.js +4 -2
  21. package/dist/view/terrain/anchor.js +20 -1
  22. package/dist/view/terrain/drape/bin-store.d.ts +11 -2
  23. package/dist/view/terrain/drape/bin-store.js +26 -2
  24. package/dist/view/terrain/drape/binning.d.ts +32 -0
  25. package/dist/view/terrain/drape/binning.js +15 -7
  26. package/dist/view/terrain/drape/geometry.d.ts +27 -0
  27. package/dist/view/terrain/drape/geometry.js +57 -6
  28. package/dist/view/terrain/drape/pass.d.ts +8 -3
  29. package/dist/view/terrain/drape/pass.js +39 -28
  30. package/dist/view/terrain/drape/renderer.d.ts +13 -0
  31. package/dist/view/terrain/drape/renderer.js +157 -30
  32. package/dist/view/ui/selection-ui/renderer.d.ts +2 -1
  33. package/dist/view/ui/selection-ui/renderer.js +88 -25
  34. package/dist/view/ui/selection-ui-drawer.js +4 -8
  35. package/package.json +1 -1
@@ -23,7 +23,7 @@ export class ShaderInitializer {
23
23
  * Makes every renderer ensure its shader
24
24
  */
25
25
  ensureShaders(shaderData) {
26
- const { featureDrawer, tentativeRenderer, quadShader, strokeRenderer, pointShapeRenderer, pointInstanceRenderer, sdfLineRenderer, polygonBatchRenderer, sdfPolygonRenderer, } = this.renderers;
26
+ const { featureDrawer, tentativeRenderer, quadShader, strokeRenderer, pointShapeRenderer, pointInstanceRenderer, sdfLineRenderer, polygonBatchRenderer, sdfPolygonRenderer, fillShaderManager, } = this.renderers;
27
27
  if (featureDrawer) {
28
28
  featureDrawer.ensureShader(shaderData);
29
29
  }
@@ -51,6 +51,7 @@ export class ShaderInitializer {
51
51
  if (sdfPolygonRenderer) {
52
52
  sdfPolygonRenderer.ensureShader(shaderData);
53
53
  }
54
+ fillShaderManager?.ensureShader(shaderData);
54
55
  }
55
56
  /**
56
57
  * Sets the projection data on the renderers that need it
@@ -79,7 +80,7 @@ export class ShaderInitializer {
79
80
  * uniforms once per frame and applies them for each slot.
80
81
  */
81
82
  applyOffsetUniforms(uniforms) {
82
- const { featureDrawer, tentativeRenderer, quadShader, strokeRenderer, pointShapeRenderer, pointInstanceRenderer, sdfLineRenderer, polygonBatchRenderer, sdfPolygonRenderer, } = this.renderers;
83
+ const { featureDrawer, tentativeRenderer, quadShader, strokeRenderer, pointShapeRenderer, pointInstanceRenderer, sdfLineRenderer, polygonBatchRenderer, sdfPolygonRenderer, fillShaderManager, } = this.renderers;
83
84
  if (featureDrawer) {
84
85
  featureDrawer.setOffsetUniforms(uniforms);
85
86
  }
@@ -107,6 +108,7 @@ export class ShaderInitializer {
107
108
  if (sdfPolygonRenderer) {
108
109
  sdfPolygonRenderer.setOffsetUniforms(uniforms);
109
110
  }
111
+ fillShaderManager?.setOffsetUniforms(uniforms);
110
112
  }
111
113
  /**
112
114
  * Increments the frame count of SDFLineRenderer
@@ -140,11 +140,30 @@ export function drawnLongitude(context, lng) {
140
140
  * @internal
141
141
  */
142
142
  export function projectAnchor(context, lng, lat) {
143
+ const frame = context.anchorFrame;
144
+ if (!frame.mainMatrix || !frame.terrain)
145
+ return null;
146
+ return projectAnchorAt(context, lng, lat, anchorElevationMeters(context, lng, lat));
147
+ }
148
+ /**
149
+ * Maps a position at a given elevation to screen coordinates, with the projection of
150
+ * {@link projectAnchor}
151
+ *
152
+ * Something drawn flat at one elevation, such as the frame of a selected point at the
153
+ * elevation of the point, is projected with this so that it lands where the shader draws it.
154
+ *
155
+ * @param elevationMeters The elevation in meters (the exaggeration included, as
156
+ * {@link anchorElevationMeters} gives it)
157
+ * @returns null when the terrain is disabled, the frame state has not been established or the
158
+ * position is behind the camera
159
+ * @internal
160
+ */
161
+ export function projectAnchorAt(context, lng, lat, elevationMeters) {
143
162
  const frame = context.anchorFrame;
144
163
  const { mainMatrix: m, offsetUniforms, terrain, width, height } = frame;
145
164
  if (!m || !terrain || width <= 0 || height <= 0)
146
165
  return null;
147
- const elevationMercator = anchorElevationMeters(context, lng, lat) * frame.elevationScale;
166
+ const elevationMercator = elevationMeters * frame.elevationScale;
148
167
  // The anchor is projected on the copy of the world it is drawn on
149
168
  const drawnLng = drawnLongitude(context, lng);
150
169
  let clipX;
@@ -19,16 +19,23 @@
19
19
  */
20
20
  import type { DrapeElement, DrapeQuadBreak, DrapeTile, TileBins } from './binning.js';
21
21
  /** The number of style texels per element */
22
- export declare const DRAPE_STYLE_TEXELS = 4;
22
+ export declare const DRAPE_STYLE_TEXELS = 5;
23
23
  /**
24
24
  * The index of the texel the selection highlight uses within the style table
25
25
  *
26
26
  * 0 = fill, 1 = stroke, 2 = specifications (width, kind, source, reference zoom), 3 =
27
- * selection. The selection alone is put in a separate texel so that the index (the binning)
27
+ * selection, 4 = the dash pattern (`DRAPE_DASH_TEXEL`). The selection alone is put in a separate texel so that the index (the binning)
28
28
  * need not be touched at all when the selection changes. Mixing it into the same texel as the
29
29
  * color or the width would mean rebuilding the other values too on every rewrite.
30
30
  */
31
31
  export declare const DRAPE_SELECTION_TEXEL = 3;
32
+ /**
33
+ * The index of the texel of the dash pattern within the style table
34
+ *
35
+ * (a, b, c, pixel ratio): the coefficients of the pattern (`DrapeDash` in `binning.ts`) and the
36
+ * physical pixels of the width per CSS pixel. A pixel ratio of 0 marks a solid outline.
37
+ */
38
+ export declare const DRAPE_DASH_TEXEL = 4;
32
39
  /** The upper bound on the number of edge texels (16 bytes per texel, so 1M is 16MB) */
33
40
  export declare const DRAPE_MAX_EDGE_TEXELS = 1048576;
34
41
  /** The key of a tile */
@@ -57,6 +64,8 @@ export declare class DrapePackBuilder {
57
64
  * transferred) */
58
65
  readonly id: number;
59
66
  edges: Float32Array<ArrayBuffer>;
67
+ /** Where each edge starts along its path (one number per edge, in step with `edges`) */
68
+ edgeStarts: Float32Array<ArrayBuffer>;
60
69
  edgeCount: number;
61
70
  runs: Float32Array<ArrayBuffer>;
62
71
  runCount: number;
@@ -2,16 +2,23 @@
2
2
  // SPDX-License-Identifier: AGPL-3.0-only
3
3
  import { binTile, drapeCellWorkBudget } from './binning.js';
4
4
  /** The number of style texels per element */
5
- export const DRAPE_STYLE_TEXELS = 4;
5
+ export const DRAPE_STYLE_TEXELS = 5;
6
6
  /**
7
7
  * The index of the texel the selection highlight uses within the style table
8
8
  *
9
9
  * 0 = fill, 1 = stroke, 2 = specifications (width, kind, source, reference zoom), 3 =
10
- * selection. The selection alone is put in a separate texel so that the index (the binning)
10
+ * selection, 4 = the dash pattern (`DRAPE_DASH_TEXEL`). The selection alone is put in a separate texel so that the index (the binning)
11
11
  * need not be touched at all when the selection changes. Mixing it into the same texel as the
12
12
  * color or the width would mean rebuilding the other values too on every rewrite.
13
13
  */
14
14
  export const DRAPE_SELECTION_TEXEL = 3;
15
+ /**
16
+ * The index of the texel of the dash pattern within the style table
17
+ *
18
+ * (a, b, c, pixel ratio): the coefficients of the pattern (`DrapeDash` in `binning.ts`) and the
19
+ * physical pixels of the width per CSS pixel. A pixel ratio of 0 marks a solid outline.
20
+ */
21
+ export const DRAPE_DASH_TEXEL = 4;
15
22
  /** The upper bound on the tiles retained (beyond it, those outside the view are dropped and
16
23
  * the arrays are repacked) */
17
24
  const MAX_CACHED_TILES = 96;
@@ -43,6 +50,13 @@ export class DrapePackBuilder {
43
50
  writable: true,
44
51
  value: new Float32Array(4 * 4096)
45
52
  });
53
+ /** Where each edge starts along its path (one number per edge, in step with `edges`) */
54
+ Object.defineProperty(this, "edgeStarts", {
55
+ enumerable: true,
56
+ configurable: true,
57
+ writable: true,
58
+ value: new Float32Array(4096)
59
+ });
46
60
  Object.defineProperty(this, "edgeCount", {
47
61
  enumerable: true,
48
62
  configurable: true,
@@ -125,6 +139,14 @@ export class DrapePackBuilder {
125
139
  styles[at + 9] = element.kind;
126
140
  styles[at + 10] = element.source;
127
141
  styles[at + 11] = element.widthZoom;
142
+ const dash = element.dash;
143
+ if (dash) {
144
+ const dashAt = at + DRAPE_DASH_TEXEL * 4;
145
+ styles[dashAt] = dash.a;
146
+ styles[dashAt + 1] = dash.b;
147
+ styles[dashAt + 2] = dash.c;
148
+ styles[dashAt + 3] = dash.pixelRatio;
149
+ }
128
150
  }
129
151
  this.styles = styles;
130
152
  this.elementCount = elements.length;
@@ -167,6 +189,8 @@ export class DrapePackBuilder {
167
189
  const edgeTexels = bins.edges.length / 4;
168
190
  this.edges = ensureCapacity(this.edges, (this.edgeCount + edgeTexels) * 4);
169
191
  this.edges.set(bins.edges, edgeBase * 4);
192
+ this.edgeStarts = ensureCapacity(this.edgeStarts, this.edgeCount + edgeTexels);
193
+ this.edgeStarts.set(bins.edgeStarts, edgeBase);
170
194
  this.edgeCount += edgeTexels;
171
195
  const runTexels = bins.runs.length / 4;
172
196
  this.runs = ensureCapacity(this.runs, (this.runCount + runTexels) * 4);
@@ -109,6 +109,26 @@ export declare function drapeCellWorkBudget(tileCount: number): number;
109
109
  export declare const DRAPE_LOD_TRIGGER = 16000;
110
110
  /** The color (0..1, with the opacity already folded in) */
111
111
  export type DrapeColor = readonly [number, number, number, number];
112
+ /**
113
+ * The dash pattern of an outline that is not solid
114
+ *
115
+ * The coefficients are those of `DASH_COEFFICIENTS` in `view/renderers/line/dash.ts`: the shader
116
+ * works the dash and the gap out of the width the line has in the frame, with the same formulas
117
+ * as the subdividing path.
118
+ */
119
+ export interface DrapeDash {
120
+ /** The shortest dash (CSS px) */
121
+ readonly a: number;
122
+ /** The dash per pixel of width */
123
+ readonly b: number;
124
+ /** The gap per pixel of width */
125
+ readonly c: number;
126
+ /**
127
+ * The physical pixels of `strokeWidthPx` per CSS pixel (the pixel ratio the width was
128
+ * resolved with). The pattern is measured in CSS pixels, so the shader divides it back out
129
+ */
130
+ readonly pixelRatio: number;
131
+ }
112
132
  /**
113
133
  * An element handed to the analytic drape (a feature of the Store and a feature of a
114
134
  * dataset have the same shape)
@@ -131,6 +151,13 @@ export interface DrapeElement {
131
151
  readonly stroke: DrapeColor;
132
152
  /** The width of the outline (physical pixels). 0 means the outline is not drawn */
133
153
  readonly strokeWidthPx: number;
154
+ /**
155
+ * The dash pattern of the outline (absent or null for a solid outline)
156
+ *
157
+ * Only an element with a pattern has the lengths along its paths worked out and binned next
158
+ * to its edges (`TileBins.edgeStarts`).
159
+ */
160
+ readonly dash?: DrapeDash | null;
134
161
  /**
135
162
  * The origin of the coefficients (0 = the Store, 1 and above = datasets)
136
163
  *
@@ -201,6 +228,11 @@ export interface TileBins {
201
228
  readonly runs: Float32Array;
202
229
  /** (x0, y0, x1, y1) per edge. In-tile 0..1 coordinates */
203
230
  readonly edges: Float32Array;
231
+ /**
232
+ * Where each edge starts along its path, one per edge (Mercator units, the 0..1 world; not
233
+ * in-tile). 0 for an edge of an element without a dash pattern
234
+ */
235
+ readonly edgeStarts: Float32Array;
204
236
  /** Whether a limit was exceeded (if it was, the caller degrades) */
205
237
  readonly overflow: boolean;
206
238
  /** The number of cells truncated for exceeding the budget (a local degradation. Diagnostic) */
@@ -1,6 +1,6 @@
1
1
  // SPDX-FileCopyrightText: 2026 SAKAIDA Atsushi
2
2
  // SPDX-License-Identifier: AGPL-3.0-only
3
- import { DRAPE_BLOCK_EDGES, drapeQuantizedGeometry } from './geometry.js';
3
+ import { DRAPE_BLOCK_EDGES, drapePathStarts, drapeQuantizedGeometry } from './geometry.js';
4
4
  /** The minimum number of divisions of the grid (per side) */
5
5
  export const DRAPE_MIN_CELLS = 8;
6
6
  /** The maximum number of divisions of the grid (per side) */
@@ -245,6 +245,7 @@ function emptyBins(grid, edgeEstimate, overflow) {
245
245
  cells: new Int32Array(grid * grid * 2),
246
246
  runs: new Float32Array(0),
247
247
  edges: new Float32Array(0),
248
+ edgeStarts: new Float32Array(0),
248
249
  overflow,
249
250
  truncatedCells: 0,
250
251
  cellWork: 0,
@@ -264,6 +265,8 @@ function assembleBins(elements, picked, tile, scale, grid, zoom) {
264
265
  const cells = new Int32Array(cellCount * 2);
265
266
  /** The edges in in-tile coordinates (four numbers per edge). Stacked across elements */
266
267
  const edgeXY = [];
268
+ /** Where each edge starts along its path (one number per edge) */
269
+ const edgeS = [];
267
270
  /** The runs per cell. Triples of (elementIndex, inside, edgeListIndex) laid out in a row */
268
271
  const cellRuns = new Array(cellCount);
269
272
  /** The lists of edge indices that the runs point to */
@@ -299,8 +302,9 @@ function assembleBins(elements, picked, tile, scale, grid, zoom) {
299
302
  scratch.clear();
300
303
  touchedRows.length = 0;
301
304
  rowTouched.fill(0);
305
+ const dashed = element.dash != null;
302
306
  for (const path of picked.geometries[p].paths) {
303
- scanPath(path, element.kind, tileX, tileY, scale, margin, grid, cellSize, edgeXY, scratch, rowDiff, rowTouched, touchedRows);
307
+ scanPath(path, element.kind, tileX, tileY, scale, margin, grid, cellSize, { xy: edgeXY, starts: edgeS }, dashed ? drapePathStarts(path) : null, scratch, rowDiff, rowTouched, touchedRows);
304
308
  }
305
309
  // Turn the cells that are inside and the cells the edges touch into runs (they are stacked
306
310
  // in the order of the elements, so this comes out as the draw order)
@@ -328,6 +332,7 @@ function assembleBins(elements, picked, tile, scale, grid, zoom) {
328
332
  // Repacking (laid out in the order cell -> run -> edge)
329
333
  const runs = [];
330
334
  const edges = [];
335
+ const edgeStarts = [];
331
336
  for (let c = 0; c < cellCount; c++) {
332
337
  const list = cellRuns[c];
333
338
  cells[c * 2] = runs.length / 4;
@@ -352,6 +357,7 @@ function assembleBins(elements, picked, tile, scale, grid, zoom) {
352
357
  break;
353
358
  }
354
359
  edges.push(edgeXY[at * 4], edgeXY[at * 4 + 1], edgeXY[at * 4 + 2], edgeXY[at * 4 + 3]);
360
+ edgeStarts.push(edgeS[at]);
355
361
  edgeCount++;
356
362
  }
357
363
  }
@@ -371,6 +377,7 @@ function assembleBins(elements, picked, tile, scale, grid, zoom) {
371
377
  cells,
372
378
  runs: new Float32Array(runs),
373
379
  edges: new Float32Array(edges),
380
+ edgeStarts: new Float32Array(edgeStarts),
374
381
  overflow: false,
375
382
  truncatedCells,
376
383
  cellWork: (runs.length / 4 + edges.length / 4) / cellCount,
@@ -430,7 +437,7 @@ function pickElements(elements, candidates, tile, scale, quantizeLevel, zoom) {
430
437
  * right of the origin, so an edge outside the tile on the right also counts (it becomes a flip
431
438
  * of a whole row). An edge outside on the left flips nothing, so it can be dropped.
432
439
  */
433
- function scanPath(path, kind, tileX, tileY, scale, margin, grid, cellSize, edgeXY, scratch, rowDiff, rowTouched, touchedRows) {
440
+ function scanPath(path, kind, tileX, tileY, scale, margin, grid, cellSize, out, starts, scratch, rowDiff, rowTouched, touchedRows) {
434
441
  const xy = path.xy;
435
442
  const vertexCount = xy.length / 2;
436
443
  const blockCount = path.blocks.length / 4;
@@ -458,7 +465,7 @@ function scanPath(path, kind, tileX, tileY, scale, margin, grid, cellSize, edgeX
458
465
  if (kind === 0) {
459
466
  accumulateInside(x0, y0, x1, y1, grid, cellSize, rowDiff, rowTouched, touchedRows);
460
467
  }
461
- scatterEdge(x0, y0, x1, y1, margin, grid, cellSize, edgeXY, scratch);
468
+ scatterEdge(x0, y0, x1, y1, starts ? starts[e] : 0, margin, grid, cellSize, out, scratch);
462
469
  }
463
470
  }
464
471
  }
@@ -511,7 +518,7 @@ function accumulateInside(x0, y0, x1, y1, grid, cellSize, rowDiff, rowTouched, t
511
518
  * that crosses the tile diagonally. The segment is cut per row to obtain the range of x, and
512
519
  * only the columns in that range are touched.
513
520
  */
514
- function scatterEdge(x0, y0, x1, y1, margin, grid, cellSize, edgeXY, scratch) {
521
+ function scatterEdge(x0, y0, x1, y1, start, margin, grid, cellSize, out, scratch) {
515
522
  const exLo = Math.min(x0, x1) - margin;
516
523
  const exHi = Math.max(x0, x1) + margin;
517
524
  const eyLo = Math.min(y0, y1) - margin;
@@ -526,8 +533,9 @@ function scatterEdge(x0, y0, x1, y1, margin, grid, cellSize, edgeXY, scratch) {
526
533
  jHi = grid - 1;
527
534
  if (jLo > jHi)
528
535
  return;
529
- const at = edgeXY.length / 4;
530
- edgeXY.push(x0, y0, x1, y1);
536
+ const at = out.starts.length;
537
+ out.xy.push(x0, y0, x1, y1);
538
+ out.starts.push(start);
531
539
  const horizontal = y0 === y1;
532
540
  const inv = horizontal ? 0 : 1 / (y1 - y0);
533
541
  for (let j = jLo; j <= jHi; j++) {
@@ -29,6 +29,20 @@ export interface DrapeGeometryPath {
29
29
  readonly closed: boolean;
30
30
  /** The bounding box of each block (minX, minY, maxX, maxY) */
31
31
  readonly blocks: Float64Array;
32
+ /**
33
+ * Where the vertices of a quantized path came from (absent on a path of the original
34
+ * geometry)
35
+ *
36
+ * A dashed line keeps the pattern of the original path on a coarse tile: the length along
37
+ * the path at a vertex is that of the original vertex it was rounded from
38
+ * ({@link drapePathStarts}).
39
+ */
40
+ readonly origin?: DrapePathOrigin;
41
+ }
42
+ /** The original path of a quantized path, and the original vertex of each of its vertices */
43
+ export interface DrapePathOrigin {
44
+ readonly path: DrapeGeometryPath;
45
+ readonly vertices: Int32Array;
32
46
  }
33
47
  /** The geometry of a single feature */
34
48
  export interface DrapeGeometry {
@@ -93,3 +107,16 @@ export declare function buildDrapeGeometry(paths: ReadonlyArray<ReadonlyArray<Co
93
107
  * @param level The fineness of the grid. The grid spacing is 2^-level (in Mercator units)
94
108
  */
95
109
  export declare function drapeQuantizedGeometry(geometry: DrapeGeometry, level: number): DrapeGeometry;
110
+ /**
111
+ * The length along a path from its first vertex to each of its vertices (Mercator units)
112
+ *
113
+ * Entry `e` is where edge `e` starts on the path, so a dashed line can place its pattern on
114
+ * every edge on its own (on a closed path the last edge, back to the first vertex, starts at
115
+ * the last vertex). It is built only for dashed lines and polygons with a dashed outline, the
116
+ * first time a tile needs it, so a solid line pays nothing for it.
117
+ *
118
+ * A quantized path takes the lengths of the original vertices it was rounded from, so the
119
+ * pattern on a coarse tile stays in step with the pattern on the fine tiles next to it. A
120
+ * square standing in for a collapsed polygon has no original and measures itself.
121
+ */
122
+ export declare function drapePathStarts(path: DrapeGeometryPath): Float64Array;
@@ -103,15 +103,18 @@ export function buildDrapeGeometry(paths, closed) {
103
103
  }
104
104
  /**
105
105
  * Assembles the geometry from paths in Mercator coordinates
106
+ *
107
+ * @param origins The origin of each path, for quantized geometry (null for a path that has none)
106
108
  */
107
- function buildFromMercatorPaths(paths, closed) {
109
+ function buildFromMercatorPaths(paths, closed, origins) {
108
110
  const out = [];
109
111
  let minX = Number.POSITIVE_INFINITY;
110
112
  let minY = Number.POSITIVE_INFINITY;
111
113
  let maxX = Number.NEGATIVE_INFINITY;
112
114
  let maxY = Number.NEGATIVE_INFINITY;
113
115
  let edgeCount = 0;
114
- for (const xy of paths) {
116
+ for (let p = 0; p < paths.length; p++) {
117
+ const xy = paths[p];
115
118
  const count = xy.length / 2;
116
119
  if (count < 2)
117
120
  continue;
@@ -156,7 +159,10 @@ function buildFromMercatorPaths(paths, closed) {
156
159
  blocks[b * 4 + 2] = bx1;
157
160
  blocks[b * 4 + 3] = by1;
158
161
  }
159
- out.push({ xy, edgeCount: edges, closed, blocks });
162
+ const origin = origins?.[p];
163
+ out.push(origin
164
+ ? { xy, edgeCount: edges, closed, blocks, origin }
165
+ : { xy, edgeCount: edges, closed, blocks });
160
166
  edgeCount += edges;
161
167
  }
162
168
  if (out.length === 0) {
@@ -193,10 +199,13 @@ export function drapeQuantizedGeometry(geometry, level) {
193
199
  return hit;
194
200
  const step = 2 ** -level;
195
201
  const paths = [];
202
+ const origins = [];
196
203
  for (const path of geometry.paths) {
197
204
  const source = path.xy;
198
205
  const count = source.length / 2;
199
206
  const out = [];
207
+ /** The original vertex of each vertex kept */
208
+ const kept = [];
200
209
  let lastX = Number.NaN;
201
210
  let lastY = Number.NaN;
202
211
  for (let i = 0; i < count; i++) {
@@ -205,6 +214,7 @@ export function drapeQuantizedGeometry(geometry, level) {
205
214
  if (qx === lastX && qy === lastY)
206
215
  continue;
207
216
  out.push(qx, qy);
217
+ kept.push(i);
208
218
  lastX = qx;
209
219
  lastY = qy;
210
220
  }
@@ -213,16 +223,20 @@ export function drapeQuantizedGeometry(geometry, level) {
213
223
  // dropped
214
224
  while (out.length >= 6 && out[out.length - 2] === out[0] && out[out.length - 1] === out[1]) {
215
225
  out.length -= 2;
226
+ kept.length -= 1;
216
227
  }
217
228
  if (out.length / 2 < 3) {
218
229
  // A polygon collapsed by the rounding is replaced with a square of one grid cell.
219
230
  // Dropping it would leave the fill missing just there and produce spotty white
220
231
  // blotches (confirmed hands-on, the city center at z8.7; it was pronounced in areas
221
232
  // where small polygons at the chome level line up).
222
- // The error stays within one grid cell (1/256 of a tile = about 2 pixels on screen)
233
+ // The error stays within one grid cell (1/256 of a tile = about 2 pixels on screen).
234
+ // It has no origin: a dash pattern on it would be smaller than a pixel anyway
223
235
  const box = squareAt(source, step);
224
- if (box)
236
+ if (box) {
225
237
  paths.push(box);
238
+ origins.push(null);
239
+ }
226
240
  continue;
227
241
  }
228
242
  }
@@ -230,8 +244,9 @@ export function drapeQuantizedGeometry(geometry, level) {
230
244
  continue;
231
245
  }
232
246
  paths.push(new Float64Array(out));
247
+ origins.push({ path, vertices: new Int32Array(kept) });
233
248
  }
234
- const built = buildFromMercatorPaths(paths, geometry.paths[0]?.closed ?? true);
249
+ const built = buildFromMercatorPaths(paths, geometry.paths[0]?.closed ?? true, origins);
235
250
  cache.set(level, built);
236
251
  return built;
237
252
  }
@@ -258,3 +273,39 @@ function squareAt(source, step) {
258
273
  const cy = Math.round(sy / count / step) * step;
259
274
  return new Float64Array([cx, cy, cx + step, cy, cx + step, cy + step, cx, cy + step]);
260
275
  }
276
+ /** The lengths along the paths (path -> the length at each vertex) */
277
+ const startsCache = new WeakMap();
278
+ /**
279
+ * The length along a path from its first vertex to each of its vertices (Mercator units)
280
+ *
281
+ * Entry `e` is where edge `e` starts on the path, so a dashed line can place its pattern on
282
+ * every edge on its own (on a closed path the last edge, back to the first vertex, starts at
283
+ * the last vertex). It is built only for dashed lines and polygons with a dashed outline, the
284
+ * first time a tile needs it, so a solid line pays nothing for it.
285
+ *
286
+ * A quantized path takes the lengths of the original vertices it was rounded from, so the
287
+ * pattern on a coarse tile stays in step with the pattern on the fine tiles next to it. A
288
+ * square standing in for a collapsed polygon has no original and measures itself.
289
+ */
290
+ export function drapePathStarts(path) {
291
+ const hit = startsCache.get(path);
292
+ if (hit)
293
+ return hit;
294
+ const count = path.xy.length / 2;
295
+ const starts = new Float64Array(count);
296
+ if (path.origin) {
297
+ const original = drapePathStarts(path.origin.path);
298
+ for (let i = 0; i < count; i++)
299
+ starts[i] = original[path.origin.vertices[i]];
300
+ }
301
+ else {
302
+ const xy = path.xy;
303
+ let total = 0;
304
+ for (let i = 1; i < count; i++) {
305
+ total += Math.hypot(xy[i * 2] - xy[i * 2 - 2], xy[i * 2 + 1] - xy[i * 2 - 1]);
306
+ starts[i] = total;
307
+ }
308
+ }
309
+ startsCache.set(path, starts);
310
+ return starts;
311
+ }
@@ -25,8 +25,8 @@ export declare const DRAPE_MAX_VERTICES = 4000000;
25
25
  export interface DrapeCollectResult {
26
26
  /** The elements in draw order */
27
27
  readonly elements: DrapeElement[];
28
- /** The IDs of the Store features that cannot be put on the drape (points, images, dashed
29
- * lines and so on) */
28
+ /** The IDs of the Store features that cannot be put on the drape (points, images and so
29
+ * on) */
30
30
  readonly excluded: Set<string>;
31
31
  /** The breaks in the stacking order (the positions of the images; dataset order =
32
32
  * ascending afterElements) */
@@ -98,8 +98,13 @@ export interface DrapeDatasetSource {
98
98
  * It is not narrowed down by the view. Narrowing it down would change the element order every
99
99
  * time the view moves and require rebuilding the tile indices (the narrowing down within a
100
100
  * tile is done by `binTile` using the bounding boxes).
101
+ *
102
+ * @param pixelRatio The rendering pixel ratio, for widths fixed in screen pixels (the rendering
103
+ * scale included; `resolvePixelRatio`)
104
+ * @param contentPixelRatio The rendering pixel ratio for widths that follow the zoom (the
105
+ * rendering scale not applied; `resolveContentPixelRatio`)
101
106
  */
102
- export declare function collectDrapeElements(store: Store, styles: RetainedStyleResolver, pixelRatio: number, datasets: readonly DrapeDatasetSource[]): DrapeCollectResult;
107
+ export declare function collectDrapeElements(store: Store, styles: RetainedStyleResolver, pixelRatio: number, contentPixelRatio: number, datasets: readonly DrapeDatasetSource[]): DrapeCollectResult;
103
108
  /**
104
109
  * Whether the analytic drape may be used
105
110
  *