forked from timbartsch/no-bounce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnoBounce.js
151 lines (130 loc) · 3.16 KB
/
noBounce.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var noBounce = function() {
var module = {};
var settings = {
animate: true
};
var track = [];
var velocity = {
x: 0,
y: 0
};
var vector = {
subtraction: function(v1, v2) {
return {
x: v1.x - v2.x,
y: v1.y - v2.y
};
},
length: function(v) {
return Math.sqrt((v.x * v.x) + (v.y * v.y));
},
unit: function(v) {
var length = vector.length(v);
v.x /= length;
v.y /= length;
},
skalarMult: function(v, s) {
v.x *= s;
v.y *= s;
}
};
var requestAnimFrame = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
function handleTouchStart(evt) {
var point,
touch;
touch = evt.changedTouches[0];
point = {
x: touch.clientX,
y: touch.clientY,
timeStamp: evt.timeStamp
};
track = [point];
}
function handleTouchMove(evt) {
var point,
touch;
evt.preventDefault();
touch = evt.changedTouches[0];
point = {
x: touch.clientX,
y: touch.clientY,
timeStamp: evt.timeStamp
};
track.push(point);
doScroll();
}
function handleTouchEnd(evt) {
if (track.length > 2 && settings.animate) {
velocity = calcVelocity();
requestAnimFrame(animate);
}
}
function calcVelocity() {
var p1,
p2,
v,
timeDiff,
length;
p1 = track[0];
p2 = track[track.length - 1];
timeDiff = p2.timeStamp - p1.timeStamp;
v = vector.subtraction(p2, p1);
length = vector.length(v);
vector.unit(v);
vector.skalarMult(v, length / timeDiff * 20);
return v;
}
function doScroll() {
var p1,
p2,
x,
y;
if (track.length > 1) {
p1 = track[track.length - 1];
p2 = track[track.length - 2];
x = p2.x - p1.x;
y = p2.y - p1.y;
requestAnimFrame(function() {
window.scrollBy(x, y);
});
}
}
function animate() {
scrollBy(-velocity.x, -velocity.y);
vector.skalarMult(velocity, 0.95);
if (vector.length(velocity) > 0.2) {
requestAnimFrame(animate);
}
}
//Returns true if it is a DOM element
function isElement(o) {
return (
typeof HTMLElement === "object" ? o instanceof HTMLElement : //DOM2
o && typeof o === "object" && o !== null && o.nodeType === 1 && typeof o.nodeName === "string"
);
}
module.init = function(options) {
if (typeof options.animate === "boolean") {
settings.animate = options.animate;
}
if (isElement(options.element)) {
settings.element = options.element;
}
var element = settings.element || document;
element.addEventListener("touchstart", handleTouchStart);
element.addEventListener("touchmove", handleTouchMove);
element.addEventListener("touchend", handleTouchEnd);
element.addEventListener("touchcancel", handleTouchEnd);
element.addEventListener("touchleave", handleTouchEnd);
};
return module;
}();