Tabletop maps
By the end of this page you have a hex map with measuring, area templates and fog of war that the game master paints and players see as solid.
@fieldnotes/vtt adds tabletop tools to a board: square and hex grids, a measuring tool, area templates and fog of war. Open the tabletop scene in the playground to try them on a hex map.
Install
npm install @fieldnotes/core @fieldnotes/vtt
Set up the table
registerVttElementTypes();
const fogPlugin = createFogPlugin();
const viewport = new Viewport(document.getElementById('board')!, {
plugins: [fogPlugin],
tools: [
new SelectTool(),
new HandTool(),
new MeasureTool({ feetPerCell: 5 }),
new TemplateTool({ templateShape: 'cone', feetPerCell: 5 }),
new FogTool(fogPlugin.manager, { operation: 'reveal', radius: 40 }),
],
});
registerVttElementTypes()registers the grid and template elements. Call it once per page, before a board loads a saved map. A second call throws.MeasureTool(measure) draws a ruler from press to release and reports the distance in cells and feet, atfeetPerCellfeet per cell (default 5). The ruler goes away on release and adds nothing to the board or to undo history.onMeasurement(listener)reports it while it changes.TemplateTool(template) places an area template: press at its origin and drag out its size. The shapes arecircle,cone,line,squareandrectangle, andsetOptions({ templateShape })switches between them. A template is an element, so it saves and undoes with the board.FogTool(fog) paints the fog, as the next sections show.
Add a grid
const grid = new GridController({
store: viewport.store,
recorder: viewport.historyRecorder,
requestRender: () => viewport.requestRender(),
getActiveLayerId: () => viewport.layerManager.activeLayerId,
toolContext: viewport.toolContext,
defaultGridSize: 48,
elementRegistry: viewport.elementRegistry,
constraintService: viewport.constraintProxy,
});
grid.add({ gridType: 'hex', hexOrientation: 'pointy', cellSize: 48 });
GridController keeps one grid on the board. add replaces any grid already there, update changes its type or cell size, and remove takes it away, each as one undo step. While a grid is on the board, the measure and template tools snap to it.
Paint the fog
const fog = fogPlugin.manager;
if (isGm)
fog.initialize({ bounds: { x: 0, y: 0, w: 2400, h: 1600 }, base: 'covered', cellSize: 32 });
fog.setViewMode(isGm ? 'editor' : 'player');
initializecovers the area in fog (base: 'covered') or leaves it clear ('revealed'), in cells ofcellSizeboard units. On a shared board only the game master calls it, and players receive the fog from the relay.- Fog is not drawn until
setViewModepicks a view.'editor'draws it translucent, so the game master still sees the map, and'player'draws it solid. - The game master paints with the fog tool.
operationis'reveal'or'conceal', andshapeis'brush'(sized byradius),'rectangle'or'polygon'.fogTool.setOptions({ operation: 'conceal' })switches. - The fog is saved with the board in
exportJSON(), and an export draws it as the current view shows it.
Share the fog
const client = new SyncClient({
store: viewport.store,
transport,
plugins: [createFogClientPlugin({ manager: fog })],
});
client.start();
// On the relay:
const { close } = createSyncServer({
port: 8080,
authenticate,
plugins: [createFogServerPlugin({ authorize: ({ role }) => role === 'gm' })],
});
createFogClientPlugin from @fieldnotes/vtt/sync sends fog edits through the sync client, and createFogServerPlugin from @fieldnotes/vtt/server applies them on the relay. Its authorize decides who may change the fog, here only the game master. With Redis, the backend also takes createFogBackendPlugin() from @fieldnotes/vtt/redis.
Fog hides the map on screen only. Every client in the room receives the whole fog and the elements under it, and the player view draws the fog over them. Put anything players must not receive, such as a trap or a secret door, on a layer the relay filters with canRead, as Multiplayer shows. The relay then never sends those elements to them, though the layer name is not hidden.