Game controls done right.
What is Contro?
Contro is a library that offers simple abstractions on top of existing Web input APIs and allows game developers to easily implement controls for keyboard, mouse and gamepad.
Installation
The easiest way to include Contro in your application is using the unpkg
CDN:
If you're using npm
, you can also install it using npm i contro
.
Usage
- Import the Contro classes and functions using Object destructuring.
const Mouse Keyboard Gamepad or and = Contro// OR
- Create instances of the components you want to use.
const keyboard = const gamepad =
- Prepare the controls using the control methods and the operator functions
and
andor
.
const controls = jump: menu: inventory: map: statusOverlay:
- In your game loop, display the relevant
.label
's and '.query()
the controls.
{ // Update the UI to reflect the player's input device(s) gamejumpButtontext = controlsjumplabel gamemenuButtontext = controlsmenulabel // ... // Query the controls and do something if controlsjump gameplayer if controlsmenu game gamestatusOverlayvisible = controlsstatusOverlay // ... }
Note that all of the code starting with game.
is fictional and just serves as an example.
this demo!
Not convinced yet? Check outWanna learn more? Check out the docs!