forked from jhugheswebdev/sound-equalizer-threejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMouseHandler.js
95 lines (78 loc) · 2.39 KB
/
MouseHandler.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
91
92
93
94
95
var MouseHandler = function(){
this.w = document.documentElement.clientWidth;
this.h = document.documentElement.clientHeight;
this.counter;
};
MouseHandler.prototype.handler = function(_evt){
clearTimeout(this.counter);
this.counter = setTimeout(this.reset.bind(this), 100.);
if (_evt.targetTouches) {
var touch = _evt.targetTouches[0];
this.x = touch.pageX;
this.y = touch.pageY;
} else {
this.x = _evt.clientX;
this.y = _evt.clientY;
}
this.norm_x = this.x / this.w;
this.norm_y = 1. - this.y / this.h;
this.delta_x = this.norm_x - this.norm_px;
this.delta_y = this.norm_y - this.norm_py;
this.dir_x = this.norm_px - this.norm_x;
this.dir_y = this.norm_py - this.norm_y;
this.norm_px = this.norm_x;
this.norm_py = this.norm_y;
};
MouseHandler.prototype.get_x = function(){
return this.x;
};
MouseHandler.prototype.get_y = function(){
return this.y;
};
MouseHandler.prototype.get_norm_x = function(){
return this.norm_x;
};
MouseHandler.prototype.get_norm_y = function(){
return this.norm_y;
};
MouseHandler.prototype.get_norm_px = function(){
return this.norm_px;
};
MouseHandler.prototype.get_norm_py = function(){
return this.norm_py;
};
MouseHandler.prototype.get_delta_x = function(){
return this.delta_x;
};
MouseHandler.prototype.get_delta_y = function(){
return this.delta_y;
};
MouseHandler.prototype.get_dir_x = function(){
return this.dir_x;
};
MouseHandler.prototype.get_dir_y = function(){
return this.dir_y;
};
MouseHandler.prototype.reset = function(){
this.norm_px = this.norm_x;
this.norm_py = this.norm_y;
this.delta_x = 0.;
this.delta_y = 0.;
this.dir_x = 0.;
this.dir_y = 0.;
};
MouseHandler.prototype.register_dom_events = function(_target){
_target.addEventListener("mousemove", this.handler.bind(this), false);
_target.addEventListener("touchmove", this.handler.bind(this), false);
console.log("MouseHandler : handler() is registered mousemove event listener");
_target.addEventListener('touchstart', function (event) {
event.preventDefault();
}, {passive: false});
window.addEventListener("resize", function(){
this.w = document.documentElement.clientWidth;
this.h = document.documentElement.clientHeight;
}.bind(this));
window.addEventListener("mouseout", function(){
this.reset();
}.bind(this));
};