-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.js
90 lines (68 loc) · 1.62 KB
/
input.js
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
var input = {};
(function(){
// ---- override these functions elsewhere to handle events
input.onclick2d = function(x, y) {};
input.startdrag2d = function(x, y) {};
input.ondrag2d = function(dx, dy) {};
input.stopdrag2d = function(x, y) {};
// ----
var registerX, registerY;
var clicking, isDown = false;
function down(x, y) {
registerX = x;
registerY = y;
clicking = true;
isDown = true;
}
function move(x, y) {
if (clicking) {
input.startdrag2d(x, y);
clicking = false;
}
input.ondrag2d(x - registerX, y - registerY);
registerX = x;
registerY = y;
}
function up(x, y) {
isDown = false;
if (clicking) {
input.onclick2d(registerX, registerY);
} else {
input.stopdrag2d(x, y);
}
}
window.addEventListener('mousedown', function(e) {
down(e.pageX, e.pageY);
});
window.addEventListener('mousemove', function(e) {
if (!isDown) return;
e.preventDefault();
e.stopPropagation();
move(e.pageX, e.pageY);
});
window.addEventListener('mouseup', function(e) {
up(e.pageX, e.pageY);
});
window.addEventListener('touchstart', function(e) {
if (e.touches.length > 1)
return;
down(e.touches[0].pageX, e.touches[0].pageY);
});
window.addEventListener('touchmove', function(e) {
e.preventDefault();
e.stopPropagation();
if (e.touches.length > 1)
return;
move(e.touches[0].pageX, e.touches[0].pageY);
});
window.addEventListener('touchend', function(e) {
if (e.touches.length > 1)
return;
up(registerX, registerY);
});
window.addEventListener('touchcancel', function(e) {
if (e.touches.length > 1)
return;
up(registerX, registerY);
});
})();