forked from jeromeetienne/threex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTHREEx.DeviceOrientationState.js
51 lines (41 loc) · 1.26 KB
/
THREEx.DeviceOrientationState.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
/** @namespace */
var THREEx = THREEx || {};
THREEx.DeviceOrientationState = function()
{
// to store the current state
this._state = { x: 0, y: 0, z: 0 };
var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
this._$callback = __bind(function(event){ this._onDeviceOrientation(event); }, this);
// bind events
// - spec http://dev.w3.org/geo/api/spec-source-orientation.html
window.addEventListener('deviceorientation', this._$callback);
}
/**
* To stop listening of the keyboard events
*/
THREEx.DeviceOrientationState.prototype.destroy = function()
{
// unbind events
window.removeEventListener('deviceorientation', this._$callback);
}
/**
* to process the keyboard dom event
*/
THREEx.DeviceOrientationState.prototype._onDeviceOrientation = function(event)
{
this._state.x = (!event.alpha ? 0 : event.alpha) * Math.PI / 180;
this._state.y = (!event.beta ? 0 : event.beta ) * Math.PI / 180;
this._state.z = (!event.gamma ? 0 : event.gamma) * Math.PI / 180;
}
THREEx.DeviceOrientationState.prototype.angleX = function()
{
return this._state.x;
}
THREEx.DeviceOrientationState.prototype.angleY = function()
{
return this._state.y;
}
THREEx.DeviceOrientationState.prototype.angleZ = function()
{
return this._state.z;
}