-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse.html
35 lines (33 loc) · 1.05 KB
/
mouse.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
<!DOCTYPE HTML>
<html><head><title>Mouse control</title>
<style>
#mousePad {
border: 2px solid black;
display: inline-block;
}
body {
text-align: center;
}
</style>
</head>
<body>
<canvas id="mousePad" width="500" height="500">Canvas not supported</canvas>
<script src ="/socket.io/socket.io.js"></script>
<script>
var socket = io(); //initialise socket
var mp = document.getElementById("mousePad");
function getMousePos(canvas,ev){
var rect = canvas.getBoundingClientRect(); //create a rectangle which boundings are the same of the
json_list = {}; //canvas, so we can have the relative position of the
json_list.x = Math.round(ev.clientX-rect.left); //mouse inside of it
json_list.y = Math.round(ev.clientY-rect.top);
json_list.device = "mouse";
json_list = JSON.stringify(json_list);
return json_list;
}
mp.addEventListener('mousemove',function(ev){ //listen mouse movements
var pos = getMousePos(mp,ev);
socket.emit('mousePositionChanged',pos);
},false);
</script>
</body></html>