68 lines
1.7 KiB
TypeScript
68 lines
1.7 KiB
TypeScript
import { type MapLayer } from '../layer/mapLayer'
|
|
import { buildCamera, type MapScene } from './mapRenderer'
|
|
import { type TileStore } from '../tile/tileStore'
|
|
|
|
export class CanvasOverlayRenderer {
|
|
canvas: any
|
|
ctx: any
|
|
dpr: number
|
|
layers: MapLayer[]
|
|
|
|
constructor(layers: MapLayer[]) {
|
|
this.canvas = null
|
|
this.ctx = null
|
|
this.dpr = 1
|
|
this.layers = layers
|
|
}
|
|
|
|
attachCanvas(canvasNode: any, width: number, height: number, dpr: number): void {
|
|
this.canvas = canvasNode
|
|
this.ctx = canvasNode.getContext('2d')
|
|
this.dpr = dpr || 1
|
|
|
|
canvasNode.width = Math.max(1, Math.floor(width * this.dpr))
|
|
canvasNode.height = Math.max(1, Math.floor(height * this.dpr))
|
|
|
|
if (typeof this.ctx.setTransform === 'function') {
|
|
this.ctx.setTransform(this.dpr, 0, 0, this.dpr, 0, 0)
|
|
} else {
|
|
this.ctx.scale(this.dpr, this.dpr)
|
|
}
|
|
}
|
|
|
|
clear(): void {
|
|
this.canvas = null
|
|
this.ctx = null
|
|
}
|
|
|
|
render(scene: MapScene, tileStore: TileStore, pulseFrame: number): void {
|
|
if (!this.ctx) {
|
|
return
|
|
}
|
|
|
|
const camera = buildCamera(scene)
|
|
const ctx = this.ctx
|
|
const previewScale = scene.previewScale || 1
|
|
const previewOriginX = scene.previewOriginX || scene.viewportWidth / 2
|
|
const previewOriginY = scene.previewOriginY || scene.viewportHeight / 2
|
|
|
|
ctx.clearRect(0, 0, scene.viewportWidth, scene.viewportHeight)
|
|
ctx.save()
|
|
ctx.translate(previewOriginX, previewOriginY)
|
|
ctx.scale(previewScale, previewScale)
|
|
ctx.translate(-previewOriginX, -previewOriginY)
|
|
|
|
for (const layer of this.layers) {
|
|
layer.draw({
|
|
ctx,
|
|
camera,
|
|
scene,
|
|
pulseFrame,
|
|
tileStore,
|
|
})
|
|
}
|
|
|
|
ctx.restore()
|
|
}
|
|
}
|