gamepad api简单例子
在做WEB VR游戏开发,没有手柄控制的VR只能看个场景,无法交互,所以手柄是VR游戏不可或缺的输入设备。
如何在浏览器里只用Javascript就实现手柄互动?我们其实可以使用gamepad api,目前虽然只是草案阶段,但是相信不久的将来会得到普及。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script type="text/javascript"> function runAnimation() { window.requestAnimationFrame(runAnimation); var gamepads = navigator.getGamepads(); for (var i = 0; i < gamepads.length; ++i) { var pad = gamepads[i]; //获取游戏摇杆上下左右方位值 x=pad.axes[0]; y=pad.axes[1]; //获取按到了哪个键 var buttons=pad.buttons; for(i=0;i<buttons.length;i++){ if(buttons[i].pressed){ buttonIndex=i; } } document.getElementById("info").innerHTML="x: "+x+" y:"+y+" Button"+buttonIndex+" pressed"; } } //在浏览器窗口动画中扫描手柄的各种动态变化 window.requestAnimationFrame(runAnimation);
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: php计算中英文混合或中文字符串的字数
- 下一篇: c++ vector 动态数组用法