-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinputHandler.js
More file actions
31 lines (28 loc) · 876 Bytes
/
inputHandler.js
File metadata and controls
31 lines (28 loc) · 876 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Input Handler: Tracks keyboard and mouse
class InputHandler {
constructor(canvas) {
this.keys = {};
this.mouse = { x: 0, y: 0 };
this.mouseDown = false;
window.addEventListener('keydown', (e) => {
this.keys[e.key.toLowerCase()] = true;
});
window.addEventListener('keyup', (e) => {
this.keys[e.key.toLowerCase()] = false;
});
canvas.addEventListener('mousemove', (e) => {
const rect = canvas.getBoundingClientRect();
this.mouse.x = e.clientX - rect.left;
this.mouse.y = e.clientY - rect.top;
});
canvas.addEventListener('mousedown', () => {
this.mouseDown = true;
});
canvas.addEventListener('mouseup', () => {
this.mouseDown = false;
});
}
isKeyDown(key) {
return !!this.keys[key.toLowerCase()];
}
}