A JavaScript input handling library for animation or game loops
npm install eylem
// or
yarn add eylem
import Eylem from 'eylem';
const inputs = new Eylem(document, ['horizantal', 'vertical', 'fire']);
//KeyCode 65:A, 68:D, 83:S, 87:W
inputs.bindInputMap(Eylem.KEY_DOWN, {
65: { action: 'horizantal', value: -1 },
68: { action: 'horizantal', value: +1 },
83: { action: 'vertical', value: -1 },
87: { action: 'vertical', value: +1 },
});
// 0 : Left Mouse Button
inputs.bindInputMap(Eylem.MOUSE_DOWN, {
0: {action : 'fire', value : 1}
});
inputs.watchMouse();
// in a Animation Loop
function step() {
const mouseLeft = inputs.getValue('action'); // 0 or 1
const horizantal = inputs.getValue('horizantal'); // -1 or 0 or 1
const mouseEvent = inputs.mouse; // MouseEvent Object
const {offsetX, screenX, movementX} = mouseEvent;
// clear the inputs at the end
inputs.clear();
window.requestAnimationFrame(step);
}
Licensed under the MIT license.