Add simulation engine, renderer, and utilities
This commit is contained in:
33
js/utils.js
Normal file
33
js/utils.js
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user