Instances of the
DeviceOrientationEvent class are fired only when the device has a gyroscope and while the user is changing the orientation. The
DeviceOrientationEvent class encapsulates the angles of rotation in degrees and heading.
- Safari Mobile 4.2+
The angles of rotation—the
gamma properties—do not represent the real world orientation. They are defined as an offset from an arbitrary direction—typically, the direction in which the device was held when the orientation was first obtained. Therefore, you can only use the angles to track changes in orientation, you cannot derive the direction in which the device is currently facing. To track changes, save the first device orientation event and use it as a reference for subsequent values. If you want a real world heading, use the
Because some devices may not have a gyroscope, you can listen for this event. If it does not occur, use the
DeviceMotionEvent class to receive raw accelerometer events. From these events you can determine an approximate orientation.
You register for device orientation events using the window’s
addEventListener method by passing
deviceorientation as the event type.