Window: devicemotion event
Baseline2023Newly available
Since September 2023, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
Secure context: This feature is available only insecure contexts (HTTPS), in some or allsupporting browsers.
Thedevicemotion
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 likeaddEventListener()
, or set an event handler property.
addEventListener("devicemotion", (event) => { })ondevicemotion = (event) => { }
Event type
ADeviceMotionEvent
. Inherits fromEvent
.
Event properties
DeviceMotionEvent.acceleration
Read onlyAn object giving the acceleration of the device on the three axis: x, y, and z. Acceleration is expressed inm/s².
DeviceMotionEvent.accelerationIncludingGravity
Read onlyAn object giving the acceleration of the device on the three axis: x, y, and z with the effect of gravity. Acceleration is expressed inm/s².
DeviceMotionEvent.rotationRate
Read onlyAn 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 onlyA number representing the interval of time, in milliseconds, at which data is obtained from the device.
Examples
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
Specification |
---|
Device Orientation and Motion # devicemotion |
Device Orientation and Motion # ref-for-dom-window-ondevicemotion |