All files / worker canvas.ts

60.6% Statements 20/33
50% Branches 2/4
66.66% Functions 2/3
60.6% Lines 20/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46  1x   1x 1x 1x                 1x 1x 12x     12x 12x 12x 12x   1x                     1x 6x     6x 6x 6x 6x 6x 1x  
/** biome-ignore-all lint/suspicious/noExplicitAny: global declaration */
import { Canvas, createCanvas, DOMMatrix, ImageData, Path2D, type SKRSContext2D } from '@napi-rs/canvas';
 
(global as any).DOMMatrix = DOMMatrix;
(global as any).Path2D = Path2D;
(global as any).ImageData = ImageData;
 
export { Canvas, type SKRSContext2D };
 
export interface CanvasAndContext {
	canvas: Canvas | null;
	context: SKRSContext2D | null;
}
 
export class CanvasFactory {
	create(width: number, height: number): CanvasAndContext {
		if (width <= 0 || height <= 0) {
			throw new Error('Invalid canvas size');
		}
		const canvas = createCanvas(width, height);
		const context = canvas.getContext('2d');
		return { canvas, context };
	}
 
	reset(canvasAndContext: CanvasAndContext, width: number, height: number): void {
		if (!canvasAndContext.canvas) {
			throw new Error('Canvas is not specified');
		}
		if (width <= 0 || height <= 0) {
			throw new Error('Invalid canvas size');
		}
		canvasAndContext.canvas.width = width;
		canvasAndContext.canvas.height = height;
	}
 
	destroy(canvasAndContext: CanvasAndContext): void {
		if (!canvasAndContext.canvas) {
			throw new Error('Canvas is not specified');
		}
		canvasAndContext.canvas.width = 0;
		canvasAndContext.canvas.height = 0;
		canvasAndContext.canvas = null;
		canvasAndContext.context = null;
	}
}