Skip to content

useDeviceMotion

类别
导出体积
703 B
上次更改
2 minutes ago

响应式 DeviceMotionEvent。提供有关设备位置和方向变化速度的信息给 Web 开发者。

示例

设备运动:
{
  "acceleration": {
    "x": null,
    "y": null,
    "z": null
  },
  "accelerationIncludingGravity": {
    "x": null,
    "y": null,
    "z": null
  },
  "rotationRate": {
    "alpha": null,
    "beta": null,
    "gamma": null
  },
  "interval": 0
}

用法

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

const {
  acceleration,
  accelerationIncludingGravity,
  rotationRate,
  interval,
} = useDeviceMotion()
状态类型描述
accelerationobject一个对象,提供设备在三个轴 X、Y 和 Z 上的加速度。
accelerationIncludingGravityobject一个对象,提供设备在三个轴 X、Y 和 Z 上受到重力影响后的加速度。
rotationRateobject一个对象,提供设备在三个方向轴 alpha、beta 和 gamma 上方向变化的速率。
intervalNumber一个表示以毫秒为单位从设备获取数据的时间间隔的数字。

你可以在 MDN 上找到有关状态的更多信息

组件用法

vue
<template>
  <UseDeviceMotion v-slot="{ acceleration }">
    加速度:{{ acceleration }}
  </UseDeviceMotion>
</template>

类型声明

typescript
export interface DeviceMotionOptions
  extends ConfigurableWindow,
    ConfigurableEventFilter {}
/**
 * 响应式 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>
}
export type UseDeviceMotionReturn = ReturnType<typeof useDeviceMotion>

源码

源码演示文档

贡献者

Anthony Fu
一纸忘忧
Antério Vieira
Anthony Fu
vaakian X
Shinigami
wheat
Alex Kozack

更新日志

v9.11.0 on 1/17/2023
d5321 - fix(components): mark defineComponent as pure (#2623)