Provides a consistent API to estimate the direction of the gravity in the device-frame on all platforms.

The direction is represented as a normalized 3-vector.


var gravity = new MotionStack.Gravity();

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



new MotionStack.Gravity([options])

Create a new Gravity instance object.

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

  • source {String}
  • The source from which to derive the gravity vector:

    • "orientation" Calculated from the processed orientation estimate.
    • "motion" Calculated by subtracting the estimated dynamic acceleration.
    • "lowPass" Calculated by applying a low-pass filter to the total acceleration.

    Default: "orientation"

  • waitForFallback {Number}
  • Duration in milliseconds to wait for deviceorientation event when source is "orientation". If deviceorientation does not return in within this time, then source is set to "motion".

    Default: 500

  • lowPassSampleSize {Number}
  • Size of stencil moving average when using a lowPass source.

    Default: 8

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

    Default: false

var gravity = new MotionStack.Gravity({source: "motion"});

Static Methods


Returns a Boolean value describing whether the device has an accelerometer to estimate gravity.

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

Instance Methods

start(callback, [context])

Start receiving gravity 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 gravity in the device-frame
  • e.source {String}
    The final source used to derive the gravity vector. This may be "orientation", "motion", or "lowPass".
  • e.timeStamp {Number}
    The time in milliseconds when the data is collected
context Object Context in which to invoke the callback (optional).
gravity.start(function(e) {
  console.log(e.x, e.y, e.z);


Stop gravity updates.