Skip to main content

motion

Listen for device motion (acceleration) events via the DeviceMotionEvent and Generic Sensor APIs.

Import: @rtorcato/browser-common/motion

๐Ÿ“– MDN: DeviceMotionEvent ยท ๐Ÿ“Š caniuse: deviceorientation

iOS 13+ Safari requires an explicit user-gesture permission grant (requestMotionPermission).

Exampleโ€‹

import { isDeviceMotionAvailable, onDeviceMotion, requestMotionPermission } from '@rtorcato/browser-common/motion'

button.addEventListener('click', async () => {
if ((await requestMotionPermission()) === 'granted' && isDeviceMotionAvailable()) {
const off = onDeviceMotion((e) => console.log(e.acceleration))
}
})

Exportsโ€‹

  • isDeviceMotionAvailable() โ€” feature check for DeviceMotionEvent
  • onDeviceMotion(callback) โ€” subscribes to motion events, returns an unsubscribe function
  • isGenericSensorApiAvailable() โ€” feature check for the Generic Sensor API
  • requestMotionPermission() โ€” requests iOS 13+ motion permission, resolves 'granted' | 'denied' | 'default'

See the API reference for full signatures.