controls
Input controls (Screen, Mouse, Touchpad, Keyboard, Gamepad)
๐ 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"),
InputCombination.key("ArrowRight")
);
if (dashCombo.evaluate(input)) {
console.log("dash!");
}๐ API โ
- Input
- 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, and DOM event dispatch. They use headless adapters, so event benchmarks report getBoundingClientRect() call counts instead of browser layout timings.
npm run bench -w @jolly-pixel/controlsUse -- --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:
$ npm run test
$ npm run lintCAUTION
In case you introduce a new feature or fix a bug, make sure to include tests for it as well.
๐ License โ
MIT