All files / src/features/project-actions exportMergedPNG.ts

0% Statements 0/13
0% Branches 0/4
0% Functions 0/1
0% Lines 0/11

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                                               
export async function exportMergedPNG(): Promise<string> {
	const canvases = Array.from(
		document.querySelectorAll<HTMLCanvasElement>('[data-layer-id], #draw-canvas'),
	);
 
	if (canvases.length === 0) return '';
 
	const base = canvases[0];
	const merged = document.createElement('canvas');
	merged.width = base.width;
	merged.height = base.height;
 
	const ctx = merged.getContext('2d', { willReadFrequently: true, alpha: true })!;
 
	// Get a current background of editor
	ctx.fillStyle = window.getComputedStyle(document.body).backgroundColor || '#ffffff';
	ctx.fillRect(0, 0, merged.width, merged.height);
 
	// Draw layers
	for (const c of canvases) ctx.drawImage(c, 0, 0);
 
	return merged.toDataURL('image/png');
}