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).
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.