-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.htm
332 lines (302 loc) · 12.5 KB
/
index.htm
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
<!DOCTYPE html>
<html>
<head>
<title>Blanket Pattern Designer</title>
<style>
h1 {
text-align: center;
font-variant: small-caps;
}
label {
font-variant: small-caps;
}
.all-settings {
padding-bottom: 10px;
display: flex;
flex-direction: row;
margin: auto;
gap: 20px;
width: max-content;
}
.settings-box {
border: 2px solid #ccc;
padding: 20px;
border-radius: 10px;
display: flex;
flex-direction: column;
gap: 10px;
width: max-content;
}
.settings-box div {
display: flex;
flex-direction: row;
gap: 30px;
}
.settings-box div div {
display: flex;
flex-direction: column;
gap: 10px;
}
.settings-box .input-group {
display: flex;
gap: 10px;
}
.settings-box label {
margin-right: 10px;
}
.settings-box input[type="number"] {
width: 50px; /* You can adjust the width as per your need */
}
.second-settings-box {
border: 2px solid #ccc;
padding: 20px;
border-radius: 10px;
display: flex;
flex-direction: column;
gap: 10px;
}
.second-settings-box div {
margin-top: auto;
margin-bottom: auto;
display: flex;
flex-direction: column;
gap: 10px;
}
/* Basic styling for the grid */
.grid {
display: grid;
background-color: #ccc;
/* Adjust grid size to fit reference image */
grid-auto-rows: minmax(0, 1fr);
grid-gap: 0;
margin: auto;
}
/* Styling for the grid cells */
.cell {
background-color: #fff;
border: 1px solid #999;
cursor: pointer;
opacity: 0.7; /* Set opacity to 0.7 (70%) */
}
/* Styling for the grid cells when they're clicked */
.cell.clicked {
background-color: #000;
}
/* Additional styling for the grid cells when reference image is provided */
.reference-image {
background-size: cover;
background-repeat: no-repeat;
background-position: center;
}
.grid {
width: max-content;
}
</style>
</head>
<body>
<h1>Blanket Pattern Designer</h1>
<!-- Input grid size -->
<div class="all-settings">
<div class="settings-box">
<div>
<div>
<label for="rows">Rows:</label>
<input type="number" id="rows" name="rows" min="1" value="8">
</div>
<div>
<label for="cols">Columns:</label>
<input type="number" id="cols" name="cols" min="1" value="8">
</div>
<!-- Input cell size -->
<div>
<label for="cellSize">Cell Size (pixels):</label>
<input type="number" id="cellSize" name="cellSize" min="1" max="100" value="30">
</div>
</div>
<!-- Input reference image -->
<label for="referenceImage">Reference Image:</label>
<input type="file" id="referenceImage" name="referenceImage">
<!-- Button to clear reference image -->
<button id="clearReferenceImage">Clear Reference Image</button>
</div>
<!-- Button to export the image -->
<div class="second-settings-box">
<div>
<label for="fileOutput">Download Pattern</label>
<button id="fileOutput" onclick="exportImage()">Save</button>
</div>
<div>
<label for="fileInput">Upload Pattern</label>
<input type="file" id="fileInput" accept="image/*">
</div>
</div>
</div>
<!-- Input grid -->
<div class="grid" id="grid">
</div>
<!-- JavaScript for generating the grid and setting reference image -->
<script>
// Define initial state of cells
const cellState = {};
// Generate the grid when the page loads
document.addEventListener("DOMContentLoaded", function(event) {
generateGrid();
});
// Generate the grid based on the input values
function generateGrid() {
const rows = document.getElementById("rows").value;
const cols = document.getElementById("cols").value;
const cellSize = document.getElementById("cellSize").value;
const grid = document.getElementById("grid");
grid.style.gridTemplateColumns = `repeat(${cols}, 1fr)`;
// Clear the grid
grid.innerHTML = '';
// Create new cells
for (let i = 0; i < rows; i++) {
for (let j = 0; j < cols; j++) {
const cell = document.createElement('div');
cell.classList.add('cell');
cell.style.width = cell.style.height = `${cellSize}px`;
cell.addEventListener('click', () => {
cell.classList.toggle('clicked');
// Update cell state
cellState[`${i}-${j}`] = cell.classList.contains('clicked');
});
// Restore previous cell state if exists
if (cellState[`${i}-${j}`]) {
cell.classList.add('clicked');
}
grid.appendChild(cell);
}
}
// Set reference image as background of the grid
const referenceImage = document.getElementById("referenceImage").files[0];
if (referenceImage) {
const reader = new FileReader();
reader.onload = function(event) {
grid.style.backgroundImage = `url(${event.target.result})`;
grid.classList.add('reference-image');
};
reader.readAsDataURL(referenceImage);
}
}
// Clear reference image
document.getElementById("clearReferenceImage").addEventListener('click', function() {
const grid = document.getElementById("grid");
grid.style.backgroundImage = '';
grid.classList.remove('reference-image');
document.getElementById("referenceImage").value = ''; // Clears the file input
});
function importImage(event) {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = function(e) {
const img = new Image();
img.onload = function() {
const hiddenCanvas = document.createElement('canvas');
const hiddenContext = hiddenCanvas.getContext('2d');
hiddenCanvas.width = img.width;
hiddenCanvas.height = img.height;
hiddenContext.drawImage(img, 0, 0);
// Analyze pixels to determine grid dimensions and cell colors
const cellWidth = 40; // Calculate cell width
const cellHeight = 40; // Calculate cell height
const gridWidth = img.width / cellWidth; // Calculate grid width
const gridHeight = img.height / cellHeight; // Calculate grid height
const cellColors = []; // Calculate cell colors
// Update UI with grid dimensions
document.getElementById("rows").value = gridHeight;
document.getElementById("cols").value = gridWidth;
generateGrid()
cells = document.getElementById("grid").children
for (let i = 0; i < gridHeight; i++) {
for (let j = 0; j < gridWidth; j++) {
const pixel = hiddenContext.getImageData(j * 40 + 20, i * 40 + 20, 1, 1);
const cell = cells[i * gridWidth + j]
if (pixel.data.some((value, index) => index != 3 && value <= 196)) {
if (! cell.classList.contains('clicked')) {
console.log("CLICKING")
cell.classList.add('clicked')
}
} else {
cell.classList.remove('clicked')
}
cellState[`${i}-${j}`] = cell.classList.contains('clicked');
}
}
// // Update UI with grid dimensions and cell colors
// document.getElementById("rows").value = gridHeight;
// document.getElementById("cols").value = gridWidth;
// document.getElementById("grid").innerHTML = ''; // Clear grid
// for (let i = 0; i < gridHeight; i++) {
// for (let j = 0; j < gridWidth; j++) {
// const cell = document.createElement('div');
// cell.classList.add('cell');
// cell.style.width = cellWidth + 'px';
// cell.style.height = cellHeight + 'px';
// cell.style.backgroundColor = cellColors[i * gridWidth + j];
// document.getElementById("grid").appendChild(cell);
// }
// }
};
img.src = e.target.result;
};
reader.readAsDataURL(file);
}
// Update the grid when the input values change
document.getElementById("rows").addEventListener('change', generateGrid);
document.getElementById("cols").addEventListener('change', generateGrid);
document.getElementById("cellSize").addEventListener('change', generateGrid);
document.getElementById("referenceImage").addEventListener('change', generateGrid);
document.getElementById("fileInput").addEventListener('change', importImage);
</script>
<!-- JavaScript for exporting the image -->
<script>
function exportImage() {
const gridHeight = document.getElementById("rows").value;
const gridWidth = document.getElementById("cols").value;
const grid = document.getElementById("grid");
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
const cellWidth = 40;
const cellHeight = 40;
canvas.width = gridWidth * cellWidth;
canvas.height = gridHeight * cellHeight;
// Clear the canvas
context.clearRect(0, 0, canvas.width, canvas.height);
// Draw grid cells
for (let i = 0; i < gridHeight; i++) {
for (let j = 0; j < gridWidth; j++) {
const cell = grid.children[i * gridWidth + j];
const color = cell.classList.contains('clicked') ? '#666' : '#fff';
context.fillStyle = color;
context.fillRect(j * cellWidth, i * cellHeight, cellWidth, cellHeight);
}
}
// Draw grid lines
context.strokeStyle = 'rgba(0, 0, 0, 1.0)'; // Adjust transparency if needed
context.lineWidth = 1;
for (let i = 1; i < gridHeight; i++) {
// Horizontal lines
context.beginPath();
context.moveTo(0, i * cellHeight);
context.lineTo(canvas.width, i * cellHeight);
context.stroke();
}
for (let i = 1; i < gridWidth; i++) {
// Vertical lines
context.beginPath();
context.moveTo(i * cellWidth, 0);
context.lineTo(i * cellWidth, canvas.height);
context.stroke();
}
// Create a download link
const downloadLink = document.createElement('a');
downloadLink.href = canvas.toDataURL();
downloadLink.download = 'pattern.png';
// Simulate a click on the download link
downloadLink.click();
}
</script>
</body>
</html>