-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
134 lines (109 loc) · 4.14 KB
/
main.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
import Grid from './grid/Grid.js';
import addStart from './userInterface/addStartEnd.js';
import { updateDisplay, drawBackground } from './userInterface/display.js';
import addWallBegin from './userInterface/addWalls.js';
import animateAlgorithmBegin from './userInterface/animateAlgorithm.js';
import randomMaze from './mazes/randomMaze.js';
import verticalMaze from './mazes/verticalMaze.js';
import horizontalMaze from './mazes/horizontalMaze.js';
import radialMaze from './mazes/radialMaze.js';
// Hexagons. Controls the size in pixels of the hexagons.
window.hexSize = { width: 16 }; // Should be divisible by 2
hexSize.height = Math.floor(hexSize.width * (Math.sqrt(3) / 2));
// Grid matrix. Stores information about the hex grid and is fed into the
// algorithms.
window.grid = [];
// Animation
window.animationDelay = 33; // milliseconds
window.timeoutId = null;
// Document elements
window.bgCanvas = document.getElementById('background');
window.bgContext = bgCanvas.getContext('2d');
window.animationCanvas = document.getElementById('animation');
window.animationContext = animationCanvas.getContext('2d');
window.instructions = document.getElementById('instructions');
function updateCanvasSize() {
const w = document.getElementById('canvasContainer').offsetWidth;
const h = document.getElementById('canvasContainer').offsetHeight;
bgCanvas.width = w;
bgCanvas.height = h;
animationCanvas.width = w;
animationCanvas.height = h;
return [w, h];
}
function updateGridSize(canvasWidth, canvasHeight) {
const rows = Math.floor(
(canvasHeight - hexSize.height) / (hexSize.height * 2)
);
const cols = Math.floor(
(canvasWidth - hexSize.width / 2) / (hexSize.width * 1.5)
);
// eslint-disable-next-line no-global-assign
grid = new Grid(rows, cols, hexSize);
}
function reset() {
// Enable buttons. May be disabled if an algorithm is running.
const algButtonsContainer = document.getElementById('algorithmButtons');
for (let i = 0; i < algButtonsContainer.children.length; i += 1) {
const button = algButtonsContainer.children[i];
button.disabled = false;
}
const mazeButtonsContainer = document.getElementById('mazeButtons');
for (let i = 0; i < mazeButtonsContainer.children.length; i += 1) {
const button = mazeButtonsContainer.children[i];
button.disabled = false;
}
// Stop animation of an algorithm. One may be running.
clearTimeout(timeoutId);
animationCanvas.removeEventListener('mousedown', addWallBegin);
grid.start = undefined;
grid.end = undefined;
grid.forEach((row) => {
row.forEach((tile) => {
tile.reset();
});
});
updateDisplay(animationContext);
animationCanvas.addEventListener('mousedown', addStart);
instructions.innerHTML = 'Select start position';
}
function resize() {
const [w, h] = updateCanvasSize();
updateGridSize(w, h);
drawBackground(bgContext);
reset();
}
function setup() {
document.getElementById('resetButton').onclick = reset;
document.getElementById('dijkstraButton').onclick = () => {
animateAlgorithmBegin('dijkstra');
};
document.getElementById('aStarButton').onclick = () => {
animateAlgorithmBegin('aStar');
};
document.getElementById('biasedAStarButton').onclick = () => {
animateAlgorithmBegin('biasedAStar');
};
document.getElementById('randomMazeButton').onclick = randomMaze;
document.getElementById('verticalMazeButton').onclick = verticalMaze;
document.getElementById('horizontalMazeButton').onclick = horizontalMaze;
document.getElementById('radialMazeButton').onclick = radialMaze;
document.getElementById('tutorialButton').onclick = () => {
document.getElementById('tutorial').style.display = 'block';
};
document.getElementById('closeTutorial').onclick = () => {
document.getElementById('tutorial').style.display = 'none';
};
window.onclick = (event) => {
const tutorial = document.getElementById('tutorial');
if (event.target === tutorial) {
tutorial.style.display = 'none';
}
};
const [w, h] = updateCanvasSize();
updateGridSize(w, h);
drawBackground();
animationCanvas.addEventListener('mousedown', addStart);
window.addEventListener('resize', resize);
}
setup();