-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpages.js
150 lines (127 loc) · 4.41 KB
/
pages.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const previewContainer = document.getElementById('preview-container');
// Configure amount of pages
let maxPages = 3;
let pages = new Array(maxPages);
let ctxs = new Array(maxPages);
let activePage = 0;
let loading = false;
const pageDescriptors = [
"Voor",
"Links",
"Rechts",
"Achter"
];
// Preview size on screen
const factor = 5;
const cssFactor = 1.9;
const previewWidth = cssWidth / factor;
const previewHeight = cssHeight / factor;
// Draw cutting margins
const cuttingLineOptions = {
stroke: 'blue',
strokeWidth: 3,
strokeDashArray: [5, 5],
selectable: false,
evented: false,
opacity: 0.5,
};
const insetPixels = postcardMargins * mmToInch * PPI;
const cuttingLineTop = new fabric.Line([0, insetPixels, canvas.width, insetPixels], cuttingLineOptions);
const cuttingLineBottom = new fabric.Line([0, canvas.height - insetPixels, canvas.width, canvas.height - insetPixels], cuttingLineOptions);
const cuttingLineLeft = new fabric.Line([insetPixels, 0, insetPixels, canvas.height], cuttingLineOptions);
const cuttingLineRight = new fabric.Line([canvas.width - insetPixels, 0, canvas.width - insetPixels, canvas.height], cuttingLineOptions);
const cuttingLineLayer = new fabric.Group([cuttingLineTop, cuttingLineBottom, cuttingLineLeft, cuttingLineRight], {
selectable: false,
evented: false,
});
canvas.add(cuttingLineLayer);
// Switch between pages
function LoadPage(i) {
loading = true;
canvas.remove(cuttingLineLayer); // Temporarily remove margins
UpdatePreview();
pages[activePage] = canvas.toJSON(); // Save current page
if (pages[i]) {
canvas.loadFromJSON(pages[i]);
} else {
canvas.clear();
}
ctxs[activePage].canvas.parentElement.classList.remove('active');
ctxs[i].canvas.parentElement.classList.add('active');
canvas.add(cuttingLineLayer); // Bring back margins
activePage = i;
loading = false;
}
function PreviousPage() {
if (activePage > 0) {
LoadPage(activePage - 1);
}
}
function NextPage() {
if (activePage < maxPages - 1) {
LoadPage(activePage + 1);
}
}
// Hide controls when rendering preview
function HideControls() {
let activeObject = canvas.getActiveObject();
if(activeObject) {
activeObject.hasControls = false;
activeObject.hasBorders = false;
canvas.renderAll();
}
}
function ShowControls() {
let activeObject = canvas.getActiveObject();
if (activeObject) {
activeObject.hasControls = true;
activeObject.hasBorders = true;
canvas.renderAll();
}
}
function UpdatePreview() {
if (loading) return;
// Set background
ctxs[activePage].clearRect(0, 0, previewWidth, previewHeight);
ctxs[activePage].fillStyle = "#ffffff";
ctxs[activePage].fillRect(0, 0, previewWidth, previewHeight);
HideControls();
cuttingLineLayer.visible = false;
canvas.renderAll();
ctxs[activePage].drawImage(canvas.lowerCanvasEl, 0, 0, previewWidth, previewHeight); // Copy canvas to preview
ShowControls();
cuttingLineLayer.visible = true;
}
// Create canvases
if (maxPages > 1) {
for (let i = 0; i < maxPages; i++) {
// Create preview canvas
let pCanvas = document.createElement('canvas');
pCanvas.width = previewWidth;
pCanvas.height = previewHeight;
pCanvas.style.width = previewWidth / cssFactor + 'px';
pCanvas.style.height = previewHeight / cssFactor + 'px';
// Create button
let pButton = document.createElement('div');
pButton.onclick = () => LoadPage(i);
pButton.className = 'preview-button';
if(i == 0) pButton.classList.add('active');
pButton.appendChild(pCanvas);
pButton.setAttribute('data-tooltip', pageDescriptors[i]);
previewContainer.appendChild(pButton);
// Fill background
ctxs[i] = pCanvas.getContext('2d');
ctxs[i].fillStyle = "#ffffff";
ctxs[i].fillRect(0, 0, previewWidth, previewHeight);
}
// For a full list of events, see https://github.com/fabricjs/fabric.js/wiki/Working-with-events or http://fabricjs.com/docs/fabric.Canvas.html#events
canvas.on({
'object:modified': UpdatePreview,
'object:added': UpdatePreview,
'object:removed': UpdatePreview,
'object:moving': UpdatePreview,
'object:scaling': UpdatePreview,
'object:rotating': UpdatePreview,
'object:skewing': UpdatePreview,
});
}