Skip to content

useDeviceMotion

Category
Export Size
817 B
Last Changed
5 days ago

Reactive DeviceMotionEvent

Demo

Usage

tsx
import { 
useDeviceMotion
} from '@reause/core'
const {
acceleration
,
accelerationIncludingGravity
,
rotationRate
,
interval
,
isSupported
,
} =
useDeviceMotion
()

Note: For iOS, you need to use ensurePermissions and bind it with user interaction. After permission is granted, the API will run automatically.

StateTypeDescription
accelerationobjectAn object giving the acceleration of the device on the three axis X, Y and Z.
accelerationIncludingGravityobjectAn object giving the acceleration of the device on the three axis X, Y and Z with the effect of gravity.
rotationRateobjectAn object giving the rate of change of the device's orientation on the three orientation axis alpha, beta and gamma.
intervalNumberA number representing the interval of time, in milliseconds, at which data is obtained from the device.
isSupportedbooleanWhether the current environment supports the DeviceMotionEvent API.
requirePermissionsbooleanWhether the platform requires permission to use the API.
ensurePermissionsPromise<void>An async function to request user permission. The API runs automatically once permission is granted.
permissionGrantedbooleanWhether the user has granted permission. The default is always false.

You can find more information about the state on the MDN.

Type Declarations

ts
export interface UseDeviceMotionOptions extends ConfigurableWindow {
    requestPermissions?: boolean;
    eventFilter?: EventFilter;
}

export type DeviceMotionOptions = UseDeviceMotionOptions

export interface UseDeviceMotionReturn {
    acceleration: DeviceMotionEventAcceleration | null;
    accelerationIncludingGravity: DeviceMotionEventAcceleration | null;
    rotationRate: DeviceMotionEventRotationRate | null;
    interval: number;
    isSupported: boolean;
    requirePermissions: boolean;
    ensurePermissions: () => Promise<void>;
    permissionGranted: boolean;
}

export function useDeviceMotion(options?: UseDeviceMotionOptions): UseDeviceMotionReturn

Source

Source · Demo · VueUse

Contributors

hairyf

Changelog

v0.1.0 on
1684b - fix(core): resolve useDeviceMotion audit findings (#539)

Released under the MIT License. v0.1.8