Window: devicemotion event

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since September 2023.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

The devicemotion event is fired at a regular interval and indicates the acceleration rate of the device with/without the contribution of the gravity force 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

A DeviceMotionEvent. Inherits from Event.

Event DeviceMotionEvent

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);

Specifications

Specification
Device Orientation and Motion
# devicemotion
Device Orientation and Motion
# dom-window-ondevicemotion

Browser compatibility

See also