gpt4 book ai didi

javascript - 如何在 HTML5 JavaScript 中检测 Joy-Con 输入/运动控件

转载 作者:行者123 更新时间:2023-12-02 23:32:56 25 4
gpt4 key购买 nike

我正在尝试创建一个使用 Nintendo Switch Joy-Con 和运动控制的 HTML5 JavaScript 游戏。问题是,当 Joy-Con 连接到我的电脑时,我不知道如何检测它们的运动控制。

我已经成功使用 Gamepad API 通过 Xbox Controller 、PS4 和 Joy Con 实现了按钮输入,但是是否可以使用 Joy-Con Action 控制来实现这一点?

如果您想查看的话,这里是 Gamepad API 的代码(同样,我的目标是 Joy-Con Action 控制):

var haveEvents = 'ongamepadconnected' in window;
var controllers = {};

function connecthandler(e) {
addgamepad(e.gamepad);
}

function addgamepad(gamepad) {
controllers[gamepad.index] = gamepad;

var d = document.createElement("div");
d.setAttribute("id", "controller" + gamepad.index);

var t = document.createElement("h1");
t.appendChild(document.createTextNode("gamepad: " + gamepad.id));
d.appendChild(t);

var b = document.createElement("div");
b.className = "buttons";
for (var i = 0; i < gamepad.buttons.length; i++) {
var e = document.createElement("span");
e.className = "button";
//e.id = "b" + i;
e.innerHTML = i;
b.appendChild(e);
}

d.appendChild(b);

var a = document.createElement("div");
a.className = "axes";

for (var i = 0; i < gamepad.axes.length; i++) {
var p = document.createElement("progress");
p.className = "axis";
//p.id = "a" + i;
p.setAttribute("max", "2");
p.setAttribute("value", "1");
p.innerHTML = i;
a.appendChild(p);
}

d.appendChild(a);

var start = document.getElementById("start");
if (start) {
start.style.display = "none";
}

document.body.appendChild(d);
requestAnimationFrame(updateStatus);
}

function disconnecthandler(e) {
removegamepad(e.gamepad);
}

function removegamepad(gamepad) {
var d = document.getElementById("controller" + gamepad.index);
document.body.removeChild(d);
delete controllers[gamepad.index];
}

function updateStatus() {
if (!haveEvents) {
scangamepads();
}

var i = 0;
var j;

for (j in controllers) {
var controller = controllers[j];
var d = document.getElementById("controller" + j);
var buttons = d.getElementsByClassName("button");

for (i = 0; i < controller.buttons.length; i++) {
var b = buttons[i];
var val = controller.buttons[i];
var pressed = val == 1.0;
if (typeof(val) == "object") {
pressed = val.pressed;
val = val.value;
}

var pct = Math.round(val * 100) + "%";
b.style.backgroundSize = pct + " " + pct;

if (pressed) {
b.className = "button pressed";
//Pressed down code here
} else {
b.className = "button";
//Release button code here
}
}

var axes = d.getElementsByClassName("axis");
for (i = 0; i < controller.axes.length; i++) {
var a = axes[i];
a.innerHTML = i + ": " + controller.axes[i].toFixed(4);
a.setAttribute("value", controller.axes[i] + 1);
}
}

requestAnimationFrame(updateStatus);
}

function scangamepads() {
var gamepads = navigator.getGamepads ? navigator.getGamepads() : (navigator.webkitGetGamepads ? navigator.webkitGetGamepads() : []);
for (var i = 0; i < gamepads.length; i++) {
if (gamepads[i]) {
if (gamepads[i].index in controllers) {
controllers[gamepads[i].index] = gamepads[i];
} else {
addgamepad(gamepads[i]);
}
}
}
}


window.addEventListener("gamepadconnected", connecthandler);
window.addEventListener("gamepaddisconnected", disconnecthandler);

if (!haveEvents) {
setInterval(scangamepads, 500);
}

使用this link供引用

最佳答案

Wei Gao explained thisReact Knowledgeable上周的聚会。

您可以通过her presentation了解她是如何做到的或her slides .

您可以访问the talk page了解更多信息。

关于javascript - 如何在 HTML5 JavaScript 中检测 Joy-Con 输入/运动控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54814200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com