-
Notifications
You must be signed in to change notification settings - Fork 0
/
anim.js
94 lines (79 loc) · 2.02 KB
/
anim.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
import {
DistanceGrid,
MazeCanvasRenderer,
MazeGrid,
RecursiveBacktracker,
} from "./legendary-mazes.js";
const maze = new MazeGrid(16, 16);
const canvas = document.getElementById("maze-canvas");
const ctx = canvas.getContext("2d");
canvas.width = maze.width;
canvas.height = maze.height;
let builder;
let distances;
let renderer;
const initializeMaze = () => {
builder = new RecursiveBacktracker(maze);
distances = new DistanceGrid(maze);
renderer = new MazeCanvasRenderer(maze, [
"#012",
"#346",
"#679",
"#f30",
"#ff3",
"#3f3",
"#3ff",
"#03f",
]);
};
const nearColor = { r: 128, g: 0, b: 32 };
const farColor = { r: 255, g: 250, b: 230 };
const DELAY_BETWEEN_STEPS_MS = 20;
const DELAY_AFTER_FINISHED_MS = 1500;
let buildMode = "backtracker";
const nextStepReady = () => window.postMessage({ type: "nextStepReady" });
const buildNextStep = () => {
switch (buildMode) {
case "backtracker":
if (!builder.isFinished) {
builder.nextStep();
renderer.render(ctx);
nextStepReady();
} else {
buildMode = "distances";
distances.start(maze.centralCell);
buildNextStep();
}
break;
case "distances":
if (!distances.isFinished) {
distances.nextStep();
renderer.setDistanceGrid(distances, nearColor, farColor);
renderer.render(ctx);
nextStepReady();
} else {
window.postMessage({ type: "mazeReady" });
// buildMode = "backtracker";
// initializeMaze();
// builder.start();
// buildNextStep();
}
}
};
initializeMaze();
builder.start();
buildNextStep();
window.addEventListener("message", ({ data }) => {
if (data.type === "buildNextStep") {
buildNextStep();
}
});
document.getElementById("nextStep").addEventListener("pointerdown", () => {
window.postMessage({ type: "buildNextStep" });
});
document.addEventListener("keydown", ({ which }) => {
if (which === 78) {
// 'n'
window.postMessage({ type: "buildNextStep" });
}
});