/
parallaxer.js
134 lines (107 loc) · 4.26 KB
/
parallaxer.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
(function(window) {
function Parallaxer(stage, options) {
var opts = options || {};
this._stage = stage;
this._range = {
x: opts.xRange || Parallaxer.X_RANGE,
y: opts.yRange || Parallaxer.Y_RANGE
};
this._inversion = opts.invert ? -1 : 1;
this._transitioning = false;
this._lastCursor = null;
if (window.DeviceMotionEvent) {
this._ax = this._ay = 0.0;
setInterval(this._onDeviceMotionRender.bind(this));
window.addEventListener('devicemotion', this._onDeviceMotion.bind(this), true);
}
else {
window.addEventListener('mousemove', this._onMouseMove.bind(this), true);
}
}
Parallaxer.X_RANGE = 50; // Max distance in x direction
Parallaxer.Y_RANGE = 100; // Max distance in y direction
Parallaxer.SENSITIVITY = 0.01;
Parallaxer.TRANSITION = 'all .01s linear';
Parallaxer.TRANSITION_DIST_SQ = 200 * 200; // Minimum distance before applying a transition effect
Parallaxer.prototype = {
_onDeviceMotion: function(e) {
var accel = e.accelerationIncludingGravity, ax, ay;
if (window.innerHeight > window.innerWidth) {
ax = accel.x,
ay = accel.y;
}
else {
ax = accel.y;
ay = accel.x;
}
// Normalize acceleration
this._ax = ax / 9.81;
this._ay = ay / 9.81;
},
_onDeviceMotionRender: function() {
var x = this._inversion * this._ax * this._range.x,
y = this._inversion * this._ay * this._range.y;
this._stage.style.WebkitTransform = 'translate3d(' + x + 'px,' + y + 'px, 0)';
},
_transition: function() {
this._transitioning = true;
this._stage.style.WebkitTransition = Parallaxer.TRANSITION;
window.addEventListener('webkitTransitionEnd', this._onEndTransition.bind(this), true);
},
_onEndTransition: function() {
this._stage.style.WebkitTransition = '';
this._transitioning = false;
},
_onMouseMove: function(e) {
if (!this._lastCursor) { // Transition to initial mouse coordinates
this._transition();
}
else if (!this._transitioning) {
var dx = e.clientX - this._lastCursor.x,
dy = e.clientY - this._lastCursor.y;
// Transition if distance between mouse events is large
if (dx * dx + dy * dy > Parallaxer.TRANSITION_DIST_SQ) {
this._transition();
}
}
var x = this._inversion * (2 * e.clientX / window.innerWidth - 1.0) * this._range.x,
y = this._inversion * (2 * e.clientY / window.innerHeight - 1.0) * this._range.y;
this._stage.style.WebkitTransform = 'translate3d(' + x + 'px,' + y + 'px, 0)';
this._lastCursor = {x: e.clientX, y: e.clientY};
}
};
// Taken from ES5-shim https://github.com/kriskowal/es5-shim/blob/master/es5-shim.js
// ES-5 15.3.4.5
// http://es5.github.com/#x15.3.4.5
if (!Function.prototype.bind) {
Function.prototype.bind = function bind(that) {
var target = this;
if (typeof target != "function") {
throw new TypeError();
}
var args = Array.prototype.slice.call(arguments, 1),
bound = function () {
if (this instanceof bound) {
var F = function(){};
F.prototype = target.prototype;
var self = new F();
var result = target.apply(
self,
args.concat(Array.prototype.slice.call(arguments))
);
if (Object(result) === result) {
return result;
}
return self;
} else {
return target.apply(
that,
args.concat(Array.prototype.slice.call(arguments))
);
}
};
return bound;
};
}
window.Parallaxer = Parallaxer;
})(window);