-
Notifications
You must be signed in to change notification settings - Fork 0
/
mouse.js
56 lines (49 loc) · 1.11 KB
/
mouse.js
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class MouseListener {
mx;
my;
ox;
oy;
get dx() {
return this.mx-this.ox;
}
get dy() {
return this.my-this.oy;
}
leftButtonDown;
leftButtonPressed;
leftButtonReleased;
constructor() {
this.mx=0;
this.my=0;
this.leftButtonDown=false;
this.leftButtonPressed=false;
this.leftButtonReleased=false;
}
/**
* Attach event listeners to a canvas. You should probably only run this once per MouseListener
* @param {HTMLCanvasElement canvas The canvas to attach to
*/
attach(canvas) {
canvas.addEventListener('mousemove',(ev)=>{
this.mx=ev.offsetX;
this.my=ev.offsetY;
},true);
canvas.addEventListener("mousedown",(ev)=>{
this.leftButtonDown=true;
this.leftButtonPressed=true;
});
window.addEventListener("mouseup",(ev)=>{
this.leftButtonDown=false;
this.leftButtonReleased=true;
});
}
/**
* Run this function after one frame, this clears keysPressed, and keysReleased which depend on the current frame.
*/
afterUpdate() {
this.leftButtonPressed=false;
this.leftButtonReleased=false;
this.ox=this.mx;
this.oy=this.my;
}
}