-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmotion.js
206 lines (189 loc) · 6.27 KB
/
motion.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/**
* Copyright (C) 2011 by Ollie Parsley
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
(function(){
window.isMotionSupported = function() {
return 'DeviceMotionEvent' in window;
}
window.isOrientationSupported = function() {
return 'DeviceOrientationEvent' in window || 'OrientationEvent' in window;
}
//Only start if motion is supported
if (window.isMotionSupported()) {
/**
* Shake detection
*/
var shake = {
events: {},
stopping: false,
starting: false,
shaking: false,
sensitivity: 10,
currentCoords: null,
init: function(){
//Initialise custom events
this.events['start'] = document.createEvent("Event");
this.events['start'].initEvent('shakestart', true, true);
this.events['end'] = document.createEvent("Event");
this.events['end'].initEvent('shakeend', true, true);
//Start listening
this.listen();
},
start: function() {
//Emit shake start
this.shaking = true;
window.dispatchEvent(this.events['start']);
},
stop: function() {
this.shaking = false;
this.lastStop = new Date().getTime();
//Emit shake end
window.dispatchEvent(this.events['end']);
},
listen: function(){
//Ugly as ios 4 was a pain
var self = this;
window.addEventListener('devicemotion', function(e) {
//Change
var change = 0;
//New Coords
var newX = e.accelerationIncludingGravity.x;
var newY = e.accelerationIncludingGravity.y;
var newZ = e.accelerationIncludingGravity.z;
//If not the first
if (self.coords != null) {
change = Math.abs(self.coords.x - newX + self.coords.y - newY + self.coords.z - newZ);
} else {
self.coords = {};
}
//Update
self.coords.x = newX;
self.coords.y = newY;
self.coords.z = newZ;
//If we have a change on 2 axis then we are shaking properly
if (change >= self.sensitivity) {
//If not currently shaking
if (!self.shaking) {
//Check to see if its starting, if so then start it properly
if (self.starting) {
self.start();
self.starting = false;
//Check to see if its starting, if it isn't then set it to starting
} else {
self.starting = true;
}
}
} else {
//If currently shaking
if (self.shaking) {
//Check to see if its starting, if so then start it properly
if (self.stopping) {
self.stop();
self.stopping = false;
//Check to see if its starting, if it isn't then set it to starting
} else {
self.stopping = true;
}
}
}
}, false);
}
}
shake.init();
}
//Only start if motion is supported
if (window.isOrientationSupported()) {
/**
* Shake detection
*/
var orientation = {
events: {},
faceupActive: false,
facedownActive: false,
rotateLeftActive: false,
rotateRightActive: false,
sensitivity: 20,
init: function() {
//Initialise custom events
this.events['faceupstart'] = document.createEvent("Event");
this.events['faceupstart'].initEvent('faceupstart', true, true);
this.events['faceupend'] = document.createEvent("Event");
this.events['faceupend'].initEvent('faceupend', true, true);
this.events['facedownstart'] = document.createEvent("Event");
this.events['facedownstart'].initEvent('facedownstart', true, true);
this.events['facedownend'] = document.createEvent("Event");
this.events['facedownend'].initEvent('facedownend', true, true);
//Start listening
this.listen();
},
faceup: function(active) {
this.faceupActive = active;
if (active) {
//Emit face up start
window.dispatchEvent(this.events['faceupstart']);
} else {
//Emit face up end
window.dispatchEvent(this.events['faceupend']);
}
},
facedown: function(active) {
this.facedownActive = active;
if (active) {
//Emit face down start
window.dispatchEvent(this.events['facedownstart']);
} else {
//Emit face down end
window.dispatchEvent(this.events['facedownend']);
}
},
listen: function(){
//Ugly as ios 4 wasn't binding was a pain
var self = this;
//W3C orientation spec
window.addEventListener("deviceorientation", function(event) {
self.process(parseInt(event.alpha, 10), parseInt(event.beta, 10), parseInt(event.gamma, 10));
}, false);
//Old Moz orientation spec
window.addEventListener("MozOrientation", function(event) {
self.process(null, parseInt(-(event.x * (180 / Math.PI))), parseInt(-(event.y * (180 / Math.PI))));
}, false);
},
process: function(a, b, g) {
//Shorter access to variable
var s = this.sensitivity;
//Check for face up
if (!this.faceupActive && b < (0 + s) && b > (0 - s) && g < (0 + s) && g > (0 - s)) {
this.faceup(true);
} else if (this.faceupActive && (b > (0 + s) || b < (0 - s) || g > (0 + s) || g < (0 - s)) ){
this.faceup(false);
}
//Check for face down
if (!this.facedownActive && b < (0 + s) && b > (0 - s) && g < (180 + s) && g > (180 - s)) {
this.facedown(true);
} else if (this.facedownActive && (b > (0 + s) || b < (0 - s) || g > (180 + s) || g < (180 - s)) ){
this.facedown(false);
}
}
}
orientation.init();
}
})();