@jolly-pixel/controls
v2.1.0
Published
Input controls (Screen, Mouse, Touchpad, Keyboard, Gamepad)
Readme
💃 Getting Started
This package is available in the Node Package Repository and can be easily installed with npm or yarn.
$ npm i @jolly-pixel/controls
# or
$ yarn add @jolly-pixel/controls👀 Usage example
import { Input } from "@jolly-pixel/controls";
const canvas = document.querySelector("canvas");
if (!canvas) {
throw new Error("No canvas element found");
}
const input = new Input(canvas);
input.connect();
function gameLoop() {
input.update();
if (input.keyboard.wasJustPressed("Space")) {
console.log("Jump!");
}
if (input.mouse.isDown("left")) {
const delta = input.mouse.viewportDelta(true);
console.log("Dragging", delta.x, delta.y);
}
requestAnimationFrame(gameLoop);
}
gameLoop();For advanced input combinations:
import { InputCombination } from "@jolly-pixel/controls";
const dashCombo = InputCombination.all(
InputCombination.key("ShiftLeft", "down"),
InputCombination.key("ArrowRight")
).bind(input);
if (dashCombo()) {
console.log("dash!");
}📚 API
- Input
- AxisMap: named scalar axes built from keys, mouse buttons, and gamepad sticks.
- InputCombination: composable input conditions for chords, alternatives, exclusions, and sequences.
- InputActionQuery: dispatch helper for
"ANY","NONE", and concrete actions.
🧪 Benchmarks
The suites cover device updates, state queries, input combinations, axis
resolution, and DOM event dispatch. They use headless adapters, so event benchmarks report
getBoundingClientRect() call counts instead of browser layout timings.
pnpm --filter @jolly-pixel/controls benchUse -- --list to inspect the suites. Filtering and measurement rules are
documented by @jolly-pixel/bench.
✨ Contributors guide
If you are a developer looking to contribute to the project, you must first read the CONTRIBUTING guide.
Once you have finished your development, check that the tests (and linter) are still good by running the following script:
$ pnpm run test
$ pnpm run lint[!CAUTION] In case you introduce a new feature or fix a bug, make sure to include tests for it as well.
📃 License
MIT
