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

gamepadder

v0.5.2

Published

JavaScript class for dealing with HTML 5 GamePad API

Downloads

5

Readme

#gamepadder 0.5.2

gamepadder is a basic JavaScript class for dealing with the intricacies of the HTML 5 Gamepad API.

It currently has fully supported mappings, button naming, and controller naming for these controllers:

  • Sony Dual Shock 4
  • Sony Dual Shock 3
  • XBox 360 Controller

Future updates will add more controller support and also provide mechanisms for more easily checking for button presses. Currently, you need to call the GamePadder.checkForButtonPress() method in your game's loop and pass it the gamepad object that you want to check for button presses. This will be updated to allow a single gamepad method to be used to check every connected gamepad for button presses.

Using

You can use Gamepadder like this:

import { Gamepadder, GamepadderUtils } from 'gamepadder';

const numberOfPlayers = 1;

class Game {
  init() {
    if (!('ongamepadconnected' in window)) {
      //No gamepad events available, poll instead.
      this.checkForGamePadsInterval = setInterval(this.pollGamepads.bind(this), 500);
    }

    this.addListeners();
  }

  pollGamepads() {
    const gamepads = GamepadderUtils.getGamepads();

    if(gamepads.length) {
      gamepads.forEach((pad, index) => {
        game.controllers[index] =  new Gamepadder(pad);
      });

      if(gamepads.length === numberOfPlayers) {
        clearInterval(game.checkForGamePadsInterval);
      }
    }
  }

  addListeners() {
    this.controllerConnectedEvent = window.addEventListener('gamepadconnected', (e) => {
      console.log('gamepad connected');

      const controller = new Gamepadder(e.gamepad);

      if(!this.controllers[controller.id]) {
        this.controllers[controller.id] = {
          controller,
          buttonMap
        };
      }

      this.controllerDisconnectedEvent = window.addEventListener('gamepaddisconnected', (e) => {
        console.log('gamepad disconnected');
        this.controllers.splice(e.gamepad.id, 1);
      });
    });
  }

  tick(event) {
    const gamepads = this.getGamepads();

    this.controllers.forEach((controller, index) => {
      let buttonPresses;
      let previousButtons;

      const buttonPressObject = controller.checkForButtonPress(gamepads[index]);

      buttonPresses = buttonPressObject.buttonPresses;
      previousButtons = buttonPressObject.previousButtons;

      //logic to respond to button presses here
    });
  }
};

document.addEventListener('DOMContentLoaded', () => {
  const game = new Game();

  game.init();
})

##Contributing

Gamepadder needs your help!

The more controller mappings we can generate, the better. If you have a controller or any input mapping software that you use, please head to the HTML5 Gamepad Tester and then fill out the template below:

{
  vendor: '054c', //found after the "Vendor:" in the controller name
  product: '05c4', //found after the "Product:" in the controller name
  name: 'Dual Shock 4', //name of the controller, if it doesn't have a specific name, use the manufacturer and abbreviated model number like Logitech F310
  buttonMap: [
    'X', //What controller button corresponds to B0?
    'Circle', //What controller button corresponds to B1?
    'Square', //What controller button corresponds to B2?
    'Triangle', //What controller button corresponds to B3?
    //... and continue until you've mapped all the buttons
  ],
  stickMap: [
    'LX', //Which stick and axis is Axis 0?
    'LY', //Which stick and axis is Axis 1?
    'RX', //Which stick and axis is Axis 2?
    'RY' //Which stick and axis is Axis 3?
    //.. and continue until you've mapped all the analog sticks
  ]
}

If you are comfortable with GitHub and Pull Requests, feel encouraged to submit button mapping PRs with updates to mappings/typemap.js and adding a new button map class for your controller to the mappings directory.

If you aren't comfortable with GitHub and Pull Requests, feel free to add the filled out template above to a GitHub Issue and we'll get your controller added.