-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
351 lines (297 loc) · 9.27 KB
/
index.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//Variables
const canvasDOM = document.getElementById("cnv");
const inputFileDOM = document.getElementById("file");
const sheetWidthDOM = document.getElementById("sheet-width");
const sheetHeightDOM = document.getElementById("sheet-height");
const sheetMarginDOM = document.getElementById("sheet-margin");
const resultWidthDOM = document.getElementById("result-width");
const resultHeightDOM = document.getElementById("result-height");
const resultDOM = document.getElementById("results");
let imgAspectRatio = 0;
let imgWidth = 0;
let imgHeight = 0;
let canvasAspectRatio = 0;
let previewWidth = 0;
let previewHeight = 0;
let previewOffsetX = 0;
let previewOffsetY = 0;
let hojaWidth = 0;
let hojaHeight = 0;
let hojaMargin = 0;
let resultWidth = 0;
let resultHeight = 0;
let hojaWidthPX = 0;
let hojaHeightPX = 0;
let hojaMarginPX = 0;
let posterWidthPX = 0;
let posterHeightPX = 0;
let scaleXPX = 0;
let scaleYPX = 0;
let scalePX = 0;
let scaledImgWidthPX = 0;
let scaledImgHeightPX = 0;
let hojasAnchoPX = 0;
let hojasAltoPX = 0;
let fHojaWidth = 0;
let fHojaHeight = 0;
let fHojaMargin = 0;
let fPosterWidth = 0;
let fPosterHeight = 0;
let fScaleX = 0;
let fScaleY = 0;
let fScale = 0;
let fScaledImgWidth = 0;
let fScaledImgHeight = 0;
let fHojasAncho = 0;
let fHojasAlto = 0;
//Functions
function fnFitCanvas() {
canvasDOM.style.width = "100%";
canvasDOM.style.height = "100%";
canvasDOM.width = canvasDOM.offsetWidth;
canvasDOM.height = canvasDOM.offsetHeight;
canvasAspectRatio = canvasDOM.width / canvasDOM.height;
}
function fnShowImgOnCanvas(inputFile) {
var ctx = canvasDOM.getContext("2d");
var img = new Image;
img.src = URL.createObjectURL(inputFile.files[0]);
img.onload = function() {
imgWidth = img.width;
imgHeight = img.height;
imgAspectRatio = imgWidth / imgHeight;
if(imgAspectRatio > canvasAspectRatio) {
previewWidth = canvasDOM.width;
previewHeight = canvasDOM.width / imgAspectRatio;
} else {
previewWidth = canvasDOM.height * imgAspectRatio;
previewHeight = canvasDOM.height;
}
ctx.clearRect(0, 0, canvasDOM.width, canvasDOM.height);
previewOffsetX = (canvasDOM.width - previewWidth) / 2;
previewOffsetY = (canvasDOM.height - previewHeight) / 2;
ctx.drawImage(img, previewOffsetX, previewOffsetY, previewWidth, previewHeight);
fnCalcDivisionsPX();
//fnCalcDivisions();
fnCreatePartsPX(img);
//fnCreateParts(img);
}
}
function fnUpdateInputValues() {
hojaWidth = sheetWidthDOM.value;
hojaHeight = sheetHeightDOM.value;
hojaMargin = sheetMarginDOM.value;
resultWidth = resultWidthDOM.value;
resultHeight = resultHeightDOM.value;
fnShowImgOnCanvas(inputFileDOM);
}
function fnCM2PX(cm) {
//96px por pulgada
//2.54cm por pulgada
//return cm * (96 / 2.54);
// return cm * (Detector.dpi / 2.54);
return Detector.dpi * (cm / 2.54);
}
function fnCM2PXV2(cm) {
return cm * (Detector.dpi / 2.54);
}
function fnCalcDivisionsPX() {
hojaWidthPX = fnCM2PX(hojaWidth - (hojaMargin * 2));
hojaHeightPX = fnCM2PX(hojaHeight - (hojaMargin * 2));
hojaMarginPX = fnCM2PX(hojaMargin);
posterWidthPX = fnCM2PX(resultWidth);
posterHeightPX = fnCM2PX(resultHeight);
scaleXPX = posterWidthPX / imgWidth;
scaleYPX = posterHeightPX / imgHeight;
scalePX = Math.min(scaleXPX, scaleYPX);
scaledImgWidthPX = imgWidth * scalePX;
scaledImgHeightPX = imgHeight * scalePX;
hojasAnchoPX = Math.ceil(scaledImgWidthPX / hojaWidthPX);
hojasAltoPX = Math.ceil(scaledImgHeightPX / hojaHeightPX);
}
function fnCalcDivisions() {
fHojaWidth = hojaWidth;
fHojaHeight = hojaHeight;
fHojaMargin = hojaMargin;
fPosterWidth = resultWidth;
fPosterHeight = resultHeight;
fScaleX = fPosterWidth / imgWidth;
fScaleY = fPosterHeight / imgHeight;
fScale = Math.min(fScaleX, fScaleY);
fScaledImgWidth = imgWidth * fScaleX;
fScaledImgHeight = imgHeight * fScaleY;
fHojasAncho = Math.ceil(fScaledImgWidth / fHojaWidth);
fHojasAlto = Math.ceil(fScaledImgHeight / fHojaHeight);
}
function fnCreatePartsPX(img) {
resultDOM.innerHTML = "";
let hojaNum = 1;
let hojaTotal = hojasAltoPX * hojasAnchoPX;
for (let y = 0; y < hojasAltoPX; y++) {
for (let x = 0; x < hojasAnchoPX; x++) {
const startX = x * hojaWidthPX;
const startY = y * hojaHeightPX;
const tempHojaDiv = document.createElement("div");
tempHojaDiv.style.width = `${hojaWidthPX}px`;
tempHojaDiv.style.height = `${hojaHeightPX}px`;
tempHojaDiv.style.position = "relative";
tempHojaDiv.style.display = "inline-block";
tempHojaDiv.classList.add("partN");
const tempHojaNum = document.createElement("p");
tempHojaNum.style.fontSize = "0.8em";
tempHojaNum.style.textAlign = "right";
tempHojaNum.style.position = "absolute";
tempHojaNum.style.bottom = `${hojaMarginPX / 1.5}px`;
tempHojaNum.style.right = `${hojaMarginPX / 1.5}px`;
tempHojaNum.style.color = "black";
tempHojaNum.innerHTML = `(${hojaNum} / ${hojaTotal}):${x + 1} / ${y + 1}`;
const tempCanvas = document.createElement("canvas");
tempCanvas.width = hojaWidthPX - (hojaMarginPX * 2);
tempCanvas.height = hojaHeightPX - (hojaMarginPX * 2);
tempCanvas.style.position = "absolute";
tempCanvas.style.top = `${hojaMarginPX}px`;
tempCanvas.style.left = `${hojaMarginPX}px`;
const tempCtx = tempCanvas.getContext("2d");
tempCtx.clearRect(0, 0, tempCanvas.width, tempCanvas.height);
tempCtx.drawImage(
img,
startX / scalePX,
startY / scalePX,
hojaWidthPX / scalePX,
hojaHeightPX / scalePX,
0,
0,
tempCanvas.width,
tempCanvas.height
);
tempHojaDiv.appendChild(tempCanvas);
tempHojaDiv.appendChild(tempHojaNum);
resultDOM.appendChild(tempHojaDiv);
hojaNum++;
}
}
}
function fnCreateParts(img) {
resultDOM.innerHTML = "";
let hojaNum = 1;
let hojaTotal = fHojasAlto * fHojasAncho;
for (let y = 0; y < fHojasAlto; y++) {
for (let x = 0; x < fHojasAncho; x++) {
const startX = x * fHojaWidth;
const startY = y * fHojaHeight;
const tempHojaDiv = document.createElement("div");
tempHojaDiv.style.width = `${fHojaWidth}cm`;
tempHojaDiv.style.height = `${fHojaHeight}cm`;
tempHojaDiv.style.position = "relative";
tempHojaDiv.style.display = "inline-block";
tempHojaDiv.classList.add("partN");
const tempHojaNum = document.createElement("p");
tempHojaNum.style.fontSize = "0.5cm";
tempHojaNum.style.textAlign = "right";
tempHojaNum.style.position = "absolute";
tempHojaNum.style.bottom = `${fHojaMargin / 2}cm`;
tempHojaNum.style.right = `${fHojaMargin / 2}cm`;
tempHojaNum.style.color = "black";
tempHojaNum.innerHTML = `(${hojaNum} / ${hojaTotal}):${x + 1} / ${y + 1}`;
const tempCanvas = document.createElement("canvas");
tempCanvas.width = fnCM2PXV2(fHojaWidth - (fHojaMargin * 2));
tempCanvas.height = fnCM2PXV2(fHojaHeight - (fHojaMargin * 2));
tempCanvas.style.position = "absolute";
tempCanvas.style.top = `${fHojaMargin}cm`;
tempCanvas.style.left = `${fHojaMargin}cm`;
const tempCtx = tempCanvas.getContext("2d");
tempCtx.clearRect(0, 0, tempCanvas.width, tempCanvas.height);
tempCtx.drawImage(
img,
startX / fScale,
startY / fScale,
fHojaWidth / fScale,
fHojaHeight / fScale,
0,
0,
tempCanvas.width,
tempCanvas.height
);
tempHojaDiv.appendChild(tempCanvas);
tempHojaDiv.appendChild(tempHojaNum);
resultDOM.appendChild(tempHojaDiv);
hojaNum++;
}
}
}
function fnDOMToBase64(dom) {
var img = "";
domtoimage.toPng(dom).then(function(dataUrl) {
img = dataUrl;
return img;
});
}
let totalPages = 0;
let totalPagesCompleted = 0;
let pdf;
function fnPDFDownload() {
if(totalPagesCompleted >= totalPages) {
pdf.save("poster.pdf");
}
}
function fnDownloadPX() {
pdf = new window.jspdf.jsPDF({
unit: "cm",
format: [hojaWidth, hojaHeight],
});
totalPages = resultDOM.children.length;
for (let index = 0; index < totalPages; index++) {
const element = resultDOM.children[index];
domtoimage.toPng(element).then(function(dataUrl) {
const imgTemp = new Image();
imgTemp.src = dataUrl;
pdf.addImage(
imgTemp,
"PNG",
0,
0,
hojaWidth,
hojaHeight
);
totalPagesCompleted++;
if(totalPagesCompleted < totalPages) {
pdf.addPage();
}
fnPDFDownload();
});
}
}
function fnDownload() {
pdf = new window.jspdf.jsPDF({
unit: "cm",
format: [fHojaWidth, fHojaHeight],
});
totalPages = resultDOM.children.length;
for (let index = 0; index < totalPages; index++) {
const element = resultDOM.children[index];
domtoimage.toPng(element).then(function(dataUrl) {
const imgTemp = new Image();
imgTemp.src = dataUrl;
pdf.addImage(
imgTemp,
"PNG",
0,
0,
fHojaWidth,
fHojaHeight
);
totalPagesCompleted++;
if(totalPagesCompleted < totalPages) {
pdf.addPage();
}
fnPDFDownload();
});
}
}
//Start
fnFitCanvas();
fnUpdateInputValues();
window.onresize = () => {
fnFitCanvas();
fnUpdateInputValues();
}