-
Notifications
You must be signed in to change notification settings - Fork 1
/
Joycon.min.js
3 lines (2 loc) · 2.46 KB
/
Joycon.min.js
1
2
/* Joycon.js // https://joycon.js.org */
let Joycon={listeners:{},controller:{},controllers:{on:{press:(a,b)=>{Joycon.listeners[a]={callback:b,lastValue:0}},move:(a,b)=>{Joycon.listeners[a+"-move"]={callback:b,lastValue:{x:0,y:0}}},connect:a=>{Joycon.listeners["controller-connect"]={callback:a}},disconnect:a=>{Joycon.listeners["controller-disconnect"]={callback:a}}},removeListener:a=>{delete Joycon.listeners[a]},vibrate:async(a,b)=>{if(a.preset){const b=Joycon.controllers.vibrationPresets,c=b[a.preset];a=c}const c={duration:b,strongMagnitude:a.strongMotorIntensity,weakMagnitude:a.mildMotorIntensity},d=Object.values(Joycon.controller);d.forEach(a=>{const b=a.vibrationActuator;b&&b.playEffect("dual-rumble",c)}),await new Promise(a=>{window.setTimeout(a,b)})},vibrationPresets:{mild:{strongMotorIntensity:0,mildMotorIntensity:.07},medium:{strongMotorIntensity:0,mildMotorIntensity:.14},strong:{strongMotorIntensity:1,mildMotorIntensity:1}}},update:()=>{Joycon.updateControllers();const a=Joycon.buttonMap,b=Joycon.axisMap,c=Object.values(Joycon.controller);c.forEach(c=>{const d=c.buttons;d.forEach((b,c)=>{const d=a[c],e=Joycon.listeners[d];e&&b.value!==e.lastValue&&(e.lastValue=b.value,e.callback(b.value))});const e=c.axes;let f={};e.forEach((a,c)=>{const d=b[c][0],e=b[c][1];f[e]||(f[e]={}),f[e][d]=a}),Object.keys(f).forEach(a=>{const b=Joycon.listeners[a];if(b){const c=f[a];(c.x!==b.lastValue.x||c.y!==b.lastValue.y)&&(b.lastValue=c,b.callback({x:c.x,y:c.y}))}})}),0!==Object.keys(Joycon.controller).length&&Joycon.onNextFrame(Joycon.update)},updateControllers:()=>{const a=navigator.getGamepads();a.forEach(a=>{a&&a.index!==void 0&&(Joycon.controller[a.index]=a)})},addListeners:()=>{window.addEventListener("gamepadconnected",a=>{Joycon.controller[a.gamepad.index]=a.gamepad,Joycon.onNextFrame(Joycon.update);const b=Joycon.listeners["controller-connect"];b&&b.callback(a.gamepad)}),window.addEventListener("gamepaddisconnected",a=>{delete Joycon.controller[a.gamepad.index];const b=Joycon.listeners["controller-disconnect"];b&&b.callback(a.gamepad)})},onNextFrame:a=>{window.requestAnimationFrame(a)},buttonMap:{0:"a",1:"b",2:"x",3:"y",4:"left-shoulder",5:"right-shoulder",6:"left-trigger",7:"right-trigger",8:"select",9:"start",10:"left-joystick",11:"right-joystick",12:"dpad-up",13:"dpad-down",14:"dpad-left",15:"dpad-right",16:"home",17:"share"},axisMap:{0:["x","left-joystick-move"],1:["y","left-joystick-move"],2:["x","right-joystick-move"],3:["y","right-joystick-move"]}};Joycon.addListeners();