-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas.js
54 lines (44 loc) · 1.95 KB
/
canvas.js
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
47
48
49
50
51
52
53
54
// Get DOM elements
const DOMCanvas = document.getElementById('canvas');
const toolbar = document.getElementById('toolbar');
const qrcode_container = document.getElementById('qrcode-container');
const save_btn = document.getElementById('save-btn');
// Get page size (in pixels)
const pageWidth = Math.max(document.documentElement.clientWidth, window.innerWidth || 0);
const pageHeight = Math.max(document.documentElement.clientHeight, window.innerHeight || 0);
const pageAspectRatio = pageWidth / pageHeight;
// Usable canvas size (in pixels)
const canvasInset = 100; // Margin around canvas (in pixels)
const canvasWidth = pageWidth - toolbar.offsetWidth - parseFloat(getComputedStyle(toolbar)['margin-right']) - canvasInset - save_btn.offsetWidth - 200;
const canvasHeight = pageHeight - canvasInset;
// Create canvas & configuration
const canvas = new fabric.Canvas('canvas', {
width: canvasWidth,
height: canvasHeight,
preserveObjectStacking: true
});
// Postcard size (in mm)
let postcardWidth = 135;
let postcardHeight = 135;
const postcardMargins = 3; // Cutting margins (in mm)
postcardWidth += postcardMargins * 2;
postcardHeight += postcardMargins * 2;
const aspectRatio = postcardWidth / postcardHeight;
const PPI = 300; // Pixels per inch (300 is standard for printing)
const mmToInch = 1 / 25.4;
let cssWidth = canvasWidth;
let cssHeight = canvasHeight;
if(aspectRatio >= pageAspectRatio) {
// Postcard is wider than page
cssHeight = canvasWidth / aspectRatio;
} else {
// Postcard is taller than page
cssWidth = canvasHeight * aspectRatio;
}
// Set canvas size
canvas.setWidth(postcardWidth * mmToInch * PPI);
canvas.setHeight(postcardHeight * mmToInch * PPI);
// Set canvas CSS size
canvas.setDimensions({width: `${cssWidth}px`, height: `${cssHeight}px`}, {cssOnly: true});
// toolbar.style.height = `calc(${cssHeight}px - 1.5rem)`; // Set toolbar height to canvas height
let sessionId = document.body.getAttribute('data-session-id');