-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsphoords.js
401 lines (326 loc) · 9.17 KB
/
sphoords.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
* Sphoords v0.1.1
* http://jeremyheleine.me
*
* Copyright (c) 2015,2016 Jérémy Heleine
*
* 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.
*/
/**
* Sphoords class allowing to retrieve the current orientation of a device supporting the Orientation API.
* @class
**/
var Sphoords = function() {
/**
* Detects the used browser engine.
* @private
* @return {void}
**/
var detectBrowserEngine = function() {
// User-Agent
var ua = navigator.userAgent;
// Gecko
if (/Gecko\/[0-9.]+/.test(ua))
return 'Gecko';
// Blink
if (/Chrome\/[0-9.]+/.test(ua))
return 'Blink';
// WebKit
if (/AppleWebKit\/[0-9.]+/.test(ua))
return 'WebKit';
// Trident
if (/Trident\/[0-9.]+/.test(ua))
return 'Trident';
// Presto
if (/Opera\/[0-9.]+/.test(ua))
return 'Presto';
// No engine detected, Gecko by default
return 'Gecko';
};
/**
* Returns the principal angle in degrees.
* @private
* @param {number} angle - The initial angle
* @return {number} The principal angle
**/
var getPrincipalAngle = function(angle) {
return angle - Math.floor(angle / 360.0) * 360.0;
};
/**
* Attaches the DeviceOrientation event to the window and starts the record, only if Device Orientation is supported.
* @public
* @return {boolean} `true` if event is attached, `false` otherwise
**/
this.start = function() {
if (Sphoords.isDeviceOrientationSupported) {
window.addEventListener('deviceorientation', onDeviceOrientation, false);
recording = true;
return true;
}
else {
console.log('Device Orientation API not supported');
return false;
}
};
/**
* Stops the record by removing the event handler.
* @public
* @return {void}
**/
this.stop = function() {
// Is there something to remove?
if (recording) {
window.removeEventListener('deviceorientation', onDeviceOrientation, false);
recording = false;
}
};
/**
* Toggles the recording state.
* @public
* @return {void}
**/
this.toggle = function() {
if (recording)
this.stop();
else
this.start();
};
/**
* Determines whether Device Orientation Event is attached.
* @public
* @return {boolean} `true` if event is attached, `false` otherwise
**/
this.isEventAttached = function() {
return recording;
};
/**
* Records the current orientation
* @private
* @param {Event} evt - The event
* @return {void}
**/
var onDeviceOrientation = function(evt) {
// Current screen orientation
orientation = Sphoords.getScreenOrientation();
// Coordinates depend on the orientation
var theta = 0, phi = 0;
switch (orientation) {
// Portrait mode
case 'portrait-primary':
theta = evt.alpha + evt.gamma;
phi = evt.beta - 90;
break;
// Landscape mode
case 'landscape-primary':
// If "-90" is not present for theta, origin won't be the same than for portrait mode
theta = evt.alpha + evt.beta - 90;
phi = -evt.gamma - 90;
// The user looks to the top while phi "looks" to the bottom
if (Math.abs(evt.beta) > 90) {
// Here browser engines have different behaviors
// Hope we have a really respected standard soon
switch (engine) {
case 'Blink':
phi += 180;
break;
case 'Gecko':
default:
phi = -phi;
break;
}
}
//fix to work on iOS (tested on Safari and Chrome)
if( engine === 'WebKit' && !!window.orientation ){
if( phi < 0 ){
phi = (phi + 180) * -1;
}
if( theta >= 180 ){
theta = theta - 180;
} else {
theta = theta + 180;
}
}
break;
// Landscape mode (inversed)
case 'landscape-secondary':
// Still the same reason for "+90"
theta = evt.alpha - evt.beta + 90;
phi = evt.gamma - 90;
// The user looks to the top while phi "looks" to the bottom
if (Math.abs(evt.beta) > 90) {
// Here again, some different behaviors…
switch (engine) {
case 'Blink':
phi += 180;
break;
case 'Gecko':
default:
phi = -phi;
break;
}
}
//fix to work on iOS (tested on Safari and Chrome)
if( engine === 'WebKit' && !!window.orientation ){
if( phi < 0 ){
phi = (phi + 180) * -1;
}
if( theta >= 180 ){
theta = theta - 180;
} else {
theta = theta + 180;
}
}
break;
// Portrait mode (inversed)
case 'portrait-secondary':
theta = evt.alpha - evt.gamma;
phi = 180 - (evt.beta - 90);
phi = 270 - evt.beta;
break;
}
// First, we want phi to be between -π and π
phi = getPrincipalAngle(phi);
if (phi >= 180)
phi -= 360;
// We store the right values
long_deg = getPrincipalAngle(theta);
lat_deg = Math.max(-90, Math.min(90, phi));
long = long_deg * DEG_TO_RAD;
lat = lat_deg * DEG_TO_RAD;
// We execute the wanted functions
executeListeners();
};
/**
* Returns the current coordinates.
* @public
* @return {object} Longitude/latitude couple
**/
this.getCoordinates = function() {
return {
longitude: long,
latitude: lat
};
};
/**
* Returns the current coordinates in degrees.
* @return {object} Longitude/latitude couple
**/
this.getCoordinatesInDegrees = function() {
return {
longitude: long_deg,
latitude: lat_deg
};
};
/**
* Returns the current screen orientation.
* @return {string|null} The screen orientation (portrait-primary, portrait-secondary, landscape-primary, landscape-secondary) or `null` if not supported
**/
this.getScreenOrientation = function() {
return orientation;
};
/**
* Adds a function to execute when device orientation changes.
* @public
* @param {function} f - The handler function
* @return {void}
**/
this.addListener = function(f) {
listeners.push(f);
};
/**
* Executes all the wanted functions for the main event.
* @private
* @return {void}
**/
var executeListeners = function() {
if (!!listeners.length) {
for (var i = 0, l = listeners.length; i < l; ++i) {
listeners[i]({
longitude: long,
latitude: lat
});
}
}
};
// Current state
var recording = false;
// Coordinates in degrees
var long_deg = 0, lat_deg = 0;
// Coordinates in radians
var long = 0, lat = 0;
// What a useful constant!
var DEG_TO_RAD = Math.PI / 180;
// Screen orientation
var orientation = Sphoords.getScreenOrientation();
// Browser engine
var engine = detectBrowserEngine();
// Listeners
var listeners = [];
};
/**
* Retrieves the current screen orientation.
* @static
* @return {string|null} Current screen orientation, `null` if not supported
**/
Sphoords.getScreenOrientation = function() {
var screen_orientation = null;
if (!!screen.orientation)
screen_orientation = screen.orientation;
else if (!!screen.mozOrientation)
screen_orientation = screen.mozOrientation;
else if (!!screen.msOrientation)
screen_orientation = screen.msOrientation;
else if (!!window.orientation || window.orientation === 0)
switch (window.orientation) {
case 0:
screen_orientation = 'portrait-primary';
break;
case 180:
screen_orientation = 'portrait-secondary';
break;
case -90:
screen_orientation = 'landscape-primary';
break;
case 90:
screen_orientation = 'landscape-secondary';
break;
}
// Are the specs respected?
return (screen_orientation !== null && (typeof screen_orientation == 'object')) ? screen_orientation.type : screen_orientation;
};
/**
* A boolean to know if device orientation is supported (`true` if it is, `false` otherwise).
* @static
**/
Sphoords.isDeviceOrientationSupported = false;
// Just testing if window.DeviceOrientationEvent is defined is not enough
// In fact, it can return false positives with some desktop browsers which run in computers that don't have the dedicated hardware
(function() {
// We attach the right event
// If it is fired, the API is really supported so we can indicate true :)
if (!!window.DeviceOrientationEvent && Sphoords.getScreenOrientation() !== null) {
function testDeviceOrientation(evt) {
if (evt !== null && evt.alpha !== null) {
Sphoords.isDeviceOrientationSupported = true;
window.removeEventListener('deviceorientation', testDeviceOrientation);
}
}
window.addEventListener('deviceorientation', testDeviceOrientation);
}
})();