UNPKG

@forward-software/reveal.js-joycontroller

Version:

Custom plugin for Reveal.js to control presentations using Nintendo Switch Joy-Cons

2 lines (1 loc) 2 kB
const e={type:"right",cooldown:300,pointerSpeed:20},n={left:{RIGHT:3,LEFT:0,UP:2,DOWN:1,PREV:4,NEXT:5,QUIT_OVERVIEW_OR_NEXT:6,TOGGLE_OVERVIEW:8,TOGGLE_POINTING:10,TOGGLE_PAUSE:9,TOGGLE_HELP:16},right:{RIGHT:0,LEFT:3,UP:1,DOWN:2,PREV:4,NEXT:5,QUIT_OVERVIEW_OR_NEXT:7,TOGGLE_OVERVIEW:8,TOGGLE_POINTING:10,TOGGLE_PAUSE:9,TOGGLE_HELP:16}},t=()=>({id:"joycontroller",init:function(t){const i=t.getConfig(),o=Object.assign({},e,i.joycon),a="GamepadEvent"in window,d="WebKitGamepadEvent"in window,E=window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||window.requestAnimationFrame,c={},s={};function r(e,i){if(a=`${e.id}-btn-${i}`,s[a]&&!(Date.now()-s[a]>=o.cooldown)||(s[a]=Date.now(),0))return;var a;const d=n[o.type];switch(i){case d.RIGHT:t.right();break;case d.DOWN:t.down();break;case d.UP:t.up();break;case d.LEFT:t.left();break;case d.PREV:t.prev();break;case d.NEXT:t.next();break;case d.TOGGLE_OVERVIEW:t.toggleOverview();break;case d.QUIT_OVERVIEW_OR_NEXT:t.isOverview()?t.toggleOverview():t.next();break;case d.TOGGLE_PAUSE:t.togglePause();break;case d.TOGGLE_POINTING:pointing=!pointing,pointer.style.display=pointing?"block":"none";break;case d.TOGGLE_HELP:t.toggleHelp()}}function G(){return navigator.getGamepads?navigator.getGamepads():navigator.webkitGetGamepads?navigator.webkitGetGamepads():[]}function w(){const e=G();if(e){for(const n in e){const t=e[n];if(t)for(let e=0;e<t.buttons.length;++e){const n=t.buttons[e];let i=1===n,o=!1;"object"==typeof n&&(i=n.pressed,"touched"in n&&(o=n.touched)),(i||o)&&r(t,e)}}E(w)}}function O(e){c[e.index]=e,E(w)}function g(e){O(e.gamepad)}function T(e){var n;n=e.gamepad,delete c[n.index]}a?(window.addEventListener("gamepadconnected",g),window.addEventListener("gamepaddisconnected",T)):d?(window.addEventListener("webkitgamepadconnected",g),window.addEventListener("webkitgamepaddisconnected",T)):setInterval((function(){const e=G();for(const n in e){const t=e[n];t&&(t.index in c?c[t.index]=t:O(t))}}),500)}});export{t as default};