Constellations/js/Grid.js
2022-08-11 09:58:57 +02:00

43 lines
887 B
JavaScript

import * as CellSet from Geometry.CellSet;
import * as Dom from UnitJS.Dom;
import {at, generate, iter, square} from Grid.Util;
import {diagonal, zero} from Geometry.Vector;
var grid = {
root: document.getElementById('grid'),
cells: null,
colors: null,
missing: null,
size: null
};
return {
clear: clear,
get: get,
init: init,
};
function init(size, eventHandlers) {
grid.size = size;
grid.cells = generate(size, size, function(cell) {
return Dom.make('td', eventHandlers(cell));
});
for(var row = 0; row < size; row++) {
grid.root.appendChild(Dom.make('tr', {}, grid.cells[row]));
}
clear();
}
function clear() {
grid.colors = square(grid.size);
grid.missing = CellSet.make(
{type: 'rectangle', origin: zero(), offset: diagonal(grid.size)}
);
iter(grid.colors, function(cell) {
at(grid.cells, cell).className = '';
});
}
function get() {
return grid;
}