-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
161 lines (155 loc) · 5.03 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta charset="UTF-8" />
<title>Web Sockets Experiment</title>
<style>
* {
box-sizing: border-box;
}
html, body {
margin: 0;
height: 100%;
overflow: hidden;
overscroll-behavior: none;
font-family: Arial, Helvetica, sans-serif;
font-size: max(1vw, 12px);
}
canvas {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: black;
}
.scoreboard-wrapper {
background: rgba(255,255,255,0.1);
pointer-events: none;
position: fixed;
top: 16px;
left: 16px;
width: auto;
height: auto;
color: #ddd;
padding: 16px;
border-radius: 4px;
letter-spacing: 0.06em;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<div class="scoreboard-wrapper">
<span>Scoreboard</span>
<div id="scoreboard"></div>
</div>
<script src="https://cdn.socket.io/4.5.4/socket.io.min.js"></script>
<script>
const socket = io();
const ID_LEN = 5;
const FOOD_RADIUS = 8;
let playersData;
let foodPos;
let foodEaten = false;
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext("2d");
// functions
function onResize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
function drawPlayer(ctx, player) {
const { x, y, color, trail, lifespan } = player;
drawCircle(ctx, x, y, player.radius, color, 1);
trail.forEach(trailItem => {
drawCircle(
ctx,
trailItem.x,
trailItem.y,
(1 - trailItem.age / lifespan) * player.radius,
color,
1 - trailItem.age / lifespan
);
});
}
function drawCircle(ctx, x, y, r, color, alpha) {
ctx.globalAlpha = alpha;
const xx = ctx.canvas.clientWidth * x;
const yy = ctx.canvas.clientHeight * y;
ctx.beginPath();
ctx.arc(xx, yy, r, 0, 2 * Math.PI);
ctx.fillStyle = `hsl(${color})`;
ctx.fill();
ctx.globalAlpha = 1;
}
// event listeners
window.addEventListener('pointermove', (e) => {
const updatedPlayerXY = {
x: e.clientX / window.innerWidth,
y: e.clientY / window.innerHeight
}
socket.emit('move', updatedPlayerXY);
});
window.addEventListener('resize', onResize);
// socket event listeners
socket.on('update-data', (data) => {
playersData = data;
});
socket.on('update-scoreboard', (data) => {
const scoreboard = document.getElementById('scoreboard');
scoreboard.innerHTML = '';
// Add self to scoreboard
const div = document.createElement('div');
div.innerHTML = `YOU (${socket.id.slice(0, ID_LEN)}): ${playersData[socket.id].score}`;
scoreboard.appendChild(div);
// Add others to scoreboard
for (const [playerId, playerData] of Object.entries(playersData).filter(([playerId, _]) => playerId !== socket.id)) {
const div = document.createElement('div');
div.innerHTML = `${playerId.slice(0, ID_LEN)}: ${playerData.score}`;
scoreboard.appendChild(div);
}
});
socket.on('spawn-food', (newFoodPos) => {
foodPos = newFoodPos;
foodEaten = false;
})
// animation loop
function update() {
ctx.fillStyle = 'black';
ctx.fillRect(0, 0, canvas.width, canvas.height);
if (foodPos) {
drawCircle(ctx, foodPos.x, foodPos.y, FOOD_RADIUS, '0,70%,100%', 1);
}
if (playersData) {
for (const [playerId, playerData] of Object.entries(playersData)) {
if (playerData.x !== null) {
drawPlayer(ctx, playerData);
}
// detect food intersection
if (playerId === socket.id) {
const playerScreenPos = { x: playerData.x * canvas.width, y: playerData.y * canvas.height };
const foodScreenPos = { x: foodPos.x * canvas.width, y: foodPos.y * canvas.height };
if (
foodScreenPos.x + FOOD_RADIUS > playerScreenPos.x - playerData.radius &&
foodScreenPos.x - FOOD_RADIUS < playerScreenPos.x + playerData.radius &&
foodScreenPos.y + FOOD_RADIUS > playerScreenPos.y - playerData.radius &&
foodScreenPos.y - FOOD_RADIUS < playerScreenPos.y + playerData.radius
) {
if (!foodEaten) {
socket.emit('food-eaten');
foodEaten = true;
}
}
}
}
}
requestAnimationFrame(update)
}
// on load
onResize();
requestAnimationFrame(update)
</script>
</body>
</html>