zippy-game-engine
Version:
A lightweight game engine for web games
43 lines • 1.15 kB
JavaScript
export class MouseSystem {
x;
y;
buttons;
wheel;
constructor() {
this.x = 0;
this.y = 0;
this.buttons = {};
this.wheel = 0;
}
setupCanvasEvents(canvas) {
const options = { passive: false };
canvas.addEventListener("mousemove", (e) => {
const rect = canvas.getBoundingClientRect();
this.x = e.clientX - rect.left;
this.y = e.clientY - rect.top;
}, options);
canvas.addEventListener("mousedown", (e) => {
this.buttons[e.button] = true;
}, options);
canvas.addEventListener("mouseup", (e) => {
this.buttons[e.button] = false;
}, options);
canvas.addEventListener("wheel", (e) => {
this.wheel = e.deltaY;
e.preventDefault();
}, { passive: false });
}
update() {
this.wheel = 0;
}
isButtonDown(button) {
return this.buttons[button] || false;
}
getPosition() {
return { x: this.x, y: this.y };
}
getWheel() {
return this.wheel;
}
}
//# sourceMappingURL=mouse-system.js.map