-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
510 lines (435 loc) · 14.1 KB
/
script.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
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
let GRID_SIZE = 8;
let gridOrientation = "top-left";
let frames = [{ coords: [], name: "Frame 1" }];
let currentFrameIndex = 0;
let scrollInterval = null;
let scrollingInProgress = false;
/* ===========================
CREATE & RENDER THE GRID
=========================== */
/**
* Dynamically creates an NxN grid of buttons (N = GRID_SIZE).
* Also sets the CSS grid styles (columns/rows) using the current GRID_SIZE.
*/
function createGrid() {
const container = document.getElementById("grid-container");
container.innerHTML = "";
container.style.gridTemplateColumns = `repeat(${GRID_SIZE}, 3.125rem)`;
container.style.gridTemplateRows = `repeat(${GRID_SIZE}, 3.125rem)`;
for (let i = 0; i < GRID_SIZE * GRID_SIZE; i++) {
const btn = document.createElement("button");
btn.textContent = " ";
btn.addEventListener("click", () => handleCellClick(btn, i));
container.appendChild(btn);
}
highlightCornerButton();
applyFrameToGrid();
updateFrameIndicator();
}
/**
* Handles a click on a specific button in the grid.
* Toggles the coordinate in our in-memory array and updates the UI.
*/
function handleCellClick(button, index) {
const { row, col } = indexToRowCol(index);
const activeFrame = frames[currentFrameIndex];
const existing = activeFrame.coords.findIndex(pt => pt.row === row && pt.col === col);
if (existing >= 0) {
// remove
activeFrame.coords.splice(existing, 1);
button.classList.remove("clicked");
} else {
// add
activeFrame.coords.push({ row, col });
button.classList.add("clicked");
}
updateCoordinatesDisplay();
}
/**
* Updates the orientation and clears any clicked buttons/coordinates
* for consistency.
*/
function updateOrientation(newOrientation) {
gridOrientation = newOrientation;
frames.forEach(f => f.coords = []);
currentFrameIndex = 0;
createGrid();
}
/**
* Reads the user's chosen grid size from the input, updates GRID_SIZE,
* and regenerates the grid.
*/
function updateGridSize() {
const val = parseInt(document.getElementById("grid-size-input").value, 10);
if (!isNaN(val) && val > 0) {
GRID_SIZE = val;
frames.forEach(f => f.coords = []);
currentFrameIndex = 0;
createGrid();
}
}
/**
* Updates the dot/marker for whichever button is row=0, col=0
* in the current orientation.
*/
function highlightCornerButton() {
const buttons = document.querySelectorAll("#grid-container button");
buttons.forEach(b => {
b.classList.remove("corner-dot");
if (b.textContent === "•") b.textContent = " ";
});
for (let i = 0; i < GRID_SIZE * GRID_SIZE; i++) {
const coords = indexToRowCol(i);
if (coords.row === 0 && coords.col === 0) {
buttons[i].classList.add("corner-dot");
buttons[i].textContent = "•";
break;
}
}
}
/* ============================
FRAMES: NEW, PREV, NEXT
============================ */
function newFrame() {
frames.push({ coords: [], name: `Frame ${frames.length + 1}` });
currentFrameIndex = frames.length - 1;
applyFrameToGrid();
updateFrameIndicator();
updateCoordinatesDisplay();
}
function prevFrame() {
if (currentFrameIndex > 0) {
currentFrameIndex--;
applyFrameToGrid();
updateFrameIndicator();
updateCoordinatesDisplay();
}
}
function nextFrame() {
if (currentFrameIndex < frames.length - 1) {
currentFrameIndex++;
applyFrameToGrid();
updateFrameIndicator();
updateCoordinatesDisplay();
}
}
/* Clear the current frame only */
function clearClickedButtons() {
frames[currentFrameIndex].coords = [];
applyFrameToGrid();
updateCoordinatesDisplay();
}
/* Apply the active frame's coords to the grid display */
function applyFrameToGrid() {
const buttons = document.querySelectorAll("#grid-container button");
buttons.forEach(b => b.classList.remove("clicked"));
const active = frames[currentFrameIndex];
active.coords.forEach(pt => {
const idx = rowColToIndex(pt.row, pt.col);
if (idx >= 0 && idx < buttons.length) {
buttons[idx].classList.add("clicked");
}
});
}
/**
* Refreshes the #clicked-buttons text content based on the clickedCoordinates array.
*/
function updateCoordinatesDisplay() {
const disp = document.getElementById("clicked-buttons");
const active = frames[currentFrameIndex];
const lines = active.coords.map(pt => `(${pt.row}, ${pt.col})`);
disp.textContent = lines.join(", ");
}
function updateFrameIndicator() {
const el = document.getElementById("frame-indicator");
if (!el) return;
el.textContent = `Active: ${frames[currentFrameIndex].name}`;
}
/* ============================
ORIENTATION MAPPING
============================ */
function indexToRowCol(index) {
switch (gridOrientation) {
case "top-left":
return {
row: Math.floor(index / GRID_SIZE),
col: index % GRID_SIZE
};
case "top-right":
return {
row: (GRID_SIZE - 1) - (index % GRID_SIZE),
col: Math.floor(index / GRID_SIZE)
};
case "bottom-left":
return {
row: index % GRID_SIZE,
col: (GRID_SIZE - 1) - Math.floor(index / GRID_SIZE)
};
case "bottom-right":
return {
row: (GRID_SIZE - 1) - Math.floor(index / GRID_SIZE),
col: (GRID_SIZE - 1) - (index % GRID_SIZE)
};
default:
return {
row: Math.floor(index / GRID_SIZE),
col: index % GRID_SIZE
};
}
}
function rowColToIndex(r, c) {
switch (gridOrientation) {
case "top-left":
return r * GRID_SIZE + c;
case "top-right":
return (GRID_SIZE - 1 - c) * GRID_SIZE + r;
case "bottom-left":
return c * GRID_SIZE + (GRID_SIZE - 1 - r);
case "bottom-right":
return (GRID_SIZE - 1 - r) * GRID_SIZE + (GRID_SIZE - 1 - c);
default:
return r * GRID_SIZE + c;
}
}
/* ============================
SCROLLING LOGIC
============================ */
function startScroll() {
if (scrollingInProgress) return;
scrollingInProgress = true;
stopScroll();
// Build one 'megaframe' from all frames
const megaCoords = buildMegaFrame();
if (!megaCoords.length) {
console.warn("No lit pixels in frames. Nothing to animate.");
scrollingInProgress = false;
return;
}
// Find bounding box (minCol, maxCol)
let minC = Infinity, maxC = -Infinity;
megaCoords.forEach(pt => {
if (pt.col < minC) minC = pt.col;
if (pt.col > maxC) maxC = pt.col;
});
// Start offset so that left edge is just beyond the right side:
let offset = GRID_SIZE - minC;
let delay = parseInt(document.getElementById("delay-input").value, 10);
if (isNaN(delay) || delay < 0) delay = 150;
scrollInterval = setInterval(() => {
renderMegaCoords(megaCoords, offset);
if (offset + maxC < 0) {
stopScroll();
} else {
offset--;
}
}, delay);
}
function stopScroll() {
if (scrollInterval) {
clearInterval(scrollInterval);
scrollInterval = null;
}
scrollingInProgress = false;
applyFrameToGrid();
}
/**
* Lays out each frame with no horizontal gap,
* building a single array of coords (megaCoords).
*/
function buildMegaFrame() {
const result = [];
let currentX = 0;
frames.forEach((frame) => {
if (!frame.coords.length) return;
// bounding box for this frame
let minC = Infinity, maxC = -Infinity;
frame.coords.forEach(pt => {
if (pt.col < minC) minC = pt.col;
if (pt.col > maxC) maxC = pt.col;
});
const width = (maxC - minC + 2);
// Shift each col so the frame's minC starts at currentX
frame.coords.forEach(pt => {
const newCol = pt.col - minC + currentX;
result.push({ row: pt.row, col: newCol });
});
// Move currentX for next frame (no gap => + width)
currentX += width;
});
return result;
}
/**
* Renders the merged coords at the given offset (shift all columns by offset).
*/
function renderMegaCoords(coords, offset) {
const buttons = document.querySelectorAll("#grid-container button");
buttons.forEach(b => b.classList.remove("clicked"));
coords.forEach(pt => {
const shiftedCol = pt.col + offset;
if (shiftedCol >= 0 && shiftedCol < GRID_SIZE) {
const idx = rowColToIndex(pt.row, shiftedCol);
if (idx >= 0 && idx < buttons.length) {
buttons[idx].classList.add("clicked");
}
}
});
}
/* ============================
EXPORT LOGIC
============================ */
/**
* Copies the current coordinates to the clipboard in JSON format.
*/
function copyCoordinates() {
const data = {
frames: frames.map(f => ({ name: f.name, coords: f.coords }))
};
const json = JSON.stringify(data, null, 2);
navigator.clipboard.writeText(json)
.then(() => alert("Copied to clipboard!"))
.catch(err => console.error("Clipboard copy failed:", err));
}
/**
* Prompts a file download in JSON format (coordinates.json).
*/
function downloadJSON() {
const data = {
frames: frames.map(f => ({ name: f.name, coords: f.coords }))
};
const jsonStr = JSON.stringify(data, null, 2);
const blob = new Blob([jsonStr], { type: "application/json" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = "frames.json";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
/**
* Prompts a Rust file download
*/
function downloadRustFile() {
const sizeValue = parseInt(document.getElementById("grid-size-input").value, 10) || 8;
const w = sizeValue;
const h = sizeValue;
const delayValue = parseInt(document.getElementById("delay-input").value, 10) || 150;
const scrollSpeed = delayValue;
const rustContent = generateRustCode(scrollSpeed, w, h);
const blob = new Blob([rustContent], { type: "text/plain" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = "nm_scroll_frames.rs";
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
/**
* Generates the Rust source code for your "NmScroll" struct, using:
* - the user-chosen scrollSpeed (columns moved per `next()`),
* - a single column gap between frames,
* - a bounding-box merge of frames.
*
* @param {number} scrollSpeed The columns to move each `next()`.
* @param {number} width The WIDTH dimension for the Rust code.
* @param {number} height The HEIGHT dimension for the Rust code.
* @returns {string} Rust source code
*/
function generateRustCode(scrollSpeed, width, height) {
let code = `use smart_leds::RGB8;
pub const WIDTH: usize = ${width};
pub const HEIGHT: usize = ${height};
pub struct NmScroll {
strip: [RGB8; WIDTH * HEIGHT],
frame: isize,
color: RGB8,
}
impl NmScroll {
pub fn new(color: RGB8) -> Self {
Self {
strip: [RGB8::new(0, 0, 0); WIDTH * HEIGHT],
frame: 0,
color,
}
}
pub fn clear(&mut self) {
for px in &mut self.strip {
*px = RGB8::new(0, 0, 0);
}
}
pub fn set(&mut self, x: usize, y: usize) {
if x < WIDTH && y < HEIGHT {
self.strip[y * WIDTH + x] = self.color;
}
}
pub fn to_list(&self) -> [RGB8; WIDTH * HEIGHT] {
self.strip
}
fn draw_frame(&mut self, frame: &[(usize, usize)], offset_x: isize) {
for &(x, y) in frame.iter() {
let x_pos = x as isize + offset_x;
if x_pos >= 0 && x_pos < WIDTH as isize {
self.set(x_pos as usize, y);
}
}
}
`;
// Define const arrays for each frame
frames.forEach((frame, i) => {
const arrItems = frame.coords
.map(({ row, col }) => `(${col}, ${row})`)
.join(", ");
code += `
// ${frame.name}
const FRAME_${i + 1}: &[(usize, usize)] = &[${arrItems}];
`;
});
code += `
pub fn next(&mut self) {
self.clear();
let scroll_increment: isize = ${scrollSpeed};
// We'll place frames consecutively (with 1-col gap).
// current_x tracks the start col for the next frame.
let mut current_x: isize = 0;
// total_width is how many columns wide the merged frames are
let mut total_width: isize = 0;
// Create an array of references to frames
let frames_data: [&[(usize, usize)]; ${frames.length}] = [
`;
frames.forEach((_, i) => {
code += ` FRAME_${i + 1},\n`;
});
code += ` ];\n\n`;
code += `
for (i, frame_data) in frames_data.iter().enumerate() {
// bounding box
let mut frame_min = isize::MAX;
let mut frame_max = isize::MIN;
for &(x, _y) in (*frame_data).iter() {
if x as isize < frame_min { frame_min = x as isize; }
if x as isize > frame_max { frame_max = x as isize; }
}
let width_of_frame = (frame_max - frame_min + 1);
let offset_x = (current_x - frame_min) - self.frame;
// Draw
self.draw_frame(frame_data, offset_x);
// 1-col gap
current_x += width_of_frame + 1;
if i == frames_data.len() - 1 {
total_width = current_x;
}
}
// Advance self.frame by scroll_increment
self.frame += scroll_increment;
// Once we've scrolled past the entire shape, loop back
let loop_length = total_width + WIDTH as isize;
if self.frame >= loop_length {
self.frame = 0;
}
}
}
`;
return code;
}
createGrid();