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).
Browser supportโ
| API | Chrome | Edge | Firefox | Safari | iOS Safari | Baseline |
|---|---|---|---|---|---|---|
DeviceMotionEvent | โ 31+ | โ 12+ | โ 6+ | โ 17+ | โ 4.2+ | Widely available |
DeviceMotionEvent.requestPermission | โ 152+ | โ 152+ | โ | โ | โ 14.5+ | Limited |
โ
supported ยท โ partial ยท โ not supported ยท [n] see note. Generated from MDN browser-compat-data and web-features. caniuse: DeviceMotionEvent ยท DeviceMotionEvent.requestPermission_static
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 forDeviceMotionEventonDeviceMotion(callback)โ subscribes to motion events, returns an unsubscribe functionisGenericSensorApiAvailable()โ feature check for the Generic Sensor APIrequestMotionPermission()โ requests iOS 13+ motion permission, resolves'granted' | 'denied' | 'default'
See the API reference for full signatures.