Skip to content

useDeviceMotion ​

Category
Export Size
758 B
Last Changed
last week

Reactive DeviceMotionEvent. Provide web developers with information about the speed of changes for the device's position and orientation.

Demo ​

Device Motion:
Not supported by your current browser.

Usage ​

js
import { useDeviceMotion } from '@vueuse/core'

const {
  acceleration,
  accelerationIncludingGravity,
  rotationRate,
  interval,
} = useDeviceMotion()

Note: For iOS, you need to use trigger and bind it with user interaction. After permission 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..
ensurePermissionsbooleanIndicates whether the platform requires permission to use the API
permissionGrantedbooleanIndicates whether the user has granted permission. The default is always false
triggerPromise<void>An async function to request user permission. The API runs automatically once permission is granted

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

Component Usage ​

This function also provides a renderless component version via the @vueuse/components package. Learn more about the usage.

vue
<template>
  <UseDeviceMotion v-slot="{ acceleration }">
    Acceleration: {{ acceleration }}
  </UseDeviceMotion>
</template>

Type Declarations ​

Show Type Declarations
typescript
export interface DeviceMotionOptions
  extends ConfigurableWindow,
    ConfigurableEventFilter {
  /**
   * Request for permissions immediately if it's not granted,
   * otherwise label and deviceIds could be empty
   *
   * @default false
   */
  requestPermissions?: boolean
}
/**
 * Reactive DeviceMotionEvent.
 *
 * @see https://vueuse.org/useDeviceMotion
 * @param options
 */
export declare function useDeviceMotion(options?: DeviceMotionOptions): {
  acceleration: Ref<
    DeviceMotionEventAcceleration | null,
    DeviceMotionEventAcceleration | null
  >
  accelerationIncludingGravity: Ref<
    DeviceMotionEventAcceleration | null,
    DeviceMotionEventAcceleration | null
  >
  rotationRate: Ref<
    DeviceMotionEventRotationRate | null,
    DeviceMotionEventRotationRate | null
  >
  interval: Ref<number, number>
  isSupported: ComputedRef<boolean>
  requirePermissions: ComputedRef<boolean>
  ensurePermissions: () => Promise<void>
  permissionGranted: Ref<boolean, boolean>
}
export type UseDeviceMotionReturn = ReturnType<typeof useDeviceMotion>

Source ​

Source • Demo • Docs

Contributors ​

Anthony Fu
Antério Vieira
Fernando Fernández
thefeymesaleng
Anthony Fu
vaakian X
Shinigami
wheat
Alex Kozack

Changelog ​

v12.4.0 on 1/10/2025
dd316 - feat: use passive event handlers everywhere is possible (#4477)
v12.1.0 on 12/22/2024
cf729 - feat: add permission controls, improve destructure (#4127)
v12.0.0-beta.1 on 11/21/2024
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v9.11.0 on 1/17/2023
d5321 - fix(components): mark defineComponent as pure (#2623)

Released under the MIT License.