roamwright 0.61.16 → 0.61.18

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 (49) hide show
  1. package/README.md +25 -24
  2. package/examples/land-features.js +130 -9
  3. package/examples/land.html +2 -1
  4. package/examples/land.js +17 -2
  5. package/examples/ruins.html +3 -2
  6. package/examples/ruins.js +184 -37
  7. package/examples/world-map.html +162 -0
  8. package/examples/world-map.js +475 -0
  9. package/geometry/farm-props.js +59 -0
  10. package/geometry/field-map.js +147 -0
  11. package/geometry/instances.js +40 -0
  12. package/geometry/plant-tuft.js +138 -0
  13. package/geometry/ruins-breach.js +329 -0
  14. package/geometry/ruins-crater.js +269 -0
  15. package/geometry/ruins-masonry.js +323 -0
  16. package/geometry/ruins-walls.js +272 -0
  17. package/geometry/strata-mesa.js +114 -0
  18. package/geometry/strata-rocks.js +73 -2
  19. package/index.js +109 -1
  20. package/package.json +1 -1
  21. package/rendering/contact-shadows.js +56 -0
  22. package/rendering/crowd-lifecycle.js +80 -0
  23. package/rendering/farm-plough.js +145 -0
  24. package/rendering/map-glyphs.js +494 -0
  25. package/rendering/map-input.js +143 -0
  26. package/rendering/map-labels.js +181 -0
  27. package/rendering/map-relief.js +330 -0
  28. package/rendering/map-signs.js +124 -0
  29. package/rendering/place-names.js +408 -0
  30. package/rendering/plant-tuft.js +190 -0
  31. package/rendering/portrait-studio.js +213 -0
  32. package/rendering/ruins-fittings.js +199 -0
  33. package/rendering/ruins-mound.js +103 -0
  34. package/rendering/screen-pick.js +75 -0
  35. package/runtime/build-freshness.js +133 -0
  36. package/runtime/compass-strip.js +232 -0
  37. package/runtime/context-lost.js +108 -0
  38. package/runtime/doorways.js +98 -0
  39. package/runtime/hud-frame.js +74 -0
  40. package/runtime/map-relief.js +236 -0
  41. package/runtime/map-spot.js +120 -0
  42. package/runtime/map-view.js +229 -0
  43. package/runtime/panel-shell.js +90 -0
  44. package/runtime/relation-graph.js +163 -0
  45. package/runtime/stage-frame.js +263 -0
  46. package/runtime/url-router.js +162 -0
  47. package/runtime/way-back.js +71 -0
  48. package/runtime/whereabouts.js +210 -0
  49. package/runtime/world-controls.js +265 -0
@@ -0,0 +1,181 @@
1
+ /**
2
+ * Collision-avoiding 2D map label placement. Offer names in descending priority order: each takes
3
+ * the first candidate spot clear of reserved marks and previously placed names, or is skipped
4
+ * (unless `must: true`, in which case it takes the least-overlapping in-bounds spot).
5
+ * @module
6
+ */
7
+ import { clamp } from '../math/scalar.js';
8
+ import { DEFAULT_MAP_FONT, drawMapLabel } from './map-glyphs.js';
9
+
10
+ const SIDES = [
11
+ [0, -1, 0],
12
+ [1, 0, 1],
13
+ [0, 1, 0],
14
+ [-1, 0, -1],
15
+ [0, -1.9, 0],
16
+ [0, 1.9, 0],
17
+ ];
18
+ const NUDGES = [0, -1, 1, -2, 2];
19
+ const SLIDES = [0, -0.5, 0.5];
20
+
21
+ /**
22
+ * Formats a region or continent name in wide-tracked capitals (`"A B C"`).
23
+ *
24
+ * @param {string} name
25
+ * @returns {string}
26
+ */
27
+ export function spacedCaps(name) {
28
+ return name.toUpperCase().split('').join(' ');
29
+ }
30
+
31
+ /**
32
+ * Computes an upright rotation angle in radians along the segment from `(ax, ay)` to `(bx, by)`.
33
+ *
34
+ * @param {number} ax
35
+ * @param {number} ay
36
+ * @param {number} bx
37
+ * @param {number} by
38
+ * @returns {number} Angle in `[-PI/2, PI/2]`.
39
+ */
40
+ export function turnAlongLine(ax, ay, bx, by) {
41
+ const t = Math.atan2(by - ay, bx - ax);
42
+ return t > Math.PI / 2 ? t - Math.PI : t < -Math.PI / 2 ? t + Math.PI : t;
43
+ }
44
+
45
+ /**
46
+ * Creates a deferred collision-aware label set for a map sheet of `width` by `height` physical pixels.
47
+ * Call `keep`, `beside`, and `on` while painting the sheet, then `flush()` last so every label renders
48
+ * cleanly above all map features.
49
+ *
50
+ * @param {CanvasRenderingContext2D} ctx
51
+ * @param {number} width Sheet canvas width in pixels.
52
+ * @param {number} height Sheet canvas height in pixels.
53
+ * @param {number} u Physical pixels per CSS pixel.
54
+ * @param {object} [options]
55
+ * @param {string} [options.font=DEFAULT_MAP_FONT]
56
+ * @param {Array<[number, number, number, number]>} [options.reserved] Initial obstacle boxes `[x0, y0, x1, y1]`; defaults to the top-right zoom-button corner.
57
+ * @param {(ctx: CanvasRenderingContext2D, text: string, x: number, y: number, px: number, italic?: boolean, color?: string, align?: CanvasTextAlign) => void} [options.drawLabel]
58
+ * @returns {{ keep: (x: number, y: number, r: number) => void, beside: (text: string, x: number, y: number, px: number, o?: object) => { x: number, y: number } | null,
59
+ * on: (text: string, x: number, y: number, px: number, o?: object) => { x: number, y: number } | null, flush: () => void }}
60
+ */
61
+ export function createMapLabelSet(
62
+ ctx,
63
+ width,
64
+ height,
65
+ u,
66
+ {
67
+ font = DEFAULT_MAP_FONT,
68
+ reserved = [[width - 50 * u, 0, width, 92 * u]],
69
+ drawLabel = (g, text, x, y, px, italic, color, align) => drawMapLabel(g, text, x, y, px, { italic, color, align, font }),
70
+ } = {},
71
+ ) {
72
+ const taken = reserved.map(box => [...box]);
73
+ const written = [];
74
+
75
+ const overlap = box => {
76
+ let sum = 0;
77
+ for (let i = 0; i < taken.length; i++) {
78
+ const o = taken[i];
79
+ sum += Math.max(0, Math.min(box[2], o[2]) - Math.max(box[0], o[0])) * Math.max(0, Math.min(box[3], o[3]) - Math.max(box[1], o[1]));
80
+ }
81
+ return sum;
82
+ };
83
+ const inside = box => box[0] >= 0 && box[2] <= width && box[1] >= 0 && box[3] <= height;
84
+ const within = (c, half, size) => clamp(c, Math.min(half, size / 2), Math.max(size - half, size / 2));
85
+
86
+ const write = (box, text, x, y, px, o, align) => {
87
+ taken.push(box);
88
+ written.push({ text, x, y, px, o, align });
89
+ return { x: (box[0] + box[2]) / 2, y: (box[1] + box[3]) / 2 };
90
+ };
91
+
92
+ const measureWidth = (text, px, italic) => {
93
+ ctx.font = (italic ? 'italic ' : '') + px + 'px ' + font;
94
+ return ctx.measureText(text).width;
95
+ };
96
+
97
+ return {
98
+ keep(x, y, r) {
99
+ taken.push([x - r, y - r, x + r, y + r]);
100
+ },
101
+ beside(text, x, y, px, o = {}) {
102
+ const tw = measureWidth(text, px, o.italic);
103
+ const th = px * 1.15;
104
+ const gap = (o.gap ?? 15) * u;
105
+ let best = null;
106
+ let least = Infinity;
107
+ for (let i = 0; i < SIDES.length; i++) {
108
+ const [sx, sy, anchor] = SIDES[i];
109
+ const ax = x + sx * gap * 0.67;
110
+ const cx = ax + (anchor * tw) / 2;
111
+ const cy = y + sy * gap;
112
+ const box = [cx - tw / 2 - 3 * u, cy - th / 2, cx + tw / 2 + 3 * u, cy + th / 2];
113
+ const align = anchor > 0 ? 'left' : anchor < 0 ? 'right' : 'center';
114
+ if (!inside(box)) continue;
115
+ const covered = overlap(box);
116
+ if (covered === 0) return write(box, text, ax, cy, px, o, align);
117
+ if (covered < least) {
118
+ least = covered;
119
+ best = { box, ax, cy, align };
120
+ }
121
+ }
122
+ if (!o.must) return null;
123
+ if (best) return write(best.box, text, best.ax, best.cy, px, o, best.align);
124
+ const cy = y - gap;
125
+ const cx = within(x, tw / 2 + 3 * u, width);
126
+ const my = within(cy, th / 2, height);
127
+ return write([cx - tw / 2 - 3 * u, my - th / 2, cx + tw / 2 + 3 * u, my + th / 2], text, cx, my, px, o, 'center');
128
+ },
129
+ on(text, x, y, px, o = {}) {
130
+ const tw = measureWidth(text, px, o.italic);
131
+ const th = px * 1.15;
132
+ const c = Math.abs(Math.cos(o.turn || 0));
133
+ const s = Math.abs(Math.sin(o.turn || 0));
134
+ const bw = tw * c + th * s + 6 * u;
135
+ const bh = tw * s + th * c;
136
+ if (x < -bw / 2 || x > width + bw / 2) return null;
137
+ const cx = within(x, bw / 2, width);
138
+ let best = null;
139
+ let least = Infinity;
140
+ const heights = o.alt ? [y, ...o.alt] : [y];
141
+ const slides = o.must ? SLIDES : SLIDES.slice(0, 1);
142
+ for (let hi = 0; hi < heights.length; hi++) {
143
+ const at = heights[hi];
144
+ for (let si = 0; si < slides.length; si++) {
145
+ const slide = slides[si];
146
+ for (let ni = 0; ni < NUDGES.length; ni++) {
147
+ const n = NUDGES[ni];
148
+ const sx = within(cx + slide * bw, bw / 2, width);
149
+ const cy = at + n * th;
150
+ const box = [sx - bw / 2, cy - bh / 2, sx + bw / 2, cy + bh / 2];
151
+ if (box[1] < 0 || box[3] > height) continue;
152
+ const covered = overlap(box);
153
+ if (covered === 0) return write(box, text, sx, cy, px, o);
154
+ if (covered < least) {
155
+ least = covered;
156
+ best = { box, sx, cy };
157
+ }
158
+ }
159
+ }
160
+ }
161
+ if (!o.must) return null;
162
+ if (best) return write(best.box, text, best.sx, best.cy, px, o);
163
+ const cy = within(y, bh / 2, height);
164
+ return write([cx - bw / 2, cy - bh / 2, cx + bw / 2, cy + bh / 2], text, cx, cy, px, o);
165
+ },
166
+ flush() {
167
+ for (let i = 0; i < written.length; i++) {
168
+ const n = written[i];
169
+ if (!n.o.turn) {
170
+ drawLabel(ctx, n.text, n.x, n.y, n.px, n.o.italic, n.o.color, n.align);
171
+ continue;
172
+ }
173
+ ctx.save();
174
+ ctx.translate(n.x, n.y);
175
+ ctx.rotate(n.o.turn);
176
+ drawLabel(ctx, n.text, 0, 0, n.px, n.o.italic, n.o.color);
177
+ ctx.restore();
178
+ }
179
+ },
180
+ };
181
+ }
@@ -0,0 +1,330 @@
1
+ /**
2
+ * Cached 2D cartographic atlas relief painter and baked terrain-sheet renderer.
3
+ * Builds vector relief once from pure data (`buildAtlasMarks`) and repaints an offscreen canvas
4
+ * whenever the map view changes, keeping coastlines, peaks, trees, dunes, and routes crisp at any zoom.
5
+ * @module
6
+ */
7
+ import { TAU } from '../math/scalar.js';
8
+ import { buildAtlasMarks, shadeTerrainPixel } from '../runtime/map-relief.js';
9
+ import { MAP_COLORS, drawDune, drawMapGlyph, drawPeak, drawTree } from './map-glyphs.js';
10
+
11
+ const WATER = '#3f7f9e';
12
+ const LAND = '#b3a777';
13
+ const TREES_FROM = 1.8;
14
+ const FADE_STRIPS = 24;
15
+ const SHIP_FROM = 70;
16
+ const TINT = {
17
+ forest: 'rgba(84,112,60,.3)',
18
+ desert: 'rgba(232,204,128,.34)',
19
+ plateau: 'rgba(244,236,206,.15)',
20
+ water: WATER,
21
+ };
22
+
23
+ function tracePatch(ctx, m) {
24
+ ctx.beginPath();
25
+ ctx.ellipse(m.at[0], m.at[1], m.r[0], m.r[1], m.turn || 0, 0, TAU);
26
+ }
27
+
28
+ /**
29
+ * Creates a cached atlas relief painter for a world sheet of `sheetWidth` by `sheetHeight` atlas units.
30
+ *
31
+ * @param {object} options
32
+ * @param {number} [options.sheetWidth=2000] Sheet width in atlas units.
33
+ * @param {number} [options.sheetHeight=1300] Sheet height in atlas units.
34
+ * @param {() => ReadonlyArray<{ poly: ReadonlyArray<[number, number]>, fade?: number[] }> | ReadonlyArray<{ poly: ReadonlyArray<[number, number]>, fade?: number[] }>} options.lands
35
+ * @param {() => ReadonlyArray<object> | ReadonlyArray<object>} [options.relief=[]]
36
+ * @param {() => ReadonlyArray<ReadonlyArray<[number, number]>> | ReadonlyArray<ReadonlyArray<[number, number]>>} [options.rivers=[]]
37
+ * @param {() => ReadonlyArray<object> | ReadonlyArray<object>} [options.routes=[]]
38
+ * @param {number} [options.seed=4411] Seed for coastline and relief scatter.
39
+ * @param {() => number} [options.random] Optional PRNG overriding `seed`.
40
+ * @param {(ctx: CanvasRenderingContext2D, marks: object, view: object) => void} [options.drawExtraClipped] Optional hook drawn inside the clipped land silhouette before shore outlines.
41
+ * @param {(route: object) => string} [options.routeColor] Returns the dashed line stroke colour for a route.
42
+ * @param {number} [options.treesFrom=1.8] Atlas units per CSS pixel below which individual trees are drawn.
43
+ * @param {number} [options.shipFrom=70] Minimum CSS pixel length at which a route carries a ship icon.
44
+ * @param {string} [options.waterColor='#3f7f9e']
45
+ * @param {string} [options.landColor='#b3a777']
46
+ * @param {object} [options.host=globalThis] Host exposing `document` and `Path2D` (or `window.Path2D`).
47
+ * @returns {{ getCanvas: (view: { x0: number, z0: number, k: number, w: number, h: number, u: number }) => HTMLCanvasElement,
48
+ * getVoyages: (view: { k: number, u: number }) => Array<object>, getMarks: () => object, clearCache: () => void }}
49
+ */
50
+ export function createAtlasRelief({
51
+ sheetWidth = 2000,
52
+ sheetHeight = 1300,
53
+ lands,
54
+ relief = [],
55
+ rivers = [],
56
+ routes = [],
57
+ seed = 4411,
58
+ random,
59
+ drawExtraClipped,
60
+ routeColor = r => (r.book === 1 ? 'rgba(243,236,217,.85)' : 'rgba(255,206,140,.9)'),
61
+ treesFrom = TREES_FROM,
62
+ shipFrom = SHIP_FROM,
63
+ waterColor = WATER,
64
+ landColor = LAND,
65
+ host = globalThis,
66
+ }) {
67
+ let cachedMarks = null;
68
+ const kept = { canvas: null, key: '' };
69
+ const tintMap = waterColor === WATER ? TINT : { ...TINT, water: waterColor };
70
+
71
+ function resolve(value) {
72
+ return typeof value === 'function' ? value() : value;
73
+ }
74
+
75
+ function getMarks() {
76
+ if (cachedMarks) return cachedMarks;
77
+ const PathCtor = host.window?.Path2D ?? host.Path2D;
78
+ const built = buildAtlasMarks({
79
+ lands: resolve(lands),
80
+ relief: resolve(relief),
81
+ rivers: resolve(rivers),
82
+ routes: resolve(routes),
83
+ seed,
84
+ random,
85
+ });
86
+ const landPath = new PathCtor();
87
+ const shorePath = new PathCtor();
88
+ for (let c = 0; c < built.coasts.length; c++) {
89
+ const coast = built.coasts[c];
90
+ const { fine, fade } = coast;
91
+ fine.forEach((q, i) => (i ? landPath.lineTo(q[0], q[1]) : landPath.moveTo(q[0], q[1])));
92
+ landPath.closePath();
93
+ if (fade) {
94
+ coast.own = new PathCtor();
95
+ fine.forEach((q, i) => (i ? coast.own.lineTo(q[0], q[1]) : coast.own.moveTo(q[0], q[1])));
96
+ coast.own.closePath();
97
+ }
98
+ let pen = false;
99
+ for (const q of fade ? fine : [...fine, fine[0]]) {
100
+ const cut = fade && q[1] > fade[2] + 30;
101
+ if (pen && !cut) shorePath.lineTo(q[0], q[1]);
102
+ else shorePath.moveTo(q[0], q[1]);
103
+ pen = !cut;
104
+ }
105
+ }
106
+ cachedMarks = { ...built, lands: landPath, shores: shorePath };
107
+ return cachedMarks;
108
+ }
109
+
110
+ function seaFill(ctx) {
111
+ const sea = ctx.createRadialGradient(
112
+ sheetWidth * 0.5,
113
+ sheetHeight * (600 / 1300),
114
+ sheetWidth * 0.1,
115
+ sheetWidth * 0.5,
116
+ sheetHeight * 0.5,
117
+ sheetHeight,
118
+ );
119
+ sea.addColorStop(0, '#16303a');
120
+ sea.addColorStop(1, '#0d1a20');
121
+ return sea;
122
+ }
123
+
124
+ function paintSea(ctx, px) {
125
+ ctx.fillStyle = seaFill(ctx);
126
+ ctx.fillRect(-sheetWidth, -sheetWidth, sheetWidth * 3, sheetHeight + sheetWidth * 2);
127
+ ctx.strokeStyle = 'rgba(255,255,255,.035)';
128
+ ctx.lineWidth = px;
129
+ ctx.beginPath();
130
+ for (let q = 100; q < sheetWidth; q += 100) {
131
+ ctx.moveTo(q, 0);
132
+ ctx.lineTo(q, sheetHeight);
133
+ if (q < sheetHeight) {
134
+ ctx.moveTo(0, q);
135
+ ctx.lineTo(sheetWidth, q);
136
+ }
137
+ }
138
+ ctx.stroke();
139
+ }
140
+
141
+ function paintRelief(ctx, marks, px, sees) {
142
+ for (const m of marks.mottle) {
143
+ ctx.fillStyle = m.dark ? 'rgba(70,58,34,.06)' : 'rgba(236,224,180,.07)';
144
+ ctx.beginPath();
145
+ ctx.ellipse(m.x, m.y, m.r * 1.6, m.r, m.turn, 0, TAU);
146
+ ctx.fill();
147
+ }
148
+ for (const [width, alpha] of [
149
+ [16, 0.16],
150
+ [6, 0.2],
151
+ ]) {
152
+ ctx.strokeStyle = `rgba(52,40,20,${alpha})`;
153
+ ctx.lineWidth = width;
154
+ ctx.stroke(marks.lands);
155
+ }
156
+ for (const m of marks.relief) {
157
+ if (tintMap[m.k]) {
158
+ tracePatch(ctx, m);
159
+ ctx.fillStyle = tintMap[m.k];
160
+ ctx.fill();
161
+ }
162
+ }
163
+ ctx.strokeStyle = '#4f8fae';
164
+ ctx.lineWidth = Math.max(1.1 * px, 0.9);
165
+ for (const r of marks.rivers) {
166
+ ctx.beginPath();
167
+ for (const p of r) ctx.lineTo(p[0], p[1]);
168
+ ctx.stroke();
169
+ }
170
+ ctx.strokeStyle = MAP_COLORS.reliefInk;
171
+ ctx.lineWidth = Math.max(0.7 * px, 0.22);
172
+ ctx.beginPath();
173
+ for (const m of marks.relief) {
174
+ if (m.k !== 'plateau') continue;
175
+ const c = Math.cos(m.turn || 0);
176
+ const s = Math.sin(m.turn || 0);
177
+ const n = Math.round((m.r[0] + m.r[1]) * 1.1);
178
+ for (let i = 0; i < n; i++) {
179
+ const a = (i / n) * TAU;
180
+ const x = Math.cos(a) * m.r[0];
181
+ const y = Math.sin(a) * m.r[1];
182
+ const l = 1 + (i % 3 === 0 ? 4.4 : 2.8) / Math.hypot(x, y);
183
+ ctx.moveTo(m.at[0] + x * c - y * s, m.at[1] + x * s + y * c);
184
+ ctx.lineTo(m.at[0] + (x * c - y * s) * l, m.at[1] + (x * s + y * c) * l);
185
+ }
186
+ }
187
+ ctx.stroke();
188
+ for (const m of marks.relief) {
189
+ if (m.k === 'water') {
190
+ tracePatch(ctx, m);
191
+ ctx.stroke();
192
+ }
193
+ }
194
+ ctx.strokeStyle = ctx.fillStyle = MAP_COLORS.duneInk;
195
+ for (const d of marks.dunes) if (sees(d.x, d.y)) drawDune(ctx, d.x, d.y, d.s);
196
+ if (px < treesFrom) for (const t of marks.trees) if (sees(t.x, t.y)) drawTree(ctx, t.x, t.y, t.s);
197
+ ctx.strokeStyle = MAP_COLORS.reliefInk;
198
+ ctx.lineWidth = Math.max(0.6 * px, 0.2);
199
+ ctx.lineJoin = 'round';
200
+ for (const p of marks.peaks) if (sees(p.x, p.y)) drawPeak(ctx, p.x, p.y, p.s);
201
+ }
202
+
203
+ function paintFades(ctx, marks) {
204
+ ctx.fillStyle = seaFill(ctx);
205
+ for (const { fade, own } of marks.coasts) {
206
+ if (!fade) continue;
207
+ const [x0, x1, y0, y1] = fade;
208
+ const step = (y1 - y0) / FADE_STRIPS;
209
+ ctx.save();
210
+ ctx.clip(own);
211
+ for (let i = 0; i < FADE_STRIPS; i++) {
212
+ ctx.globalAlpha = (i + 1) / FADE_STRIPS;
213
+ ctx.fillRect(x0, y0 + i * step, x1 - x0, i === FADE_STRIPS - 1 ? sheetHeight : step);
214
+ }
215
+ ctx.restore();
216
+ }
217
+ ctx.globalAlpha = 1;
218
+ }
219
+
220
+ function paintRoutes(ctx, marks, px) {
221
+ ctx.lineCap = 'round';
222
+ ctx.lineJoin = 'round';
223
+ for (const r of marks.routes) {
224
+ ctx.beginPath();
225
+ ctx.moveTo(r.pts[0][0], r.pts[0][1]);
226
+ for (let i = 1; i < r.pts.length - 1; i++) {
227
+ const a = r.pts[i];
228
+ const b = r.pts[i + 1];
229
+ const end = i === r.pts.length - 2;
230
+ ctx.quadraticCurveTo(a[0], a[1], end ? b[0] : (a[0] + b[0]) / 2, end ? b[1] : (a[1] + b[1]) / 2);
231
+ }
232
+ if (r.pts.length === 2) ctx.lineTo(r.pts[1][0], r.pts[1][1]);
233
+ ctx.setLineDash([]);
234
+ ctx.strokeStyle = 'rgba(8,18,22,.55)';
235
+ ctx.lineWidth = 2.6 * px;
236
+ ctx.stroke();
237
+ ctx.setLineDash([5 * px, 4.5 * px]);
238
+ ctx.strokeStyle = routeColor(r);
239
+ ctx.lineWidth = 1.2 * px;
240
+ ctx.stroke();
241
+ }
242
+ ctx.setLineDash([]);
243
+ }
244
+
245
+ const getVoyages = view => getMarks().routes.filter(r => r.len * view.k >= shipFrom * view.u);
246
+
247
+ function getCanvas(view) {
248
+ const key = [view.x0, view.z0, view.k, view.w, view.h].join();
249
+ if (key === kept.key) return kept.canvas;
250
+ const canvas = (kept.canvas ??= host.document.createElement('canvas'));
251
+ const marks = getMarks();
252
+ const px = view.u / view.k;
253
+ const sees = (x, y) => x > view.x0 - 12 && x < view.x0 + view.w / view.k + 12 && y > view.z0 - 12 && y < view.z0 + view.h / view.k + 12;
254
+ if (canvas.width !== view.w || canvas.height !== view.h) {
255
+ canvas.width = view.w;
256
+ canvas.height = view.h;
257
+ }
258
+ kept.key = key;
259
+ const ctx = canvas.getContext('2d');
260
+ ctx.setTransform(view.k, 0, 0, view.k, -view.x0 * view.k, -view.z0 * view.k);
261
+ paintSea(ctx, px);
262
+ ctx.lineJoin = 'round';
263
+ ctx.strokeStyle = 'rgba(96,160,184,.09)';
264
+ for (const width of [34, 20, 9]) {
265
+ ctx.lineWidth = width;
266
+ ctx.stroke(marks.lands);
267
+ }
268
+ ctx.fillStyle = landColor;
269
+ ctx.fill(marks.lands);
270
+ ctx.save();
271
+ ctx.clip(marks.lands);
272
+ paintRelief(ctx, marks, px, sees);
273
+ if (drawExtraClipped) drawExtraClipped(ctx, marks, view);
274
+ ctx.restore();
275
+ paintFades(ctx, marks);
276
+ ctx.strokeStyle = '#231d13';
277
+ ctx.lineWidth = 1.2 * px;
278
+ ctx.stroke(marks.shores);
279
+ paintRoutes(ctx, marks, px);
280
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
281
+ for (const r of getVoyages(view)) {
282
+ drawMapGlyph(ctx, 'route', (r.mid[0] - view.x0) * view.k, (r.mid[1] - view.z0) * view.k, view.u * 0.8);
283
+ }
284
+ return canvas;
285
+ }
286
+
287
+ return {
288
+ getCanvas,
289
+ getVoyages,
290
+ getMarks,
291
+ clearCache() {
292
+ kept.key = '';
293
+ cachedMarks = null;
294
+ },
295
+ };
296
+ }
297
+
298
+ /**
299
+ * Paints a land's baked terrain grid onto an offscreen 2D canvas, either hillshaded from its
300
+ * terrain colours and normals or in flat parchment tone, with water overlaid where depth > 0.
301
+ *
302
+ * @param {{ colors: ArrayLike<number>, stride: number, normal: (index: number, out: number[]) => number[], depth: (index: number) => number }} land
303
+ * @param {boolean} shaded True for hillshaded terrain, false for parchment mode.
304
+ * @param {object} [options]
305
+ * @param {(x: number, y: number, salt: number) => number} [options.noise]
306
+ * @param {ReadonlyArray<number>} [options.parchment]
307
+ * @param {ReadonlyArray<number>} [options.water]
308
+ * @param {ReadonlyArray<number>} [options.waterPlain]
309
+ * @param {object} [options.host=globalThis]
310
+ * @returns {HTMLCanvasElement}
311
+ */
312
+ export function paintTerrainSheet(land, shaded, { noise, parchment, water, waterPlain, host = globalThis } = {}) {
313
+ const n = land.stride;
314
+ const rgb = [0, 0, 0];
315
+ const canvas = host.document.createElement('canvas');
316
+ canvas.width = canvas.height = n;
317
+ const ctx = canvas.getContext('2d');
318
+ const img = ctx.createImageData(n, n);
319
+ const data = img.data;
320
+ const opts = { noise, parchment, water, waterPlain };
321
+ for (let k = 0, o = 0; k < n * n; k++, o += 4) {
322
+ shadeTerrainPixel(land, k, shaded, rgb, opts);
323
+ data[o] = rgb[0];
324
+ data[o + 1] = rgb[1];
325
+ data[o + 2] = rgb[2];
326
+ data[o + 3] = 255;
327
+ }
328
+ ctx.putImageData(img, 0, 0);
329
+ return canvas;
330
+ }
@@ -0,0 +1,124 @@
1
+ /**
2
+ * High-contrast map overlay signs drawn last over every map sheet: the picked-place pulsing pin
3
+ * and the visitor's breathing position beacon with directional sight cone.
4
+ * @module
5
+ */
6
+ import { TAU } from '../math/scalar.js';
7
+ import { DEFAULT_MAP_FONT, drawMapGlyph } from './map-glyphs.js';
8
+
9
+ const PIN = '#ff6a45';
10
+ const INK = 'rgba(6,12,14,.9)';
11
+ const RING_SECONDS = 2.2;
12
+ const RINGS = 2;
13
+ const RING_REACH = 46;
14
+ const HEAD = 11;
15
+ const STEM = 34;
16
+ const NAME_PX = 17;
17
+
18
+ const YOU_HALO = 24;
19
+ const YOU_BREATH = 2.6;
20
+ const CONE = 0.62;
21
+ const CONE_REACH = 42;
22
+
23
+ /**
24
+ * Draws an animated selection pin with expanding concentric rings and an optional bold label above it.
25
+ *
26
+ * @param {CanvasRenderingContext2D} ctx
27
+ * @param {number} x Pin foot X in canvas pixels.
28
+ * @param {number} y Pin foot Y in canvas pixels.
29
+ * @param {number} u Physical pixels per CSS pixel.
30
+ * @param {string} [name] Optional title rendered above the pin head.
31
+ * @param {object} [options]
32
+ * @param {string} [options.font=DEFAULT_MAP_FONT]
33
+ * @param {number} [options.now] Elapsed seconds for ring animation (defaults to `performance.now() / 1000`).
34
+ */
35
+ export function paintMapPick(ctx, x, y, u, name, { font = DEFAULT_MAP_FONT, now } = {}) {
36
+ const t = now ?? performance.now() / 1000;
37
+ ctx.save();
38
+ for (let i = 0; i < RINGS; i++) {
39
+ const s = (t / RING_SECONDS + i / RINGS) % 1;
40
+ const r = (8 + s * (RING_REACH - 8)) * u;
41
+ ctx.globalAlpha = (1 - s) * 0.9;
42
+ ctx.lineWidth = 3 * u;
43
+ ctx.strokeStyle = PIN;
44
+ ctx.beginPath();
45
+ ctx.arc(x, y, r, 0, TAU);
46
+ ctx.stroke();
47
+ }
48
+ ctx.globalAlpha = 1;
49
+ ctx.fillStyle = INK;
50
+ ctx.beginPath();
51
+ ctx.arc(x, y, 5 * u, 0, TAU);
52
+ ctx.fill();
53
+ ctx.lineWidth = 2.5 * u;
54
+ ctx.strokeStyle = PIN;
55
+ ctx.stroke();
56
+ const hy = y - STEM * u;
57
+ const hr = HEAD * u;
58
+ const side = Math.asin(hr / (STEM * u));
59
+ ctx.beginPath();
60
+ ctx.moveTo(x, y - 4 * u);
61
+ ctx.arc(x, hy, hr, Math.PI / 2 + side, Math.PI / 2 - side + TAU);
62
+ ctx.closePath();
63
+ ctx.fillStyle = PIN;
64
+ ctx.fill();
65
+ ctx.lineWidth = 2.5 * u;
66
+ ctx.strokeStyle = INK;
67
+ ctx.stroke();
68
+ ctx.fillStyle = '#fff6ea';
69
+ ctx.beginPath();
70
+ ctx.arc(x, hy, hr * 0.38, 0, TAU);
71
+ ctx.fill();
72
+ if (name) {
73
+ ctx.font = 'bold ' + NAME_PX * u + 'px ' + font;
74
+ ctx.textAlign = 'center';
75
+ ctx.textBaseline = 'bottom';
76
+ ctx.lineJoin = 'round';
77
+ ctx.lineWidth = NAME_PX * 0.32 * u;
78
+ ctx.strokeStyle = INK;
79
+ const ny = hy - hr - 6 * u;
80
+ ctx.strokeText(name, x, ny);
81
+ ctx.fillStyle = '#ffe2d6';
82
+ ctx.fillText(name, x, ny);
83
+ }
84
+ ctx.restore();
85
+ }
86
+
87
+ /**
88
+ * Draws the visitor's map marker: a softly breathing blue halo, an optional directional sight wedge
89
+ * (when `yaw !== null`), and the player arrow disc.
90
+ *
91
+ * @param {CanvasRenderingContext2D} ctx
92
+ * @param {number} x Centre X in canvas pixels.
93
+ * @param {number} y Centre Y in canvas pixels.
94
+ * @param {number} u Physical pixels per CSS pixel.
95
+ * @param {number | null} yaw Facing angle in radians (0 = north), or null when direction is unknown.
96
+ * @param {object | boolean} [options=true] Boolean `breathe` flag or options object.
97
+ */
98
+ export function paintMapVisitor(ctx, x, y, u, yaw, options = true) {
99
+ const opts = typeof options === 'boolean' ? { breathe: options } : options;
100
+ const breathe = opts.breathe ?? true;
101
+ const t = opts.now ?? performance.now() / 1000;
102
+ const breath = breathe ? 0.5 + 0.5 * Math.sin((t / YOU_BREATH) * TAU) : 0.6;
103
+ ctx.save();
104
+ ctx.translate(x, y);
105
+ ctx.fillStyle = `rgba(64,160,255,${0.16 + 0.14 * breath})`;
106
+ ctx.beginPath();
107
+ ctx.arc(0, 0, (YOU_HALO - 5 + 5 * breath) * u, 0, TAU);
108
+ ctx.fill();
109
+ if (yaw !== null) {
110
+ ctx.rotate(-yaw);
111
+ const cone = ctx.createRadialGradient(0, 0, 4 * u, 0, 0, CONE_REACH * u);
112
+ cone.addColorStop(0, 'rgba(160,215,255,.55)');
113
+ cone.addColorStop(1, 'rgba(160,215,255,0)');
114
+ ctx.fillStyle = cone;
115
+ ctx.beginPath();
116
+ ctx.moveTo(0, 0);
117
+ ctx.arc(0, 0, CONE_REACH * u, -Math.PI / 2 - CONE, -Math.PI / 2 + CONE);
118
+ ctx.closePath();
119
+ ctx.fill();
120
+ }
121
+ if (opts.drawPlayer) opts.drawPlayer(ctx, u * 1.25);
122
+ else drawMapGlyph(ctx, 'player', 0, 0, u * 1.25);
123
+ ctx.restore();
124
+ }