Skip to content

Instantly share code, notes, and snippets.

@diska
Last active August 7, 2018 11:41
Show Gist options
  • Select an option

  • Save diska/03f047bb9d10a071e619da48c2504c9f to your computer and use it in GitHub Desktop.

Select an option

Save diska/03f047bb9d10a071e619da48c2504c9f to your computer and use it in GitHub Desktop.
Web Gamepad APIがDirectInputでとりあえず動いたコード
<div id="GamePadUI">
buttons<input id="b" type="text"><br/>
axis0<input id="a0" type="range"><br/>
axis1<input id="a1" type="range"><br/>
axis2<input id="a2" type="range"><br/>
axis3<input id="a3" type="range"><br/>
</div>
<script>
UI={};UI.a=[];
UI.b=document.getElementById("b");
UI.a[0]=document.getElementById("a0");UI.a[1]=document.getElementById("a1");
UI.a[2]=document.getElementById("a2");UI.a[3]=document.getElementById("a3");
UI.update=function(buttons, axes){
var str="";
for(i=0; i<buttons.length; i++)str+=(buttons[i].pressed)?1:0;
UI.b.value=str;
for(i=0; i<axes.length; i++)UI.a[i].value=axes[i]*50+50;
}
var gamepads=[];
conn=function(e){
var gp=e.gamepad, str="";
if(e.type=="gamepadconnected"){
str=`接続#${gp.index}, ${gp.id}`;
str+=`${gp.buttons.length}ボタン, ${gp.axes.length}軸.`;
gamepads[gp.index]=gp;
}else{
str=`切断#${gp.index}, ${gp.id}.`;
delete gamepads[gp.index];
}
}
update=function(now){
var gp;
if(navigator.webkitGetGamepads){
gp=navigator.webkitGetGamepads()[0];
}else{
gp=navigator.getGamepads()[0];
}
if(gp==null){setTimeout(arguments.callee,1000);return;}
UI.update(gp.buttons, gp.axes);
setTimeout(arguments.callee,1000/60);
}
window.addEventListener("gamepadconnected", conn);
window.addEventListener("gamepaddisconnected", conn);
update();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment