Repository navigation
Expand file tree
/
Copy pathstate.js
More file actions
565 lines (545 loc) · 33.2 KB
/
Copy pathstate.js
File metadata and controls
565 lines (545 loc) · 33.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/* state.js — shared hub for vwag-table.
* Owns the data every module reads/mutates: the `state` object, the offscreen canvases,
* the runtime caches, the `controls` DOM map, the app constants, and the pure utilities.
* Everything here is either immutable or MUTATED (never reassigned across a module
* boundary), so it survives ES-module live-binding rules. Modules import from here; nothing
* here imports back. (Module-level `let`s that get reassigned still live with their code
* and travel into their own modules as the split proceeds.)
*/
const canvas = document.getElementById("battlemap");
const ctx = canvas.getContext("2d");
// Fog is composited from two independent layers so erasing freeform fog can never get
// "stuck": polyCanvas holds the polygon/stamp areas, strokeCanvas holds the freeform
// brush/bucket layer (its own erases baked in chronologically), and fogCanvas is their
// union (what's drawn). liveCanvas previews the in-progress brush stroke.
const fogCanvas = document.createElement("canvas");
const fogCtx = fogCanvas.getContext("2d");
const liveCanvas = document.createElement("canvas");
const liveCtx = liveCanvas.getContext("2d");
const polyCanvas = document.createElement("canvas");
const polyCtx = polyCanvas.getContext("2d");
const strokeCanvas = document.createElement("canvas");
const strokeCtx = strokeCanvas.getContext("2d");
// Line-of-sight mask (step 5b): black everywhere the players' tokens can't currently see,
// cut out with the cast visibility polygons, composited over the player view only.
const losCanvas = document.createElement("canvas");
const losCtx = losCanvas.getContext("2d");
// 5c: the darkness overlay actually drawn to the player view (unexplored black, explored dim,
// visible clear), and a per-floor accumulating record of everywhere the party has ever seen.
// exploredMasks is runtime-only (it resets on reload) and keyed by floor id.
const darkCanvas = document.createElement("canvas");
const darkCtx = darkCanvas.getContext("2d");
// 5d: light coverage for the current frame (union of every placed light, wall-occluded and
// radius-clipped). When darkness is on, the player only sees line-of-sight INTERSECT this.
const lightCanvas = document.createElement("canvas");
const lightCtx = lightCanvas.getContext("2d");
// 5e: colored-light tint for the current frame — each source's wall-occluded reach painted in
// its own color, composited additively over the map on both the GM and player views.
const tintCanvas = document.createElement("canvas");
const tintCtx = tintCanvas.getContext("2d");
const exploredMasks = new Map(); // floorId -> offscreen canvas at fog-buffer resolution
const lightCache = new Map(); // "version|lx|ly|radius" -> light visibility polygon (cleared on invalidateCast)
const shell = document.querySelector(".app-shell");
const emptyState = document.getElementById("emptyState");
const channel = new BroadcastChannel("fog-table-state");
const APP_NAME = "Battlemap Screen and GM Streaming Tool";
const LEGACY_APP_NAME = "Fog Table";
const SAVE_FILE_VERSION = 11;
const DB_NAME = "fog-table-library";
const DB_VERSION = 5;
const MAP_STORE = "maps";
const IMAGE_STORE = "images";
const MODULE_STORE = "modules";
const SESSION_STORE = "sessions";
const TOKEN_STORE = "tokens";
// Small key/value store for app-level settings that must survive a reload and cannot live in
// localStorage — currently just the backup folder's FileSystemDirectoryHandle, which is
// structured-cloneable (so IndexedDB can hold it) but not stringifiable.
const SETTINGS_STORE = "settings";
const FOG_MAX_EDGE = 4096; // cap fog raster resolution so huge maps stay within canvas/memory limits
const HISTORY_LIMIT = 80;
// Tabler stair icons (24×24), copied verbatim from tabler.io.
// NEUTRAL = tabler/stairs · UP = tabler/stairs-up · DOWN = tabler/stairs-down.
const STAIRS_ICON_NEUTRAL = new Path2D("M22 5h-5v5h-5v5h-5v5h-5");
const STAIRS_ICON_UP = new Path2D("M22 6h-5v5h-5v5h-5v5h-5 M6 10v-7 M3 6l3 -3l3 3");
const STAIRS_ICON_DOWN = new Path2D("M22 21h-5v-5h-5v-5h-5v-5h-5 M18 3v7 M15 7l3 3l3 -3");
// Lucide map-link marker icons (24×24, stroke-only). A map-link is a marker placed on a world/town
// map that descends into a child map (chunk 2). Like CONDITIONS, each `d` feeds BOTH the canvas
// Path2D markers (built here) and the placement icon-picker buttons (built in main.js). Lucide
// <circle> elements are written as arc subpaths so a single Path2D draws the whole glyph. A placed
// link stores the destination map's record id verbatim (survives the fallon cutover); icon defaults
// to "map-pin". Source: lucide.dev, ISC license (see CREDITS.md).
const MAP_LINK_ICONS = [
{ id: "landmark", label: "Town", d: "M10 18v-7 M11.119 2.205a2 2 0 0 1 1.762 0l7.84 3.846A.5 .5 0 0 1 20.5 7h-17a.5 .5 0 0 1-.22-.949z M14 18v-7 M18 18v-7 M3 22h18 M6 18v-7" },
{ id: "house", label: "Building", d: "M15 21v-8a1 1 0 0 0-1-1h-4a1 1 0 0 0-1 1v8 M3 10a2 2 0 0 1 .709-1.528l7-6a2 2 0 0 1 2.582 0l7 6A2 2 0 0 1 21 10v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z" },
{ id: "castle", label: "Keep", d: "M10 5V3 M14 5V3 M15 21v-3a3 3 0 0 0-6 0v3 M18 3v8 M18 5H6 M22 11H2 M22 9v10a2 2 0 0 1-2 2H4a2 2 0 0 1-2-2V9 M6 3v8" },
{ id: "skull", label: "Dungeon", d: "m12.5 17-.5-1-.5 1h1z M15 22a1 1 0 0 0 1-1v-1a2 2 0 0 0 1.56-3.25 8 8 0 1 0-11.12 0A2 2 0 0 0 8 20v1a1 1 0 0 0 1 1z M14 12a1 1 0 1 0 2 0a1 1 0 1 0-2 0 M8 12a1 1 0 1 0 2 0a1 1 0 1 0-2 0" },
{ id: "tent", label: "Camp", d: "M3.5 21 14 3 M20.5 21 10 3 M15.5 21 12 15l-3.5 6 M2 21h20" },
{ id: "door-open", label: "Entrance", d: "M11 20H2 M11 4.562v16.157a1 1 0 0 0 1.242 .97L19 20V5.562a2 2 0 0 0-1.515-1.94l-4-1A2 2 0 0 0 11 4.561z M11 4H8a2 2 0 0 0-2 2v14 M14 12h.01 M22 20h-3" },
{ id: "signpost", label: "Road", d: "M12 13v8 M12 3v3 M2.354 10.354a1.207 1.207 0 0 1 0-1.708l2.06-2.06A2 2 0 0 1 5.828 6h12.344a2 2 0 0 1 1.414 .586l2.06 2.06a1.207 1.207 0 0 1 0 1.708l-2.06 2.06a2 2 0 0 1-1.414 .586H5.828a2 2 0 0 1-1.414-.586z" },
{ id: "map-pin", label: "Default", d: "M20 10c0 4.993-5.539 10.193-7.399 11.799a1 1 0 0 1-1.202 0C9.539 20.193 4 14.993 4 10a8 8 0 0 1 16 0 M9 10a3 3 0 1 0 6 0a3 3 0 1 0-6 0" },
];
const MAP_LINK_DEFAULT_ICON = "map-pin";
// Prebuilt canvas markers, keyed by icon id (mirrors STAIRS_ICON_*).
const MAP_LINK_ICON_PATHS = Object.fromEntries(MAP_LINK_ICONS.map((i) => [i.id, new Path2D(i.d)]));
// Which subsystems each map kind exposes (chunk 3). A world/region map and a town have no floors and
// no tactical initiative; only a battlemap does. Driven off the map's mapKind (set from the module
// record on load, chosen via the Map type selector). Toggled into body classes by applyMapKindCaps;
// the actual hiding is CSS (.cap-no-floors / .cap-no-initiative) so it overrides the live show/hide
// logic without touching it. Fog/LOS stay available everywhere — hiding regions is useful on any map.
const MAP_KIND_CAPS = {
world: { floors: false, initiative: false },
settlement: { floors: false, initiative: false },
battle: { floors: true, initiative: true },
};
// 5e status conditions (token.conditions holds the active ids). Each glyph is an SVG path in a
// 24x24 box, stroked like the stair icons; `d` is shared by the canvas markers (tokens.js builds a
// Path2D) and the authoring grid (index.html buttons, built in main.js from this list). The order
// here is the order markers appear above the token and buttons appear in the panel. Exhaustion is
// leveled, so it lives in its own field (token.exhaustion 0-6) and renders one marker with a numeral.
const CONDITIONS = [
{ id: "blinded", label: "Blinded", color: "#6b8cae", d: "M2 12 C5 7 19 7 22 12 C19 17 5 17 2 12 Z M12 9 a3 3 0 1 0 0.01 0 M4 4 L20 20" },
{ id: "charmed", label: "Charmed", color: "#d4537e", d: "M12 20 C12 20 3 14 3 8 A4.5 4.5 0 0 1 12 6 A4.5 4.5 0 0 1 21 8 C21 14 12 20 12 20 Z" },
{ id: "deafened", label: "Deafened", color: "#9c9a92", d: "M9 18 a5 5 0 0 1 -2 -4 a5 5 0 1 1 10 0 c0 2 -2 2 -2 4 M4 4 L20 20" },
{ id: "frightened", label: "Frightened", color: "#8f88e6", d: "M12 4 L12 14 M12 18 a0.7 0.7 0 1 0 0.01 0" },
{ id: "grappled", label: "Grappled", color: "#e07a4d", d: "M5 7 L10 12 L5 17 M19 7 L14 12 L19 17" },
{ id: "incapacitated", label: "Incapacitated", color: "#9c9a92", d: "M12 12 m-8 0 a8 8 0 1 0 16 0 a8 8 0 1 0 -16 0 M6 6 L18 18" },
{ id: "invisible", label: "Invisible", color: "#3fbf94", d: "M5 20 L5 11 a7 7 0 0 1 14 0 L19 20 L16 18 L13 20 L10 18 L7 20 Z M9.5 11 a0.7 0.7 0 1 0 0.01 0 M14.5 11 a0.7 0.7 0 1 0 0.01 0" },
{ id: "paralyzed", label: "Paralyzed", color: "#ef9f27", d: "M13 2 L5 13 L11 13 L9 22 L19 9 L13 9 Z" },
{ id: "petrified", label: "Petrified", color: "#b4b2a9", d: "M12 3 L20 7.5 L20 16.5 L12 21 L4 16.5 L4 7.5 Z M4 7.5 L12 12 L20 7.5 M12 12 L12 21" },
{ id: "poisoned", label: "Poisoned", color: "#97c459", d: "M12 3 C12 3 6 11 6 15 a6 6 0 0 0 12 0 C18 11 12 3 12 3 Z" },
{ id: "prone", label: "Prone", color: "#b4b2a9", d: "M12 4 L12 17 M6 11 L12 18 L18 11" },
{ id: "restrained", label: "Restrained", color: "#4f9be0", d: "M8 10 L8 7 a4 4 0 0 1 8 0 L16 10 M6 10 L18 10 L18 20 L6 20 Z" },
{ id: "stunned", label: "Stunned", color: "#f0b53e", d: "M10 4 L11.5 8 L15.5 9.5 L11.5 11 L10 15 L8.5 11 L4.5 9.5 L8.5 8 Z M17 13 L17.8 15 L19.8 15.8 L17.8 16.6 L17 18.6 L16.2 16.6 L14.2 15.8 L16.2 15 Z" },
{ id: "unconscious", label: "Unconscious", color: "#8d90a8", d: "M7 8 L13 8 L7 16 L13 16 M15 5 L19 5 L15 10 L19 10" },
{ id: "concentration", label: "Concentration", color: "#8f88e6", d: "M12 12 m-8 0 a8 8 0 1 0 16 0 a8 8 0 1 0 -16 0 M12 12 m-3 0 a3 3 0 1 0 6 0 a3 3 0 1 0 -6 0" },
];
const EXHAUSTION_ICON = { id: "exhaustion", label: "Exhaustion", color: "#e08a52", d: "M3 9 L16 9 L16 15 L3 15 Z M16 11 L19 11 L19 13 L16 13 M5 11 L7 11 L7 13 L5 13 Z" };
const FEET_PER_CELL = 5;
// Real-world distance one grid cell represents, per measurement system.
const MEASURE_UNITS = {
imperial: { perCell: 5, label: "ft" },
metric: { perCell: 1.5, label: "m" },
};
const PING_DURATION = 1300;
// The player-view broadcast region — a stable, GM-anchored 16:9 rectangle (the red
// box). Every player device fits THIS world-rectangle to its own screen, so the
// framing is identical across phone / laptop / TV and never depends on which device
// reported its size last. 1920x1080 = the 43" TV target aspect.
const PLAYER_FRAME_REF = { w: 1920, h: 1080 };
const controls = {
mapUpload: document.getElementById("mapUpload"),
dttUpload: document.getElementById("dttUpload"),
splashUpload: document.getElementById("splashUpload"),
splashEnabled: document.getElementById("splashEnabled"),
blackoutEnabled: document.getElementById("blackoutEnabled"),
losEnabled: document.getElementById("losEnabled"),
losOptions: document.getElementById("losOptions"),
loadLibrary: document.getElementById("loadLibrary"),
mapUp: document.getElementById("mapUp"),
saveSession: document.getElementById("saveSession"),
openPlayer: document.getElementById("openPlayer"),
exportLibrary: document.getElementById("exportLibrary"),
importLibrary: document.getElementById("importLibrary"),
backupFolder: document.getElementById("backupFolder"),
backupStatus: document.getElementById("backupStatus"),
gridEnabled: document.getElementById("gridEnabled"),
gridSnap: document.getElementById("gridSnap"),
gridSize: document.getElementById("gridSize"),
gridOffsetX: document.getElementById("gridOffsetX"),
gridOffsetY: document.getElementById("gridOffsetY"),
gridColor: document.getElementById("gridColor"),
gridOpacity: document.getElementById("gridOpacity"),
mapScale: document.getElementById("mapScale"),
brushSize: document.getElementById("brushSize"),
fogTint: document.getElementById("fogTint"),
gmFogOpacity: document.getElementById("gmFogOpacity"),
losBrightness: document.getElementById("losBrightness"),
losSource: document.getElementById("losSource"),
resetExplored: document.getElementById("resetExplored"),
tokenColor: document.getElementById("tokenColor"),
tokenCells: document.getElementById("tokenCells"),
tokenLabel: document.getElementById("tokenLabel"),
tokenType: document.getElementById("tokenType"),
tokenLight: document.getElementById("tokenLight"),
paletteGrid: document.getElementById("paletteGrid"),
paletteAdd: document.getElementById("paletteAdd"),
paletteSize: document.getElementById("paletteSize"),
paletteImport: document.getElementById("paletteImport"),
paletteFolder: document.getElementById("paletteFolder"),
paletteExport: document.getElementById("paletteExport"),
paletteJson: document.getElementById("paletteJson"),
paletteSelected: document.getElementById("paletteSelected"),
paletteSelectedName: document.getElementById("paletteSelectedName"),
paletteDelete: document.getElementById("paletteDelete"),
tokenLightVal: document.getElementById("tokenLightVal"),
tokenSelPanel: document.getElementById("tokenSelPanel"),
tokenSelType: document.getElementById("tokenSelType"),
tokenSelLabel: document.getElementById("tokenSelLabel"),
tokenSelColor: document.getElementById("tokenSelColor"),
tokenSelCells: document.getElementById("tokenSelCells"),
tokenSelLight: document.getElementById("tokenSelLight"),
tokenSelLightVal: document.getElementById("tokenSelLightVal"),
tokenSelConditions: document.getElementById("tokenSelConditions"),
tokenSelExhDown: document.getElementById("tokenSelExhDown"),
tokenSelExhUp: document.getElementById("tokenSelExhUp"),
tokenSelExhVal: document.getElementById("tokenSelExhVal"),
tokenSelDown: document.getElementById("tokenSelDown"),
tokenImage: document.getElementById("tokenImage"),
tokenImagePreview: document.getElementById("tokenImagePreview"),
tokenImageClear: document.getElementById("tokenImageClear"),
panMode: document.getElementById("panMode"),
fogToggle: document.getElementById("fogToggle"),
fogRibbon: document.getElementById("fogRibbon"),
polygonMode: document.getElementById("polygonMode"),
namedPolygonMode: document.getElementById("namedPolygonMode"),
brushMode: document.getElementById("brushMode"),
eraserMode: document.getElementById("eraserMode"),
stampMode: document.getElementById("stampMode"),
tokenMode: document.getElementById("tokenMode"),
aoeMode: document.getElementById("aoeMode"),
aoeOptions: document.getElementById("aoeOptions"),
aoeCircle: document.getElementById("aoeCircle"),
aoeSquare: document.getElementById("aoeSquare"),
aoeCone: document.getElementById("aoeCone"),
aoeColor: document.getElementById("aoeColor"),
aoeSelPanel: document.getElementById("aoeSelPanel"),
aoeSelRename: document.getElementById("aoeSelRename"),
aoeSelColor: document.getElementById("aoeSelColor"),
aoeSelSize: document.getElementById("aoeSelSize"),
aoeSelAngleRow: document.getElementById("aoeSelAngleRow"),
aoeSelAngleSlider: document.getElementById("aoeSelAngleSlider"),
aoeSelDelete: document.getElementById("aoeSelDelete"),
aoePresetsRow: document.getElementById("aoePresetsRow"),
aoeCustomSize: document.getElementById("aoeCustomSize"),
aoeAngleSlider: document.getElementById("aoeAngleSlider"),
aoeAngleRow: document.getElementById("aoeAngleRow"),
measureMode: document.getElementById("measureMode"),
measureOptions: document.getElementById("measureOptions"),
measureUnit: document.getElementById("measureUnit"),
measureCalibrate: document.getElementById("measureCalibrate"),
measureCalibrateRow: document.getElementById("measureCalibrateRow"),
scaleDist: document.getElementById("scaleDist"),
scaleUnit: document.getElementById("scaleUnit"),
calibrateScale: document.getElementById("calibrateScale"),
scaleReadout: document.getElementById("scaleReadout"),
gridCalibrate: document.getElementById("gridCalibrate"),
roundShape: document.getElementById("roundShape"),
squareShape: document.getElementById("squareShape"),
brushSizeRow: document.getElementById("brushSizeRow"),
brushShapeRow: document.getElementById("brushShapeRow"),
stampShapeRow: document.getElementById("stampShapeRow"),
fogToolHeading: document.getElementById("fogToolHeading"),
stampRect: document.getElementById("stampRect"),
stampSquare: document.getElementById("stampSquare"),
stampEllipse: document.getElementById("stampEllipse"),
stampCircle: document.getElementById("stampCircle"),
stampTriangle: document.getElementById("stampTriangle"),
clearFog: document.getElementById("clearFog"),
bucketFill: document.getElementById("bucketFill"),
undo: document.getElementById("undoBtn"),
redo: document.getElementById("redoBtn"),
brushOptions: document.getElementById("brushOptions"),
tokenOptions: document.getElementById("tokenOptions"),
tokenPalette: document.getElementById("tokenPalette"),
drawOptions: document.getElementById("drawOptions"),
drawMode: document.getElementById("drawMode"),
lightMode: document.getElementById("lightMode"),
lightOptions: document.getElementById("lightOptions"),
lightRadius: document.getElementById("lightRadius"),
lightRadiusVal: document.getElementById("lightRadiusVal"),
lightColor: document.getElementById("lightColor"),
darknessEnabled: document.getElementById("darknessEnabled"),
obstacleKind: document.getElementById("obstacleKind"),
showObstacles: document.getElementById("showObstacles"),
castDebug: document.getElementById("castDebug"),
fitMap: document.getElementById("fitMap"),
playerMatchDM: document.getElementById("playerMatchDM"),
playerFrameToggle: document.getElementById("playerFrameToggle"),
rotateMapLeft: document.getElementById("rotateMapLeft"),
rotateMapRight: document.getElementById("rotateMapRight"),
rotatePlayerLeft: document.getElementById("rotatePlayerLeft"),
rotatePlayerRight: document.getElementById("rotatePlayerRight"),
playerFit: document.getElementById("playerFit"),
lockPlayerSquare: document.getElementById("lockPlayerSquare"),
stairColor: document.getElementById("stairColor"),
addImageInput: document.getElementById("addImageInput"),
addNoteBtn: document.getElementById("addNoteBtn"),
imageSnap: document.getElementById("imageSnap"),
imageSelPanel: document.getElementById("imageSelPanel"),
imageShowPlayers: document.getElementById("imageShowPlayers"),
imageSize: document.getElementById("imageSize"),
imageRotation: document.getElementById("imageRotation"),
noteSelPanel: document.getElementById("noteSelPanel"),
noteSize: document.getElementById("noteSize"),
notesToggle: document.getElementById("notesToggle"),
notesPanel: document.getElementById("notesPanel"),
notesClose: document.getElementById("notesClose"),
notesAdd: document.getElementById("notesAdd"),
notesShowAll: document.getElementById("notesShowAll"),
notesHideAll: document.getElementById("notesHideAll"),
notesSearch: document.getElementById("notesSearch"),
notesList: document.getElementById("notesList"),
notesEditor: document.getElementById("notesEditor"),
notesLabel: document.getElementById("notesLabel"),
notesBody: document.getElementById("notesBody"),
notesDelete: document.getElementById("notesDelete"),
playerFrameColor: document.getElementById("playerFrameColor"),
playerFrameOpacity: document.getElementById("playerFrameOpacity"),
panelToggle: document.getElementById("panelToggle"),
copyDMView: document.getElementById("copyDMView"),
playerZoom: document.getElementById("playerZoom"),
playerOffsetX: document.getElementById("playerOffsetX"),
playerOffsetY: document.getElementById("playerOffsetY"),
modeHint: document.getElementById("modeHint"),
playerFullscreen: document.getElementById("playerFullscreen"),
playerFollow: document.getElementById("playerFollow"),
dpadUp: document.getElementById("dpadUp"),
dpadDown: document.getElementById("dpadDown"),
dpadLeft: document.getElementById("dpadLeft"),
dpadRight: document.getElementById("dpadRight"),
libraryDialog: document.getElementById("libraryDialog"),
savedMapList: document.getElementById("savedMapList"),
mapSize: document.getElementById("mapSize"),
nameDialog: document.getElementById("nameDialog"),
roomNameInput: document.getElementById("roomNameInput"),
aoeNameDialog: document.getElementById("aoeNameDialog"),
aoeNameInput: document.getElementById("aoeNameInput"),
stairMode: document.getElementById("stairMode"),
stairDialog: document.getElementById("stairDialog"),
stairFloorSelect: document.getElementById("stairFloorSelect"),
stairLabelInput: document.getElementById("stairLabelInput"),
stairSelPanel: document.getElementById("stairSelPanel"),
stairSelFloor: document.getElementById("stairSelFloor"),
stairSelLabel: document.getElementById("stairSelLabel"),
stairSelDelete: document.getElementById("stairSelDelete"),
mapLinkMode: document.getElementById("mapLinkMode"),
mapKindSelect: document.getElementById("mapKindSelect"),
mapJump: document.getElementById("mapJump"),
loadFallon: document.getElementById("loadFallon"),
fallonDialog: document.getElementById("fallonDialog"),
fallonMapList: document.getElementById("fallonMapList"),
fallonMapSize: document.getElementById("fallonMapSize"),
publishFallon: document.getElementById("publishFallon"),
pullFallon: document.getElementById("pullFallon"),
inviteFallon: document.getElementById("inviteFallon"),
inviteDialog: document.getElementById("inviteDialog"),
inviteCode: document.getElementById("inviteCode"),
inviteCopy: document.getElementById("inviteCopy"),
importPartyWorld: document.getElementById("importPartyWorld"),
mapLinkDialog: document.getElementById("mapLinkDialog"),
mapLinkTargetSelect: document.getElementById("mapLinkTargetSelect"),
mapLinkIconGrid: document.getElementById("mapLinkIconGrid"),
mapLinkLabelInput: document.getElementById("mapLinkLabelInput"),
mapLinkSelPanel: document.getElementById("mapLinkSelPanel"),
mapLinkSelTarget: document.getElementById("mapLinkSelTarget"),
mapLinkSelIconGrid: document.getElementById("mapLinkSelIconGrid"),
mapLinkSelLabel: document.getElementById("mapLinkSelLabel"),
mapLinkSelDelete: document.getElementById("mapLinkSelDelete"),
floorOverlay: document.getElementById("floorOverlay"),
floorName: document.getElementById("floorName"),
addFloorUp: document.getElementById("addFloorUp"),
addFloorDown: document.getElementById("addFloorDown"),
deleteFloor: document.getElementById("deleteFloor"),
pushToTable: document.getElementById("pushToTable"),
pinTable: document.getElementById("pinTable"),
followInitiative: document.getElementById("followInitiative"),
playerFloorBadge: document.getElementById("playerFloorBadge"),
playerPartyRoster: document.getElementById("playerPartyRoster"),
fitMapBtn: document.getElementById("fitMapBtn"),
initToggle: document.getElementById("initToggle"),
initiativePanel: document.getElementById("initiativePanel"),
initiativeOverlay: document.getElementById("initiativeOverlay"),
initShowPlayers: document.getElementById("initShowPlayers"),
initShowOverlay: document.getElementById("initShowOverlay"),
initClose: document.getElementById("initClose"),
initPrev: document.getElementById("initPrev"),
initNext: document.getElementById("initNext"),
initReset: document.getElementById("initReset"),
initRoundLabel: document.getElementById("initRoundLabel"),
initList: document.getElementById("initList"),
initAddForm: document.getElementById("initAddForm"),
initAddTokens: document.getElementById("initAddTokens"),
initImportChars: document.getElementById("initImportChars"),
initName: document.getElementById("initName"),
initRoll: document.getElementById("initRoll"),
initHp: document.getElementById("initHp"),
initType: document.getElementById("initType"),
};
const isPlayer = new URLSearchParams(window.location.search).get("view") === "player";
// True on the public sandbox host (vtt.worhl.net), or when ?sandbox is set (for local
// preview). Lets the client hide backend-only UI and auto-load a demo map. vtt is a
// different origin from game.worhl.net, so login/localStorage never carry over.
const isSandbox = window.location.hostname === "vtt.worhl.net"
|| new URLSearchParams(window.location.search).has("sandbox");
const DEFAULT_GM_FOG_OPACITY = 0.3;
const INITIAL_FLOOR_ID = "floor-1";
// Ephemeral player-screen follow-camera flags — deliberately NOT part of `state`, so they're never
// synced to the GM or saved to the DB. They live in the hub only so the read side (geometry.js's
// viewTransform/followView) and the write side (the view actions in app.js / later view.js) share one
// object across module boundaries. Mutated in place, never reassigned, so live bindings are safe.
const playerCam = { follow: false, fitZoom: false, ease: null };
// Active drawing-tool state, shared between the input/sync handlers (which write it) and the
// aoe-measure draw functions (which read it). The `aoe` sub-object is mirrored window-to-window by
// the sync handlers exactly as the old bare lets were; `measureLine` and the calibration fields are
// local to each window. Mutated in place, never reassigned. NOT part of the saved `state` document.
const tools = {
aoe: { shape: "circle", color: "#e2603a", sizeFt: 10, angle: -Math.PI / 2, template: { visible: false, x: 0, y: 0 } },
measureLine: null,
dragMeasureLine: null,
calibrating: null,
calibrationDraft: null,
lightRadius: 3, // radius (cells) applied to newly placed lights — small=torch, large=firepit
lightColor: "#ffd9a0", // color applied to newly placed lights (warm torch by default)
drawingRoom: [], // in-progress room polyline (native px) while drawing in Draw Mode
drawingObstacle: [], // in-progress obstacle polyline (native px) while drawing in Draw Mode
showObstacles: true, // GM-only obstacle overlay toggle ("Walls Visible to DM")
obstacleKind: "wall", // kind applied to newly drawn obstacles
};
// Last-rendered transform scale, cached by render() and read by every draw function so overlays and
// markers keep a constant on-screen width regardless of zoom. cur.k = screen px per world px,
// cur.ms = map.scale. Ephemeral render cache; not part of the saved `state` document.
const cur = { k: 1, ms: 1 };
// Orchestration hooks — wired once at startup (app.js) so feature modules can trigger a redraw, a
// redraw+sync, or a relay without importing the render/sync layer (which is split out last, step 14).
const hooks = { render: () => {}, renderAndSync: () => {}, relay: () => {}, pushHistory: () => {}, syncPanels: () => {} };
// GM Move-mode selection of non-token objects, read by the annotation draws (to ring the selected
// item) and written by the Move-mode handlers. Ephemeral; not part of the saved `state` document.
const sel = { image: null, note: null, token: null, stair: null, mapLink: null, aoe: null, playerTokens: [], marquee: null };
// Handle to the popped-out player window (GM side); the player side reaches the GM via window.opener.
// Mutated when the popup opens or a message identifies its source. Never rebound across modules.
const peerWindow = { ref: null };
// GM-side interaction + overlay state. mode = active tool; lastPointer = last cursor pos (for previews);
// castDebug = draw the LOS cast polygon; playerFrame* = the dashed rect on the GM screen showing the player viewport.
const ui = { mode: "pan", lastPointer: { clientX: 0, clientY: 0 }, castDebug: false, playerFrameColor: "#e24a4a", playerFrameOpacity: 0.9, showPlayerFrame: true, pinTable: false, followInitiative: false, roaming: false, tableMapId: null };
// The two background <img> sources: the active map and the splash image.
const scene = { map: new Image(), splash: new Image() };
// Cast/light visibility caches (promoted from app.js): the cast-polygon cache keyed by version+origin,
// and the per-frame key sets render() prunes against. Mutated by vision.js, pruned by render.
const castCache = new Map();
const castFrameKeys = new Set();
const lightFrameKeys = new Set();
// Ephemeral fog raster buffer + in-progress tool drafts (distinct from state.fog, the saved doc).
// Shared by fog.js (raster) and vision.js (reads resScale). Mutated, never rebound.
const fogBuf = { dirty: true, resScale: 1, activeStroke: null, stampDraft: null };
// Each floor is its own map (image + fog + tokens + stairs + view). The top-level
// imageData/fog/tokens/stairs/view fields below always mirror the CURRENT floor so the
// render/fog/token code can stay floor-agnostic; captureCurrentFloor()/applyFloor() swap
// them in and out of state.floors when navigating between floors.
function makeFloor(id) {
return {
id: id || `floor-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 6)}`,
imageId: "",
imageData: "",
imageName: "",
imageWidth: 0,
imageHeight: 0,
mapScale: 1,
rooms: [],
strokes: [],
tokens: [],
stairs: [],
mapLinks: [],
obstacles: [],
lights: [],
aoes: [],
images: [],
notes: [],
view: { scale: 1, cx: 0, cy: 0, rotation: 0 },
};
}
const state = {
imageId: "",
imageData: "",
imageName: "",
imageWidth: 0,
imageHeight: 0,
splash: { enabled: false, imageData: "", imageName: "" },
blackout: false,
los: { enabled: false, brightness: 0.5, darkness: false, source: "party" }, // line of sight + explored dim level + lighting gate (darkness: only lit areas are seen); source = whose sight is unioned into the visible area (party = every player token; later: active combatant / selected token)
grid: { enabled: true, snap: true, snapImages: false, size: 70, offsetX: 0, offsetY: 0, color: "#000000", opacity: 0.45 },
map: { scale: 1 },
fog: {
rooms: [],
strokes: [],
toolSize: 70,
toolShape: "round",
stampShape: "rectangle",
gmColor: "#080909",
gmOpacity: DEFAULT_GM_FOG_OPACITY,
},
tokens: [],
stairs: [],
// Map-links for the current floor — authored markers that descend into a child map (chunk 2).
// Mirrors the floor record like stairs; GM-only (never synced to the player display).
mapLinks: [],
// Obstacle geometry (walls/doors/windows…) for the current floor — authored, mirrors the
// floor record like tokens/stairs. Points are stored in cell units.
obstacles: [],
// Placed lights for the current floor (5d): { id, x, y, radius, color }. Authored, per-floor
// like obstacles. color (5e) tints the light's reach; absent on old saves -> warm default.
lights: [],
// Area-of-effect zones for the current floor — authored, per-floor like tokens/stairs. Mirrors
// the floor record (makeFloor); without it here, a freshly installed map with no prior floor
// restore (e.g. the sandbox autoload) left state.aoes undefined and AoE drops threw.
aoes: [],
// Droppable map images (synced to players; only those with showPlayers render there) and
// GM-only floating notes (never synced). Both are per-floor like tokens/stairs.
images: [],
notes: [],
stairColor: "#ffffff", // GM-only stair marker color (stairs never show on the player display)
// Initiative tracker: a turn order shared across all floors. When showPlayers is on, a
// compact order overlay is mirrored to the player display.
initiative: { active: false, showPlayers: false, showOverlay: true, round: 1, turn: 0, combatants: [] },
// Measurement: unit system + an optional calibrated cell size (world px) used when the
// grid overlay is off but the map has its own printed grid.
measure: { unit: "imperial", cellSize: 0, unitsPerPx: 0, scaleLabel: "" },
// Views carry a rotation (degrees) so the map can be re-oriented. The GM "rotate map"
// drives view.rotation; the player can be rotated independently via playerView.rotation.
view: { scale: 1, cx: 0, cy: 0, rotation: 0 },
playerView: { matchDM: true, scale: 1, cx: 0, cy: 0, rotation: 0, frameRef: PLAYER_FRAME_REF },
// Containment-trail metadata mirrored from the active map's module record. mapKind gates which
// subsystems run (chunk 3); parentId positions this map in the breadcrumb; source marks local vs
// fallon-served (chunk 4). Round-trip through splitState/mergeModuleSession/loadSnapshot.
mapKind: "battle",
parentId: null,
source: "local",
currentFloorId: INITIAL_FLOOR_ID,
activeFloorId: INITIAL_FLOOR_ID, // the floor the players' table is showing; == currentFloorId unless the table is pinned
floors: [makeFloor(INITIAL_FLOOR_ID)],
floorPosition: 1, // player-side display only
floorCount: 1, // player-side display only
floorSummary: [], // player-side only: names-only "rest of party" lines when the party is split
};
function normalizeInput(value) {
const number = Number(value);
return Number.isNaN(number) ? value : number;
}
function uuid() {
return crypto.randomUUID ? crypto.randomUUID() : `id-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 9)}`;
}
function escapeHtml(text) {
return String(text).replace(/[&<>"']/g, (ch) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" }[ch]));
}
export {
canvas, ctx, fogCanvas, fogCtx, liveCanvas, liveCtx, polyCanvas, polyCtx,
strokeCanvas, strokeCtx, losCanvas, losCtx, darkCanvas, darkCtx, lightCanvas, lightCtx, tintCanvas, tintCtx,
exploredMasks, lightCache, shell, emptyState, channel, APP_NAME, LEGACY_APP_NAME, SAVE_FILE_VERSION,
DB_NAME, DB_VERSION, MAP_STORE, IMAGE_STORE, MODULE_STORE, SESSION_STORE, TOKEN_STORE, SETTINGS_STORE, FOG_MAX_EDGE,
HISTORY_LIMIT, STAIRS_ICON_NEUTRAL, STAIRS_ICON_UP, STAIRS_ICON_DOWN, FEET_PER_CELL, MEASURE_UNITS, PING_DURATION, PLAYER_FRAME_REF, controls,
MAP_LINK_ICONS, MAP_LINK_ICON_PATHS, MAP_LINK_DEFAULT_ICON, MAP_KIND_CAPS,
CONDITIONS, EXHAUSTION_ICON,
isPlayer, isSandbox, DEFAULT_GM_FOG_OPACITY, INITIAL_FLOOR_ID, makeFloor, state, normalizeInput, uuid, escapeHtml,
playerCam,
tools,
cur,
hooks,
sel,
fogBuf,
peerWindow,
ui,
scene,
castCache,
castFrameKeys,
lightFrameKeys,
};