roamwright 0.61.16 → 0.61.17
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/README.md +25 -24
- package/examples/world-map.html +162 -0
- package/examples/world-map.js +475 -0
- package/index.js +67 -0
- package/package.json +1 -1
- package/rendering/map-glyphs.js +494 -0
- package/rendering/map-input.js +143 -0
- package/rendering/map-labels.js +181 -0
- package/rendering/map-relief.js +330 -0
- package/rendering/map-signs.js +124 -0
- package/rendering/place-names.js +408 -0
- package/rendering/portrait-studio.js +213 -0
- package/rendering/screen-pick.js +75 -0
- package/runtime/build-freshness.js +133 -0
- package/runtime/compass-strip.js +232 -0
- package/runtime/context-lost.js +108 -0
- package/runtime/doorways.js +98 -0
- package/runtime/hud-frame.js +74 -0
- package/runtime/map-relief.js +236 -0
- package/runtime/map-spot.js +120 -0
- package/runtime/map-view.js +229 -0
- package/runtime/panel-shell.js +90 -0
- package/runtime/relation-graph.js +163 -0
- package/runtime/stage-frame.js +263 -0
- package/runtime/url-router.js +162 -0
- package/runtime/way-back.js +71 -0
- package/runtime/whereabouts.js +210 -0
- package/runtime/world-controls.js +265 -0
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cartographic 2D canvas glyphs, lettering, compass rose, scale bar, and sheet notes shared by
|
|
3
|
+
* world sheets, land sheets, mini-maps, legends, and gazetteers.
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { TAU } from '../math/scalar.js';
|
|
7
|
+
|
|
8
|
+
/** Default serif stack for cartographic lettering. */
|
|
9
|
+
export const DEFAULT_MAP_FONT = '"Iowan Old Style","Palatino Linotype",Palatino,Georgia,serif';
|
|
10
|
+
|
|
11
|
+
/** Shared cartographic palette constants. */
|
|
12
|
+
export const MAP_COLORS = Object.freeze({
|
|
13
|
+
ink: '#10181c',
|
|
14
|
+
paper: '#f3ecd9',
|
|
15
|
+
accent: '#ffb25a',
|
|
16
|
+
stone: '#d6cbb0',
|
|
17
|
+
doorLight: '#ffc27a',
|
|
18
|
+
reliefInk: 'rgba(62,46,26,.8)',
|
|
19
|
+
duneInk: 'rgba(120,86,40,.75)',
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const DIAMOND = [0, -5, 5, 0, 0, 5, -5, 0];
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Traces a closed polygon path from flat `[x0, y0, x1, y1, ...]` coordinates.
|
|
26
|
+
* @param {CanvasRenderingContext2D} ctx
|
|
27
|
+
* @param {ReadonlyArray<number>} pts
|
|
28
|
+
*/
|
|
29
|
+
export function drawMapPoly(ctx, pts) {
|
|
30
|
+
ctx.beginPath();
|
|
31
|
+
for (let i = 0; i < pts.length; i += 2) ctx.lineTo(pts[i], pts[i + 1]);
|
|
32
|
+
ctx.closePath();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Draws a filled and stroked disc centred at the origin.
|
|
37
|
+
* @param {CanvasRenderingContext2D} ctx
|
|
38
|
+
* @param {number} radius
|
|
39
|
+
* @param {string} fill
|
|
40
|
+
* @param {string} stroke
|
|
41
|
+
* @param {number} lineWidth
|
|
42
|
+
*/
|
|
43
|
+
export function drawMapDisc(ctx, radius, fill, stroke, lineWidth) {
|
|
44
|
+
ctx.fillStyle = fill;
|
|
45
|
+
ctx.strokeStyle = stroke;
|
|
46
|
+
ctx.lineWidth = lineWidth;
|
|
47
|
+
ctx.beginPath();
|
|
48
|
+
ctx.arc(0, 0, radius, 0, TAU);
|
|
49
|
+
ctx.fill();
|
|
50
|
+
ctx.stroke();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Traces an arch of half-width `halfWidth` standing on `y = foot` with straight jambs and a round head.
|
|
55
|
+
* @param {CanvasRenderingContext2D} ctx
|
|
56
|
+
* @param {number} halfWidth
|
|
57
|
+
* @param {number} foot
|
|
58
|
+
*/
|
|
59
|
+
export function drawMapArch(ctx, halfWidth, foot) {
|
|
60
|
+
const spring = foot - halfWidth * 1.1;
|
|
61
|
+
ctx.beginPath();
|
|
62
|
+
ctx.moveTo(-halfWidth, foot);
|
|
63
|
+
ctx.lineTo(-halfWidth, spring);
|
|
64
|
+
ctx.arc(0, spring, halfWidth, Math.PI, 0);
|
|
65
|
+
ctx.lineTo(halfWidth, foot);
|
|
66
|
+
ctx.closePath();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Draws one shaded mountain peak with a lit west slope, shaded east slope, and ridge outline.
|
|
71
|
+
* @param {CanvasRenderingContext2D} ctx
|
|
72
|
+
* @param {number} x Base centre X.
|
|
73
|
+
* @param {number} y Base Y.
|
|
74
|
+
* @param {number} size Half-width of the peak.
|
|
75
|
+
*/
|
|
76
|
+
export function drawPeak(ctx, x, y, size) {
|
|
77
|
+
ctx.fillStyle = 'rgba(238,228,196,.75)';
|
|
78
|
+
drawMapPoly(ctx, [x - size, y, x - 0.1 * size, y - 1.25 * size, x + 0.15 * size, y - 0.5 * size, x - 0.1 * size, y]);
|
|
79
|
+
ctx.fill();
|
|
80
|
+
ctx.fillStyle = 'rgba(74,56,34,.62)';
|
|
81
|
+
drawMapPoly(ctx, [x - 0.1 * size, y - 1.25 * size, x + size, y, x - 0.1 * size, y, x + 0.15 * size, y - 0.5 * size]);
|
|
82
|
+
ctx.fill();
|
|
83
|
+
ctx.beginPath();
|
|
84
|
+
ctx.moveTo(x - size, y);
|
|
85
|
+
ctx.lineTo(x - 0.1 * size, y - 1.25 * size);
|
|
86
|
+
ctx.lineTo(x + size, y);
|
|
87
|
+
ctx.stroke();
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Draws one forest tree crown with darker underside shading.
|
|
92
|
+
* @param {CanvasRenderingContext2D} ctx
|
|
93
|
+
* @param {number} x Trunk base X.
|
|
94
|
+
* @param {number} y Trunk base Y.
|
|
95
|
+
* @param {number} size Crown radius.
|
|
96
|
+
*/
|
|
97
|
+
export function drawTree(ctx, x, y, size) {
|
|
98
|
+
ctx.fillStyle = 'rgba(64,92,52,.9)';
|
|
99
|
+
ctx.beginPath();
|
|
100
|
+
ctx.arc(x, y - size, size, 0, TAU);
|
|
101
|
+
ctx.fill();
|
|
102
|
+
ctx.fillStyle = 'rgba(36,54,32,.55)';
|
|
103
|
+
ctx.beginPath();
|
|
104
|
+
ctx.arc(x, y - size, size, 0.15, Math.PI - 0.55);
|
|
105
|
+
ctx.fill();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Draws one desert dune crest with wind-blown sand dots in its lee.
|
|
110
|
+
* @param {CanvasRenderingContext2D} ctx
|
|
111
|
+
* @param {number} x Centre X.
|
|
112
|
+
* @param {number} y Base Y.
|
|
113
|
+
* @param {number} size Half-width.
|
|
114
|
+
*/
|
|
115
|
+
export function drawDune(ctx, x, y, size) {
|
|
116
|
+
ctx.beginPath();
|
|
117
|
+
ctx.moveTo(x - size, y);
|
|
118
|
+
ctx.quadraticCurveTo(x - 0.1 * size, y - 0.9 * size, x + size, y);
|
|
119
|
+
ctx.stroke();
|
|
120
|
+
ctx.fillRect(x - 0.2 * size, y + 0.3 * size, 0.14 * size, 0.14 * size);
|
|
121
|
+
ctx.fillRect(x + 0.45 * size, y + 0.5 * size, 0.14 * size, 0.14 * size);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Built-in cartographic glyph drawers keyed by symbol id, each painting in a nominal `[-10, 10]` local box.
|
|
126
|
+
* Worlds may pass additional glyphs via `extraGlyphs` in `drawMapGlyph`.
|
|
127
|
+
* @type {Record<string, (ctx: CanvasRenderingContext2D, font: string) => void>}
|
|
128
|
+
*/
|
|
129
|
+
export const MAP_GLYPHS = {
|
|
130
|
+
cities(ctx) {
|
|
131
|
+
ctx.fillStyle = MAP_COLORS.paper;
|
|
132
|
+
ctx.strokeStyle = MAP_COLORS.ink;
|
|
133
|
+
ctx.lineWidth = 1.6;
|
|
134
|
+
ctx.fillRect(-5, -5, 10, 10);
|
|
135
|
+
ctx.strokeRect(-5, -5, 10, 10);
|
|
136
|
+
ctx.fillStyle = MAP_COLORS.ink;
|
|
137
|
+
ctx.fillRect(-2, -2, 4, 4);
|
|
138
|
+
},
|
|
139
|
+
landmarks(ctx) {
|
|
140
|
+
ctx.fillStyle = MAP_COLORS.paper;
|
|
141
|
+
ctx.strokeStyle = MAP_COLORS.ink;
|
|
142
|
+
ctx.lineWidth = 1.5;
|
|
143
|
+
drawMapPoly(ctx, DIAMOND);
|
|
144
|
+
ctx.fill();
|
|
145
|
+
ctx.stroke();
|
|
146
|
+
},
|
|
147
|
+
enter(ctx) {
|
|
148
|
+
const blur = 6 * Math.hypot(ctx.getTransform().a, ctx.getTransform().b);
|
|
149
|
+
drawMapArch(ctx, 6.2, 5.5);
|
|
150
|
+
ctx.fillStyle = '#1c130b';
|
|
151
|
+
ctx.fill();
|
|
152
|
+
ctx.shadowColor = 'rgba(255,186,104,.95)';
|
|
153
|
+
ctx.shadowBlur = blur;
|
|
154
|
+
ctx.strokeStyle = MAP_COLORS.doorLight;
|
|
155
|
+
ctx.lineWidth = 1.7;
|
|
156
|
+
ctx.stroke();
|
|
157
|
+
ctx.shadowBlur = 0;
|
|
158
|
+
drawMapArch(ctx, 2.6, 3.9);
|
|
159
|
+
ctx.fillStyle = '#ffd59a';
|
|
160
|
+
ctx.fill();
|
|
161
|
+
},
|
|
162
|
+
scenes: ctx => drawMapDisc(ctx, 6, 'rgba(20,12,4,.75)', MAP_COLORS.accent, 2),
|
|
163
|
+
waypoint(ctx) {
|
|
164
|
+
ctx.strokeStyle = MAP_COLORS.accent;
|
|
165
|
+
ctx.lineWidth = 1.4;
|
|
166
|
+
ctx.setLineDash([3, 3]);
|
|
167
|
+
ctx.beginPath();
|
|
168
|
+
ctx.moveTo(-9, 7);
|
|
169
|
+
ctx.lineTo(0, 0);
|
|
170
|
+
ctx.stroke();
|
|
171
|
+
ctx.setLineDash([]);
|
|
172
|
+
ctx.fillStyle = MAP_COLORS.accent;
|
|
173
|
+
drawMapPoly(ctx, [0, -6, 6, 0, 0, 6, -6, 0]);
|
|
174
|
+
ctx.fill();
|
|
175
|
+
},
|
|
176
|
+
player(ctx) {
|
|
177
|
+
ctx.fillStyle = '#2f8fff';
|
|
178
|
+
ctx.strokeStyle = '#06100f';
|
|
179
|
+
ctx.lineWidth = 4.4;
|
|
180
|
+
ctx.beginPath();
|
|
181
|
+
ctx.arc(0, 0, 8.5, 0, TAU);
|
|
182
|
+
ctx.stroke();
|
|
183
|
+
ctx.strokeStyle = '#ffffff';
|
|
184
|
+
ctx.lineWidth = 2.2;
|
|
185
|
+
ctx.fill();
|
|
186
|
+
ctx.stroke();
|
|
187
|
+
ctx.fillStyle = '#ffffff';
|
|
188
|
+
drawMapPoly(ctx, [0, -5.5, 4, 4, 0, 1.8, -4, 4]);
|
|
189
|
+
ctx.fill();
|
|
190
|
+
},
|
|
191
|
+
roads(ctx) {
|
|
192
|
+
ctx.strokeStyle = '#2a2118';
|
|
193
|
+
ctx.lineWidth = 4;
|
|
194
|
+
ctx.beginPath();
|
|
195
|
+
ctx.moveTo(-10, 5);
|
|
196
|
+
ctx.quadraticCurveTo(0, -8, 10, 3);
|
|
197
|
+
ctx.stroke();
|
|
198
|
+
ctx.strokeStyle = '#ecd9a8';
|
|
199
|
+
ctx.lineWidth = 2;
|
|
200
|
+
ctx.setLineDash([4, 3]);
|
|
201
|
+
ctx.stroke();
|
|
202
|
+
},
|
|
203
|
+
rivers(ctx) {
|
|
204
|
+
ctx.strokeStyle = '#5a9cc4';
|
|
205
|
+
ctx.lineWidth = 2.6;
|
|
206
|
+
ctx.beginPath();
|
|
207
|
+
ctx.moveTo(-10, -4);
|
|
208
|
+
ctx.quadraticCurveTo(-2, 8, 2, -1);
|
|
209
|
+
ctx.quadraticCurveTo(5, -7, 10, 4);
|
|
210
|
+
ctx.stroke();
|
|
211
|
+
},
|
|
212
|
+
regions(ctx, font) {
|
|
213
|
+
ctx.fillStyle = MAP_COLORS.paper;
|
|
214
|
+
ctx.font = 'italic 13px ' + font;
|
|
215
|
+
ctx.textAlign = 'center';
|
|
216
|
+
ctx.textBaseline = 'middle';
|
|
217
|
+
ctx.fillText('Aa', 0, 0);
|
|
218
|
+
},
|
|
219
|
+
terrain(ctx) {
|
|
220
|
+
const swatches = ['#a59c5e', '#6f8a45', '#8d887f', '#3a6a86'];
|
|
221
|
+
for (let i = 0; i < 4; i++) {
|
|
222
|
+
ctx.fillStyle = swatches[i];
|
|
223
|
+
ctx.fillRect(-9 + (i % 2) * 9, -9 + (i >> 1) * 9, 9, 9);
|
|
224
|
+
}
|
|
225
|
+
ctx.strokeStyle = MAP_COLORS.ink;
|
|
226
|
+
ctx.strokeRect(-9, -9, 18, 18);
|
|
227
|
+
},
|
|
228
|
+
walls(ctx) {
|
|
229
|
+
ctx.strokeStyle = 'rgba(243,236,217,.6)';
|
|
230
|
+
ctx.lineWidth = 4.4;
|
|
231
|
+
ctx.beginPath();
|
|
232
|
+
ctx.moveTo(-10, 0);
|
|
233
|
+
ctx.lineTo(-2.5, 0);
|
|
234
|
+
ctx.moveTo(2.5, 0);
|
|
235
|
+
ctx.lineTo(10, 0);
|
|
236
|
+
ctx.stroke();
|
|
237
|
+
ctx.strokeStyle = '#1f1810';
|
|
238
|
+
ctx.lineWidth = 2.4;
|
|
239
|
+
ctx.stroke();
|
|
240
|
+
ctx.fillStyle = '#1f1810';
|
|
241
|
+
ctx.strokeStyle = 'rgba(243,236,217,.6)';
|
|
242
|
+
ctx.lineWidth = 1;
|
|
243
|
+
for (const x of [-10, 10]) {
|
|
244
|
+
ctx.fillRect(x - 2.5, -2.5, 5, 5);
|
|
245
|
+
ctx.strokeRect(x - 2.5, -2.5, 5, 5);
|
|
246
|
+
}
|
|
247
|
+
},
|
|
248
|
+
quays(ctx) {
|
|
249
|
+
ctx.fillStyle = '#3a6a86';
|
|
250
|
+
ctx.fillRect(-10, -9, 20, 11);
|
|
251
|
+
ctx.fillStyle = MAP_COLORS.stone;
|
|
252
|
+
ctx.strokeStyle = MAP_COLORS.ink;
|
|
253
|
+
ctx.lineWidth = 1;
|
|
254
|
+
ctx.fillRect(-10, 1, 20, 3.4);
|
|
255
|
+
ctx.strokeRect(-10, 1, 20, 3.4);
|
|
256
|
+
ctx.fillRect(-1.6, -8, 3.2, 9);
|
|
257
|
+
ctx.strokeRect(-1.6, -8, 3.2, 9);
|
|
258
|
+
},
|
|
259
|
+
holy(ctx) {
|
|
260
|
+
ctx.fillStyle = MAP_COLORS.paper;
|
|
261
|
+
ctx.strokeStyle = MAP_COLORS.ink;
|
|
262
|
+
ctx.lineWidth = 1.5;
|
|
263
|
+
ctx.fillRect(-5, -5, 10, 10);
|
|
264
|
+
ctx.strokeRect(-5, -5, 10, 10);
|
|
265
|
+
ctx.fillStyle = '#b8862e';
|
|
266
|
+
drawMapPoly(ctx, [0, -3.4, 3.4, 0, 0, 3.4, -3.4, 0]);
|
|
267
|
+
ctx.fill();
|
|
268
|
+
},
|
|
269
|
+
town: ctx => drawMapDisc(ctx, 3.2, MAP_COLORS.paper, MAP_COLORS.ink, 1.3),
|
|
270
|
+
battle(ctx) {
|
|
271
|
+
ctx.strokeStyle = MAP_COLORS.ink;
|
|
272
|
+
ctx.lineWidth = 3.4;
|
|
273
|
+
ctx.lineCap = 'round';
|
|
274
|
+
for (let q = 0; q < 2; q++) {
|
|
275
|
+
ctx.beginPath();
|
|
276
|
+
ctx.moveTo(-4.5, -4.5);
|
|
277
|
+
ctx.lineTo(4.5, 4.5);
|
|
278
|
+
ctx.moveTo(4.5, -4.5);
|
|
279
|
+
ctx.lineTo(-4.5, 4.5);
|
|
280
|
+
ctx.stroke();
|
|
281
|
+
ctx.strokeStyle = '#e39a66';
|
|
282
|
+
ctx.lineWidth = 1.6;
|
|
283
|
+
}
|
|
284
|
+
},
|
|
285
|
+
frame(ctx) {
|
|
286
|
+
ctx.strokeStyle = '#62e6cb';
|
|
287
|
+
ctx.lineWidth = 1.8;
|
|
288
|
+
ctx.strokeRect(-7, -7, 14, 14);
|
|
289
|
+
ctx.fillStyle = 'rgba(98,230,203,.18)';
|
|
290
|
+
ctx.fillRect(-7, -7, 14, 14);
|
|
291
|
+
},
|
|
292
|
+
route(ctx) {
|
|
293
|
+
ctx.fillStyle = MAP_COLORS.paper;
|
|
294
|
+
ctx.strokeStyle = MAP_COLORS.ink;
|
|
295
|
+
ctx.lineWidth = 1.2;
|
|
296
|
+
drawMapPoly(ctx, [-6, 1.5, 6, 1.5, 4, 5, -4, 5]);
|
|
297
|
+
ctx.fill();
|
|
298
|
+
ctx.stroke();
|
|
299
|
+
drawMapPoly(ctx, [-3.6, -6, 3.6, -6, 4.2, 0, -4.2, 0]);
|
|
300
|
+
ctx.fill();
|
|
301
|
+
ctx.stroke();
|
|
302
|
+
},
|
|
303
|
+
range(ctx) {
|
|
304
|
+
ctx.strokeStyle = MAP_COLORS.reliefInk;
|
|
305
|
+
ctx.lineWidth = 0.9;
|
|
306
|
+
drawPeak(ctx, -4.5, 3, 5);
|
|
307
|
+
drawPeak(ctx, 4, 6, 6);
|
|
308
|
+
},
|
|
309
|
+
forest(ctx) {
|
|
310
|
+
drawTree(ctx, -5, 0, 3.4);
|
|
311
|
+
drawTree(ctx, 3, -1, 3.8);
|
|
312
|
+
drawTree(ctx, -1, 6, 3.6);
|
|
313
|
+
drawTree(ctx, 7, 6, 3);
|
|
314
|
+
},
|
|
315
|
+
desert(ctx) {
|
|
316
|
+
ctx.strokeStyle = ctx.fillStyle = MAP_COLORS.paper;
|
|
317
|
+
ctx.lineWidth = 1.1;
|
|
318
|
+
drawDune(ctx, -4, -2, 5);
|
|
319
|
+
drawDune(ctx, 4, 5, 5.5);
|
|
320
|
+
},
|
|
321
|
+
plateau(ctx) {
|
|
322
|
+
ctx.strokeStyle = MAP_COLORS.paper;
|
|
323
|
+
ctx.lineWidth = 1;
|
|
324
|
+
ctx.beginPath();
|
|
325
|
+
ctx.ellipse(0, 0, 8, 5, 0, 0, TAU);
|
|
326
|
+
for (let a = 0; a < TAU; a += TAU / 12) {
|
|
327
|
+
ctx.moveTo(Math.cos(a) * 8, Math.sin(a) * 5);
|
|
328
|
+
ctx.lineTo(Math.cos(a) * 10.5, Math.sin(a) * 7);
|
|
329
|
+
}
|
|
330
|
+
ctx.stroke();
|
|
331
|
+
},
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Draws a map glyph at `(x, y)` scaled by `scale`. Supports built-in `MAP_GLYPHS`, caller-supplied
|
|
336
|
+
* `extraGlyphs`, and `f:<key>` coloured marker discs resolved through `factionColors`.
|
|
337
|
+
*
|
|
338
|
+
* @param {CanvasRenderingContext2D} ctx
|
|
339
|
+
* @param {string} id Glyph identifier or `'f:<key>'`.
|
|
340
|
+
* @param {number} x Canvas X in pixels.
|
|
341
|
+
* @param {number} y Canvas Y in pixels.
|
|
342
|
+
* @param {number} scale Uniform scale factor.
|
|
343
|
+
* @param {object} [options]
|
|
344
|
+
* @param {string} [options.font=DEFAULT_MAP_FONT]
|
|
345
|
+
* @param {Record<string, (ctx: CanvasRenderingContext2D, font: string) => void>} [options.extraGlyphs]
|
|
346
|
+
* @param {Record<string, string | { color: string }>} [options.factionColors]
|
|
347
|
+
*/
|
|
348
|
+
export function drawMapGlyph(ctx, id, x, y, scale, { font = DEFAULT_MAP_FONT, extraGlyphs, factionColors } = {}) {
|
|
349
|
+
ctx.save();
|
|
350
|
+
ctx.translate(x, y);
|
|
351
|
+
ctx.scale(scale, scale);
|
|
352
|
+
ctx.lineJoin = 'round';
|
|
353
|
+
if (id.startsWith('f:')) {
|
|
354
|
+
const entry = factionColors?.[id.slice(2)];
|
|
355
|
+
const color = typeof entry === 'string' ? entry : (entry?.color ?? MAP_COLORS.paper);
|
|
356
|
+
drawMapDisc(ctx, 4, color, '#06100f', 1.2);
|
|
357
|
+
} else {
|
|
358
|
+
const fn = extraGlyphs?.[id] ?? MAP_GLYPHS[id];
|
|
359
|
+
if (fn) fn(ctx, font);
|
|
360
|
+
}
|
|
361
|
+
ctx.restore();
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Draws haloed cartographic text centred vertically at `(x, y)`.
|
|
366
|
+
*
|
|
367
|
+
* @param {CanvasRenderingContext2D} ctx
|
|
368
|
+
* @param {string} text
|
|
369
|
+
* @param {number} x Canvas X in pixels.
|
|
370
|
+
* @param {number} y Canvas Y in pixels.
|
|
371
|
+
* @param {number} px Font size in canvas pixels.
|
|
372
|
+
* @param {object} [options]
|
|
373
|
+
* @param {boolean} [options.italic=false]
|
|
374
|
+
* @param {string} [options.color=MAP_COLORS.paper]
|
|
375
|
+
* @param {CanvasTextAlign} [options.align='center']
|
|
376
|
+
* @param {string} [options.font=DEFAULT_MAP_FONT]
|
|
377
|
+
*/
|
|
378
|
+
export function drawMapLabel(ctx, text, x, y, px, { italic = false, color, align, font = DEFAULT_MAP_FONT } = {}) {
|
|
379
|
+
ctx.font = (italic ? 'italic ' : '') + px + 'px ' + font;
|
|
380
|
+
ctx.textAlign = align || 'center';
|
|
381
|
+
ctx.textBaseline = 'middle';
|
|
382
|
+
ctx.lineWidth = px * 0.28;
|
|
383
|
+
ctx.strokeStyle = 'rgba(6,12,14,.85)';
|
|
384
|
+
ctx.strokeText(text, x, y);
|
|
385
|
+
ctx.fillStyle = color || MAP_COLORS.paper;
|
|
386
|
+
ctx.fillText(text, x, y);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Draws a north arrow and `'N'` label at `(x, y)` on a map sheet.
|
|
391
|
+
*
|
|
392
|
+
* @param {CanvasRenderingContext2D} ctx
|
|
393
|
+
* @param {number} x Canvas X in pixels.
|
|
394
|
+
* @param {number} y Canvas Y in pixels.
|
|
395
|
+
* @param {number} u Physical pixels per CSS pixel.
|
|
396
|
+
* @param {object} [options]
|
|
397
|
+
* @param {string} [options.font=DEFAULT_MAP_FONT]
|
|
398
|
+
* @param {string} [options.label='N']
|
|
399
|
+
* @param {string} [options.paper=MAP_COLORS.paper]
|
|
400
|
+
* @param {string} [options.accent=MAP_COLORS.accent]
|
|
401
|
+
*/
|
|
402
|
+
export function drawMapCompass(
|
|
403
|
+
ctx,
|
|
404
|
+
x,
|
|
405
|
+
y,
|
|
406
|
+
u,
|
|
407
|
+
{ font = DEFAULT_MAP_FONT, label = 'N', paper = MAP_COLORS.paper, accent = MAP_COLORS.accent } = {},
|
|
408
|
+
) {
|
|
409
|
+
ctx.save();
|
|
410
|
+
ctx.translate(x, y);
|
|
411
|
+
ctx.strokeStyle = 'rgba(6,12,14,.85)';
|
|
412
|
+
ctx.lineWidth = 3 * u;
|
|
413
|
+
ctx.fillStyle = paper;
|
|
414
|
+
ctx.beginPath();
|
|
415
|
+
ctx.moveTo(0, -13 * u);
|
|
416
|
+
ctx.lineTo(5 * u, 6 * u);
|
|
417
|
+
ctx.lineTo(0, 2 * u);
|
|
418
|
+
ctx.lineTo(-5 * u, 6 * u);
|
|
419
|
+
ctx.closePath();
|
|
420
|
+
ctx.stroke();
|
|
421
|
+
ctx.fill();
|
|
422
|
+
ctx.restore();
|
|
423
|
+
drawMapLabel(ctx, label, x, y - 21 * u, 11 * u, { italic: false, color: accent, font });
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
/**
|
|
427
|
+
* Draws a scale bar and compass rose at the bottom corners of a land map sheet.
|
|
428
|
+
*
|
|
429
|
+
* @param {CanvasRenderingContext2D} ctx
|
|
430
|
+
* @param {{ w: number, h: number, k: number, u: number }} view Map view metrics (`k`: pixels per metre, `u`: DPR).
|
|
431
|
+
* @param {object} [options]
|
|
432
|
+
* @param {string} [options.font=DEFAULT_MAP_FONT]
|
|
433
|
+
* @param {string} [options.unit='paces']
|
|
434
|
+
* @param {ReadonlyArray<number>} [options.choices=[50, 100, 200, 500, 1000, 2000]]
|
|
435
|
+
* @param {number} [options.maxCssPx=150]
|
|
436
|
+
*/
|
|
437
|
+
export function drawMapScaleBar(
|
|
438
|
+
ctx,
|
|
439
|
+
{ w, h, k, u },
|
|
440
|
+
{ font = DEFAULT_MAP_FONT, unit = 'paces', choices = [50, 100, 200, 500, 1000, 2000], maxCssPx = 150 } = {},
|
|
441
|
+
) {
|
|
442
|
+
const bx = 14 * u;
|
|
443
|
+
const by = h - 16 * u;
|
|
444
|
+
let d = choices[0];
|
|
445
|
+
for (let i = 0; i < choices.length; i++) if (choices[i] * k <= maxCssPx * u) d = choices[i];
|
|
446
|
+
ctx.strokeStyle = 'rgba(6,12,14,.85)';
|
|
447
|
+
ctx.lineWidth = 4.5 * u;
|
|
448
|
+
ctx.lineCap = 'butt';
|
|
449
|
+
ctx.beginPath();
|
|
450
|
+
ctx.moveTo(bx, by);
|
|
451
|
+
ctx.lineTo(bx + d * k, by);
|
|
452
|
+
ctx.stroke();
|
|
453
|
+
ctx.strokeStyle = MAP_COLORS.paper;
|
|
454
|
+
ctx.lineWidth = 2 * u;
|
|
455
|
+
ctx.beginPath();
|
|
456
|
+
ctx.moveTo(bx, by - 4 * u);
|
|
457
|
+
ctx.lineTo(bx, by);
|
|
458
|
+
ctx.lineTo(bx + d * k, by);
|
|
459
|
+
ctx.lineTo(bx + d * k, by - 4 * u);
|
|
460
|
+
ctx.stroke();
|
|
461
|
+
drawMapLabel(ctx, `${d} ${unit}`, bx + (d * k) / 2, by - 10 * u, 10.5 * u, { italic: false, color: MAP_COLORS.paper, font });
|
|
462
|
+
drawMapCompass(ctx, w - 26 * u, h - 30 * u, u, { font });
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* Wraps and centres an italic note across an empty or unbuilt map sheet.
|
|
467
|
+
*
|
|
468
|
+
* @param {CanvasRenderingContext2D} ctx
|
|
469
|
+
* @param {string} text
|
|
470
|
+
* @param {number} width Canvas width in pixels.
|
|
471
|
+
* @param {number} height Canvas height in pixels.
|
|
472
|
+
* @param {number} u Physical pixels per CSS pixel.
|
|
473
|
+
* @param {object} [options]
|
|
474
|
+
* @param {string} [options.font=DEFAULT_MAP_FONT]
|
|
475
|
+
* @param {string} [options.color='rgba(243,236,217,.8)']
|
|
476
|
+
*/
|
|
477
|
+
export function drawMapNote(ctx, text, width, height, u, { font = DEFAULT_MAP_FONT, color = 'rgba(243,236,217,.8)' } = {}) {
|
|
478
|
+
const px = 13 * u;
|
|
479
|
+
const lines = [''];
|
|
480
|
+
ctx.font = 'italic ' + px + 'px ' + font;
|
|
481
|
+
for (const word of text.split(' ')) {
|
|
482
|
+
const line = lines[lines.length - 1];
|
|
483
|
+
const longer = line ? line + ' ' + word : word;
|
|
484
|
+
if (line && ctx.measureText(longer).width > width - 40 * u) lines.push(word);
|
|
485
|
+
else lines[lines.length - 1] = longer;
|
|
486
|
+
}
|
|
487
|
+
lines.forEach((line, i) =>
|
|
488
|
+
drawMapLabel(ctx, line, width / 2, height / 2 + (i - (lines.length - 1) / 2) * px * 1.4, px, {
|
|
489
|
+
italic: true,
|
|
490
|
+
color,
|
|
491
|
+
font,
|
|
492
|
+
}),
|
|
493
|
+
);
|
|
494
|
+
}
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interactive map canvas sizing and pointer/pinch/wheel/tap controller.
|
|
3
|
+
* @module
|
|
4
|
+
*/
|
|
5
|
+
import { fitMapDimensions } from '../runtime/map-view.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Resizes a map canvas to fill its layout box at screen pixel density (capped at `maxDpr`),
|
|
9
|
+
* returning the updated `{ w, h, dpr }` physical pixel metrics.
|
|
10
|
+
*
|
|
11
|
+
* @param {HTMLCanvasElement} canvas
|
|
12
|
+
* @param {HTMLElement} box Container element holding the canvas.
|
|
13
|
+
* @param {object} [options]
|
|
14
|
+
* @param {() => number} [options.viewportHeight] Live viewport height in CSS pixels.
|
|
15
|
+
* @param {number} [options.narrowShare=0.52] Maximum height fraction when stacked below a legend.
|
|
16
|
+
* @param {number} [options.minSide=240] Minimum CSS width and height.
|
|
17
|
+
* @param {number} [options.maxDpr=2] Maximum pixel density multiplier.
|
|
18
|
+
* @param {object} [options.host=globalThis] Host exposing `getComputedStyle`, `innerHeight`, `devicePixelRatio`.
|
|
19
|
+
* @returns {{ w: number, h: number, dpr: number }}
|
|
20
|
+
*/
|
|
21
|
+
export function fitMapCanvas(canvas, box, { viewportHeight, narrowShare = 0.52, minSide = 240, maxDpr = 2, host = globalThis } = {}) {
|
|
22
|
+
const parent = box.parentNode;
|
|
23
|
+
const stacked = host.getComputedStyle(parent).flexDirection === 'column';
|
|
24
|
+
const dims = fitMapDimensions({
|
|
25
|
+
stacked,
|
|
26
|
+
parentWidth: parent.clientWidth,
|
|
27
|
+
boxWidth: box.clientWidth,
|
|
28
|
+
boxHeight: box.clientHeight,
|
|
29
|
+
viewportHeight: viewportHeight ? viewportHeight() : host.innerHeight,
|
|
30
|
+
narrowShare,
|
|
31
|
+
minSide,
|
|
32
|
+
maxDpr,
|
|
33
|
+
devicePixelRatio: host.devicePixelRatio || 1,
|
|
34
|
+
});
|
|
35
|
+
canvas.style.width = dims.cssWidth + 'px';
|
|
36
|
+
canvas.style.height = dims.cssHeight + 'px';
|
|
37
|
+
canvas.width = dims.pixelWidth;
|
|
38
|
+
canvas.height = dims.pixelHeight;
|
|
39
|
+
return { w: dims.pixelWidth, h: dims.pixelHeight, dpr: dims.dpr };
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Binds pointer dragging (pan), two-finger pinching (zoom), mouse-wheel zooming, and short-tap
|
|
44
|
+
* picking onto a map canvas. Returns a `dispose()` cleanup function that removes all listeners.
|
|
45
|
+
*
|
|
46
|
+
* @param {HTMLCanvasElement} canvas
|
|
47
|
+
* @param {object} options
|
|
48
|
+
* @param {(dx: number, dy: number) => void} options.onPan Called with CSS-pixel deltas on single-pointer drag.
|
|
49
|
+
* @param {(factor: number) => void} options.onZoom Called with a multiplicative zoom factor on wheel or pinch.
|
|
50
|
+
* @param {(canvasX: number, canvasY: number) => void} options.onTap Called with canvas pixel coordinates on short tap/click.
|
|
51
|
+
* @param {(event: PointerEvent) => { x: number, y: number }} [options.getPoint] Reads event coordinates in the stage frame.
|
|
52
|
+
* @param {(el: HTMLElement) => { left: number, top: number, width: number, height: number }} [options.getRect] Reads element bounds in the stage frame.
|
|
53
|
+
* @param {() => { w: number, h: number }} [options.getSize] Current physical canvas pixel dimensions.
|
|
54
|
+
* @param {number} [options.dragThreshold=7] Maximum Manhattan drag in CSS pixels still treated as a tap.
|
|
55
|
+
* @param {number} [options.wheelFactor=1.18] Zoom step per wheel notch.
|
|
56
|
+
* @returns {{ dispose: () => void }}
|
|
57
|
+
*/
|
|
58
|
+
export function bindMapInput(
|
|
59
|
+
canvas,
|
|
60
|
+
{
|
|
61
|
+
onPan,
|
|
62
|
+
onZoom,
|
|
63
|
+
onTap,
|
|
64
|
+
getPoint = e => ({ x: e.clientX, y: e.clientY }),
|
|
65
|
+
getRect = el => el.getBoundingClientRect(),
|
|
66
|
+
getSize = () => ({ w: canvas.width, h: canvas.height }),
|
|
67
|
+
dragThreshold = 7,
|
|
68
|
+
wheelFactor = 1.18,
|
|
69
|
+
},
|
|
70
|
+
) {
|
|
71
|
+
const pointers = new Map();
|
|
72
|
+
let drag = null;
|
|
73
|
+
let pinch = 0;
|
|
74
|
+
|
|
75
|
+
const onDown = e => {
|
|
76
|
+
try {
|
|
77
|
+
canvas.setPointerCapture?.(e.pointerId);
|
|
78
|
+
} catch {}
|
|
79
|
+
const pt = getPoint(e);
|
|
80
|
+
pointers.set(e.pointerId, { x: pt.x, y: pt.y });
|
|
81
|
+
drag = { x: pt.x, y: pt.y, moved: 0 };
|
|
82
|
+
if (pointers.size === 2) {
|
|
83
|
+
const pair = [...pointers.values()];
|
|
84
|
+
pinch = Math.hypot(pair[0].x - pair[1].x, pair[0].y - pair[1].y);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const onMove = e => {
|
|
89
|
+
const p = pointers.get(e.pointerId);
|
|
90
|
+
if (!p) return;
|
|
91
|
+
const pt = getPoint(e);
|
|
92
|
+
const dx = pt.x - p.x;
|
|
93
|
+
const dy = pt.y - p.y;
|
|
94
|
+
p.x = pt.x;
|
|
95
|
+
p.y = pt.y;
|
|
96
|
+
if (pointers.size === 2) {
|
|
97
|
+
const pair = [...pointers.values()];
|
|
98
|
+
const d = Math.hypot(pair[0].x - pair[1].x, pair[0].y - pair[1].y);
|
|
99
|
+
if (pinch > 0) onZoom(d / pinch);
|
|
100
|
+
pinch = d;
|
|
101
|
+
if (drag) drag.moved = 99;
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
onPan(dx, dy);
|
|
105
|
+
if (drag) drag.moved += Math.abs(dx) + Math.abs(dy);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
const onUp = e => {
|
|
109
|
+
pointers.delete(e.pointerId);
|
|
110
|
+
pinch = 0;
|
|
111
|
+
if (drag && drag.moved < dragThreshold && e.type === 'pointerup') {
|
|
112
|
+
const rect = getRect(canvas);
|
|
113
|
+
const pt = getPoint(e);
|
|
114
|
+
const { w, h } = getSize();
|
|
115
|
+
onTap(((pt.x - rect.left) / rect.width) * w, ((pt.y - rect.top) / rect.height) * h);
|
|
116
|
+
}
|
|
117
|
+
if (pointers.size === 0) drag = null;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const onWheel = e => {
|
|
121
|
+
e.preventDefault();
|
|
122
|
+
onZoom(e.deltaY < 0 ? wheelFactor : 1 / wheelFactor);
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
canvas.addEventListener('pointerdown', onDown);
|
|
126
|
+
canvas.addEventListener('pointermove', onMove);
|
|
127
|
+
canvas.addEventListener('pointerup', onUp);
|
|
128
|
+
canvas.addEventListener('pointercancel', onUp);
|
|
129
|
+
canvas.addEventListener('wheel', onWheel, { passive: false });
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
dispose() {
|
|
133
|
+
canvas.removeEventListener('pointerdown', onDown);
|
|
134
|
+
canvas.removeEventListener('pointermove', onMove);
|
|
135
|
+
canvas.removeEventListener('pointerup', onUp);
|
|
136
|
+
canvas.removeEventListener('pointercancel', onUp);
|
|
137
|
+
canvas.removeEventListener('wheel', onWheel);
|
|
138
|
+
pointers.clear();
|
|
139
|
+
drag = null;
|
|
140
|
+
pinch = 0;
|
|
141
|
+
},
|
|
142
|
+
};
|
|
143
|
+
}
|