-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimatePath.ts
41 lines (39 loc) · 1.35 KB
/
animatePath.ts
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
import {
EXTENDED_SLEEP_TIME,
PATH_TILE_STYLE,
SLEEP_TIME,
SPEEDS,
TRAVERSED_TILE_STYLE,
} from "./constants";
import { isEqual } from "./helpers";
import { SpeedType, TileType } from "./types";
export const animatePath = (
traversedTiles: TileType[],
path: TileType[],
startTile: TileType,
endTile: TileType,
speed: SpeedType
) => {
for (let i = 0; i < traversedTiles.length; i++) {
setTimeout(() => {
const tile = traversedTiles[i];
if (!isEqual(tile, startTile) && !isEqual(tile, endTile)) {
document.getElementById(
`${tile.row}-${tile.col}`
)!.className = `${TRAVERSED_TILE_STYLE} animate-traversed`;
}
}, SLEEP_TIME * i * SPEEDS.find((s) => s.value === speed)!.value); // Calculate delay based on speed
}
setTimeout(() => {
for (let i = 0; i < path.length; i++) {
setTimeout(() => {
const tile = path[i];
if (!isEqual(tile, startTile) && !isEqual(tile, endTile)) {
document.getElementById(
`${tile.row}-${tile.col}`
)!.className = `${PATH_TILE_STYLE} animate-path`;
}
}, EXTENDED_SLEEP_TIME * i * SPEEDS.find((s) => s.value === speed)!.value); // Calculate delay based on speed
}
}, SLEEP_TIME * traversedTiles.length * SPEEDS.find((s) => s.value === speed)!.value); // Calculate delay based on the total traversal time
};