print.js (1.3 KB)


 1 // Rescale images against the mm-sized print cell on beforeprint, restore screen scale on afterprint
 2 
 3 const PRINT_CELL_SIZE_PX = CELL_SIZE_MM * 96 / 25.4; // Cell size in pixels at 96 DPI
 4 
 5 window.addEventListener('beforeprint', () => {
 6 	images.forEach(img => {
 7 		const imgElement = img.container.querySelector('img');
 8 		if (!imgElement || !img.naturalWidth || !img.naturalHeight) return;
 9 
10 		// Calculate print container size
11 		const printWidth = img.widthCells * PRINT_CELL_SIZE_PX;
12 		const printHeight = img.heightCells * PRINT_CELL_SIZE_PX;
13 
14 		// Recalculate base scale for print
15 		const isRotated = img.rotation % 180 !== 0;
16 		const effectiveW = isRotated ? img.naturalHeight : img.naturalWidth;
17 		const effectiveH = isRotated ? img.naturalWidth : img.naturalHeight;
18 		const printBaseScale = Math.max(printWidth / effectiveW, printHeight / effectiveH);
19 
20 		// Apply print transform
21 		const printScale = printBaseScale * img.userScale;
22 		imgElement.style.transform = `translate(-50%, -50%) scale(${printScale}) rotate(${img.rotation}deg) translate(${img.panX}px, ${img.panY}px)`;
23 	});
24 });
25 
26 window.addEventListener('afterprint', () => {
27 	// Wait for layout to settle after exiting print mode
28 	requestAnimationFrame(() => {
29 		requestAnimationFrame(() => {
30 			images.forEach(img => updateImagePosition(img));
31 		});
32 	});
33 });