-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforcetouch.js
66 lines (53 loc) · 1.78 KB
/
forcetouch.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
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(function() {
return (root.ForceTouch = factory());
});
} else if (typeof module === 'object' && module.exports) {
module.exports = (root.ForceTouch = factory());
} else {
root.ForceTouch = factory();
}
}(this, function() {
var ForceTouch = function() {}
ForceTouch.prototype.callback = null;
ForceTouch.prototype.startListen = function(elem){
elem.addEventListener('touchstart', this.onTouchStart.bind(this), false);
elem.addEventListener('touchmove', this.onTouchMove.bind(this), false);
elem.addEventListener('touchend', this.onTouchEnd.bind(this), false);
}
ForceTouch.prototype.stopListen = function(elem){
elem.removeEventListener('touchstart', this.onTouchStart.bind(this) , false);
elem.removeEventListener('touchmove', this.onTouchMove.bind(this) , false);
elem.removeEventListener('touchend', this.onTouchEnd.bind(this) , false);
}
ForceTouch.prototype.touch = null;
ForceTouch.prototype.onTouchStart = function(e) {
e.preventDefault();
this.checkForce(e);
}
ForceTouch.prototype.onTouchMove = function(e) {
e.preventDefault();
this.checkForce(e);
}
ForceTouch.prototype.onTouchEnd = function(e) {
e.preventDefault();
this.touch = null;
}
ForceTouch.prototype.checkForce = function(e) {
this.touch = e.touches[0];
setTimeout(this.refreshForceValue.bind(this), 10);
}
ForceTouch.prototype.refreshForceValue = function() {
var touchEvent = this.touch;
var forceValue = 0;
if(touchEvent) {
forceValue = touchEvent.force || 0;
setTimeout(this.refreshForceValue.bind(this), 10);
}else{
forceValue = 0;
}
this.callback(forceValue);
}
return ForceTouch;
}));