Device Motions

The first-generation device motions support is a part of Device Orientation API. It allows web applications to access the accelerometer data expressed as acceleration (in m/s2) and gyroscope data expressed as rotation angle change (in °/s) for each of the three dimensions, provided as events.

There also exist the newer, separate specifications for each sensor type, based on the Generic Sensor API - the Accelerometer Sensor API and Gyroscope Sensor API. Both are expected to be implemented for the first time in Chrome in late 2016.

For the detection of the device's static position and orientation, see Device Orientation.

API glimpse

As a part of Device Orientation API

window.addEventListener('devicemotion', listener)
An event fired when the significant changes in the device's acceleration or rotation has occured.
event.acceleration
A part of the event's payload returning the data about the current device's acceleration excluding gravity for all three axes (acceleration.x, acceleration.y, acceleration.z).
event.accelerationIncludingGravity
A part of the event's payload returning the data about the current device's acceleration including gravity if the device is unable to provide the data without the gravity effect using event.acceleration.
event.rotationRate
A part of the event's payload returning the data about the current device's rotation rates for all three axes (rotationRate.alpha, rotationRate.beta, rotationRate.gamma).
event.interval
A part of the event's payload returning the interval (in ms) at which the data is obtained from the accelerometer.

Accelerometer API

sensor = new AccelerometerSensor({includeGravity: true})
Creates an object serving as an accessor to the accelerometer readings and specifying whether the acceleration values should include gravity.
sensor.addEventListener('change', listener)
An event fired when the accelerometer reading has changed, containing acceleration values in m/s2 for all three axes (event.reading.accelerationX, event.reading.accelerationY, event.reading.accelerationZ).
sensor.start()
Starts listening for the sensor readings.

Gyroscope API

sensor = new GyroscopeSensor()
Creates an object serving as an accessor to the gyroscope readings.
sensor.addEventListener('change', listener)
An event fired when the gyroscope reading has changed, containing rotation rates in rad/s for all three axes (event.reading.rotationRateX, event.reading.rotationRateY, event.reading.rotationRateZ).
sensor.start()
Starts listening for the sensor readings.

Resources