-
Notifications
You must be signed in to change notification settings - Fork 170
/
mouse-observer.ts
executable file
·497 lines (458 loc) · 16 KB
/
mouse-observer.ts
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/**
* @file Mouse Observer
* @author Alexander Rose <alexander.rose@weirdbyte.de>
* @private
*/
import { Vector2 } from 'three'
import { Signal } from 'signals'
import { LeftMouseButton, RightMouseButton } from '../constants'
import { defaults } from '../utils'
import Viewer from '../viewer/viewer'
import MouseControls from '../controls/mouse-controls'
type Optional<T, K extends keyof T> = Pick<Partial<T>, K> & Omit<T, K>;
/**
* @example
* mouseObserver.signals.scrolled.add( function( delta ){ ... } );
*
* @typedef {Object} MouseSignals
* @property {Signal<Integer, Integer>} moved - on move: deltaX, deltaY
* @property {Signal<Number>} scrolled - on scroll: delta
* @property {Signal<Integer, Integer>} dragged - on drag: deltaX, deltaY
* @property {Signal} dropped - on drop
* @property {Signal} clicked - on click
* @property {Signal} hovered - on hover
*/
function getTouchDistance (event: TouchEvent) {
const dx = event.touches[ 0 ].pageX - event.touches[ 1 ].pageX
const dy = event.touches[ 0 ].pageY - event.touches[ 1 ].pageY
return Math.sqrt(dx * dx + dy * dy)
}
function getMouseButtons (event: MouseEvent) {
if (typeof event === 'object') {
if ('buttons' in event) {
return event.buttons
} else if ('which' in event as any) {
const b = (event as any).which
if (b === 2) {
return 4
} else if (b === 3) {
return 2
} else if (b > 0) {
return 1 << (b - 1)
}
} else if ('button' in event as any) {
const b = (event as any).button
if (b === 1) {
return 4
} else if (b === 2) {
return 2
} else if (b >= 0) {
return 1 << b
}
}
}
return 0
}
export interface MouseSignals {
moved: Signal // on move: deltaX, deltaY
scrolled: Signal // on scroll: delta
dragged: Signal // on drag: deltaX, deltaY
dropped: Signal // on drop
clicked: Signal // on click
hovered: Signal // on hover
doubleClicked: Signal
}
export interface MouseParams {
hoverTimeout?: number
handleScroll?:boolean
doubleClickSpeed?: number
}
/**
* Mouse observer
*
* @example
* // listen to mouse moving (and touch-moving) events
* mouseObserver.moved.moved.add( function( deltaX, deltaY ){ ... } );
*
* @example
* // listen to scrolling (and pinching) events
* mouseObserver.signals.scrolled.add( function( delta ){ ... } );
*
* @example
* // listen to dragging (and touch-dragging) events
* mouseObserver.signals.dragged.add( function( deltaX, deltaY ){ ... } );
*
* @example
* // listen to clicking (and tapping) events
* mouseObserver.signals.clicked.add( function(){ ... } );
*
* @example
* // listen to double clicking (and double tapping) events
* mouseObserver.signals.doubleClicked.add( function(){ ... } );
*
* @example
* // listen to hovering events
* mouseObserver.signals.hovered.add( function(){ ... } );
*/
class MouseObserver {
signals: MouseSignals = {
moved: new Signal(),
scrolled: new Signal(),
dragged: new Signal(),
dropped: new Signal(),
clicked: new Signal(),
hovered: new Signal(),
doubleClicked: new Signal()
}
hoverTimeout: number
handleScroll:boolean
doubleClickSpeed: number
viewer: Viewer
mouse: MouseObserver
controls: MouseControls
position = new Vector2() // Position on page
prevPosition = new Vector2() // Previous position on page
down = new Vector2() // Position on page when clicked
canvasPosition = new Vector2() // Position on dom element
prevClickCP = new Vector2()
moving = false // Flag indicating if the mouse is moving
hovering = true // Flag indicating if the mouse is hovering
scrolled = false // Flag indicating if there was a scolling event since the last mouse move
lastMoved = Infinity // Timestamp of last mouse move
which? = 0 // 0: No button; 1: Left button; 2: Middle button; 3: Right button
buttons? = 0 // 0: No button; 1: Left button; 2: Right button; 4: Middle button
pressed? = false // Flag indicating if the mouse is pressed down
altKey = false // Flag indicating if the alt key is pressed
ctrlKey = false // Flag indicating if the ctrl key is pressed
metaKey = false // Flag indicating if the meta key is pressed
shiftKey = false // Flag indicating if the shift key is pressed
doubleClickPending: boolean
lastClicked: number
overElement: boolean
lastTouchDistance: number
private frameRequest: number
/**
* @param {Element} domElement - the dom element to observe mouse events in
* @param {Object} params - parameters object
* @param {Integer} params.hoverTimeout - timeout in ms until the {@link MouseSignals.hovered}
* signal is fired, set to -1 to ignore hovering
* @param {Boolean} params.handleScroll - whether or not to handle scroll events
* @param {Integer} params.doubleClickSpeed - max time in ms to trigger double click
*/
constructor (readonly domElement: HTMLCanvasElement, params: MouseParams = {}) {
this.domElement.style.touchAction = 'none'
this.hoverTimeout = defaults(params.hoverTimeout, 50)
this.handleScroll = defaults(params.handleScroll, true)
this.doubleClickSpeed = defaults(params.doubleClickSpeed, 500)
this._listen = this._listen.bind(this)
this._onMousewheel = this._onMousewheel.bind(this)
this._onMousemove = this._onMousemove.bind(this)
this._onMousedown = this._onMousedown.bind(this)
this._onMouseup = this._onMouseup.bind(this)
this._onContextmenu = this._onContextmenu.bind(this)
this._onTouchstart = this._onTouchstart.bind(this)
this._onTouchend = this._onTouchend.bind(this)
this._onTouchmove = this._onTouchmove.bind(this)
this._listen()
const opt = { passive: false } // treat as 'passive' so preventDefault can be called
document.addEventListener('mousewheel', this._onMousewheel, opt)
document.addEventListener('wheel', this._onMousewheel, opt)
document.addEventListener('MozMousePixelScroll', this._onMousewheel, opt)
document.addEventListener('mousemove', this._onMousemove, opt)
document.addEventListener('mousedown', this._onMousedown, opt)
document.addEventListener('mouseup', this._onMouseup, opt)
document.addEventListener('contextmenu', this._onContextmenu, opt)
document.addEventListener('touchstart', this._onTouchstart, opt)
document.addEventListener('touchend', this._onTouchend, opt)
document.addEventListener('touchmove', this._onTouchmove, opt)
}
get key () {
let key = 0
if (this.altKey) key += 1
if (this.ctrlKey) key += 2
if (this.metaKey) key += 4
if (this.shiftKey) key += 8
return key
}
setParameters (params: MouseParams = {}) {
this.hoverTimeout = defaults(params.hoverTimeout, this.hoverTimeout)
}
/**
* listen to mouse actions
* @emits {MouseSignals.clicked} when clicked
* @emits {MouseSignals.hovered} when hovered
* @return {undefined}
*/
_listen () {
const now = window.performance.now()
const cp = this.canvasPosition
if (this.doubleClickPending && now - this.lastClicked > this.doubleClickSpeed) {
this.doubleClickPending = false
}
if (now - this.lastMoved > this.hoverTimeout) {
this.moving = false
}
if (this.scrolled || (!this.moving && !this.hovering)) {
this.scrolled = false
if (this.hoverTimeout !== -1 && this.overElement) {
this.hovering = true
this.signals.hovered.dispatch(cp.x, cp.y)
}
}
this.frameRequest = window.requestAnimationFrame(this._listen)
}
/**
* handle mouse scroll
* @emits {MouseSignals.scrolled} when scrolled
* @param {Event} event - mouse event
* @return {undefined}
*/
_onMousewheel(event: Optional<WheelEvent, 'detail'> & { wheelDelta?: number, wheelDeltaY?: number }) {
if (event.target !== this.domElement || !this.handleScroll) {
return
}
event.preventDefault()
this._setKeys(event as any)
let delta = 0
// This has to be written in a particular way to handle old browsers that
// all send events with different properties set in different ways.
if ('deltaY' in event && 'deltaMode' in event &&
event.deltaY !== undefined && event.deltaMode !== undefined) {
// all modern browsers, using WheelEvent; deltaY + down (toward user)
if (event.deltaMode === WheelEvent.DOM_DELTA_PIXEL)
// everything except Firefox: normally 100 per wheel click
delta = -event.deltaY * (2.5 / 100.0)
else if (event.deltaMode === WheelEvent.DOM_DELTA_LINE)
// Firefox in line mode, normally 3 per wheel click
delta = -event.deltaY * (2.5 / 3.0)
else // page mode: 1 per wheel click
delta = -event.deltaY * 2.5
} else if ('deltaY' in event && !('detail' in event)) {
// Old Firefox or IE 11: deltaY but no deltaMode; treat as pixels
delta = -event.deltaY * (2.5 / 100.0)
} else if (event.wheelDelta !== undefined) {
delta = -event.wheelDelta * (2.5 / 100)
} else if (event.wheelDeltaY !== undefined) {
delta = -event.wheelDeltaY * (2.5 / 100)
} else if (event.detail !== undefined){
// Old Firefox, MouseWheelEvent
delta = -event.detail / 3
}
this.signals.scrolled.dispatch(delta)
setTimeout(() => {
this.scrolled = true
}, this.hoverTimeout)
}
/**
* handle mouse move
* @emits {MouseSignals.moved} when moved
* @emits {MouseSignals.dragged} when dragged
* @param {Event} event - mouse event
* @return {undefined}
*/
_onMousemove (event: MouseEvent) {
if (event.target === this.domElement) {
event.preventDefault()
this.overElement = true
} else {
this.overElement = false
}
this._setKeys(event)
this.moving = true
this.hovering = false
this.lastMoved = window.performance.now()
this.prevPosition.copy(this.position)
this.position.set(event.clientX, event.clientY)
this._setCanvasPosition(event)
const dx = this.prevPosition.x - this.position.x
const dy = this.prevPosition.y - this.position.y
this.signals.moved.dispatch(dx, dy)
if (this.pressed) {
this.signals.dragged.dispatch(dx, dy)
}
}
_onMousedown (event: MouseEvent) {
if (event.target !== this.domElement) {
return
}
event.preventDefault()
this._setKeys(event)
this.moving = false
this.hovering = false
this.down.set(event.clientX, event.clientY)
this.position.set(event.clientX, event.clientY)
this.which = event.which
this.buttons = getMouseButtons(event)
this.pressed = true
this._setCanvasPosition(event)
}
/**
* handle mouse up
* @emits {MouseSignals.doubleClicked} when double clicked
* @emits {MouseSignals.dropped} when dropped
* @param {Event} event - mouse event
* @return {undefined}
*/
_onMouseup (event: MouseEvent) {
if (event.target === this.domElement) {
event.preventDefault()
}
this._setKeys(event)
const cp = this.canvasPosition
if (this._distance() < 4) {
this.lastClicked = window.performance.now()
if (this.doubleClickPending && this.prevClickCP.distanceTo(cp) < 4) {
this.signals.doubleClicked.dispatch(cp.x, cp.y)
this.doubleClickPending = false
}
this.signals.clicked.dispatch(cp.x, cp.y)
this.doubleClickPending = true
this.prevClickCP.copy(cp)
}
this.which = undefined
this.buttons = undefined
this.pressed = undefined
// if (this._distance() > 3 || event.which === RightMouseButton) {
// this.signals.dropped.dispatch();
// }
}
_onContextmenu (event: MouseEvent) {
if (event.target === this.domElement) {
event.preventDefault()
}
}
_onTouchstart (event: TouchEvent) {
if (event.target !== this.domElement) {
return
}
event.preventDefault()
this.pressed = true
switch (event.touches.length) {
case 1: {
this.moving = false
this.hovering = false
this.down.set(
event.touches[ 0 ].pageX,
event.touches[ 0 ].pageY
)
this.position.set(
event.touches[ 0 ].pageX,
event.touches[ 0 ].pageY
)
this._setCanvasPosition(event.touches[ 0 ])
break
}
case 2: {
this.down.set(
(event.touches[ 0 ].pageX + event.touches[ 1 ].pageX) / 2,
(event.touches[ 0 ].pageY + event.touches[ 1 ].pageY) / 2
)
this.position.set(
(event.touches[ 0 ].pageX + event.touches[ 1 ].pageX) / 2,
(event.touches[ 0 ].pageY + event.touches[ 1 ].pageY) / 2
)
this.lastTouchDistance = getTouchDistance(event)
}
}
}
_onTouchend (event: TouchEvent) {
if (event.target === this.domElement) {
event.preventDefault()
}
this.which = undefined
this.buttons = undefined
this.pressed = undefined
}
_onTouchmove (event: TouchEvent) {
if (event.target === this.domElement) {
event.preventDefault()
this.overElement = true
} else {
this.overElement = false
}
switch (event.touches.length) {
case 1: {
this._setKeys(event)
this.which = LeftMouseButton
this.buttons = 1
this.moving = true
this.hovering = false
this.lastMoved = window.performance.now()
this.prevPosition.copy(this.position)
this.position.set(
event.touches[ 0 ].pageX,
event.touches[ 0 ].pageY
)
this._setCanvasPosition(event.touches[ 0 ])
const dx = this.prevPosition.x - this.position.x
const dy = this.prevPosition.y - this.position.y
this.signals.moved.dispatch(dx, dy)
if (this.pressed) {
this.signals.dragged.dispatch(dx, dy)
}
break
}
case 2: {
const touchDistance = getTouchDistance(event)
const delta = touchDistance - this.lastTouchDistance
this.lastTouchDistance = touchDistance
this.prevPosition.copy(this.position)
this.position.set(
(event.touches[ 0 ].pageX + event.touches[ 1 ].pageX) / 2,
(event.touches[ 0 ].pageY + event.touches[ 1 ].pageY) / 2
)
if (Math.abs(delta) > 2 && this.handleScroll &&
this.position.distanceTo(this.prevPosition) < 2
) {
this.which = 0
this.buttons = 0
this.signals.scrolled.dispatch(delta / 2)
} else {
this.which = RightMouseButton
this.buttons = 2
const dx = this.prevPosition.x - this.position.x
const dy = this.prevPosition.y - this.position.y
this.signals.moved.dispatch(dx, dy)
if (this.pressed) {
this.signals.dragged.dispatch(dx, dy)
}
}
}
}
}
_distance () {
return this.position.distanceTo(this.down)
}
_setCanvasPosition (event: any) { // TODO
const box = this.domElement.getBoundingClientRect()
let offsetX, offsetY;
if ('clientX' in event && 'clientY' in event) {
offsetX = event.clientX - box.left
offsetY = event.clientY - box.top
} else {
offsetX = event.offsetX
offsetY = event.offsetY
}
this.canvasPosition.set(offsetX, box.height - offsetY)
}
_setKeys (event: MouseEvent|TouchEvent) {
this.altKey = event.altKey
this.ctrlKey = event.ctrlKey
this.metaKey = event.metaKey
this.shiftKey = event.shiftKey
}
dispose () {
document.removeEventListener('mousewheel', this._onMousewheel)
document.removeEventListener('wheel', this._onMousewheel)
document.removeEventListener('MozMousePixelScroll', this._onMousewheel)
document.removeEventListener('mousemove', this._onMousemove)
document.removeEventListener('mousedown', this._onMousedown)
document.removeEventListener('mouseup', this._onMouseup)
document.removeEventListener('contextmenu', this._onContextmenu)
document.removeEventListener('touchstart', this._onTouchstart)
document.removeEventListener('touchend', this._onTouchend)
document.removeEventListener('touchmove', this._onTouchmove)
window.cancelAnimationFrame(this.frameRequest)
}
}
export default MouseObserver