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.
Bubbles | No |
---|---|
Cancelable | No |
Interface | DeviceOrientationEvent |
Event handler property | window.ondeviceorientation |
Examples
if (window.DeviceOrientationEvent) {
window.addEventListener("deviceorientation", function(event) {
// alpha: rotation around z-axis
var rotateDegrees = event.alpha;
// gamma: left to right
var leftToRight = event.gamma;
// beta: front back motion
var frontToBack = event.beta;
handleOrientationEvent(frontToBack, leftToRight, rotateDegrees);
}, true);
}
var handleOrientationEvent = function(frontToBack, leftToRight, rotateDegrees) {
// do something amazing
};
Specifications
Specification | Status |
---|---|
DeviceOrientation Event Specification The definition of 'DeviceOrientation event' in that specification. |
Editor's Draft |
Browser compatibility
BCD tables only load in the browser
See also
devicemotion
- Detecting device orientation
- Orientation and motion data explained
- Simulating orientation events on desktop browsers with the orientation-devtool