Create an array which corresponds to the grid. When clicked simply set the array's value to something else than what it is initially.
Initialize an array with number of columns x number of rows:
var cols = 27;
var rows = 40;
var clicked = new Uint8Array(cols * rows); // initialized with 0
Then simply calculate the index of the cell in that array and check for a value, for example 1:
build_canvas.onclick = function (e) {
var rect = build_canvas.getBoundingClientRect(),
x = e.clientX - rect.left,
y = e.clientY - rect.top,
cellX = (x / 15)|0, // get hor. cell position
cellY = (y / 10)|0, // get ver. cell position
index = cols * cellY + cellX; // get array index
if (clicked[index] === 1) { // clicked ? (===1)
alert('clicked already'); // do some action
return;
}
clicked[index] = 1; // is clicked now...
x = cellX * 15; // get pixel position
y = cellY * 10;
build_context.fillRect(x + 1, y + 1, 14, 9);
}
The index of a 1-dimensional array representing a 2D array is always calculated width x y position + x position
.