-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstartAnimationfile.js
72 lines (61 loc) · 2.34 KB
/
startAnimationfile.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
function startAnimation(visited, arrayOfNodes, isPathFound) {
let delay = 0;
visited = visited;
arrayOfNodes = arrayOfNodes;
colorVisited(visited);
if (isPathFound) {
colorPath(visited, arrayOfNodes);
} else {
setTimeout(() => {
alert("no path found");
}, (delay += parseInt(newBoard.speed)));
}
function colorVisited(visited) {
for (let i = 1; i < visited.length - 1; i++) {
let x = visited[i][0];
let y = visited[i][1];
setTimeout(() => {
document.getElementById(x + "-" + y).className += " visited_color";
}, (delay += parseInt(newBoard.speed)));
}
}
function colorPath(visited, arrayOfNodes) {
delay += 100;
lastNode = visited[visited.length - 1];
path = [];
for (let i = visited.length - 2; i > -1; i--) {
let str1 = arrayOfNodes[lastNode[0]][lastNode[1]].previousNode;
let str2 = arrayOfNodes[visited[i][0]][visited[i][1]].id;
if (str1.localeCompare(str2) == 0) {
path.push([visited[i][0], visited[i][1]]);
lastNode = [visited[i][0], visited[i][1]];
}
}
path.unshift(newBoard.endNode);
for (let i = path.length - 2; i > 0; i--) {
console.log(path)
let x = path[i][0];
let y = path[i][1];
let previous_x = path[i - 1][0];
let previous_y = path[i - 1][1];
setTimeout(() => {
if (previous_x - x == -1 && previous_y - y == 0) {
document.getElementById(x + "-" + y).classList.remove("visited_color")
document.getElementById(x + "-" + y).className += " path_up";
}
if (previous_x - x == 1 && previous_y - y == 0) {
document.getElementById(x + "-" + y).classList.remove("visited_color")
document.getElementById(x + "-" + y).className += " path_down";
}
if (previous_x - x == 0 && previous_y - y == -1) {
document.getElementById(x + "-" + y).classList.remove("visited_color")
document.getElementById(x + "-" + y).className += " path_left";
}
if (previous_x - x == 0 && previous_y - y == 1) {
document.getElementById(x + "-" + y).classList.remove("visited_color")
document.getElementById(x + "-" + y).className += " path_right";
}
}, (delay += parseInt(newBoard.speed) * 3));
}
}
}