-
Notifications
You must be signed in to change notification settings - Fork 3
/
maze.js
293 lines (253 loc) · 7.57 KB
/
maze.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
(function(maze, undefined) {
// cross-browser support for requestAnimationFrame and cancelAnimationFrame
const requestAnimFrame = window.requestAnimationFrame
|| window.webkitRequestAnimationFrame
|| window.msRequestAnimationFrame
|| window.mozRequestAnimationFrame
|| function(callback) { return window.setTimeout(callback, 1000 / 60); };
const cancelAnimFrame = window.cancelAnimationFrame
|| window.webkitCancelRequestAnimationFrame
|| window.webkitCancelAnimationFrame
|| window.mozCancelRequestAnimationFrame || window.mozCancelAnimationFrame
|| window.oCancelRequestAnimationFrame || window.oCancelAnimationFrame
|| window.msCancelRequestAnimationFrame || window.msCancelAnimationFrame
|| function(id) { clearTimeout(id); };
class Direction {
constructor(x, y) {
this.x = x;
this.y = y;
}
isOpposite(other) {
return (this.x != 0 && this.x == -other.x)
|| (this.y != 0 && this.y == -other.y);
}
}
Direction.NONE = new Direction(0, 0);
Direction.RIGHT = new Direction(1, 0);
Direction.LEFT = new Direction(-1, 0);
Direction.UP = new Direction(0, -1);
Direction.DOWN = new Direction(0, 1);
class Cell {
constructor(x, y, color, fromDir) {
this.x = Math.floor(x);
this.y = Math.floor(y);
this.color = color || 0;
this.fromDir = fromDir || Direction.NONE;
}
}
class Settings {
constructor(guiSettings) {
this.startX = guiSettings.startX;
this.startY = guiSettings.startY;
this.blockSize = Math.floor(guiSettings.blockSize);
this.cellDistance = Math.floor(guiSettings.cellDistance);
this.directionShuffleProbability = guiSettings.directionShuffleProbability;
this.disallowSameDirection = guiSettings.disallowSameDirection;
this.circleProbability = guiSettings.circleProbability;
this.backgroundColor = guiSettings.backgroundColor;
this.colorFactor = Math.floor(guiSettings.colorFactor);
this.colorFunction = guiSettings.colorFunction;
this.border = this.cellDistance == 1 ? 0 : 1;
}
}
let settings;
let updatesPerFrame;
let running;
// array for the direction that the maze expands in
// the first direction is taken first (if possible)
let directions;
let animFrameReqId;
let data;
let width;
let height;
let dfsStack;
let canvas;
let context;
let bufferCanvas;
let bufferContext;
let changedCells = new Array();
/**
* Shuffles array in place.
* @see http://stackoverflow.com/a/6274381
* @param {Array} a items The array containing the items.
*/
function shuffle(a) {
for(let i = a.length; i; i--) {
let j = Math.floor(Math.random() * i);
let x = a[i - 1];
a[i - 1] = a[j];
a[j] = x;
}
}
function initCanvas() {
bufferCanvas = document.createElement("canvas");
[canvas, bufferCanvas].forEach(can => {
can.width = window.innerWidth;
can.height = window.innerHeight;
});
context = canvas.getContext("2d");
bufferContext = bufferCanvas.getContext("2d");
[context, bufferContext].forEach(ctx => {
ctx.msImageSmoothingEnabled = false;
ctx.imageSmoothingEnabled = false;
});
}
function initMaze() {
width = Math.floor(canvas.width / settings.blockSize);
height = Math.floor(canvas.height / settings.blockSize);
data = new Array(height);
changedCells.length = 0;
for(let y = 0; y < height; y++) {
data[y] = new Array(width);
for(let x = 0; x < width; x++) {
data[y][x] = null;
}
}
}
function initGeneration() {
// put starting position on the stack
dfsStack = new Array();
const border = settings.border;
dfsStack.push(new Cell(
settings.startX * (width - 2*border - 1) + border,
settings.startY * (height - 2*border - 1) + border));
directions = [ Direction.RIGHT, Direction.LEFT, Direction.UP, Direction.DOWN ];
}
function addCell(cell) {
data[cell.y][cell.x] = cell;
changedCells.push(cell);
}
maze.performStep = function() {
// get next cell
let cell;
let makeCircle = false;
while(true) {
if(dfsStack.length == 0) {
return false;
}
cell = dfsStack.pop();
if(data[cell.y][cell.x] != null) {
if(Math.random() < settings.circleProbability && settings.cellDistance > 1) {
makeCircle = true;
} else {
continue;
}
}
// only one element per update
break;
}
let color = cell.color;
// draw the intermediate cells
if(cell.fromDir !== Direction.NONE) {
for(let i = 1; i < settings.cellDistance; i++) {
// minus fromDir, as the directions points from old to new position (p is the new position)
addCell(new Cell(
cell.x - i * cell.fromDir.x,
cell.y - i * cell.fromDir.y,
color));
color++;
}
}
// draw the cell (if this step is not a circle connection)
if(makeCircle)
return true;
addCell(cell);
color++;
// shuffle directions
if(Math.random() < settings.directionShuffleProbability)
shuffle(directions);
// expand maze in all directions
// opposite order so that the first element will be the top-most on the stack
for(let i = directions.length-1; i >= 0; i--) {
// no need to check the direction that we are coming from
if(directions[i].isOpposite(cell.fromDir))
continue;
// disallow continuing in the same direction
if(settings.disallowSameDirection && directions[i] === cell.fromDir)
continue;
const newX = cell.x + settings.cellDistance * directions[i].x;
if(newX < settings.border || newX >= width - settings.border)
continue;
const newY = cell.y + settings.cellDistance * directions[i].y;
if(newY < settings.border || newY >= height - settings.border)
continue;
dfsStack.push(new Cell(newX, newY, color, directions[i]));
}
return true;
}
function redrawChanged() {
changedCells.forEach(drawCell);
changedCells.length = 0;
}
maze.redrawAll = function(guiSettings) {
settings.backgroundColor = guiSettings.backgroundColor;
settings.colorFactor = guiSettings.colorFactor;
settings.colorFunction = guiSettings.colorFunction;
for(let y = 0; y < height; y++) {
for(let x = 0; x < width; x++) {
if(data[y][x] != null) {
drawCell(data[y][x]);
}
}
}
}
function drawCell(cell) {
if(settings.colorFactor == 0) {
bufferContext.fillStyle = "#ffffff";
} else {
bufferContext.fillStyle = settings.colorFunction(
(cell.color % settings.colorFactor) / settings.colorFactor,
cell.x / width,
cell.y / height);
}
bufferContext.fillRect(
cell.x * settings.blockSize,
cell.y * settings.blockSize,
settings.blockSize,
settings.blockSize);
}
function drawBuffer() {
context.fillStyle = settings.backgroundColor;
context.fillRect(0, 0, canvas.width, canvas.height);
context.drawImage(bufferCanvas, 0, 0);
}
function updateAndDrawFrame() {
if(running) {
for(let i = 0; i < updatesPerFrame; i++) {
if(!maze.performStep()) {
break;
}
}
}
redrawChanged();
drawBuffer();
animFrameReqId = requestAnimFrame(updateAndDrawFrame);
};
maze.start = function(guiSettings) {
settings = new Settings(guiSettings);
maze.setSpeed(guiSettings.speed);
maze.setRunning(guiSettings.running);
initCanvas();
initMaze();
initGeneration();
// start the draw-loop
if(animFrameReqId !== undefined) {
cancelAnimFrame(animFrameReqId);
}
animFrameReqId = requestAnimFrame(updateAndDrawFrame);
}
maze.completeGeneration = function() {
while(maze.performStep()) {
// performStep will return false when no more cells are left
}
}
maze.setRunning = function(_running) {
running = _running;
}
maze.setSpeed = function(_updatesPerFrame) {
updatesPerFrame = Math.min(Math.max(_updatesPerFrame, 1), 10000);
}
maze.setCanvas = function(canvasElement) {
canvas = canvasElement;
}
}(window.maze = window.maze || {}));