@hughsk/fulltilt
Version:
Standalone device orientation + device motion normalization and conversion library
55 lines (48 loc) • 1.6 kB
JavaScript
/**
*
* FULL TILT
* http://github.com/richtr/Full-Tilt
*
* A standalone DeviceOrientation + DeviceMotion JavaScript library that
* normalises orientation sensor input, applies relevant screen orientation
* transforms, returns Euler Angle, Quaternion and Rotation
* Matrix representations back to web developers and provides conversion
* between all supported orientation representation types.
*
* Copyright: 2014 Rich Tibbett
* License: MIT
*
*/
(function ( window ) {
// Only initialize the FULLTILT API if it is not already attached to the DOM
if ( window.FULLTILT !== undefined && window.FULLTILT !== null ) {
return;
}
var M_PI = Math.PI;
var M_PI_2 = M_PI / 2;
var M_2_PI = 2 * M_PI;
// Degree to Radian conversion
var degToRad = M_PI / 180;
var radToDeg = 180 / M_PI;
// Internal device orientation + motion variables
var sensors = {
"orientation": {
active: false,
callbacks: [],
data: undefined
},
"motion": {
active: false,
callbacks: [],
data: undefined
}
};
var screenActive = false;
// Internal screen orientation variables
var hasScreenOrientationAPI = window.screen && window.screen.orientation && window.screen.orientation.angle !== undefined && window.screen.orientation.angle !== null ? true : false;
var screenOrientationAngle = ( hasScreenOrientationAPI ? window.screen.orientation.angle : ( window.orientation || 0 ) ) * degToRad;
var SCREEN_ROTATION_0 = 0,
SCREEN_ROTATION_90 = M_PI_2,
SCREEN_ROTATION_180 = M_PI,
SCREEN_ROTATION_270 = M_2_PI / 3,
SCREEN_ROTATION_MINUS_90 = - M_PI_2;