Giter VIP home page Giter VIP logo

jpad-components's Introduction

<jpad-components>

NPM Version GitHub License

A set of web-components to build declarative virtual gamepad layouts.

The aim of this project is to enable rapid prototyping and to provide a simple way of implementing touch / keyboard inputs without having to write custom UI code.

The jpad-components are designed to work with any framework and game engine, you can use the jpad-controller API directly in your game loop or listen to the controller, trackpad or button events as needed.

Installation

jpad-components is available on npm:

npm i jpad-components

Usage

Import the components separately or as a bundle:

// import as a bundle
import 'jpad-components'; // Load all elements in one go

// or just the components you need
import 'jpad-components/jpad-controller.js'; // Layout controller and simple API
import 'jpad-components/jpad-tile.js'; // Create familiar button layouts
import 'jpad-components/jpad-button.js'; // Handle input as a button or trigger
import 'jpad-components/jpad-trackpad.js'; // Handle movement in any direction
<!-- available on CDN too -->
<head>
  ...
  <script src="https://esm.run/jpad-components" type="module"></script>
</head>

The components API is documented here.

Here's a minimal example:

<!-- Define the layout -->
<jpad-controller>
    <jpad-trackpad
      name="movement"
      upkeys="ArrowUp, KeyW"
      downkeys="ArrowDown, KeyS"
      leftkeys="ArrowLeft, KeyA"
      rightkeys="ArrowRight, KeyD"
      slot="left"
    ></jpad-trackpad>

    <jpad-tile slot="right">
      <jpad-button passby name="jump" keys="Z">Z</jpad-button>
      <jpad-button passby name="attack" keys="X">X</jpad-button>
    </jpad-button>
</jpad-controller>

<script>
  // Get the element
  const jpad = document.querySelector('jpad-controller');

  // Use it with the input api
  jpad.getAxis('movement');
  jpad.isPressed('attack');
  jpad.isJustPressed('attack');

  // You can also listen for the events
  jpad.addEventListener('buttontrigger', (e) => {
    console.log(e.detail.name, e.detail.pressed);
  });
  jpad.addEventListener('trackpadmove', (e) => {
    console.log(e.detail.name, e.detail.axis);
  });
</script>

Some examples in the wild

Here's some demos that use jpad-components to handle their inputs:

License

This project is released under the MIT license.

Contributions

Any contribution or feedback is warmly welcomed, this project has been tested on a limited set of devices and browsers and might display some bugs.

If you need support or found a bug

If you need and help or have found a bug, feel free to leave an issue or DM me on Nostr.

jpad-components's People

Contributors

nostrangerdev avatar

Watchers

 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.