Add simulation engine, renderer, and utilities

This commit is contained in:
2026-07-08 19:47:21 +00:00
parent f255073b97
commit 5f0a510a99
3 changed files with 259 additions and 0 deletions

33
js/utils.js Normal file
View File

@@ -0,0 +1,33 @@
export function clamp(value, min, max) {
return Math.min(max, Math.max(min, value));
}
export function debounce(callback, delayMs = 150) {
let timeoutId = null;
return (...args) => {
clearTimeout(timeoutId);
timeoutId = setTimeout(() => callback(...args), delayMs);
};
}
export function cellCoordsToIndex(row, col, cols) {
return row * cols + col;
}
export function listLiveCells(buffer, rows, cols) {
const result = [];
for (let row = 0; row < rows; row += 1) {
for (let col = 0; col < cols; col += 1) {
const idx = row * cols + col;
if (buffer[idx] === 1) {
result.push([row, col]);
}
}
}
return result;
}
export function parsePositiveInt(value, fallback) {
const parsed = Number.parseInt(value, 10);
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
}