-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouse_position.html
46 lines (40 loc) · 1.19 KB
/
mouse_position.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Get mouse position</title>
<style>
#canvas{width: 500px; height: 400px; background: #666;}
</style>
</head>
<body>
<div id="canvas"></div>
<script>
var utils = {};
utils.captureMouse = function (element) {
var mouse = {x: 0, y: 0};
element.addEventListener('mousemove', function(event) {
var x, y;
if ( event.pageX || event.pageY ) {
x = event.pageX;
y = event.pageY;
} else {
x = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
y = event.clientY + document.body.srollTop + document.documentElement.scrollTop;
}
x -= element.offsetleft;
y -= element.offsetTop;
mouse.x = x;
mouse.y = y;
}, false);
return mouse;
}
function init(){
var $canvas = document.getElementById('canvas'),
mouse = utils.captureMouse($canvas);
console.log(mouse);
}
window.load = init();
</script>
</body>
</html>