• Stars
    star
    573
  • Rank 77,865 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ•Ή A tiny event handler for Switch controllers!

Unswitch

npm version gzip size license dependencies

Unswitch is a tiny 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

Use script tags or modules? Check out the version on unpkg!

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';

// Observe a controller
const unswitch = new Unswitch({
  side: 'L', // or R
  buttons: (button, pressed, side) => console.log(`Catch-all - button: ${button} was ${pressed ? 'pressed' : 'released'} on the ${side} side`),
  b: p => {},
  a: p => {},
  y: p => {},
  x: p => {},
  sl: p => {},
  sr: p => {},
  minus: p => {},
  plus: p => {},
  lstick: p => {},
  rstick: p => {},
  home: p => {},
  screenshot: p => {},
  bumper: p => {}, // L or R
  trigger: p => {}, // ZL or ZR
  axes: p => {},
});

function render() {
  // Call the update function manually
  unswitch.update();
  requestAnimationFrame(render);
}

render();

Please note that it's not required to pass all button-functions to Unswitch and will only be executed when you provide them. You are able to use the buttons function to catch all button presses and implement your own logic using the provided data. The buttons function will always be executed when provided, even when the button is also passed as property.

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).

Have a look at the demo on Codepen!

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

More Repositories

1

phenomenon

⚑️ A fast 2kB low-level WebGL API.
TypeScript
1,773
star
2

THREE.Phenomenon

⭐️ A tiny wrapper around three.js built for high-performance WebGL experiences.
TypeScript
360
star
3

uos

🐭 A tiny 250b scroll listener with progress.
TypeScript
350
star
4

scroll-transitions-webgl

A demo that demonstrates how a smooth WebGL transition can be realized using Phenomenon (based on three.js).
JavaScript
154
star
5

offline-gallery

🎈 A 16kb Preact & Redux based Progressive Web App that offers an offline gallery experience of external images.
JavaScript
96
star
6

uon

🐨 A tiny 200b route change observer.
TypeScript
84
star
7

phenomenon-px

⚑️ The fastest way to create pixel shaders.
JavaScript
81
star
8

unvault

πŸ“¦ A minimal layer for node that allows results of time-consuming tasks to be stored.
JavaScript
61
star
9

periodic-weather

β˜€οΈ A 17kb Preact & Redux based Progressive Web App that provides relevant information to the user outside of the tab.
JavaScript
59
star
10

rupi

πŸ’―An alternative to Preact with the same syntax, size and functionality.
44
star
11

webgl-structure

πŸš€ A modern, ES6 based, javascript structure for WebGL based projects with THREE.js!
JavaScript
44
star
12

preact-gallery

πŸ™ A modern gallery experience disguised as a tiny (8kb) Preact based PWA.
JavaScript
43
star
13

uot

🦁 A tiny setTimeout alternative with progress.
JavaScript
43
star
14

rainbow-explorer

🌈 A 20kb Preact & Redux based Progressive Web App that translates real life color to digital color.
JavaScript
27
star
15

untrace

🐳 Minimal event tracking on the client in 300 bytes.
JavaScript
25
star
16

periodic-weather-analytics

⚑️ A high performance Preact based PWA providing an overview of information from Periodic Weather.
JavaScript
25
star
17

hold.js

A tiny (no dependency) library for click / touch and hold functionality!
JavaScript
19
star
18

prefers

✍️ Detect system (or manually set) preferences for color scheme and reduced motion.
JavaScript
19
star
19

zero-distraction-js13k

πŸ”₯A simple (6kb) WebGL game made for the JS13K competition!
JavaScript
15
star
20

unvault-middleware

πŸ“¦ A middleware to automatically setup routes, store expected responses and deliver results fast.
JavaScript
7
star