-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws.html
85 lines (76 loc) · 3.76 KB
/
ws.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
<html>
<style>
html {
background-color: #444;
}
</style>
<body>
<div>
<span>X0 Pos: <em id="xpos0">-</em></span></br>
<span>Y0 Pos: <em id="ypos0">-</em></span></br>
<span>X1 Pos: <em id="xpos1">-</em></span></br>
<span>Y1 Pos: <em id="ypos1">-</em></span></br>
<span>X0_k Pos: <em id="xpos0_k">-</em></span></br>
<span>Y0_k Pos: <em id="ypos0_k">-</em></span></br>
<span>X1_k Pos: <em id="xpos1_k">-</em></span></br>
<span>Y1_k Pos: <em id="ypos1_k">-</em></span></br>
</div>
<svg style="position: absolute; top: 0px; left: 0px; border: none; height: 100vh; width: 100vw">
<circle id="circle0" cx='0' cy='0' r="30" stroke="green" fill="red" />
<circle id="circle1" cx='0' cy='0' r="30" stroke="red" fill="blue" />
<circle id="circle0_k" cx='0' cy='0' r="30" stroke="red" fill="green" />
<circle id="circle1_k" cx='0' cy='0' r="30" stroke="red" fill="green" />
</svg>
<script>
const coord = new URLSearchParams(window.location.search);
const screen_x = coord.getAll('x');
const screen_y = coord.getAll('y');
const theta = coord.getAll('theta');
const canva_y = document.getElementsByTagName('svg')[0].clientHeight
const canva_x = document.getElementsByTagName('svg')[0].clientWidth
var ws = new WebSocket((window.location.protocol === 'https:' ? 'wss:' : 'ws:') + '//' + window.location.host);
// event emmited when connected
ws.onopen = function () {
console.log('websocket is connected ...')
// sending a send event to websocket server
ws.send('connected')
}
// event emmited when receiving message
ws.onmessage = function (ev) {
console.log("on message data", ev);
ts_mes = JSON.parse(ev.data);
pos = JSON.parse(ts_mes.message);
console.log("onmessage", pos)
for (var key in pos){
if (pos.hasOwnProperty(key)){
if (pos[key].x < screen_x){
var d;
var alpha;
d = Math.sqrt(Math.pow(pos[key].x - screen_x, 2) + Math.pow(pos[key].y - screen_y, 2));
alpha = Math.asin(d/(6));
document.getElementById('xpos'+key).innerText = Math.round(alpha*canva_x/theta);
document.getElementById('ypos'+key).innerText = Math.round((2.55 - pos[key].z)*canva_y);
document.getElementById('circle'+key).setAttribute('cx', Math.round(alpha*canva_x/theta));
document.getElementById('circle'+key).setAttribute('cy', Math.round((2.55 - pos[key].z)*canva_y));
}
if (pos[key].x_k < screen_x){
var d_k;
var alpha_k;
d_k = Math.sqrt(Math.pow(pos[key].x_k - screen_x, 2) + Math.pow(pos[key].y_k - screen_y, 2));
alpha_k = Math.asin(d_k/(6));
document.getElementById('xpos'+key+'_k').innerText = Math.round(alpha_k*canva_x/theta);
document.getElementById('ypos'+key+'_k').innerText = Math.round((2.55 - pos[key].z_k)*canva_y);
document.getElementById('circle'+key+'_k').setAttribute('cx', Math.round(alpha_k*canva_x/theta));
document.getElementById('circle'+key+'_k').setAttribute('cy', Math.round((2.55 - pos[key].z_k)*canva_y));
}
}
}
console.log(pos.x)
// document.getElementById('zpos').innerText = pos.z;
}
window.test = function (str) {
ws.send(str)
}
</script>
</body>
</html>