-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.js
97 lines (83 loc) · 3.71 KB
/
grid.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Based on: https://bl.ocks.org/cagrimmett/07f8c8daea00946b9e704e3efcbd5739
var gridData = [
[true, true, true, true, true, true, true, true, true, true],
[true, false, false, false, false, false, false, false, false, true],
[true, false, false, false, false, false, false, false, false, true],
[true, true, true, true, true, true, true, true, true, true],
[true, true, true, true, true, true, true, true, true, true],
[true, false, false, false, false, false, false, false, false, true],
[true, false, false, false, false, false, false, false, false, true],
[true, true, true, true, true, true, true, true, true, true],
[true, true, true, true, true, true, true, true, true, true],
[true, false, false, false, false, false, false, false, false, true],
[true, false, false, false, false, false, false, false, false, true],
[true, true, true, true, true, true, true, true, true, true],
[true, true, true, true, true, true, true, true, true, true],
[true, false, false, false, false, false, false, false, false, true],
[true, false, false, false, false, false, false, false, false, true],
[true, true, true, true, true, true, true, true, true, true],
];
// height, width
var cellDimensions = [50, 50];
/**
* Generates a 2D array of the same shape of grid containing all the data needed to render a D3 grid
* @param grid - A grid of booleans indicating if a cell is traversable (true) or not (false)
* @param cellDimensions - The pixel height, width of a cell in the grid
*/
function getGridDataForD3(grid, cellDimensions) {
var data = new Array();
// starting xpos and ypos at 1 so the stroke will show when we make the grid below
var xPosition = 1;
var yPosition = 1;
// Dimensions of the cell in pixels
var cellHeight = cellDimensions[0];
var cellWidth = cellDimensions[1];
var numRows = grid.length;
var numCols = grid[0].length;
// iterate for rows
for (var row = 0; row < numRows; row++) {
data.push( new Array() );
// iterate for cells/columns inside rows
for (var column = 0; column < numCols; column++) {
data[row].push({
x: xPosition,
y: yPosition,
width: cellWidth,
height: cellHeight,
traversable: grid[row][column]
});
// increment the x position. I.e. move it over by 50 (width variable)
xPosition += cellWidth;
}
// reset the x position after a row is complete
xPosition = 1;
// increment the y position for the next row. Move it down 50 (height variable)
yPosition += cellHeight;
}
return data;
}
// Render Grid with D3
var grid = d3.select("#grid")
.append("svg")
.attr("width", cellDimensions[1] * gridData[0].length + 10 + "px")
.attr("height", cellDimensions[0] * gridData.length + 10 + "px");
var row = grid.selectAll(".row")
.data(getGridDataForD3(gridData, cellDimensions))
.enter().append("g")
.attr("class", "row");
var column = row.selectAll(".square")
.data(function(d) { return d; })
.enter().append("rect")
.attr("class","square")
.attr("x", function(d) { return d.x; })
.attr("y", function(d) { return d.y; })
.attr("width", function(d) { return d.width; })
.attr("height", function(d) { return d.height; })
.attr("fill", function (d) {
if (d.traversable) {
return "#fff";
} else {
return "#ddd";
}
})
.style("stroke", "#222");