W3cubDocs

/Web APIs

Window: devicemotion event

The devicemotion event is fired at a regular interval and indicates the amount of physical force of acceleration the device is receiving at that time. It also provides information about the rate of rotation, if available.

This event is not cancelable and does not bubble.

Syntax

Use the event name in methods like addEventListener(), or set an event handler property.

js

addEventListener("devicemotion", (event) => {});

ondevicemotion = (event) => {};

Event type

Event properties

DeviceMotionEvent.acceleration Read only

An object giving the acceleration of the device on the three axis: x, y, and z. Acceleration is expressed in m/s².

DeviceMotionEvent.accelerationIncludingGravity Read only

An object giving the acceleration of the device on the three axis: x, y, and z with the effect of gravity. Acceleration is expressed in m/s².

DeviceMotionEvent.rotationRate Read only

An object giving the rate of change of the device's orientation on the three orientation axis: alpha, beta and gamma. Rotation rate is expressed in degrees per seconds.

DeviceMotionEvent.interval Read only

A number representing the interval of time, in milliseconds, at which data is obtained from the device.

Examples

js

function handleMotionEvent(event) {
  const x = event.accelerationIncludingGravity.x;
  const y = event.accelerationIncludingGravity.y;
  const z = event.accelerationIncludingGravity.z;

  // Do something awesome.
}

window.addEventListener("devicemotion", handleMotionEvent, true);

Specifications

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
devicemotion_event 31 12 6 11 18 No 4.4.3 31 6 18 4.2 2.0

See also

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Window/devicemotion_event