-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualizer.html
277 lines (225 loc) · 13.7 KB
/
visualizer.html
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<!DOCTYPE html>
<html>
<head>
<script src="https://pixijs.download/release/pixi.min.js"></script>
<script src="https://github.com/davidfig/pixi-viewport/releases/download/4.34.4/viewport.min.min.js"></script>
</head>
<body style="margin: 0">
<script>
async function main() {
const data = {}
const roadElementTextureHorizontal = await PIXI.Texture.fromURL('https://img.fuserobotics.io/roadh.png', { mipmap: true })
const roadElementTextureVertical = await PIXI.Texture.fromURL('https://img.fuserobotics.io/roadv.png', { mipmap: true })
const crossroadTexture = await PIXI.Texture.fromURL("https://img.fuserobotics.io/crossroad.png", { mipmap: true })
const roadElementLength = 25
const personalVehicleTextureHorizontal = await PIXI.Texture.fromURL('https://img.fuserobotics.io/personalh.png', { mipmap: true })
const personalVehicleTextureVertical = await PIXI.Texture.fromURL('https://img.fuserobotics.io/personalv.png', { mipmap: true })
const busVehicleTextureHorizontal = await PIXI.Texture.fromURL('https://img.fuserobotics.io/bush.png', { mipmap: true })
const busVehicleTextureVertical = await PIXI.Texture.fromURL('https://img.fuserobotics.io/busv.png', { mipmap: true })
const fireTruckVehicleTextureHorizontal = await PIXI.Texture.fromURL('https://img.fuserobotics.io/fireh.png', { mipmap: true })
const fireTruckVehicleTextureVertical = await PIXI.Texture.fromURL('https://img.fuserobotics.io/firev.png', { mipmap: true })
const ambulanceVehicleTextureHorizontal = await PIXI.Texture.fromURL('https://img.fuserobotics.io/ambulanceh.png', { mipmap: true })
const ambulanceVehicleTextureVertical = await PIXI.Texture.fromURL('https://img.fuserobotics.io/ambulancev.png', { mipmap: true })
const policeVehicleTextureHorizontal = await PIXI.Texture.fromURL('https://img.fuserobotics.io/policeh.png', { mipmap: true })
const policeVehicleTextureVertical = await PIXI.Texture.fromURL('https://img.fuserobotics.io/policev.png', { mipmap: true })
const busStopTexture = await PIXI.Texture.fromURL('https://img.fuserobotics.io/busstop.png', { mipmap: true })
const vehicleTypes = {
0: [personalVehicleTextureHorizontal, personalVehicleTextureVertical],
1: [busVehicleTextureHorizontal, busVehicleTextureVertical],
2: [fireTruckVehicleTextureHorizontal, fireTruckVehicleTextureVertical],
3: [ambulanceVehicleTextureHorizontal, ambulanceVehicleTextureVertical],
4: [policeVehicleTextureHorizontal, policeVehicleTextureVertical],
}
const app = new PIXI.Application({
width: window.innerWidth,
height: window.innerHeight,
resolution: window.devicePixelRatio || 1,
antialias: true,
autoResize: true
})
document.body.appendChild(app.view)
const viewport = new pixi_viewport.Viewport({
screenWidth: window.innerWidth,
screenHeight: window.innerHeight,
worldWidth: 10_000_000,
worldHeight: 10_000_000,
interaction: app.renderer.plugins.interaction, // the interaction module is important for wheel to work properly when renderer.view is placed or scaled
})
app.stage.addChild(viewport)
viewport.drag().pinch().wheel().decelerate()
viewport.sortableChildren = true
const createCircle = (color, x, y) => {
const circle = new PIXI.Graphics()
circle.beginFill(color)
circle.lineStyle(5, 0xffffff)
circle.arc(0, 0, 50, 0, Math.PI * 2)
circle.position = { x: x, y: y }
return circle
}
const getMaxY = () => {
let maxY = 0
for (const child of viewport.children) {
let y = child.y + Math.max(child.width, child.height)
if (y > maxY) maxY = y
}
return maxY
}
// Create a new texture
let undrawnRoads = data.roads.filter((road) => !road.drawn)
while (undrawnRoads.length > 0) {
placeRoad(data.roads.indexOf(undrawnRoads[0]), { x: 0, y: getMaxY() + 100 }, false)
undrawnRoads = data.roads.filter((road) => !road.drawn)
}
let vehicles = new PIXI.Container();
let trafficLights = new PIXI.Container();
viewport.addChild(vehicles)
viewport.addChild(trafficLights)
for (const log of data.logs) {
clearVehicles()
clearTrafficLights()
for (const vehicle of log.vehicles) drawVehicle(vehicle)
for (const trafficLight of log.trafficLights) drawTrafficLight(trafficLight)
await new Promise((r) => setTimeout(r), parseFloat(data.simTime) * 1000)
}
function clearVehicles() {
viewport.removeChild(vehicles)
vehicles = new PIXI.Container()
viewport.addChild(vehicles)
}
function clearTrafficLights() {
viewport.removeChild(trafficLights)
trafficLights = new PIXI.Container()
viewport.addChild(trafficLights)
}
function drawVehicle(vehicle) {
const roadName = vehicle.road
const road = data.roads.find(road => road.name === roadName)
const start = road.start
const texture = !road.invert ? vehicleTypes[vehicle.type][0] : vehicleTypes[vehicle.type][1]
const sprite = new PIXI.Sprite(texture)
sprite.scale.set(1.5, 1.5)
let x, y
if (road.invert) {
y = start.y + (vehicle.position / roadElementLength) * roadElementTextureVertical.height - sprite.height / 2
x = start.x - sprite.width / 2
} else {
y = start.y - sprite.height / 2
x = start.x + (vehicle.position / roadElementLength) * roadElementTextureHorizontal.width - sprite.width / 2
}
sprite.x = x
sprite.y = y
vehicles.addChild(sprite)
}
function drawTrafficLight(trafficLight) {
const roadName = trafficLight.road
const road = data.roads.find(road => road.name === roadName)
const start = road.start
let x, y
if (road.invert) {
y = start.y + (trafficLight.position / roadElementLength) * roadElementTextureVertical.height
x = start.x - roadElementTextureVertical.width / 2
} else {
y = start.y + roadElementTextureHorizontal.height / 2
x = start.x + (trafficLight.position / roadElementLength) * roadElementTextureHorizontal.width
}
const c = createCircle([0x00ff00, 0xff0000, 0xffa500][trafficLight.state], x, y)
trafficLights.addChild(c)
}
function drawBusStop(busStop) {
const roadName = busStop.road
const road = data.roads.find(road => road.name === roadName)
const start = road.start
const sprite = new PIXI.Sprite(busStopTexture)
sprite.zIndex = 10
sprite.scale.set(0.75, 0.75)
let x, y
if (road.invert) {
y = start.y + (busStop.position / roadElementLength) * roadElementTextureVertical.height - sprite.height / 2
x = start.x - sprite.width / 2 + 180
} else {
y = start.y - sprite.height / 2 + 80
x = start.x + (busStop.position / roadElementLength) * roadElementTextureHorizontal.width - sprite.width / 2
}
sprite.x = x
sprite.y = y
viewport.addChild(sprite)
}
function placeRoad(roadIndex, start, invert) {
const road = data.roads[roadIndex]
if (road.drawn) return
// Create label
const text = new PIXI.Text(road.name, {
fontFamily: 'Arial',
fontSize: 48,
fill: 0x000000,
align: 'left',
})
if (invert) {
text.x = start.x - 70
text.y = start.y + 50
text.rotation = Math.PI / 2
} else {
text.x = start.x + 50
text.y = start.y + 70
}
viewport.addChild(text);
// Draw road itself
[...Array(road.length / roadElementLength).keys()].forEach((i) => {
let roadElement
if (invert) {
roadElement = new PIXI.Sprite(roadElementTextureVertical)
roadElement.y = start.y + i * roadElementTextureVertical.height
roadElement.x = start.x - roadElementTextureVertical.width / 2
roadElement.zIndex = -2
} else {
roadElement = new PIXI.Sprite(roadElementTextureHorizontal)
roadElement.y = start.y - roadElementTextureHorizontal.height / 2
roadElement.x = start.x + i * roadElementTextureHorizontal.width
roadElement.zIndex = -2
}
viewport.addChild(roadElement)
})
// Update road metadata
data.roads[roadIndex].drawn = true
data.roads[roadIndex].start = start
data.roads[roadIndex].invert = invert
// Fetch all busstops
const relevantBusStops = (data.busStops || []).filter(busStop => busStop.road === road.name)
relevantBusStops.forEach(busStop => drawBusStop(busStop))
// Fetch all crossroads
const relevantCrossroads = (data.crossroads || []).filter((crossroad) => crossroad.find((r) => road.name === r.name))
for (const [road1, road2] of relevantCrossroads) {
const crossroadOther = road1.name === road.name ? road2 : road1
const crossroadCurrent = road1.name === road.name ? road1 : road2
const otherRoad = data.roads.find((r) => r.name === crossroadOther.name)
if (otherRoad.drawn) continue
if (invert) {
const crossroadX = start.x
const crossroadY = start.y + (crossroadCurrent.position / roadElementLength) * roadElementTextureVertical.height;
const c = new PIXI.Sprite(crossroadTexture)
c.zIndex = -1
c.x = crossroadX - crossroadTexture.width / 2 + 1
c.y = crossroadY - crossroadTexture.height / 2 - 2
viewport.addChild(c)
const offsetX = ((otherRoad.length / roadElementLength) * roadElementTextureHorizontal.width * crossroadOther.position) / otherRoad.length
const offsetY = ((road.length / roadElementLength) * roadElementTextureVertical.height * crossroadCurrent.position) / road.length
placeRoad(data.roads.indexOf(otherRoad), { x: start.x - offsetX, y: start.y + offsetY }, !invert)
} else {
const crossroadX = start.x + (crossroadCurrent.position / roadElementLength) * roadElementTextureHorizontal.width
const crossroadY = start.y;
const c = new PIXI.Sprite(crossroadTexture)
c.zIndex = -1
c.x = crossroadX - crossroadTexture.width / 2 + 1
c.y = crossroadY - crossroadTexture.height / 2 - 2
viewport.addChild(c)
const offsetX = ((road.length / roadElementLength) * roadElementTextureHorizontal.width * crossroadCurrent.position) / road.length
const offsetY = ((otherRoad.length / roadElementLength) * roadElementTextureVertical.height * crossroadOther.position) / otherRoad.length
placeRoad(data.roads.indexOf(otherRoad), { x: start.x + offsetX, y: start.y - offsetY }, !invert)
}
}
}
}
main().catch(console.log)
</script>
</body>
</html>