GamepadHapticActuator:playEffect() 方法

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

GamepadHapticActuator 接口的 playEffect() 方法使硬件播放特定的振动模式。

语法

js
playEffect(type, params)

参数

type

表示所需效果的字符串。这可能因硬件类型而异,可能的值为 "dual-rumble"(双重隆隆声)或 "vibration"(振动)。

params

描述所需触觉效果的对象。期望值是:

duration

效果的持续时间(以毫秒为单位)。

startDelay

效果开始前的延迟(以毫秒为单位)。

strongMagnitude

低频(强)隆隆声电机的隆隆声强度,标准化为 0.0 到 1.0 之间的范围。

weakMagnitude

高频(弱)隆隆声电机的隆隆声强度,标准化为 0.0 到 1.0 之间的范围。

备注: 一个新的 playEffect() 调用会覆盖上一个仍在进行中的调用。

返回值

当 playEffect 成功完成时,返回一个兑现为 true 的 Promise。

示例

js
const gamepad = navigator.getGamepads()[0];

gamepad.vibrationActuator.playEffect("dual-rumble", {
  startDelay: 0,
  duration: 200,
  weakMagnitude: 1.0,
  strongMagnitude: 1.0,
});

规范

Specification
Gamepad
# dom-gamepadhapticactuator-playeffect

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
playEffect
params.leftTrigger parameter
Experimental
params.rightTrigger parameter
Experimental
trigger-rumble effect type
Experimental

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.

参见