-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReapCardscript.js
More file actions
1034 lines (893 loc) · 37.7 KB
/
Copy pathReapCardscript.js
File metadata and controls
1034 lines (893 loc) · 37.7 KB
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Layout-Editor-Funktionalität
const canvas = document.getElementById('layout-canvas');
const ctx = canvas.getContext('2d');
const elements = [];
// Globale Variable für den Image Uplaod
let cardData = [];
let imageFiles = {};
if (!canvas) {
console.error('Canvas element not found');
}
if (!ctx) {
console.error('2D context not available');
}
ctx.font = '16px Arial';
function addElement(type, x, y, width, height, content, options = {}) {
elements.push({ type, x, y, width, height, content, ...options });
redrawCanvas();
}
const history = [];
let currentStep = -1;
function saveState() {
currentStep++;
history.splice(currentStep);
history.push(JSON.parse(JSON.stringify(elements)));
}
function undo() {
if (currentStep > 0) {
currentStep--;
elements.length = 0;
elements.push(...JSON.parse(JSON.stringify(history[currentStep])));
redrawCanvas();
}
}
function redo() {
if (currentStep < history.length - 1) {
currentStep++;
elements.length = 0;
elements.push(...JSON.parse(JSON.stringify(history[currentStep])));
redrawCanvas();
}
}
// Modify the addElement function to save state after adding an element
function addElement(type, x, y, width, height, content, options = {}) {
elements.push({ type, x, y, width, height, content, ...options });
redrawCanvas();
saveState(); // Add this line
}
canvas.addEventListener('click', (e) => {
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
const clickedElement = elements.find(el =>
x >= el.x && x <= el.x + el.width &&
y >= el.y && y <= el.y + el.height
);
if (clickedElement && (clickedElement.type === 'text' || clickedElement.type === 'box' || clickedElement.type === 'image-box')) {
showEditMenu(clickedElement, e.clientX, e.clientY);
}
});
// Add these lines at the end of the mouseup event listener for drag and resize operations
canvas.addEventListener('mouseup', () => {
if (isDragging || isResizing) {
saveState(); // Save state after drag or resize operation
}
isDragging = false;
isResizing = false;
currentElement = null;
});
// Modify the event listener for removing elements to save state
canvas.addEventListener('contextmenu', (e) => {
e.preventDefault();
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
const index = elements.findIndex(el =>
x >= el.x && x <= el.x + el.width &&
y >= el.y && y <= el.y + el.height
);
if (index !== -1) {
elements.splice(index, 1);
redrawCanvas();
saveState(); // Add this line
}
});
document.getElementById('apply-canvas-settings').addEventListener('click', function () {
const newWidth = document.getElementById('canvas-width').value;
const newHeight = document.getElementById('canvas-height').value;
const newRadius = document.getElementById('canvas-radius').value;
const newBgColor = document.getElementById('canvas-bg-color').value;
// Update canvas size
canvas.width = newWidth;
canvas.height = newHeight;
// Update global radius and background color
canvas.style.borderRadius = `${newRadius}px`;
canvas.style.backgroundColor = newBgColor;
// Adjust the border color for better visibility
ctx.strokeStyle = (newBgColor === '#ffffff') ? '#000000' : '#000000'; // Adjust color logic as needed
// Redraw canvas with new settings
redrawCanvas();
});
// Function to calculate the grid size for A4
function calculateGridSize() {
const a4Width = 2480; // A4 width at 300 DPI
const a4Height = 3508; // A4 height at 300 DPI
const cardWidth = canvas.width;
const cardHeight = canvas.height;
const cols = Math.floor(a4Width / cardWidth);
const rows = Math.floor(a4Height / cardHeight);
return { rows, cols };
}
// Update the grid info display
function updateGridInfo() {
const { rows, cols } = calculateGridSize();
const gridInfoDiv = document.getElementById('grid-info');
gridInfoDiv.textContent = `Fits: ${cols}x${rows} grid on A4`;
}
// Call this function whenever the canvas size changes
document.getElementById('apply-canvas-settings').addEventListener('click', updateGridInfo);
// Also, call it on page load to show the default value
window.addEventListener('load', updateGridInfo);
// Angepasste redrawCanvas-Funktion mit dynamischer Schriftgröße
function redrawCanvas() {
// Clear the entire canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Set the background color of the canvas
ctx.fillStyle = canvas.style.backgroundColor || '#ffffff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
elements.forEach(element => {
if (element.type === 'text' || element.type === 'box' || element.type === 'image-box') {
ctx.fillStyle = element.backgroundColor || 'white';
if (element.type === 'box') {
roundRect(ctx, element.x, element.y, element.width, element.height, 10, true, true);
} else if (element.type === 'text') {
ctx.setLineDash([5, 5]);
ctx.strokeRect(element.x, element.y, element.width, element.height);
ctx.setLineDash([]);
} else if (element.type === 'image-box') {
ctx.beginPath();
ctx.moveTo(element.x, element.y);
ctx.lineTo(element.x + element.width, element.y + element.height);
ctx.moveTo(element.x + element.width, element.y);
ctx.lineTo(element.x, element.y + element.height);
ctx.stroke();
} else {
ctx.fillStyle = element.fontColor || 'black';
fitTextToBox(ctx, element.content, element.x, element.y, element.width, element.height);
}
let fontSize = Math.min(element.width, element.height);
let padding = 5;
ctx.font = `${fontSize}px Arial`;
let textMetrics = ctx.measureText(element.content);
while ((textMetrics.width > element.width - 2 * padding || fontSize > element.height - 2 * padding) && fontSize > 0) {
fontSize--;
ctx.font = `${fontSize}px Arial`;
textMetrics = ctx.measureText(element.content);
}
const textX = element.x + element.width / 2;
const textY = element.y + element.height / 2;
ctx.textBaseline = 'middle';
ctx.textAlign = 'center';
ctx.fillStyle = element.fontColor || 'black';
ctx.fillText(element.content, textX, textY);
} else if (element.type === 'image' && element.image) {
ctx.drawImage(element.image, element.x, element.y, element.width, element.height);
}
const handleSize = 15;
ctx.fillStyle = 'black';
ctx.fillRect(element.x + element.width - handleSize, element.y + element.height - handleSize, handleSize, handleSize);
});
}
function roundRect(ctx, x, y, width, height, radius, fill, stroke) {
ctx.beginPath();
ctx.moveTo(x + radius, y);
ctx.lineTo(x + width - radius, y);
ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
ctx.lineTo(x + width, y + height - radius);
ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
ctx.lineTo(x + radius, y + height);
ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
ctx.lineTo(x, y + radius);
ctx.quadraticCurveTo(x, y, x + radius, y);
ctx.closePath();
if (fill) {
ctx.fill();
}
if (stroke) {
ctx.stroke();
}
}
// Funktion zum Hinzufügen einer Box (vereinfacht)
function addBox() {
addElement('box', 50, 50, 100, 50, 'Box', { fontColor: 'black', backgroundColor: 'white' });
}
// Funktion zum Anzeigen des Bearbeitungsmenüs
function showEditMenu(element, x, y) {
// Remove any existing menu
const oldMenu = document.querySelector('.edit-menu');
if (oldMenu) {
oldMenu.remove();
}
// Create a new menu
const menu = document.createElement('div');
menu.className = 'edit-menu';
menu.style.position = 'absolute';
menu.style.left = `${x}px`;
menu.style.top = `${y}px`;
menu.style.backgroundColor = 'white';
menu.style.border = '1px solid black';
menu.style.padding = '5px';
menu.style.zIndex = '1000';
// Name input field (common for all types)
const textLabel = document.createElement('label');
textLabel.textContent = 'Name:';
const textInput = document.createElement('input');
textInput.type = 'text';
textInput.value = element.content;
textInput.addEventListener('change', () => {
element.content = textInput.value;
redrawCanvas();
saveState();
});
// Append the name input field to the menu
menu.appendChild(textLabel);
menu.appendChild(textInput);
// Additional settings for box and text elements
if (element.type !== 'image-box') {
// Font color picker
const fontColorLabel = document.createElement('label');
fontColorLabel.textContent = 'Text Color:';
const fontColorPicker = document.createElement('input');
fontColorPicker.type = 'color';
fontColorPicker.value = element.fontColor || '#000000';
fontColorPicker.addEventListener('input', (event) => {
element.fontColor = event.target.value;
redrawCanvas();
saveState();
});
// Background color picker
const bgColorLabel = document.createElement('label');
bgColorLabel.textContent = 'Background Color:';
const bgColorPicker = document.createElement('input');
bgColorPicker.type = 'color';
bgColorPicker.value = element.backgroundColor || '#FFFFFF';
bgColorPicker.addEventListener('input', (event) => {
element.backgroundColor = event.target.value;
redrawCanvas();
saveState();
});
// Append additional settings to the menu
menu.appendChild(fontColorLabel);
menu.appendChild(fontColorPicker);
menu.appendChild(bgColorLabel);
menu.appendChild(bgColorPicker);
}
// Append the menu to the document body
document.body.appendChild(menu);
// Remove the menu when clicking outside
const removeMenu = (e) => {
if (!menu.contains(e.target)) {
menu.remove();
document.removeEventListener('click', removeMenu);
}
};
setTimeout(() => document.addEventListener('click', removeMenu), 0);
}
// Bild hinzufügen
function addImage() {
const input = document.createElement('input');
input.type = 'file';
input.accept = 'image/*';
input.onchange = e => {
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = event => {
const img = new Image();
img.onload = () => {
const element = {
type: 'image',
x: 50,
y: 50,
width: 100,
height: 100,
content: '',
image: img,
imageSrc: img.src // Speichern der Bild-URL
};
elements.push(element);
redrawCanvas();
saveState();
};
img.src = event.target.result;
};
reader.readAsDataURL(file);
};
input.click();
}
// Drag-and-Drop-Funktionalität
let isDragging = false;
let isResizing = false;
let currentElement = null;
canvas.addEventListener('mousedown', (e) => {
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
currentElement = elements.find(el =>
x >= el.x && x <= el.x + el.width &&
y >= el.y && y <= el.y + el.height
);
if (currentElement) {
const handleSize = 15; // Ensure this matches the handle size in redrawCanvas
const resizeHandleX = currentElement.x + currentElement.width - handleSize;
const resizeHandleY = currentElement.y + currentElement.height - handleSize;
if (x >= resizeHandleX && x <= resizeHandleX + handleSize &&
y >= resizeHandleY && y <= resizeHandleY + handleSize) {
isResizing = true;
} else {
isDragging = true;
}
}
});
canvas.addEventListener('mousemove', (e) => {
if (isDragging && currentElement) {
const rect = canvas.getBoundingClientRect();
currentElement.x = e.clientX - rect.left - currentElement.width / 2;
currentElement.y = e.clientY - rect.top - currentElement.height / 2;
redrawCanvas();
} else if (isResizing && currentElement) {
const rect = canvas.getBoundingClientRect();
currentElement.width = Math.max(e.clientX - rect.left - currentElement.x, 10); // Ensure minimum width
currentElement.height = Math.max(e.clientY - rect.top - currentElement.y, 10); // Ensure minimum height
redrawCanvas();
}
});
canvas.addEventListener('mouseup', () => {
isDragging = false;
isResizing = false;
currentElement = null;
});
// Box entfernen
canvas.addEventListener('contextmenu', (e) => {
e.preventDefault();
const rect = canvas.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
const index = elements.findIndex(el =>
x >= el.x && x <= el.x + el.width &&
y >= el.y && y <= el.y + el.height
);
if (index !== -1) {
elements.splice(index, 1);
redrawCanvas();
}
});
// Funktion zum Speichern des Layouts als JSON
function saveLayoutAsJSON() {
const layout = {
canvasWidth: canvas.width,
canvasHeight: canvas.height,
canvasBackgroundColor: canvas.style.backgroundColor || '#ffffff',
canvasBorderRadius: parseInt(canvas.style.borderRadius) || 0,
elements: elements.map(el => {
if (el.type === 'image' && el.image) {
return { ...el, imageSrc: el.image.src, image: null };
}
return el;
}),
version: "1.3" // Increment version to track this change
};
const json = JSON.stringify(layout);
const blob = new Blob([json], { type: "application/json" });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = "card_layout.json";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
}
// Funktion zum Laden des Layouts aus JSON
function loadLayoutFromJSON(file) {
const reader = new FileReader();
reader.onload = (event) => {
try {
const layout = JSON.parse(event.target.result);
if (layout.version === "1.3") {
// Restore canvas settings
canvas.width = layout.canvasWidth;
canvas.height = layout.canvasHeight;
canvas.style.backgroundColor = layout.canvasBackgroundColor;
canvas.style.borderRadius = `${layout.canvasBorderRadius}px`;
// Update the UI inputs with the loaded canvas settings
document.getElementById('canvas-width').value = layout.canvasWidth;
document.getElementById('canvas-height').value = layout.canvasHeight;
document.getElementById('canvas-radius').value = layout.canvasBorderRadius;
// Convert RGB to Hex if necessary
const backgroundColor = layout.canvasBackgroundColor;
const hexColor = rgbToHex(backgroundColor);
// Update the background color picker with the correct value
const colorPicker = document.getElementById('canvas-bg-color');
colorPicker.value = hexColor;
// Restore elements
elements.length = 0;
const imagePromises = layout.elements.map(el => {
if (el.type === 'image' && el.imageSrc) {
return new Promise((resolve) => {
const img = new Image();
img.onload = () => {
el.image = img;
resolve(el);
};
img.src = el.imageSrc;
});
}
return Promise.resolve(el);
});
Promise.all(imagePromises).then(loadedElements => {
elements.push(...loadedElements);
redrawCanvas(); // Ensure all elements are redrawn with the correct settings
saveState();
});
// Update the grid information after loading the layout
updateGridInfo(); // This line ensures the grid info is updated
} else {
alert("Unsupported layout version.");
}
} catch (error) {
alert("Error loading layout: " + error.message);
}
};
reader.readAsText(file);
}
// Utility function to convert RGB to Hex
function rgbToHex(rgb) {
if (rgb.startsWith('#')) return rgb; // If it's already hex, return it directly
const rgbArray = rgb.match(/\d+/g).map(Number);
if (rgbArray && rgbArray.length >= 3) {
const r = rgbArray[0].toString(16).padStart(2, '0');
const g = rgbArray[1].toString(16).padStart(2, '0');
const b = rgbArray[2].toString(16).padStart(2, '0');
return `#${r}${g}${b}`;
}
return '#ffffff'; // Fallback to white if there's an issue
}
// Batch-Verarbeitung und PNG-Generierung
const xlsxUpload = document.getElementById('xlsx-upload');
const generateButton = document.getElementById('generate-cards');
const progressBar = document.getElementById('progress-bar');
const outputFormatSelect = document.getElementById('output-format');
// Initial message when the page loads
document.getElementById('xlsx-upload-status').style.display = 'block';
document.getElementById('uploaded-xlsx-status-text').textContent = 'No file uploaded';
document.getElementById('uploaded-xlsx-status-text').style.color = 'red';
// XLSX-Datei einlesen
xlsxUpload.addEventListener('change', async (e) => {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (event) => {
try {
const data = new Uint8Array(event.target.result);
const workbook = XLSX.read(data, { type: 'array' });
const sheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[sheetName];
cardData = XLSX.utils.sheet_to_json(worksheet);
console.log('Card data loaded:', cardData);
// Display the checkmark and file name
document.getElementById('uploaded-xlsx-status-text').textContent = `Uploaded: ${file.name}`;
document.getElementById('uploaded-xlsx-status-text').style.color = 'green';
} catch (error) {
console.error('Error reading XLSX file:', error);
document.getElementById('uploaded-xlsx-status-text').textContent = 'Error reading file';
document.getElementById('uploaded-xlsx-status-text').style.color = 'red';
}
};
reader.readAsArrayBuffer(file);
} else {
document.getElementById('uploaded-xlsx-status-text').textContent = 'No file uploaded';
document.getElementById('uploaded-xlsx-status-text').style.color = 'red';
}
});
// XLSX-Datei einlesen
xlsxUpload.addEventListener('change', async (e) => {
const file = e.target.files[0];
if (file) {
const reader = new FileReader();
reader.onload = (event) => {
const data = new Uint8Array(event.target.result);
const workbook = XLSX.read(data, {type: 'array'});
const sheetName = workbook.SheetNames[0];
const worksheet = workbook.Sheets[sheetName];
cardData = XLSX.utils.sheet_to_json(worksheet);
console.log('Card data loaded:', cardData);
// Display the checkmark and file name
document.getElementById('uploaded-xlsx-name').textContent = file.name;
document.getElementById('xlsx-upload-status').style.display = 'block';
};
reader.readAsArrayBuffer(file);
}
});
// Function to create an XLSX template based on the canvas elements
function generateXLSXTemplate() {
const workbook = XLSX.utils.book_new();
const worksheetData = [];
const headers = [];
// Extract the relevant elements from the canvas
elements.forEach(element => {
if (element.type === 'text' || element.type === 'box' || element.type === 'image-box') {
headers.push(element.content); // Use the content as the header name
}
});
// Remove duplicates and create headers row
const uniqueHeaders = [...new Set(headers)];
worksheetData.push(uniqueHeaders);
// Create a worksheet
const worksheet = XLSX.utils.aoa_to_sheet(worksheetData);
XLSX.utils.book_append_sheet(workbook, worksheet, 'Template');
// Generate XLSX file
const xlsxFile = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
// Trigger the download
const blob = new Blob([xlsxFile], { type: 'application/octet-stream' });
const a = document.createElement('a');
a.href = URL.createObjectURL(blob);
a.download = 'card_template.xlsx';
a.click();
URL.revokeObjectURL(a.href);
}
// Event listener for the button
document.getElementById('generate-xlsx-template').addEventListener('click', generateXLSXTemplate);
// New function to handle image file uploads
function handleImageUpload(event) {
const files = event.target.files;
for (let i = 0; i < files.length; i++) {
const file = files[i];
imageFiles[file.name] = file;
}
console.log('Imagedate loaded:', Object.keys(imageFiles));
// Prevent any potential default behavior
event.preventDefault();
}
// Add a new file input for images
const imageUpload = document.createElement('input');
imageUpload.type = 'file';
imageUpload.multiple = true;
imageUpload.accept = 'image/*';
imageUpload.style.display = 'none'; // Hide the input
imageUpload.addEventListener('change', handleImageUpload);
document.body.appendChild(imageUpload); // Append to the document
const uploadPicturesButton = document.getElementById('upload-images');
uploadPicturesButton.addEventListener('click', (event) => {
event.preventDefault(); // Prevent any default behavior
console.log('Button clicked');
imageUpload.click();
});
//upload pictures to canvas
async function loadImage(imageName) {
return new Promise((resolve, reject) => {
if (!imageName) {
reject(new Error('No Image Name provided'));
return;
}
const file = imageFiles[imageName];
if (!file) {
reject(new Error(`Image not found: ${imageName}`));
return;
}
const reader = new FileReader();
reader.onload = (event) => {
const img = new Image();
img.onload = () => {
console.log(`Image loaded: ${imageName}`);
resolve(img);
};
img.onerror = (error) => {
console.error(`error while loading the image: ${imageName}`, error);
reject(new Error(`cannot load image: ${imageName}`));
};
img.src = event.target.result;
};
reader.onerror = (error) => {
console.error(`error while loading the imagefile: ${imageName}`, error);
reject(new Error(`error while loading the imagefile: ${imageName}`));
};
reader.readAsDataURL(file);
});
}
async function generateCard(data) {
const cardCanvas = document.createElement('canvas');
cardCanvas.width = canvas.width; // Use current canvas width
cardCanvas.height = canvas.height; // Use current canvas height
const cardCtx = cardCanvas.getContext('2d');
cardCtx.fillStyle = canvas.style.backgroundColor || 'white';
cardCtx.fillRect(0, 0, cardCanvas.width, cardCanvas.height);
cardCtx.lineWidth = 2;
cardCtx.strokeStyle = ctx.strokeStyle || 'black';
roundRect(cardCtx, 1, 1, cardCanvas.width - 2, cardCanvas.height - 2, parseInt(canvas.style.borderRadius) || 0, false, true);
for (const element of elements) {
if (element.type === 'text' || element.type === 'box') {
cardCtx.fillStyle = element.backgroundColor || 'white';
if (element.type === 'box') {
roundRect(cardCtx, element.x, element.y, element.width, element.height, parseInt(canvas.style.borderRadius) || 0, true, true);
}
cardCtx.fillStyle = element.fontColor || 'black';
const content = data[element.content] || element.content;
fitTextToBox(cardCtx, content, element.x, element.y, element.width, element.height);
} else if (element.type === 'image' && element.image) {
cardCtx.drawImage(element.image, element.x, element.y, element.width, element.height);
} else if (element.type === 'image-box') {
const imageName = data[element.content];
if (imageName) {
try {
const img = await loadImage(imageName);
cardCtx.drawImage(img, element.x, element.y, element.width, element.height);
} catch (error) {
cardCtx.fillStyle = 'red';
cardCtx.fillRect(element.x, element.y, element.width, element.height);
cardCtx.fillStyle = 'white';
cardCtx.font = '12px Arial';
cardCtx.fillText('Image Load Error', element.x + 5, element.y + 20);
}
} else {
cardCtx.fillStyle = 'yellow';
cardCtx.fillRect(element.x, element.y, element.width, element.height);
cardCtx.fillStyle = 'black';
cardCtx.font = '12px Arial';
cardCtx.fillText('No Image Name', element.x + 5, element.y + 20);
}
}
}
return new Promise((resolve) => {
cardCanvas.toBlob((blob) => {
resolve(blob);
}, 'image/png');
});
}
function fitTextToBox(ctx, text, x, y, width, height) {
// Convert text to string and handle undefined/null cases
text = String(text || '');
const padding = 5;
let fontSize = Math.min(height, width) / 2; // Start with a large font size
ctx.font = `${fontSize}px Arial`;
let lines = [];
let currentLine = '';
const words = text.split(' ');
while (fontSize > 8) { // Minimum font size
lines = [];
currentLine = '';
for (let word of words) {
const testLine = currentLine + word + ' ';
const metrics = ctx.measureText(testLine);
const testWidth = metrics.width;
if (testWidth > width - 2 * padding) {
lines.push(currentLine);
currentLine = word + ' ';
} else {
currentLine = testLine;
}
}
lines.push(currentLine);
if (lines.length * fontSize <= height - 2 * padding) {
break;
}
fontSize--;
ctx.font = `${fontSize}px Arial`;
}
const lineHeight = fontSize * 1.2;
const totalTextHeight = lines.length * lineHeight;
let offsetY = y + (height - totalTextHeight) / 2 + fontSize;
for (let line of lines) {
const lineWidth = ctx.measureText(line).width;
const offsetX = x + (width - lineWidth) / 2;
ctx.fillText(line.trim(), offsetX, offsetY);
offsetY += lineHeight;
}
}
// Function to generate cards on A4 based on dynamic grid size
async function generateCardsOnA4(cardBlobs) {
const { rows, cols } = calculateGridSize();
const totalCardsPerPage = rows * cols;
const a4Canvas = document.createElement('canvas');
a4Canvas.width = 2480; // A4 width at 300 DPI
a4Canvas.height = 3508; // A4 height at 300 DPI
const a4Ctx = a4Canvas.getContext('2d');
a4Ctx.fillStyle = 'white';
a4Ctx.fillRect(0, 0, a4Canvas.width, a4Canvas.height);
// Get the spacing option
const addSpacing = document.getElementById('card-spacing-toggle').checked;
const margin = addSpacing ? 20 : 0;
const marginX = (a4Canvas.width - (cols * canvas.width + (cols - 1) * margin)) / 2;
const marginY = (a4Canvas.height - (rows * canvas.height + (rows - 1) * margin)) / 2;
let loadedImages = 0;
for (let i = 0; i < cardBlobs.length; i++) {
const img = new Image();
img.onload = () => {
const row = Math.floor((loadedImages % totalCardsPerPage) / cols);
const col = loadedImages % cols;
const x = marginX + col * (canvas.width + margin);
const y = marginY + row * (canvas.height + margin);
a4Ctx.drawImage(img, x, y, canvas.width, canvas.height);
loadedImages++;
// If this is the last card on the page or the last card in the batch
if (loadedImages % totalCardsPerPage === 0 || loadedImages === cardBlobs.length) {
a4Canvas.toBlob((a4Blob) => {
if (a4Blob) {
const a = document.createElement('a');
const objectURL = URL.createObjectURL(a4Blob);
a.href = objectURL;
a.download = `a4_page_${Math.ceil(loadedImages / totalCardsPerPage)}.png`;
a.click();
URL.revokeObjectURL(objectURL);
} else {
console.error("Failed to create blob for A4 page");
alert("Error generating cards: Could not create blob.");
}
}, 'image/png');
}
// Reset the canvas for the next page if needed
if (loadedImages % totalCardsPerPage === 0 && loadedImages < cardBlobs.length) {
a4Ctx.clearRect(0, 0, a4Canvas.width, a4Canvas.height);
a4Ctx.fillStyle = 'white';
a4Ctx.fillRect(0, 0, a4Canvas.width, a4Canvas.height);
}
};
img.onerror = (error) => {
console.error("Failed to load image for card generation", error);
alert("Error generating cards: Could not load image.");
};
img.src = URL.createObjectURL(cardBlobs[i]);
}
}
/// Batch-Verarbeitung aller Karten
generateButton.addEventListener('click', async () => {
if (cardData.length === 0) {
alert('Please upload an XLSX file first.');
return;
}
if (Object.keys(imageFiles).length === 0) {
alert('Please upload an image file first.');
return;
}
const totalCards = cardData.length;
progressBar.max = totalCards;
progressBar.value = 0;
const outputFormat = outputFormatSelect.value;
const cardBlobs = [];
try {
for (let i = 0; i < totalCards; i++) {
const cardBlob = await generateCard(cardData[i]);
cardBlobs.push(cardBlob);
progressBar.value = i + 1;
}
if (outputFormat === 'single') {
for (let i = 0; i < cardBlobs.length; i++) {
const a = document.createElement('a');
const objectURL = URL.createObjectURL(cardBlobs[i]);
a.href = objectURL;
a.download = `card_${i + 1}.png`;
a.click();
URL.revokeObjectURL(objectURL);
}
} else if (outputFormat === 'a4') {
await generateCardsOnA4(cardBlobs);
}
alert('generating cards finished!');
} catch (error) {
console.error("Error during card generation:", error);
alert("Error during card generation: " + error.message);
}
});
// Vorschaufunktion
async function previewCard(data) {
const previewCanvas = document.createElement('canvas');
previewCanvas.width = canvas.width; // Use current canvas width
previewCanvas.height = canvas.height; // Use current canvas height
const previewCtx = previewCanvas.getContext('2d');
previewCtx.fillStyle = canvas.style.backgroundColor || 'white';
previewCtx.fillRect(0, 0, previewCanvas.width, previewCanvas.height);
previewCtx.lineWidth = 2;
previewCtx.strokeStyle = ctx.strokeStyle || 'black';
roundRect(previewCtx, 1, 1, previewCanvas.width - 2, previewCanvas.height - 2, parseInt(canvas.style.borderRadius) || 0, false, true);
for (const element of elements) {
if (element.type === 'text' || element.type === 'box') {
previewCtx.fillStyle = element.backgroundColor || 'white';
if (element.type === 'box') {
roundRect(previewCtx, element.x, element.y, element.width, element.height, parseInt(canvas.style.borderRadius) || 0, true, true);
}
previewCtx.fillStyle = element.fontColor || 'black';
const content = data[element.content] || element.content;
fitTextToBox(previewCtx, content, element.x, element.y, element.width, element.height);
} else if (element.type === 'image' && element.image) {
previewCtx.drawImage(element.image, element.x, element.y, element.width, element.height);
} else if (element.type === 'image-box') {
const imageName = data[element.content];
if (imageName) {
try {
const img = await loadImage(imageName);
previewCtx.drawImage(img, element.x, element.y, element.width, element.height);
} catch (error) {
previewCtx.fillStyle = 'red';
previewCtx.fillRect(element.x, element.y, element.width, element.height);
previewCtx.fillStyle = 'white';
previewCtx.font = '12px Arial';
previewCtx.fillText('Image Load Error', element.x + 5, element.y + 20);
}
} else {
previewCtx.fillStyle = 'yellow';
previewCtx.fillRect(element.x, element.y, element.width, element.height);
previewCtx.fillStyle = 'black';
previewCtx.font = '12px Arial';
previewCtx.fillText('No Image Name', element.x + 5, element.y + 20);
}
}
}
return previewCanvas;
}
//Bild aus Excel Liste holen
function addImageBox() {
const imageBox = {
type: 'image-box',
x: 50,
y: 50,
width: 200,
height: 200,
content: 'New Image Box', // Default name
backgroundColor: 'white'
};
elements.push(imageBox);
redrawCanvas();
saveState();
showEditMenu(imageBox, imageBox.x + imageBox.width / 2, imageBox.y + imageBox.height / 2);
}
// Vorschau-Button-Funktionalität
document.getElementById('preview').addEventListener('click', async () => {
if (cardData.length > 0) {
const previewCanvas = await previewCard(cardData[0]);
const previewWindow = window.open('', 'Card Preview', 'width=697,height=1075');
previewWindow.document.body.appendChild(previewCanvas);
} else {
alert('Please upload an XLSX file first.');
}
});
// Event-Listener für UI-Elemente
document.getElementById('add-box').addEventListener('click', addBox);
document.getElementById('add-text').addEventListener('click', () => {
addElement('text', 50, 50, 200, 30, 'New Text');
});
document.getElementById('add-image').addEventListener('click', addImage);
document.getElementById('add-image-box').addEventListener('click', addImageBox);
document.getElementById('undo').addEventListener('click', undo);
document.getElementById('redo').addEventListener('click', redo);
document.getElementById('save-layout').addEventListener('click', saveLayoutAsJSON);
document.getElementById('load-layout').addEventListener('click', () => document.getElementById('load-layout-input').click());
document.getElementById('load-layout-input').addEventListener('change', (e) => {
if (e.target.files.length > 0) {
loadLayoutFromJSON(e.target.files[0]);
}
});
document.getElementById('canvas-standard-size').addEventListener('change', function () {
const selectedValue = this.value;
if (selectedValue) {
const [width, height] = selectedValue.split(',').map(Number);
document.getElementById('canvas-width').value = width;
document.getElementById('canvas-height').value = height;