-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathmouse-event.js
153 lines (142 loc) · 4.46 KB
/
mouse-event.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import { MOUSEBUTTON_NONE } from './constants.js';
/**
* Returns true if pointer lock is currently enabled.
*
* @returns {boolean} True if pointer lock is currently enabled.
* @ignore
*/
function isMousePointerLocked() {
return !!(document.pointerLockElement || document.mozPointerLockElement || document.webkitPointerLockElement);
}
/**
* MouseEvent object that is passed to events 'mousemove', 'mouseup', 'mousedown' and 'mousewheel'.
*/
class MouseEvent {
/**
* Create a new MouseEvent instance.
*
* @param {import('./mouse.js').Mouse} mouse - The Mouse device that is firing this event.
* @param {globalThis.MouseEvent} event - The original browser event that fired.
*/
constructor(mouse, event) {
let coords = {
x: 0,
y: 0
};
if (event) {
if (event instanceof MouseEvent) {
throw Error('Expected MouseEvent');
}
coords = mouse._getTargetCoords(event);
} else {
event = { };
}
if (coords) {
/**
* The x coordinate of the mouse pointer relative to the element {@link Mouse} is
* attached to.
*
* @type {number}
*/
this.x = coords.x;
/**
* The y coordinate of the mouse pointer relative to the element {@link Mouse} is
* attached to.
*
* @type {number}
*/
this.y = coords.y;
} else if (isMousePointerLocked() || event.type === 'mouseout') {
this.x = 0;
this.y = 0;
} else {
return;
}
/**
* A value representing the amount the mouse wheel has moved, only valid for
* {@link mousewheel} events.
*
* @type {number}
*/
this.wheelDelta = 0;
// deltaY is in a different range across different browsers. The only thing
// that is consistent is the sign of the value so snap to -1/+1.
if (event.type === 'wheel') {
if (event.deltaY > 0) {
this.wheelDelta = 1;
} else if (event.deltaY < 0) {
this.wheelDelta = -1;
}
}
// Get the movement delta in this event
if (isMousePointerLocked()) {
/**
* The change in x coordinate since the last mouse event.
*
* @type {number}
*/
this.dx = event.movementX || event.webkitMovementX || event.mozMovementX || 0;
/**
* The change in y coordinate since the last mouse event.
*
* @type {number}
*/
this.dy = event.movementY || event.webkitMovementY || event.mozMovementY || 0;
} else {
this.dx = this.x - mouse._lastX;
this.dy = this.y - mouse._lastY;
}
if (event.type === 'mousedown' || event.type === 'mouseup') {
/**
* The mouse button associated with this event. Can be:
*
* - {@link MOUSEBUTTON_LEFT}
* - {@link MOUSEBUTTON_MIDDLE}
* - {@link MOUSEBUTTON_RIGHT}
*
* @type {number}
*/
this.button = event.button;
} else {
this.button = MOUSEBUTTON_NONE;
}
this.buttons = mouse._buttons.slice(0);
/**
* The element that the mouse was fired from.
*
* @type {Element}
*/
this.element = event.target;
/**
* True if the ctrl key was pressed when this event was fired.
*
* @type {boolean}
*/
this.ctrlKey = event.ctrlKey || false;
/**
* True if the alt key was pressed when this event was fired.
*
* @type {boolean}
*/
this.altKey = event.altKey || false;
/**
* True if the shift key was pressed when this event was fired.
*
* @type {boolean}
*/
this.shiftKey = event.shiftKey || false;
/**
* True if the meta key was pressed when this event was fired.
*
* @type {boolean}
*/
this.metaKey = event.metaKey || false;
/**
* The original browser event.
*
* @type {globalThis.MouseEvent}
*/
this.event = event;
}
}
export { isMousePointerLocked, MouseEvent };