npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gamecontroller.js

v1.5.0

Published

A JavaScript library that lets you handle, configure, and use gamepad and controllers on a browser, using the Gamepad API

Downloads

599

Readme

gameController.js

A JavaScript library that lets you handle, configure, and use gamepad and controllers on a browser.

Build Status npm npm

Getting started

GameController.js is a lightweight library (~6KB) that uses JavaScript and the standard Gamepad API, and does not have any plugin/library dependencies.

Installation

From npm:

npm i gamecontroller.js

From yarn:

yarn add gamecontroller.js

Directly into your webpage (check latest release on github):

<script src="./gamecontroller.min.js"></script>

Usage

After importing the library into your webpage/project, gameControl will be available to use. This object comes with a series of properties and methods that will allow to handle the different gamepads connected to the computer.

The connected gamepads will be stored in a list of gamepad objects in gameControl. This gamepad object is not the default one returned by the browser but a higher-level interface to interact with it and simplify its usability.

Once the file is imported into the project, the object gameControl will be available and ready to be used.

gameControl.on('connect', function(gamepad) {
  gamepad.on('up', moveCharacterUp);
});

Visit the Wiki for a full list of the properties, methods and events of these two objects.

Events for gameControl

For the object gameControl, events are associated using the .on() method:

gameControl.on('connect', gamepad => {
  console.log('A new gamepad was connected!');
});

Here is a list of the events that can be associated using .on():

Events for gamepad

The events for the gamepad objects work a little bit different. The event name, is the name of the button/direction that was activated (e.g. button0, up, etc.) And there are three functions that can be used to associate event handlers for them in different situations:

  • .on(): triggered every cycle, while the button/joystick is pressed/active.
  • .before(): triggered the first cycle that a button/joystick is pressed.
  • .after(): triggered the first cycle after a button/joystick stopped being pressed.

All three functions can be chained and allow two parameters: the first one is the button/direction that was activated, and the second parameter is the callback function. Example:

gamepad.on('button0',     () => { console.log('Button 0 still pressed...'); })
       .before('button0', () => { console.log('Button 0 pressed...');       })
       .after('button0',  () => { console.log('Button 0 was released';      });

To see the event flow and how the different events are lined-up and interact with each other, visit the Event Flow wikipage.

Thisus

These are the events that can be passed as first parameter to the event functions:

These names are not arbitrary. They match the buttons and axes described in the W3C Gamepad API specicification:

https://github.com/alvaromontoro/gamecontroller.js/blob/master/public/gamepad.svg

Browser Support

| Edge Logo 32x32Edge | Firefox LogoFirefox | ChromeChrome | Safari LogoSafari | Opera logoOpera | | ---- | ------- | ------ | ------ | ----- | | 12+ | 29+ | 25+ | 10.1+ | 24+ |

Examples

The examples folder contains different examples to showcase how to use the library:

  • Connectivity: shows how to detect if a gamepad was connected/disconnected.
  • Buttons and Joysticks: see how the buttons from your gamepad map to the default gamepad.
  • SNES Controller: replica of a SNES controller (based on a previous CodePen demo).
  • Alvanoid: small Arkanoid-based game (based on a previous CodePen demo).
  • Pong: multiplayer demo with the classic game Pong for 2 players on 2 gamepads.