forked from nikoladimitroff/canvas-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.html
157 lines (135 loc) · 4.24 KB
/
maze.html
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<canvas id="maze" width="100%" height="100%"/>
<style>
html, body {
margin: 0;
padding: 0;
}
</style>
<script src="js/resizer.js"></script>
<script>
var canvas = document.getElementById("maze"),
context = canvas.getContext("2d");
Resizer.installHandler(canvas);
var Node = function (r, c) {
this.r = r;
this.c = c;
}
var SIZE = 100;
var graphSize = {
rows: SIZE,
cols: ~~(SIZE / window.aspect)
}
function children(node) {
var children = [];
if (node.r > 0)
children.push(new Node(node.r - 1, node.c))
if (node.r < graphSize.rows - 1)
children.push(new Node(node.r + 1, node.c))
if (node.c > 0)
children.push(new Node(node.r, node.c - 1))
if (node.c < graphSize.cols - 1)
children.push(new Node(node.r, node.c + 1))
return children;
}
function extendedChildren(node) {
var children = []
if (node.r > 0 && node.c > 0)
children.push(new Node(node.r - 1, node.c - 1));
if (node.r < graphSize.rows - 1 && node.c > 0)
children.push(new Node(node.r + 1, node.c - 1));
if (node.r > 0 && node.c < graphSize.cols - 1)
children.push(new Node(node.r - 1, node.c + 1));
if (node.r < graphSize.rows - 1 && node.c < graphSize.cols - 1)
children.push(new Node(node.r + 1, node.c + 1));
return children;
}
function isVisited(visited, parent, node) {
parentIndex = index(parent)
nodeIndex = index(node);
if (visited[nodeIndex]) return false;
var neighbours = children(node);
var count = 0;
for (var i = 0; i < neighbours.length; i++) {
var nextIndex = index(neighbours[i])
if (visited[nextIndex] &&
nextIndex != nodeIndex &&
nextIndex != parentIndex)
count++;
}
if (count > 0) return false;
neighbours = extendedChildren(node);
count = 0;
var siblings = children(parent).map(index);
for (var i = 0; i < neighbours.length; i++) {
var nextIndex = index(neighbours[i])
if (visited[nextIndex] &&
siblings.indexOf(nextIndex) == -1)
count++;
}
return count <= 0;
}
function index(node) {
return node.r * graphSize.cols + node.c;
}
frames = [];
function build(graph, node) {
var frontier = [node],
visited = [];
var boardSize = graphSize.rows * graphSize.cols;
var visitTest = isVisited.bind(undefined, visited);
while (Object.keys(visited).length <= boardSize - 1) {
var nodeIndex = index(node);
var neighbours = children(node).filter(visitTest.bind(undefined, node));
if (neighbours.length != 0) {
graph[nodeIndex] = true;
node = neighbours[~~(Math.random() * neighbours.length)];
frontier.push(node);
}
else if (frontier.length != 0) {
node = frontier.pop();
}
else {
var nextNode, nextNodeIndex;
do {
nextNode = new Node(~~(Math.random() * graphSize.rows),
~~(Math.random() * graphSize.cols));
nextNodeIndex = index(nextNode);
}
while (visited[nextNodeIndex]);
node = nextNode;
}
visited[nodeIndex] = true;
var frameString = JSON.stringify(graph);
if (frameString != JSON.stringify(frames[frames.length - 1]))
frames.push(JSON.parse(frameString));
}
}
function draw() {
context.fillStyle = "black";
context.fillRect(0, 0, canvas.width, canvas.height);
context.fillStyle = "white";
var cellSizeX = canvas.width / graphSize.rows;
var cellSizeY = canvas.height / graphSize.cols;
for (var i = 0; i < graphSize.rows; i++) {
for (var j = 0; j < graphSize.cols; j++) {
var node = new Node(i, j);
if (graph[index(node)]) {
context.fillRect(i * cellSizeX, j * cellSizeY, cellSizeX, cellSizeY);
}
}
}
}
function drawAnim() {
var i = 0;
setInterval(function () {
if (i < frames.length) {
graph = frames[i++];
draw();
}
}, 1000/60);
}
var starting = new Node(0, 0);
var graph = [index(starting)];
build(graph, starting);
drawAnim();
</script>