Giter Site home page Giter Site logo

unswitch's Introduction

Unswitch

npm version gzip size license dependencies

Unswitch is a tiny (400b) event handler for Switch controllers on the web based on the Gamepad API. Attach callbacks to button presses (up & down) and the joystick!

Install

$ npm install --save unswitch

Setup

Connecting a Switch controller is easy: pair it with bluetooth and you're ready to go! This library doesn't listen to connected or disconnected events but in some cases it might be useful. Here's how you can listen to these events:

window.addEventListener("gamepadconnected", ({ gamepad }) => {});
window.addEventListener("gamepaddisconnected", ({ gamepad }) => {});

Usage

// Import the library
import Unswitch from 'unswitch';

// Create the listener
const unswitch = new Unswitch({
  side: 'L', // or R
  b: (pressed) => {},
  a: (pressed) => {},
  y: (pressed) => {},
  x: (pressed) => {},
  l: (pressed) => {},
  r: (pressed) => {},
  axes: (position) => {},
});

function render() {
  // Check for button stateupdates
  unswitch.update();
  requestAnimationFrame(render);
}

render();

It's possible to connect up to two controllers at the same time. To make this work side is to be passed with either L (left) or R (right) for the controllers respectively. Calling unswitch.update() will check every button for a change in state. If a callback is provided the new state is passed along. The axis works in the same way, but instead of a boolean it will return a number from 0 to 8. Number 0 to 7 are for the joystick positions going clockwise, number 8 is used as default (center).

Buttons are mapped based on a single controller positioned horizontally.

Contribute

Are you excited about this library and have interesting ideas on how to improve it? Please tell me or contribute directly! ๐Ÿ™Œ

npm install > npm run demo > http://localhost:8080

License

MIT ยฉ Colin van Eenige

unswitch's People

Contributors

vaneenige avatar

Watchers

Alvar Laigna avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.