-
Notifications
You must be signed in to change notification settings - Fork 1
/
Motionizer.js
169 lines (141 loc) · 3.57 KB
/
Motionizer.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* @class Motionizer
* @param {String|Element} elem The Id of the element to manipulate, or a reference to the element.
* @param {Boolean} [rotateZ] Whether to rotate the element around the Z-axis. Default is true.
*/
var Motionizer = function(elem, rotateZ) {
/**
* @function
* @author Paul Irish
* @see http://paulirish.com/2011/requestanimationframe-for-smart-animating/
*/
window.requestAnimFrame = (function() {
return window.requestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.oRequestAnimationFrame ||
window.msRequestAnimationFrame ||
function(callback, element){
window.setTimeout(callback, 1000 / 60);
};
})();
/**
* Class variables
* @private
* @var _hasDeviceOrientation {Boolean} - Whether we can access device motion data
* @var _element {Element} - The DOM element to be animated
* @var _windowOrientation {Number} - Device orientation [0, 90, 180, -90]
*/
var _hasDeviceOrientation = !!window.DeviceOrientationEvent;
var _element;
var _rotateZ = (typeof rotateZ == "undefined") ? true : rotateZ;
var _windowOrientation = 0;
/**
* @constructs
* @private
*/
var _init = function() {
if (!_hasDeviceOrientation) {
return;
}
if (typeof elem == "object") {
_element = elem;
} else {
_element = document.getElementById(elem);
}
window.addEventListener("deviceorientation", function(ev) {
_orientationHelper.ev = ev;
}, false);
window.addEventListener("orientationchange", function(ev) {
_windowOrientation = window.orientation;
}, false);
_windowOrientation = window.orientation;
_animate();
}
/**
* @class
* @private
*/
var _orientationHelper = {
/**
* @var ev {Event} - Last fired DeviceOrientation event
*/
ev: {},
/**
* @returns {Number} Value of X
*/
getX: function() {
return _orientationHelper.ev.gamma || 0;
},
/**
* @returns {Number} Value of Y
*/
getY: function() {
return _orientationHelper.ev.beta || 0;
},
/**
* @returns {Number} Value of Z
*/
getOrientZ: function() {
return _orientationHelper.ev.alpha || 0;
},
/**
* @returns {Number} Value of X in degrees, corrected for device orientation.
*/
getXDeg: function() {
switch (_windowOrientation) {
case 90:
return -_orientationHelper.getX();
break;
case 0:
return _orientationHelper.getY();
break;
case -90:
return _orientationHelper.getX();
break;
case 180:
return _orientationHelper.getY();
break;
}
},
/**
* @returns {Number} Value of Y in degrees, corrected for device orientation.
*/
getYDeg: function() {
switch (_windowOrientation) {
case 90:
return -_orientationHelper.getY();
break;
case 0:
return -_orientationHelper.getX();
break;
case -90:
return _orientationHelper.getY();
break;
case 180:
return _orientationHelper.getX();
break;
}
},
/**
* @returns {Number} Value of Z in degrees, corrected for device orientation.
*/
getZDeg: function() {
return _orientationHelper.getOrientZ();
}
};
/**
* @private
*/
var _animate = function() {
requestAnimFrame(_animate);
var style = "rotateX(" + _orientationHelper.getXDeg() + "deg) rotateY(" + _orientationHelper.getYDeg() + "deg)";
if (_rotateZ) {
style += "rotateZ(" + _orientationHelper.getZDeg() + "deg)";
}
_element.style.webkitTransform = style;
_element.style.mozTransform = style;
_element.style.transform = style;
};
_init();
};