-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitialiseGrid.js
More file actions
29 lines (24 loc) · 926 Bytes
/
initialiseGrid.js
File metadata and controls
29 lines (24 loc) · 926 Bytes
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
const sideLength = 40;
let lengthStr = sideLength+"px";
// dynamically create a grid of divs with CSS class block
// then create Blocks and add them to the grid so they can be manipulated by js
function createGrid(container, rows, cols){
let grid = [];
for (let i = 0; i < rows; i++){
let row = [];
for (let j = 0; j < cols; j++){
let newBlock = document.createElement("div");
newBlock.className = "block";
newBlock.style.width = lengthStr;
newBlock.style.height = lengthStr;
newBlock.style.top = i*sideLength;
newBlock.style.left = j*sideLength;
container.appendChild(newBlock);
row.push(new Block(newBlock));
}
grid.push(row)
}
container.style.width = (cols*sideLength) + "px";
container.style.height = (rows*sideLength) + "px";
return grid;
}