-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvasHandler.js
More file actions
260 lines (234 loc) · 6.12 KB
/
canvasHandler.js
File metadata and controls
260 lines (234 loc) · 6.12 KB
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
const DRAW_GRID = true;
const WIDTH_PX = 1200;
const HEIGHT_PX = parseInt(WIDTH_PX / 2);
const NBR_CASE_WIDTH = 50;
const NBR_CASE_HEIGHT = parseInt(NBR_CASE_WIDTH / 2);
const CASE_SIZE = WIDTH_PX / NBR_CASE_WIDTH;
/* Colors */
const GRID_COLOR = "#2daae0";
/* Runtime Vars */
var running = false
var canvas = document.getElementById("mainCanvas");
canvas.width = WIDTH_PX;
canvas.height = HEIGHT_PX;
var ctx = canvas.getContext("2d");
const rect = canvas.getBoundingClientRect();
const paddingCase = Math.floor(CASE_SIZE*0.1)
let mouseIsDown = false;
var startPos = {
x: -1,
y: -1,
placed: false,
};
var endPos = {
x: -1,
y: -1,
placed: false,
};
var map = new Map(NBR_CASE_WIDTH, NBR_CASE_HEIGHT);
var obstaclesPosFromMap = [];
var obstaclesPos = [];
var selectedButton = null;
const start = document.getElementById("start");
const obstacle = document.getElementById("obstacle");
const end = document.getElementById("end");
const back = document.getElementById("back");
const reset = document.getElementById("reset");
const play = document.getElementById("play");
const pause = document.getElementById("pause");
start.addEventListener("click", () => {
selectedButton = start;
});
obstacle.addEventListener("click", () => {
selectedButton = obstacle;
});
end.addEventListener("click", () => {
selectedButton = end;
});
back.addEventListener("click", () => { //delete last obstacle
map.deleteObstacle(obstaclesPos[obstaclesPos.length-1])
obstaclesPos.pop();
});
reset.addEventListener("click", () => {
startPos.placed = false
endPos.placed = false
obstaclesPos = []
map = new Map(NBR_CASE_WIDTH, NBR_CASE_WIDTH);
});
play.addEventListener("click", () => {
running = true;
});
pause.addEventListener("click", () => {
running = false;
});
canvas.addEventListener("mouseup", () => {
mouseIsDown = false;
});
canvas.addEventListener("mousemove", (e) => {
if (mouseIsDown && selectedButton === obstacle) {
const x = Math.floor((e.clientX - rect.left) / CASE_SIZE);
const y = Math.floor((e.clientY - rect.top) / CASE_SIZE);
let canAdd = true;
obstaclesPosFromMap = map.getAllObstacleAs2DArray();
for (i = 0; i < obstaclesPosFromMap.length; i++) {
if (arraysEqual(obstaclesPosFromMap[i], [y, x]) || (x == startPos.x && y == startPos.y) || (x == endPos.x && y == endPos.y)) {
canAdd = false;
}
}
if (canAdd) {
obstaclesPos.push([x, y]);
map.arr[y][x] = CasesType.obstacle
}
}
});
canvas.oncontextmenu = function(e) { e.preventDefault(); e.stopPropagation(); }
canvas.addEventListener("mousedown", function (e) {
const x = Math.floor((e.clientX - rect.left) / CASE_SIZE);
const y = Math.floor((e.clientY - rect.top) / CASE_SIZE);
mouseIsDown = true;
if(map.arr[y][x] == 0) {
switch (selectedButton) {
case start:
startPos.x = x;
startPos.y = y;
startPos.placed = true;
currentNaiveNode = [startPos.y, startPos.x]
break;
case obstacle:
if((x != startPos.x || y != startPos.y) && (x != endPos.x || y != endPos.y)) {
obstaclesPos.push([x, y]);
map.arr[y][x] = CasesType.obstacle
}
break;
case end:
endPos.x = x;
endPos.y = y;
endPos.placed = true;
break;
default:
break;
}
}
});
/* from https://stackoverflow.com/a/16436975 */
function arraysEqual(a, b) {
if (a === b) return true;
if (a == null || b == null) return false;
if (a.length !== b.length) return false;
for (var i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false;
}
return true;
}
/* Create Grid */
function initCanvas() {
if (DRAW_GRID) {
ctx.beginPath();
for (x = 0; x < NBR_CASE_HEIGHT; x++) {
ctx.moveTo(0, x * CASE_SIZE + 1);
ctx.lineTo(WIDTH_PX, x * CASE_SIZE);
}
for (y = 0; y < NBR_CASE_WIDTH; y++) {
ctx.moveTo(y * CASE_SIZE + 1, 0);
ctx.lineTo(y * CASE_SIZE, HEIGHT_PX);
}
ctx.strokeStyle = GRID_COLOR;
ctx.stroke();
}
}
function drawCanvas() {
var imgStart = new Image();
imgStart.src = "../assets/icons/startPos.png";
var imgEnd = new Image();
imgEnd.src = "../assets/icons/endPos.png";
if (startPos.placed)
ctx.drawImage(
imgStart,
startPos.x * CASE_SIZE,
startPos.y * CASE_SIZE,
CASE_SIZE,
CASE_SIZE
);
if (endPos.placed)
ctx.drawImage(
imgEnd,
endPos.x * CASE_SIZE,
endPos.y * CASE_SIZE,
CASE_SIZE,
CASE_SIZE
);
for (i = 0; i < map.arr.length; i++) {
for (j = 0; j < map.arr[i].length; j++) {
switch (map.arr[i][j]) {
case CasesType.obstacle:
ctx.fillStyle = "black";
ctx.fillRect(
j * CASE_SIZE + paddingCase,
i * CASE_SIZE + paddingCase,
CASE_SIZE - 2*paddingCase,
CASE_SIZE - 2*paddingCase,
);
break;
case CasesType.visualiser:
ctx.fillStyle = "green";
ctx.fillRect(
j * CASE_SIZE + paddingCase,
i * CASE_SIZE + paddingCase,
CASE_SIZE - 2*paddingCase,
CASE_SIZE - 2*paddingCase,
);
break;
default:
break;
}
}
}
ctx.fillStyle = "black";
}
/* Algo functions and vars */
//Naive
var naiveAlgorithm = new Naive(NBR_CASE_WIDTH, NBR_CASE_HEIGHT);
var currentNaiveNode = null;
function naiveAlgo() {
let toAdd = naiveAlgorithm.nextTurn(map, currentNaiveNode);
if(toAdd !== undefined) {
currentNaiveNode = toAdd[0];
console.log(toAdd[1])
for (let i = 0; i < toAdd[1].length; i++) {
map.arr[toAdd[1][0]][toAdd[1][1]] = CasesType.visualiser;
}
return true;
} else {
return false;
}
}
var isFinished = false;
function run() {
const algo = document.getElementById("algo-select");
ctx.clearRect(0, 0, WIDTH_PX + 1, HEIGHT_PX + 1);
initCanvas();
drawCanvas();
if(!isFinished) {
if(running) {
if(startPos.placed && endPos.placed) {
switch (algo.options[algo.selectedIndex].value) {
case "NAIVE":
if(!naiveAlgo()) {
isFinished = true;
alert("Finished ...");
}
break;
default:
running = false;
alert("Please select an algorithm")
break;
}
} else {
//force pause if start and end are not yet defined
running = false
}
}
}
window.requestAnimationFrame(run);
}
window.requestAnimationFrame(run);