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
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>websweeper</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
</head>
<style>
@font-face {
font-family: vt323;
src: url("font.ttf");
}
#board-container {
font-family: vt323, monospace;
font-size: 28pt;
line-height: 0.6em;
}
body {
background-color: black;
color: white;
}
.unsel {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.cursor * {
font-size: 16pt;
margin: 0 0;
}
</style>
<body>
<div class="">
<div id="board-container">
<div id="board"></div>
</div>
<p id="miscinfo">Loading...</p>
</div>
</body>
<script src="https://p.masba.net:8443/ansispan.js"></script>
<script>
let id = NaN;
let s = new WebSocket("ws://127.0.0.1:31236");
let info_elem = document.getElementById("miscinfo");
let board_elem = document.getElementById("board");
let last_packet = {};
let cursors = new Map();
let board = {};
s.onopen = function(e) { info_elem.innerHTML = "Connected"; }
s.onmessage = function(e) {
console.log(`got msg ${e.data}`);
last_packet = e;
let d = e.data;
if (typeof d == "object") {
d.arrayBuffer().then(acceptPacket);
} else if (typeof e.data == "string") {
let fields = d.split(" ");
if (d.startsWith("pos")) {
let oid = fields[1];
let name = fields[2];
let x = fields[3];
let y = fields[4];
if (!cursors.has(oid)) {
createCursor(oid, name);
}
let celem = cursors.get(oid).elem;
celem.style.left = x + 'px';
celem.style.top = y + 'px';
}
else if (d.startsWith("id")) {
id = fields[1];
createCursor(id, "You");
}
}
}
s.onerror = function(e) { info_elem.innerHTML += `<br>error ${e}`; }
s.onclose = function(e) { info_elem.innerHTML = "Closed"; }
function acceptPacket(data) {
let vals = new Uint8Array(data);
if (vals[0] == 27) {
// starts with escape char, is a board dump
board = Array.from(vals.subarray(1,vals.length).values()).reduce((s,c) => s +
String.fromCodePoint(c), "");
board_elem.innerHTML = ansispan(board);
}
else if (vals[0] == 'p'.charCodeAt(0)) {
// starts with a p, is a cursor position update
// unimplemented!
}
}
function createCursor(id, name) {
// shit doesn't line up
let cursor = document.createElement("div");
cursor.innerHTML = "<p>x</p>";
cursor.style.position = "absolute";
let nametag = document.createElement("p");
nametag.innerHTML = name;
cursor.appendChild(nametag);
cursor.classList.add('cursor');
let cb = cursor.getBoundingClientRect();
let tip = cursor.firstChild.getBoundingClientRect();
let dx = tip.width/2 - cb.width/2;
let dy = -(tip.height/2 - cb.height/2);
document.getElementById('board-container').append(cursor);
document.addEventListener('mousemove', e => {
cursor.style.left = (e.pageX + dx) + 'px';
cursor.style.top = (e.pageY + dy) + 'px';
s.send(`pos ${e.pageX} ${e.pageY}`);
},
false);
cursors.set(id, {name: name, elem: cursor});
return cursor;
}
</script>
</html>
|