Magnetometer

This API provides an estimate for the direction of north.

This north estimate is dip-corrected, meaning that it is perpendicular to the direction of gravity. The estimate is reported as a normalized 3-vector in the device frame. See the Coordinate Frames page for more details on coordinate frames.

When motion events are not available on the device, Magnetometer events are polyfilled with pan touch events.

Example

var magnetometer = new MotionStack.Magnetometer();

magnetometer.start(function (e) {
  console.log(e.x, e.y, e.z);
});

Constructor

new MotionStack.Magnetometer([options])

Create a new Magnetometer instance object.

Argument Type Description
options Object Options to pass in for the new Magnetometer instance (optional)

  • fireOnAnimationFrame {Boolean}
  • Only emit the latest event within requestAnimationFrame intervals.


    Default: false

var magnetometer = new MotionStack.Magnetometer();

Static Methods

isSupported()

Returns a Boolean value describing whether the device can report magnetometer data.

MotionStack.Magnetometer.isSupported(); // true or false

Instance Methods

start(callback, [context])

Start receiving magnetometer updates.

Argument Type Description
callback Function Callback function that receives an object as an argument

  • function(e)
  • e.x, e.y, e.z {Number}
    Axial components of the magnetometer in the device-frame
  • e.timeStamp {Number}
    The time in milliseconds when the data is collected
  • e.source {String}
    The source used to calculate the reported data.

    • "rawOrientation" browser estimate.
    • "sensorFusion" sensor fusion via a Kalman filter.
    • "filteredOrientation" dynamic low-pass filter on the browser estimate.
    • "gravityPolyfill" accelerometer gravity estimate.
    • "touch" touch sensor.

context Object Context in which to invoke the callback (optional).
magnetometer.start(function(e) {
  console.log(e.x, e.y, e.z);
});

stop()

Stop magnetometer updates.

magnetometer.stop();

Source

src/interfaces/Magnetometer.js