threex
Version:
Game Extensions for three.js http://www.threejsgames.com/extensions/
52 lines (41 loc) • 1.29 kB
JavaScript
/** @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;
}