Skip to content

useVibrate

类别
导出大小
697 B
最近修改
2 days ago

响应式 振动 API

🌐 Reactive Vibration API

大多数现代移动设备都配备了振动硬件,这使得软件可以通过让设备震动来向用户提供物理反馈。

🌐 Most modern mobile devices include vibration hardware, which lets software code provides physical feedback to the user by causing the device to shake.

振动 API 为 Web 应用提供了访问该硬件的能力(如果硬件存在),如果设备不支持,则不会执行任何操作。

🌐 The Vibration API offers Web apps the ability to access this hardware, if it exists, and does nothing if the device doesn't support it.

示例

Your browser does not support the Vibration API

用法

🌐 Usage

振动被描述为一种开关脉冲的模式,其长度可能各不相同。

🌐 Vibration is described as a pattern of on-off pulses, which may be of varying lengths.

该模式可以由一个整数组成,表示振动的毫秒数,也可以由一个整数数组组成,描述振动和暂停的模式。

🌐 The pattern may consist of either a single integer describing the number of milliseconds to vibrate, or an array of integers describing a pattern of vibrations and pauses.

ts
import { 
useVibrate
} from '@vueuse/core'
// This vibrates the device for 300 ms // then pauses for 100 ms before vibrating the device again for another 300 ms: const {
vibrate
,
stop
,
isSupported
} =
useVibrate
({
pattern
: [300, 100, 300] })
// Start the vibration, it will automatically stop when the pattern is complete:
vibrate
()
// But if you want to stop it, you can:
stop
()