-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
139 lines (125 loc) · 4.71 KB
/
index.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
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
import * as pf from "@cetfox24/pathfinding-js"
let paper = Raphael("grid-container", window.innerWidth-250, window.innerHeight);
let mapVertices = [new pf.Point(50, 50), new pf.Point(window.innerWidth - 300, 50), new pf.Point(window.innerWidth-300, window.innerHeight-50), new pf.Point(50, window.innerHeight-50)];
let map = new pf.Shapes.Polygon(mapVertices);
let path = paper.path(map.toPath());
path.attr({fill: '#d8d8c8', stroke: '#938181', "stroke-width": 3});
let finder = new pf.AStar();
let start = new pf.Point(100, 100);
let startCircle: RaphaelElement = paper.circle(100, 100, 8).attr({fill: 'green', stroke: 'black'});
let end = new pf.Point(500, 500)
let endCircle: RaphaelElement = paper.circle(500, 500, 8).attr({fill: 'red', stroke: 'black'});
let astarPath: RaphaelPath;
let objects: pf.Shapes.Polygon[] = [];
type modes = "add shape" | "set start" | "none";
let mode: modes = "none";
let newShapeVertices: pf.Point[] = [];
let newShapeOutline: RaphaelPath | undefined;
// let objects: pf.Shapes.Polygon[] = [];
let objectPaths: RaphaelPath[] = [];
let grid: pf.MeshGrid;
let triangles: RaphaelPath[] = [];
let points: RaphaelElement[] = [];
let addShapeButton: HTMLElement = document.getElementById("addShape")!;
let generateMapButton: HTMLElement = document.getElementById("generateMap")!;
let clearMapButton: HTMLElement = document.getElementById("clearMap")!;
addShapeButton.onclick = (event) => {
mode = "add shape";
newShapeVertices.length = 0;
}
generateMapButton.onclick = (event) => {
mode = "set start";
grid = new pf.MeshGrid(objects, map.vertices);
let result = grid.generateMap();
triangles.forEach(triangle => triangle.remove());
triangles.length = 0;
result.triangulation.forEach(triangle => {
let path = paper.path(triangle.toPath());
path.attr({stroke: 'blue'});
triangles.push(path);
});
points.forEach(point => point.remove());
points.length = 0;
result.points.forEach(point => {
let circle = paper.circle(point.x, point.y, 3);
circle.attr({fill: 'red', stroke: 'black'});
points.push(circle);
});
}
clearMapButton.onclick = (event) => {
mode ="none";
triangles.forEach(triangle => triangle.remove());
points.forEach(point => point.remove());
if (astarPath) astarPath.remove();
objectPaths.forEach(outline => outline.remove());
objectPaths.length = 0;
objects.length = 0;
}
paper.canvas.onclick = (event) => {
if (mode == "add shape") {
newShapeVertices.push(new pf.Point(event.x, event.y));
} else if (mode == "set start") {
setStart(event.x, event.y);
}
}
paper.canvas.oncontextmenu = (event) => {
event.preventDefault();
if (mode == "set start") {
setEnd(event.x, event.y);
}
}
paper.canvas.onmousemove = (event) => {
if (mode == "add shape" && newShapeVertices.length != 0) {
if (newShapeOutline !== undefined) newShapeOutline.remove();
let pathString = `M${newShapeVertices[0].x},${newShapeVertices[0].y}`
for (let i = 1; i < newShapeVertices.length; i++) {
pathString += `L${newShapeVertices[i].x},${newShapeVertices[i].y}`;
}
pathString += `L${event.x},${event.y}`;
newShapeOutline = paper.path(pathString);
}
}
window.addEventListener("keyup", event => {
if (event.which === 13) { // enter
if (newShapeVertices.length < 3) return;
mode = "none";
newShapeOutline!.remove();
let polygon = new pf.Shapes.Polygon([...newShapeVertices]);
objects.push(polygon);
let path = drawPolygon(polygon);
objectPaths.push(path);
}
});
function drawPolygon (polygon: pf.Shapes.Polygon): RaphaelPath {
let path = paper.path(polygon.toPath());
path.attr({fill: 'white', stroke: '#938181', "stroke-width": 3});
return path;
}
function setStart (x: number, y: number): void {
if (astarPath) astarPath.remove();
startCircle.remove();
start.x = x;
start.y = y;
let result = finder.findPathMesh(start, end, grid);
let pathString = "M";
result.path.forEach(point => {
pathString += point.x+' '+point.y+'L';
});
startCircle = paper.circle(start.x, start.y, 8).attr({fill: 'green', stroke: 'black'});
astarPath = paper.path(pathString);
astarPath.attr({stroke: "purple", "stroke-width": 3});
}
function setEnd (x: number, y: number): void {
if (astarPath) astarPath.remove();
endCircle.remove();
end.x = x;
end.y = y;
let result = finder.findPathMesh(start, end, grid);
let pathString = "M";
result.path.forEach(point => {
pathString += point.x+' '+point.y+'L';
});
endCircle = paper.circle(end.x, end.y, 8).attr({fill: 'red', stroke: 'black'});
astarPath = paper.path(pathString);
astarPath.attr({stroke: "purple", "stroke-width": 3});
}