使用一个
ImageData
要直接将数组blit到临时画布,然后在单个操作中将其绘制到具有适当比例的目标画布,请执行以下操作:
const { width, height } = this.size;
const tCan = document.createElement('canvas');
// block scope to clean up temporary variables
{
const tCtx = tCan.getContext('2d');
const imgData = tCtx.createImageData(width, height);
tCan.width = width;
tCan.height = height;
imgData.data.set(this.pixArray);
tCtx.putImageData(imgData, 0, 0);
}
this.draw = (ctx, x, y, scale) => {
ctx.drawImage(tCan, x, y, Math.round(width * scale), Math.round(height * scale));
};