avoid/js/InputHandler.js
2022-09-26 12:30:36 +02:00

23 lines
616 B
JavaScript

class InputHandler {
constructor(idOfWatchedArea) {
const elem = document.getElementById(idOfWatchedArea);
const handleMouse = ({ offsetX, offsetY }) =>
this.setPointerPosition([offsetX, offsetY]);
const handleTouch = ({ touches }) =>
this.setPointerPosition([touches[0].clientX, touches[0].clientY]);
elem.addEventListener("mousemove", handleMouse);
elem.addEventListener("touchmove", handleTouch);
this.pointerPosition = [0, 0];
}
setPointerPosition(newPosition) {
this.pointerPosition = newPosition;
}
get position() {
return this.pointerPosition;
}
}