-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
157 lines (141 loc) · 4.72 KB
/
index.html
File metadata and controls
157 lines (141 loc) · 4.72 KB
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
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Emulator</title>
<style>
body, html {
padding: 0;
}
body {
margin: 20px;
background: #ddd;
}
canvas {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
border: 3px solid #666;
background: #fff;
}
.actions > div {
padding: 5px;
color: #bbb;
}
.overlay {
display: none;
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
opacity: 0.3;
background: repeating-linear-gradient(
-55deg,
#222,
#222 30px,
#333 30px,
#333 60px
);
}
.overlay.active {
display: block;
}
.disconnected {
display: none;
position: absolute;
font-family: sans-serif;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-transform: uppercase;
border: 6px solid crimson;
color: crimson;
padding: 2px 10px;
font-size: 4em;
background: #FFF;
}
.disconnected.active {
display: block;
}
#wires {
background: transparent;
}
</style>
</head>
<body>
<canvas id="board" width="1875" height="1100"></canvas>
<canvas id="wires" width="1875" height="1100"></canvas>
<div class="overlay active"></div>
<div class="disconnected active">Disconnected</div>
<script src="/node_modules/socket.io-client/dist/socket.io.js"></script>
<script>
const socket = io('//:3000'),
position = null,
stepAmount = 10,
boardCanvas = document.querySelector('canvas#board'),
wiresCanvas = document.querySelector('canvas#wires'),
boardCtx = boardCanvas.getContext('2d'),
wiresCtx = wiresCanvas.getContext('2d')
function setCanvasSize() {
const canvasSizeRatio = boardCanvas.height / boardCanvas.width;
const canvasHeight = window.innerHeight - 46;
const canvasWidth = canvasHeight / canvasSizeRatio;
boardCanvas.style.width = `${canvasWidth}px`;
boardCanvas.style.height = `${canvasHeight}px`;
wiresCanvas.style.width = `${canvasWidth}px`;
wiresCanvas.style.height = `${canvasHeight}px`;
}
function clearCanvas() {
boardCtx.clearRect(0,0,boardCanvas.width,boardCanvas.height)
}
window.addEventListener('resize', setCanvasSize);
setCanvasSize();
wiresCanvas.width = boardCanvas.width
wiresCanvas.height = boardCanvas.height
boardCtx.lineWidth = 3
boardCtx.strokeStyle = '#3ac11f'
wiresCtx.strokeStyle = '#aaa'
wiresCtx.lineWidth = 3
wiresCtx.setLineDash([3, 3])
socket.on('disconnect', socket => {
document.querySelector('.disconnected').classList.add('active')
document.querySelector('.overlay').classList.add('active')
})
let lastPoint = null;
socket.on('connect', function () {
clearCanvas()
document.querySelector('.disconnected').classList.remove('active')
document.querySelector('.overlay').classList.remove('active')
getPoints();
});
function getPoints() {
socket.emit('getPoints', { count: 200 }, points => {
if (lastPoint === null) {
lastPoint = points.shift()
}
boardCtx.beginPath()
points.forEach(point => {
boardCtx.moveTo(lastPoint.x/4, lastPoint.y/4)
boardCtx.lineTo(point.x/4, point.y/4)
points.shift()
lastPoint = point;
})
boardCtx.stroke()
boardCtx.closePath();
wiresCtx.clearRect(0, 0, wiresCanvas.width, wiresCanvas.height)
wiresCtx.beginPath()
wiresCtx.moveTo(0, 0)
wiresCtx.lineTo(lastPoint.x/4, lastPoint.y/4)
wiresCtx.stroke()
wiresCtx.moveTo(wiresCanvas.width, 0)
wiresCtx.lineTo(lastPoint.x/4, lastPoint.y/4)
wiresCtx.stroke()
wiresCtx.closePath();
requestAnimationFrame(getPoints)
});
}
</script>
</body>
</html>