files/en-us/web/api/window/deviceorientation_event/index.md
{{APIRef("Device Orientation Events")}}{{securecontext_header}}
The deviceorientation event is fired when fresh data is available from an orientation sensor about the current orientation of the device as compared to the Earth coordinate frame. This data is gathered from a magnetometer inside the device.
See Orientation and motion data explained for details.
This event is not cancelable and does not bubble.
Use the event name in methods like {{domxref("EventTarget.addEventListener", "addEventListener()")}}, or set an event handler property.
addEventListener("deviceorientation", (event) => { })
ondeviceorientation = (event) => { }
A {{domxref("DeviceOrientationEvent")}}. Inherits from {{domxref("Event")}}.
{{InheritanceDiagram("DeviceOrientationEvent")}}
DeviceOrientationEvent.webkitCompassHeading {{Non-Standard_Inline}} {{ReadOnlyInline}}
DeviceOrientationEvent.webkitCompassAccuracy {{Non-Standard_Inline}} {{ReadOnlyInline}}
if (window.DeviceOrientationEvent) {
window.addEventListener(
"deviceorientation",
(event) => {
const rotateDegrees = event.alpha; // alpha: rotation around z-axis
const leftToRight = event.gamma; // gamma: left to right
const frontToBack = event.beta; // beta: front back motion
handleOrientationEvent(frontToBack, leftToRight, rotateDegrees);
},
true,
);
}
function handleOrientationEvent(frontToBack, leftToRight, rotateDegrees) {
// do something amazing
}
{{Specifications}}
{{Compat}}