orientation
Read device orientation events and lock/unlock the screen orientation via the DeviceOrientation and Screen Orientation APIs.
Import: @rtorcato/browser-common/orientation
๐ MDN: DeviceOrientationEvent ยท ๐ caniuse: deviceorientation
iOS 13+ Safari requires an explicit user-gesture permission grant (see motion's requestMotionPermission, which covers orientation too).
Browser supportโ
| API | Chrome | Edge | Firefox | Safari | iOS Safari | Baseline |
|---|---|---|---|---|---|---|
DeviceOrientationEvent | โ 7+ [1] | โ 12+ | โ 6+ [2] | โ 17+ | โ 4.2+ | Widely available |
ScreenOrientation | โ 38+ | โ 79+ | โ 43+ | โ 16.4+ | โ 16.4+ | Widely available |
- Chrome: Before version 50, Chrome provided absolute values instead of relative values for this event. Developers still needing absolute values may use the deviceorientationabsolute event.
- Firefox: Firefox 3.6, 4, and 5 supported mozOrientation instead of the standard DeviceOrientationEvent interface.
โ
supported ยท โ partial ยท โ not supported ยท [n] see note. Generated from MDN browser-compat-data and web-features. caniuse: DeviceOrientationEvent ยท ScreenOrientation
Exampleโ
import {
isDeviceOrientationAvailable,
onDeviceOrientation,
lockScreenOrientation,
unlockScreenOrientation,
} from '@rtorcato/browser-common/orientation'
if (isDeviceOrientationAvailable()) {
const off = onDeviceOrientation((e) => console.log(e.alpha, e.beta, e.gamma))
off()
}
await lockScreenOrientation('landscape-primary')
unlockScreenOrientation()
Exportsโ
isDeviceOrientationAvailable()โ feature check forDeviceOrientationEventonDeviceOrientation(callback)โ subscribes to orientation changes, returns an unsubscribe functiongetScreenOrientationType()โ current orientation type (e.g.'portrait-primary')lockScreenOrientation(type)โ locks the screen to an orientation, if supportedunlockScreenOrientation()โ unlocks the screen orientation, if supported
See the API reference for full signatures.