-
Notifications
You must be signed in to change notification settings - Fork 2
/
home.html
169 lines (144 loc) · 4.74 KB
/
home.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
<html lang="en">
<head>
<script type="text/javascript">
const onLoadWindow = () => {
if (!window["WebSocket"]) {
throw new Error('browser doesn\'t support websocket')
}
const websocket = new WebSocket(`ws://${document.location.host}/ws`)
const canvasElement = document.getElementById('canvas')
const ctx = canvasElement.getContext('2d')
canvasElement.style.border = '2px solid white'
const DEBUG_PADDING_X = 20
const DEBUG_PADDING_Y = 15
const DEFAULT_LINE_HEIGHT = 16
// User interface
websocket.onmessage = event => {
const {
debug,
fps,
tps,
gameWidth, gameHeight,
p1Score, p2Score,
ballWidth, ballHeight,
ballX, ballY,
ballXVelocity, ballYVelocity,
p1Width, p1Height,
p2Width, p2Height,
p1X, p1Y,
p2X, p2Y,
p1YVelocity,
p2YVelocity,
} = JSON.parse(event.data)
// Game
canvasElement.width = gameWidth
canvasElement.height = gameHeight
// Scores
// P1
ctx.font = '14px sans-serif'
ctx.fillStyle = 'white'
ctx.fillText(`${p1Score}`, gameWidth / 2 - DEBUG_PADDING_X, 0 + DEBUG_PADDING_Y * 1.5)
// P2
ctx.direction = 'rtl'
ctx.fillText(`${p2Score}`, gameWidth / 2 + DEBUG_PADDING_X, 0 + DEBUG_PADDING_Y * 1.5)
// Reset
ctx.direction = 'ltr'
// Dotted centered line
ctx.beginPath()
ctx.setLineDash([2, 4])
ctx.beginPath()
ctx.moveTo(gameWidth / 2, 0)
ctx.lineTo(gameWidth / 2, gameHeight)
ctx.strokeStyle = 'white'
ctx.lineWidth = 2
ctx.stroke()
// Reset
ctx.setLineDash([0, 0])
ctx.lineWidth = null
// Ball
ctx.beginPath()
ctx.arc(ballX, ballY, ballHeight, 0, 2 * Math.PI)
ctx.fillStyle = 'white'
ctx.fill()
// P1
ctx.beginPath()
ctx.rect(p1X, p1Y, p1Width, p1Height)
ctx.fillStyle = 'white'
ctx.fill()
// P2
ctx.beginPath()
ctx.rect(p2X, p2Y, p2Width, p2Height)
ctx.fill()
if (debug) {
// FPS/TPS
ctx.fillStyle = 'white'
ctx.direction = 'rtl'
ctx.fillText(`fps: ${fps}, tps: ${tps}`, gameWidth - DEBUG_PADDING_X / 2, 0 + DEBUG_PADDING_Y * 1.5)
// Reset
ctx.direction = 'ltr'
// P1
ctx.fillText('P1', p1X + DEBUG_PADDING_X, p1Y + DEBUG_PADDING_Y)
ctx.fillText(`x: ${p1X}, y: ${p1Y}`, p1X + DEBUG_PADDING_X, p1Y + DEFAULT_LINE_HEIGHT + DEBUG_PADDING_Y)
ctx.fillText(`y velocity: ${p1YVelocity}`, p1X + DEBUG_PADDING_X, p1Y + DEFAULT_LINE_HEIGHT * 2 + DEBUG_PADDING_Y)
// P2
ctx.direction = 'rtl'
ctx.fillText('P2', p2X - DEBUG_PADDING_X + p2Width, p2Y + DEBUG_PADDING_Y)
ctx.fillText(`x: ${p2X}, y: ${p2Y}`, p2X - DEBUG_PADDING_X + p2Width, p2Y + DEFAULT_LINE_HEIGHT + DEBUG_PADDING_Y)
ctx.fillText(`y velocity: ${p2YVelocity}`, p2X - DEBUG_PADDING_X + p2Width, p2Y + DEFAULT_LINE_HEIGHT * 2 + DEBUG_PADDING_Y)
// Reset
ctx.direction = 'ltr'
// Ball
ctx.fillText('Ball', ballX + DEBUG_PADDING_X - (ballHeight / 2), ballY + DEBUG_PADDING_Y - (ballHeight / 2))
ctx.fillText(`x: ${ballX}, y: ${ballY}`, ballX + DEBUG_PADDING_X - (ballHeight / 2), ballY + DEFAULT_LINE_HEIGHT + DEBUG_PADDING_Y - (ballHeight / 2))
ctx.fillText(`x velocity: ${ballXVelocity}, y velocity: ${ballYVelocity}`, ballX + DEBUG_PADDING_X - (ballHeight / 2), ballY + DEFAULT_LINE_HEIGHT * 2 + DEBUG_PADDING_Y - (ballHeight / 2))
// Ball velocity
ctx.beginPath()
ctx.moveTo(ballX, ballY)
ctx.lineTo(ballX + ballXVelocity, ballY + ballYVelocity)
ctx.lineWidth = 1
ctx.stroke()
}
}
// Inputs
document.addEventListener('keydown', event => {
console.debug(event)
const { key } = event
switch (key) {
case 'ArrowUp':
websocket.send(key)
break
case 'ArrowDown':
websocket.send(key)
break
}
})
// Error
websocket.addEventListener('error', console.error)
}
// Wait for DOM
window.onload = onLoadWindow
</script>
</head>
<body>
<style>
html {
background: black;
}
#container {
width: 100%;
}
canvas {
border: none;
bottom: 0;
left: 0;
margin: auto;
position: absolute;
right: 0;
top: 0;
}
</style>
<div id="container">
<canvas width="0" height="0" id="canvas"></canvas>
</div>
</body>
</html>