32 lines
963 B
JavaScript
32 lines
963 B
JavaScript
import { createCanvasSurface } from '../core/canvas_surface.js';
|
|
|
|
export function drawCachedStaticLayer(targetCtx, shared, layerId, key, x, y, width, height, build) {
|
|
if (!targetCtx || !shared || typeof build !== 'function') return false;
|
|
if (!shared._staticLayers) shared._staticLayers = new Map();
|
|
let layer = shared._staticLayers.get(layerId);
|
|
const w = Math.max(1, Math.ceil(width));
|
|
const h = Math.max(1, Math.ceil(height));
|
|
const needsRebuild = !layer
|
|
|| layer.key !== key
|
|
|| layer.width !== w
|
|
|| layer.height !== h;
|
|
|
|
if (needsRebuild) {
|
|
layer = createCanvasSurface(w, h);
|
|
if (!layer) return false;
|
|
layer.key = key;
|
|
layer.ctx.save();
|
|
try {
|
|
layer.ctx.setTransform(1, 0, 0, 1, 0, 0);
|
|
layer.ctx.clearRect(0, 0, w, h);
|
|
build(layer.ctx);
|
|
} finally {
|
|
layer.ctx.restore();
|
|
}
|
|
shared._staticLayers.set(layerId, layer);
|
|
}
|
|
|
|
targetCtx.drawImage(layer.canvas, x, y);
|
|
return true;
|
|
}
|