-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.html
53 lines (46 loc) · 1.42 KB
/
example.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
<html>
<head>
<title>Multiplayer.js example</title>
<script src="http://gibbs.tk:4000/socket.io/socket.io.js"></script>
<script src="multiplayer.min.js"></script>
</head>
<style>
span, div {
width: 10px;
height: 10px;
background-color: red;
position: absolute;
}
</style>
<body>
<span id="me"></span>
<script type="text/javascript">
onload = function() {
// Sets up and connects to the multiplayer server
var mul = new Multiplayer({ x:0, y:0 }, {
// Adds squares when other players connect
onconnect: function( client, id ) {
document.body.appendChild( document.createElement( "div" ) )
},
// Removes squares when other players disconnect
ondisconnect: function( client, id ) {
document.body.removeChild( document.getElementsByTagName("div")[id] )
},
// Updates player locations when we receive their data
onreceive: function( clients ) {
for( var i in clients ) {
document.getElementsByTagName("div")[i].style.left = clients[i].x
document.getElementsByTagName("div")[i].style.top = clients[i].y
}
}
});
// Updates mul.me and square position on the screen
addEventListener( "mousemove", function( event ) {
mul.update({ x:event.pageX, y:event.pageY })
document.getElementById("me").style.left = mul.me.x
document.getElementById("me").style.top = mul.me.y
});
}
</script>
</body>
</html>