A cordova plugin using sensor fusion to offer more precise orientation data. It is based on a work of Alexander Pacha (https://bitbucket.org/apacha/sensor-fusion-demo).
DEMO: https://github.com/adirtyshame/threecordova
Access is via a global navigator.fusion
object.
Although the object is attached to the global scoped navigator
, it is not available until after the deviceready
event.
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
console.log(navigator.fusion);
}
Install via cordova CLI:
cordova plugin add https://github.com/adirtyshame/cordova-plugin-device-sensor-fusion.git
- Android
- navigator.fusion.setMode
- navigator.fusion.getCurrentSensorFusion
- navigator.fusion.watchSensorFusion
- navigator.fusion.clearWatch
Set the operation-mode for the plugin.
navigator.fusion.setMode(success, err, mode);
Available modes are (from '0' to '5'):
- 0: Improved Orientation Sensor 1 (Sensor fusion of Android Rotation Vector and Calibrated Gyroscope - less stable but more accurate)
- 1: Improved Orientation Sensor 2 (Sensor fusion of Android Rotation Vector and Calibrated Gyroscope - more stable but less accurate)
- 2: Android Rotation Vector (Kalman filter fusion of Accelerometer + Gyroscope + Compass)
- 3: Calibrated Gyroscope (Separate result of Kalman filter fusion of Accelerometer + Gyroscope + Compass)
- 4: Gravity + Compass
- 5: Accelerometer + Compass
function success(result) {
alert('new Mode: ' + result);
};
function err(error) {
alert('Error: ' + error);
};
// Set operation mode to 'Android Rotation Vector'
navigator.fusion.setMode(onSuccess, onError, 2);
Get the current sensor fusion. The result is returned via a FusionResult
object using the fusionSuccess
callback function.
navigator.fusion.getCurrentSensorFusion(fusionSuccess, fusionError);
function onSuccess(result) {
alert('x: ' + result.x);
};
function onError(error) {
alert('FusionError: ' + error.code);
};
navigator.fusion.getCurrentSensorFusion(onSuccess, onError);
Gets the device's current sensor fusion data at a regular interval. Each time the data
is retrieved, the fusionSuccess
callback function is executed.
The returned watch ID references the sensor fusion watch interval. The watch
ID can be used with navigator.fusion.clearWatch
to stop watching the navigator.fusion.
var watchID = navigator.fusion.watchSensorFusion(fusionSuccess, fusionError, [fusionOptions]);
fusionOptions
may contain the following keys:
- frequency: How often to retrieve the sensor fusion data in milliseconds. (Number) (Default: 100)
function onSuccess(result) {
var element = document.getElementById('result');
element.innerHTML = 'Result.x: ' + result.x;
};
function onError(fusionError) {
alert('Fusion error: ' + fusionError.code);
};
var options = {
frequency: 3000
}; // Update every 3 seconds
var watchID = navigator.fusion.watchSensorFusion(onSuccess, onError, options);
Stop watching the sensor fusion referenced by the watch ID parameter.
navigator.fusion.clearWatch(watchID);
- watchID: The ID returned by
navigator.fusion.watchSensorFusion
.
var watchID = navigator.fusion.watchSensorFusion(onSuccess, onError, options);
// ... later on ...
navigator.fusion.clearWatch(watchID);
A FusionResult
object is returned to the fusionSuccess
callback function.
-
FusionResult. (ATTENTION: will be deprecated soon)
- x: The x-component of the resulting quaternion. (Number)
- y: The y-component of the resulting quaternion. (Number)
- z: The z-component of the resulting quaternion. (Number)
- w: The w-component of the resulting quaternion. (Number)
-
FusionResult.quaternion.
- x: The x-component of the resulting quaternion. (Number)
- y: The y-component of the resulting quaternion. (Number)
- z: The z-component of the resulting quaternion. (Number)
- w: The w-component of the resulting quaternion. (Number)
-
FusionResult.eulerAngles.
- yaw: The Euler-Angles yaw component. (Number)
- pitch: The Euler-Angles pitch component. (Number)
- roll: The Euler-Angles roll component. (Number)
-
FusionResult.timestamp: The time at which the data was determined. (milliseconds)