Skip to main content

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โ€‹

APIChromeEdgeFirefoxSafariiOS SafariBaseline
DeviceOrientationEventโœ… 7+ [1]โœ… 12+โœ… 6+ [2]โœ… 17+โœ… 4.2+Widely available
ScreenOrientationโœ… 38+โœ… 79+โœ… 43+โœ… 16.4+โœ… 16.4+Widely available
  1. 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.
  2. 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 for DeviceOrientationEvent
  • onDeviceOrientation(callback) โ€” subscribes to orientation changes, returns an unsubscribe function
  • getScreenOrientationType() โ€” current orientation type (e.g. 'portrait-primary')
  • lockScreenOrientation(type) โ€” locks the screen to an orientation, if supported
  • unlockScreenOrientation() โ€” unlocks the screen orientation, if supported

See the API reference for full signatures.