game-village-politics-frontend/app/workers/mapTiles.worker.ts

15 lines
646 B
TypeScript
Raw Normal View History

// Paints the map's village pictures off the main thread, one look at a time, and hands each
// back as a PNG the moment it is done (useMapTiles).
import { MAP_LOOKS, mapTileDrawing, paintTile } from '../diorama/mapTiles'
self.onmessage = async (e: MessageEvent<{ ids: string[]; size: number }>) => {
const { ids, size } = e.data
for (const id of ids) {
const look = MAP_LOOKS.find((l) => l.id === id)
if (!look) continue
const canvas = new OffscreenCanvas(size, size)
paintTile(canvas.getContext('2d')!, mapTileDrawing(look), size)
self.postMessage({ id, blob: await canvas.convertToBlob({ type: 'image/png' }) })
}
}