Gamepad: axes property
Gamepad 介面的 axes 屬性返回一個數組,表示裝置上存在的軸控制元件(例如,模擬搖桿)。
陣列中的每個條目都是一個介於 -1.0 到 1.0 之間的浮點值,表示軸位置,其中 -1.0 是最低值,1.0 是最高值。
值
一個數字陣列。
示例
js
function gameLoop() {
const [gp] = navigator.getGamepads();
let a = 0;
let b = 0;
if (gp.axes[0] !== 0) {
b -= gp.axes[0];
} else if (gp.axes[1] !== 0) {
a += gp.axes[1];
} else if (gp.axes[2] !== 0) {
b += gp.axes[2];
} else if (gp.axes[3] !== 0) {
a -= gp.axes[3];
}
ball.style.left = `${a * 2}px`;
ball.style.top = `${b * 2}px`;
const start = requestAnimationFrame(gameLoop);
}
規範
| 規範 |
|---|
| Gamepad # dom-gamepad-axes |
瀏覽器相容性
載入中…