-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviewer.html
293 lines (250 loc) · 10.2 KB
/
viewer.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<!doctype html>
<html lang="en">
<head>
<title>SSL Veiwer</title>
</head>
<body>
<canvas id="field" width="1040" height="740"> </canvas>
<script type="text/javascript">
const fieldWidth = 1040;
const fieldHeight = 740;
const c = document.getElementById("field");
const ctx = c.getContext("2d", { alpha: false });
fixCanvasHighPixelDensity(c, ctx);
renderField(ctx);
let objectsToDraw = [];
let lastFps;
let stop = false;
let frameCount = 0;
let fps, fpsInterval, startTime, now, then, elapsed;
startAnimating(60);
function startAnimating(fps) {
fpsInterval = 1000 / fps;
then = window.performance.now();
startTime = then;
console.log(startTime);
animate();
}
function animate(newtime) {
// stop
if (stop) {
return;
}
// request another frame
requestAnimationFrame(animate);
// calc elapsed time since last loop
now = newtime;
elapsed = now - then;
// if enough time has elapsed, draw the next frame
if (elapsed > fpsInterval) {
// Get ready for next frame by setting then=now, but...
// Also, adjust for fpsInterval not being multiple of 16.67
then = now - (elapsed % fpsInterval);
// draw stuff here
renderField(ctx);
for (const o of objectsToDraw) {
render(ctx, o);
}
// TESTING...Report #seconds since start and achieved fps.
// display fps
ctx.fillStyle = "white";
ctx.strokeStyle = "white";
ctx.font = "13px Arial";
ctx.fillText(`${Math.round(lastFps)} FPS`, 10, 20);
let sinceStart = now - startTime;
let currentFps =
Math.round((1000 / (sinceStart / ++frameCount)) * 100) /
100;
lastFps = currentFps;
}
}
function canvas_arrow(context, fromx, fromy, tox, toy) {
var headlen = 10; // length of head in pixels
var dx = tox - fromx;
var dy = toy - fromy;
var angle = Math.atan2(dy, dx);
context.moveTo(fromx, fromy);
context.lineTo(tox, toy);
context.lineTo(
tox - headlen * Math.cos(angle - Math.PI / 6),
toy - headlen * Math.sin(angle - Math.PI / 6),
);
context.moveTo(tox, toy);
context.lineTo(
tox - headlen * Math.cos(angle + Math.PI / 6),
toy - headlen * Math.sin(angle + Math.PI / 6),
);
}
function fixCanvasHighPixelDensity(canvas, ctx) {
// fix blur on high pixel density screens https://stackoverflow.com/a/59143499
const sizeX = 1040;
const sizeY = 740;
canvas.style.width = sizeX + "px";
canvas.style.height = sizeY + "px";
// Set actual size in memory (scaled to account for extra pixel density).
const scale = window.devicePixelRatio; // Change to 1 on retina screens to see blurry canvas.
canvas.width = sizeX * scale;
canvas.height = sizeY * scale;
// Normalize coordinate system to use css pixels.
ctx.scale(scale, scale);
}
function renderField(ctx) {
// set pen width
ctx.lineWidth = 1;
// div B field from https://robocup-ssl.github.io/ssl-rules/sslrules.html
// background
ctx.fillStyle = "green";
ctx.fillRect(0, 0, fieldWidth, fieldHeight);
// black square
ctx.lineWidth = 4;
ctx.strokeStyle = "black";
ctx.strokeRect(40, 40, 960, 660);
// white outer rect
ctx.lineWidth = 1;
ctx.fillStyle = "white";
ctx.strokeStyle = "white";
ctx.strokeRect(70, 70, 900, 600);
// white center circle
ctx.strokeStyle = "white";
ctx.beginPath();
ctx.ellipse(
1040 / 2,
740 / 2,
50,
50,
Math.PI / 4,
0,
2 * Math.PI,
);
ctx.stroke();
// white vertical line
ctx.strokeStyle = "white";
ctx.moveTo(1040 / 2, 70);
ctx.lineTo(1040 / 2, 670);
ctx.stroke();
// white horizontal line
ctx.strokeStyle = "white";
ctx.moveTo(70, 740 / 2);
ctx.lineTo(970, 740 / 2);
ctx.stroke();
// white left defense area
ctx.strokeStyle = "white";
ctx.strokeRect(70, 740 / 2 - 100, 100, 200);
// white right defense area
ctx.strokeStyle = "white";
ctx.strokeRect(970 - 100, 740 / 2 - 100, 100, 200);
// white left goal
ctx.strokeStyle = "white";
ctx.strokeRect(70 - 18, 740 / 2 - 50, 18, 100);
// white right goal
ctx.strokeStyle = "white";
ctx.strokeRect(970, 740 / 2 - 50, 18, 100);
}
function renderRobot(ctx, robotRenderCommandData) {
let color;
switch (robotRenderCommandData.color) {
case "Blue":
color = "blue";
break;
case "Yellow":
color = "yellow";
break;
default:
console.log(
"unknown color",
robotRenderCommandData.color,
);
return;
}
if (robotRenderCommandData.has_ball) {
color = "purple";
}
const pos_x =
robotRenderCommandData.pos.x * 100 + fieldWidth / 2;
const pos_y =
-robotRenderCommandData.pos.y * 100 + fieldHeight / 2;
const vel_x = robotRenderCommandData.vel.x * 100;
const vel_y = -robotRenderCommandData.vel.y * 100;
ctx.lineWidth = 1;
ctx.fillStyle = color;
ctx.strokeStyle = color;
// vel vector
ctx.beginPath();
canvas_arrow(ctx, pos_x, pos_y, pos_x + vel_x, pos_y + vel_y);
ctx.stroke();
// robot circle
ctx.beginPath();
ctx.arc(pos_x, pos_y, 9, 0, 2 * Math.PI);
ctx.fill();
// robot id
const textColor = color == "blue" ? "white" : "black";
ctx.fillStyle = textColor;
ctx.strokeStyle = textColor;
ctx.font = "13px Arial";
ctx.fillText(
robotRenderCommandData.id,
pos_x - 3.5,
pos_y + 4.5,
); // magic offsets to center the id inside the robot
}
function renderPoint(ctx, pointRenderCommandData) {
const color = pointRenderCommandData.color;
const pos_x =
pointRenderCommandData.pos.x * 100 + fieldWidth / 2;
const pos_y =
-pointRenderCommandData.pos.y * 100 + fieldHeight / 2;
ctx.lineWidth = 1;
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(pos_x, pos_y, 2.5, 0, 2 * Math.PI);
ctx.fill();
}
function renderSegment(ctx, segmentRenderCommandData) {
const color = segmentRenderCommandData.color;
const start_x =
segmentRenderCommandData.start.x * 100 + fieldWidth / 2;
const start_y =
-segmentRenderCommandData.start.y * 100 + fieldHeight / 2;
const end_x =
segmentRenderCommandData.end.x * 100 + fieldWidth / 2;
const end_y =
-segmentRenderCommandData.end.y * 100 + fieldHeight / 2;
ctx.lineWidth = 1;
ctx.strokeStyle = color;
// white horizontal line
ctx.moveTo(start_x, start_y);
ctx.lineTo(end_x, end_y);
ctx.stroke();
}
function render(ctx, renderCommandData) {
switch (renderCommandData.type) {
case "Robot": {
renderRobot(ctx, renderCommandData);
break;
}
case "Point": {
renderPoint(ctx, renderCommandData);
break;
}
case "Segment": {
renderSegment(ctx, renderCommandData);
break;
}
}
}
window.onload = function () {
const ws = new WebSocket("ws://127.0.0.1:8282");
ws.onmessage = (event) => {
// update objectsToDraw when a new frame is received
const frame = JSON.parse(event.data);
objectsToDraw = frame.objects;
};
};
</script>
</body>
<style>
body {
background-color: #0a0a0a;
}
</style>
</html>