cr8 commited on
Commit
b971f5e
·
verified ·
1 Parent(s): 54e243c

Create script.js

Browse files
Files changed (1) hide show
  1. script.js +205 -0
script.js ADDED
@@ -0,0 +1,205 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ let images = [];
2
+ let gridOrder = [];
3
+
4
+ function loadImages() {
5
+ const files = document.getElementById('imageInput').files;
6
+ const preview = document.getElementById('preview');
7
+ const layout = document.getElementById('layout').value;
8
+ images = [];
9
+ gridOrder = [];
10
+ preview.innerHTML = '';
11
+
12
+ if (files.length === 0) {
13
+ alert('Drop some pics first!');
14
+ return;
15
+ }
16
+
17
+ let gridConfig;
18
+ if (layout === 'random') {
19
+ const rows = Math.ceil(Math.sqrt(files.length));
20
+ const cols = Math.ceil(files.length / rows);
21
+ gridConfig = { rows, cols, slots: Array(rows * cols).fill({ w: 1, h: 1 }) };
22
+ } else if (layout === 'jagged4') {
23
+ gridConfig = {
24
+ rows: 2,
25
+ cols: 3,
26
+ slots: [
27
+ { w: 2, h: 1 }, { w: 1, h: 1 }, { w: 0, h: 0 }, // Top: wide, narrow, skip
28
+ { w: 1, h: 1 }, { w: 1, h: 1 }, { w: 1, h: 1 } // Bottom: three even
29
+ ]
30
+ };
31
+ } else if (layout === 'split6') {
32
+ gridConfig = {
33
+ rows: 3,
34
+ cols: 3,
35
+ slots: [
36
+ { w: 1, h: 2 }, { w: 2, h: 1 }, { w: 0, h: 0 }, // Left tall, top wide, skip
37
+ { w: 0, h: 0 }, { w: 1, h: 1 }, { w: 1, h: 1 }, // Skip, two small
38
+ { w: 1, h: 1 }, { w: 1, h: 1 }, { w: 0, h: 0 } // Bottom two, skip
39
+ ]
40
+ };
41
+ } else if (layout === 'chaos5') {
42
+ gridConfig = {
43
+ rows: 3,
44
+ cols: 2,
45
+ slots: [
46
+ { w: 1, h: 1 }, { w: 1, h: 2 }, // Small, tall
47
+ { w: 1, h: 1 }, { w: 0, h: 0 }, // Small, skip
48
+ { w: 2, h: 1 }, { w: 0, h: 0 } // Wide, skip
49
+ ]
50
+ };
51
+ } else {
52
+ const [rows, cols] = layout.split('x').map(Number);
53
+ gridConfig = { rows, cols, slots: Array(rows * cols).fill({ w: 1, h: 1 }) };
54
+ }
55
+
56
+ const maxSlots = gridConfig.slots.filter(s => s.w > 0 && s.h > 0).length;
57
+ Array.from(files).slice(0, maxSlots).forEach((file, index) => {
58
+ const img = new Image();
59
+ img.onload = () => {
60
+ images.push({ img, width: img.width, height: img.height, id: index });
61
+ gridOrder.push(index);
62
+ updatePreview();
63
+ setupGrid(gridConfig);
64
+ };
65
+ img.src = URL.createObjectURL(file);
66
+ });
67
+ }
68
+
69
+ function updatePreview() {
70
+ const preview = document.getElementById('preview');
71
+ preview.innerHTML = '';
72
+ images.forEach((image) => {
73
+ const imgElement = document.createElement('img');
74
+ imgElement.src = image.img.src;
75
+ imgElement.draggable = true;
76
+ imgElement.dataset.id = image.id;
77
+ imgElement.addEventListener('dragstart', (e) => {
78
+ e.dataTransfer.setData('text/plain', image.id);
79
+ });
80
+ preview.appendChild(imgElement);
81
+ });
82
+ }
83
+
84
+ function setupGrid(config) {
85
+ const gridPreview = document.getElementById('gridPreview');
86
+ gridPreview.innerHTML = '';
87
+ gridPreview.style.display = 'grid';
88
+ gridPreview.style.gridTemplateColumns = `repeat(${config.cols}, 80px)`;
89
+ gridPreview.style.gridTemplateRows = `repeat(${config.rows}, 80px)`;
90
+
91
+ config.slots.forEach((slot, index) => {
92
+ if (slot.w > 0 && slot.h > 0) {
93
+ const div = document.createElement('div');
94
+ div.className = 'grid-slot';
95
+ div.style.gridColumn = `span ${slot.w}`;
96
+ div.style.gridRow = `span ${slot.h}`;
97
+ div.style.width = `${slot.w * 80}px`;
98
+ div.style.height = `${slot.h * 80}px`;
99
+ div.dataset.index = index;
100
+ div.addEventListener('dragover', (e) => e.preventDefault());
101
+ div.addEventListener('drop', (e) => {
102
+ e.preventDefault();
103
+ const id = parseInt(e.dataTransfer.getData('text/plain'));
104
+ gridOrder[config.slots.filter((s, i) => i <= index && s.w > 0 && s.h > 0).length - 1] = id;
105
+ updateGrid(config);
106
+ });
107
+ gridPreview.appendChild(div);
108
+ }
109
+ });
110
+ updateGrid(config);
111
+ document.getElementById('generateBtn').style.display = 'block';
112
+ }
113
+
114
+ function updateGrid(config) {
115
+ const slots = document.querySelectorAll('.grid-slot');
116
+ let slotIndex = 0;
117
+ slots.forEach((slot) => {
118
+ slot.innerHTML = '';
119
+ const imageId = gridOrder[slotIndex];
120
+ if (imageId !== undefined) {
121
+ const image = images.find(img => img.id === imageId);
122
+ if (image) {
123
+ const imgElement = document.createElement('img');
124
+ imgElement.src = image.img.src;
125
+ slot.appendChild(imgElement);
126
+ }
127
+ }
128
+ slotIndex++;
129
+ });
130
+ }
131
+
132
+ function createTiles() {
133
+ const layout = document.getElementById('layout').value;
134
+ const canvas = document.getElementById('canvas');
135
+ const ctx = canvas.getContext('2d');
136
+
137
+ let gridConfig;
138
+ if (layout === 'random') {
139
+ const rows = Math.ceil(Math.sqrt(images.length));
140
+ const cols = Math.ceil(images.length / rows);
141
+ gridConfig = { rows, cols, slots: Array(rows * cols).fill({ w: 1, h: 1 }) };
142
+ } else if (layout === 'jagged4') {
143
+ gridConfig = {
144
+ rows: 2,
145
+ cols: 3,
146
+ slots: [
147
+ { w: 2, h: 1 }, { w: 1, h: 1 }, { w: 0, h: 0 },
148
+ { w: 1, h: 1 }, { w: 1, h: 1 }, { w: 1, h: 1 }
149
+ ]
150
+ };
151
+ } else if (layout === 'split6') {
152
+ gridConfig = {
153
+ rows: 3,
154
+ cols: 3,
155
+ slots: [
156
+ { w: 1, h: 2 }, { w: 2, h: 1 }, { w: 0, h: 0 },
157
+ { w: 0, h: 0 }, { w: 1, h: 1 }, { w: 1, h: 1 },
158
+ { w: 1, h: 1 }, { w: 1, h: 1 }, { w: 0, h: 0 }
159
+ ]
160
+ };
161
+ } else if (layout === 'chaos5') {
162
+ gridConfig = {
163
+ rows: 3,
164
+ cols: 2,
165
+ slots: [
166
+ { w: 1, h: 1 }, { w: 1, h: 2 },
167
+ { w: 1, h: 1 }, { w: 0, h: 0 },
168
+ { w: 2, h: 1 }, { w: 0, h: 0 }
169
+ ]
170
+ };
171
+ } else {
172
+ const [rows, cols] = layout.split('x').map(Number);
173
+ gridConfig = { rows, cols, slots: Array(rows * cols).fill({ w: 1, h: 1 }) };
174
+ }
175
+
176
+ const maxWidth = Math.max(...images.map(img => img.width));
177
+ const maxHeight = Math.max(...images.map(img => img.height));
178
+ canvas.width = gridConfig.cols * maxWidth;
179
+ canvas.height = gridConfig.rows * maxHeight;
180
+ canvas.style.display = 'block';
181
+
182
+ let slotIndex = 0;
183
+ gridConfig.slots.forEach((slot, index) => {
184
+ if (slot.w > 0 && slot.h > 0) {
185
+ const imageId = gridOrder[slotIndex];
186
+ if (imageId !== undefined) {
187
+ const image = images.find(img => img.id === imageId);
188
+ if (image) {
189
+ const x = (index % gridConfig.cols) * maxWidth;
190
+ const y = Math.floor(index / gridConfig.cols) * maxHeight;
191
+ ctx.drawImage(image.img, x, y, image.width, image.height);
192
+ }
193
+ }
194
+ slotIndex++;
195
+ }
196
+ });
197
+
198
+ updateDownloadLink(canvas);
199
+ }
200
+
201
+ function updateDownloadLink(canvas) {
202
+ const downloadLink = document.getElementById('download');
203
+ downloadLink.href = canvas.toDataURL('image/png');
204
+ downloadLink.style.display = 'block';
205
+ }