Gamepad: axes-Eigenschaft
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since März 2017.
Die Gamepad.axes-Eigenschaft des Gamepad-Interfaces gibt ein Array zurück, das die Steuerungen mit Achsen darstellt, die auf dem Gerät vorhanden sind (z. B. analoge Joysticks).
Jeder Eintrag im Array ist ein Gleitkommawert im Bereich von -1,0 bis 1,0, der die Achsenposition vom niedrigsten Wert (-1,0) bis zum höchsten Wert (1,0) darstellt.
Wert
Ein Array von Zahlen.
Beispiele
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);
}
Spezifikationen
| Specification |
|---|
| Gamepad> # dom-gamepad-axes> |