-
Notifications
You must be signed in to change notification settings - Fork 65
API Documentation
Constructor function. Instantiate a new GyroNorm instance.
var gn = new GyroNorm();
Returns a promise object that resolves when GyroNorm is ready. (Attempt to add events to Window object is complete). If extra arguments are provided overwrites the default options (see above).
Make sure to call all other function after this promise resolves.
gn.init(args);
args - object (optional) - Passes the values to overwrite the default option values. See README for usage.
Starts returning values via the callback function. The callback function is called every many milliseconds defined by the frequency option. See README on how to set the frequency. The default frequency is 50ms.
gn.start(callback);
callback - function(data) - Function that returns values via the data object. The available values via data are listed above.
Tells the availability of device orientation or device motion values on the device+browser combination.
gn.isAvailable(valueType);
// or
gn.isAvailable();
valueType - string - optional - If passed, the method returns true
or false
, depending on the availablity of the specified value. Possible values are GyroNorm.DEVICE_ORIENTATION
,GyroNorm.ACCELERATION
,GyroNorm.ACCELERATION_INCLUDING_GRAVITY
or GyroNorm.ROTATION_RATE
When called without a parameter returns availibility for all values.
var gn = new GyroNorm();
gn.init().then(ongnReady);
var ongnReady = function(){
var doAvailable = gn.isAvailable(GyroNorm.DEVICE_ORIENTATION);
// Parameter can also be GyroNorm.DEVICE_ORIENTATION, GyroNorm.ACCELERATION, GyroNorm.ACCELERATION_INCLUDING_GRAVITY or GyroNorm.ROTATION_RATE
// This example returns true if deviceorientation is available. Returns false if not.
var gnAvailable = gn.isAvailable();
/* Returns the following object
{
deviceOrientationAvailable : <true or false>,
accelerationAvailable : <true or false>,
accelerationIncludingGravityAvailable : <true or false>,
rotationRateAvailable : <true or false>,
compassNeedsCalibrationAvailable : <true or false>
}
*/
}
If a certain set of sensor values (e.g. DeviceOrientation) is not supported by the device, the first gn.init(...)
promise will catch this. You will receive an error message, for instance DeviceOrientaion not supported
and you can handle it at that point.
But in some old devices, although the values seem to be supported they return null
, or does not return at all.
So gn.init()
resolves properly but the values are not as expected.
isAvailable
function aims to checks these cases. The availability conditions are not documented in the standards. So the check I have implemented is from the learnings and self experience, and can be incomplete or buggy.
I suggest you use this function only for debugging certain old devices.
Changes the value of the gravityNormalized option. Even when GyroNorm is running.
gn.normalizeGravity(flag);
flag - boolean - true sets the option gravityNormalized on, false set it to off.
var gn = new GyroNorm();
gn.init().then(ongnReady);
var ongnReady = function(){
gn.start(function(){
// Process return values here
});
}
// At this point callback function returns normalized gravity-related values.
gn.normalizeGravity(false);
// At this point callback function returns native gravity-related values as provided by the device.
Must be called after the start()
method. This can only be used if GyroNorm is tracking game-based and not-screen-adjusted values.
When called, the callback function starts returning the do.alpha value relative to the current head direction of the device.
It will return true
if head direction is set successfully; false
if not.
gn.setHeadDirection();
var gn = new GyroNorm();
gn.init().then(ongnReady);
var ongnReady = function(){
gn.start(function(){
// Process return values here
});
}
// At this point callback function returns do.alpha values relative to the initial position of the device
gn.setHeadDirection();
// At this point callback function starts to return do.alpha values relative to the position of the device when the above line is called
Stops executing the callback function, that was started by the start()
method. It does not remove the event listeners. If you want to remove the event listeners you should call end()
method.
gn.stop();
Stops executing the callback function, that was started by the start()
method. Also removes the event listeners.
gn.end();
Returns boolean value showing if the gn
object is running. It starts running when gn.start()
is called and returns values to the callback function. It stops when gn.stop()
or gn.end()
is called.
gn.isRunning();
// Returns true or false