-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbeads2.html
255 lines (218 loc) · 7.07 KB
/
beads2.html
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
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
body {
display: grid;
grid-template-columns: auto auto;
gap: 1px;
}
#grid-container {
display: grid;
grid-template-columns: repeat(29, 32px);
/* Adjust based on the size of each cell */
grid-template-rows: repeat(29, 32px);
gap: 2px;
/* This creates a 1px space between grid items */
justify-content: center;
/* Center the grid horizontally */
align-items: center;
/* Center the grid vertically */
width: fit-content;
/* Adjust the width to fit the content */
}
#grid-container div {
width: 32px;
/* Width of each cell */
height: 32px;
/* Height of each cell */
position: relative;
box-sizing: border-box;
/* Include padding and border in the element's size */
}
#grid-container div.selected {
box-shadow: 0 0 0 5px red inset;
/* Create an inner shadow that looks like a border */
}
#grid-container p {
margin: 0;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
font-size: 12px;
font-weight: bold;
color: white;
text-shadow:
-1px -1px 0 #000,
1px -1px 0 #000,
-1px 1px 0 #000,
1px 1px 0 #000;
letter-spacing: 1px;
}
.inactive {
opacity: 0.4;
}
.active {
/* margin: 4px; */
/* outline: 4px green */
}
.palette-item {
display: inline-block;
vertical-align: top;
margin: 5px;
}
.palette-item p {
display: inline-block;
vertical-align: top;
margin: 0 0 0 10px;
/* Add some space between the div and the text */
font-size: 12px;
font-weight: bold;
color: black;
}
</style>
</head>
<body>
<div id="grid-container">
<!-- Grid will be generated here -->
</div>
<div id="palette-container"></div>
<script>
function createPalette(colorData) {
const container = document.getElementById('palette-container');
const allColors = colorData.flat()
const allUniqueColors = [...new Set(allColors.map(pixel => pixel.color))];
const allUniquePixels = allUniqueColors.map(color => allColors.find(pixel => pixel.color === color));
allUniquePixels
.sort((a, b) => a.number - b.number)
.forEach(({ name, number, color }) => {
const colorContainer = document.createElement('div');
colorContainer.style.display = 'flex'
const colorDiv = document.createElement('div');
colorDiv.style.backgroundColor = color;
colorDiv.style.width = '32px';
colorDiv.style.height = '32px';
colorDiv.style.border = '1px solid black';
colorDiv.className = 'palette-item';
const colorDivP = document.createElement('p');
colorDiv.append(colorDivP);
colorDivP.textContent = number;
const p = document.createElement('p');
p.textContent = `${number} ${name}`;
colorContainer.appendChild(colorDiv);
colorContainer.appendChild(p);
container.appendChild(colorContainer);
});
}
function createGrid(colorData) {
const container = document.getElementById('grid-container');
container.innerHTML = ''; // Clear existing grid
for (let row = 0; row < 29; row++) {
for (let col = 0; col < 29; col++) {
const div = document.createElement('div');
const pixel = colorData[row][col];
div.style.backgroundColor = pixel.color;
const p = document.createElement('p');
p.style.display = 'none';
p.textContent = pixel?.number;
div.appendChild(p);
container.appendChild(div);
if (row === 0 && col === 0) {
div.classList.add('selected');
}
}
}
}
// Function to move the selected class
function moveSelected(direction) {
const grid = document.getElementById('grid-container');
let current = grid.querySelector('.selected');
let rowIndex = Math.floor(Array.from(grid.children).indexOf(current) / 29);
let colIndex = Array.from(grid.children).indexOf(current) % 29;
current.classList.remove('selected');
if (direction === 'ArrowUp') {
if (rowIndex > 0) {
rowIndex--;
} else {
rowIndex = 28;
}
} else if (direction === 'ArrowDown') {
if (rowIndex < 28) {
rowIndex++;
} else {
rowIndex = 0;
}
} else if (direction === 'ArrowLeft') {
if (colIndex > 0) {
colIndex--;
} else {
colIndex = 28;
rowIndex = rowIndex > 0 ? rowIndex - 1 : 28;
}
} else if (direction === 'ArrowRight') {
if (colIndex < 28) {
colIndex++;
} else {
colIndex = 0;
rowIndex = rowIndex < 28 ? rowIndex + 1 : 0;
}
}
const newSelected = grid.children[rowIndex * 29 + colIndex];
newSelected.classList.add('selected');
// Get the innerHTML of the <p> element and set the document's title
const pContent = newSelected.querySelector('p').innerHTML;
document.title = pContent;
}
// Function to read JSON file
async function readJSON(filename) { }
let selectedPValue = '';
function handleSpacebarPress() {
const grid = document.getElementById('grid-container');
const selectedDiv = grid.querySelector('.selected');
const newSelectedPValue = selectedDiv.querySelector('p').innerHTML;
const selectedColor = selectedDiv.style.backgroundColor;
// First make all divs active
Array.from(grid.children).forEach(div => div.classList.remove('inactive', 'active'));
if (newSelectedPValue !== selectedPValue) {
// Add 'inactive' class to divs not matching the color
Array.from(grid.children).forEach(div => {
if (div.style.backgroundColor !== selectedColor) {
div.classList.add('inactive');
} else {
div.classList.add('active');
}
});
selectedPValue = newSelectedPValue;
} else {
selectedPValue = '';
}
}
// Event listener for arrow keys
document.addEventListener('keydown', function (event) {
if (['ArrowUp', 'ArrowDown', 'ArrowLeft', 'ArrowRight'].includes(event.key)) {
moveSelected(event.key);
}
});
document.addEventListener('DOMContentLoaded', async function () {
const queryString = window.location.search;
const urlParams = new URLSearchParams(queryString);
const local = urlParams.get('u');
let url = local;
if (!local) {
const load = urlParams.get('t');
url = `https://tinyurl.com/${load.replaceAll("_", "ZZ")}`;
}
const imageData = await (await fetch(url)).json();
createGrid(imageData);
createPalette(imageData);
});
document.addEventListener('keydown', function (event) {
if (event.key === ' ') {
handleSpacebarPress();
}
});
</script>
</body>
</html>