1 | import { CoordinatesEngine } from './CoordinatesEngine'
|
---|
2 | import { wheelValues } from '../utils/events'
|
---|
3 | import { V } from '../utils/maths'
|
---|
4 | import { clampStateInternalMovementToBounds } from '../utils/state'
|
---|
5 |
|
---|
6 | export interface WheelEngine extends CoordinatesEngine<'wheel'> {
|
---|
7 | wheel(this: WheelEngine, event: WheelEvent): void
|
---|
8 | wheelChange(this: WheelEngine, event: WheelEvent): void
|
---|
9 | wheelEnd(this: WheelEngine): void
|
---|
10 | }
|
---|
11 |
|
---|
12 | export class WheelEngine extends CoordinatesEngine<'wheel'> {
|
---|
13 | ingKey = 'wheeling' as const
|
---|
14 |
|
---|
15 | wheel(event: WheelEvent) {
|
---|
16 | if (!this.state._active) this.start(event)
|
---|
17 | this.wheelChange(event)
|
---|
18 | this.timeoutStore.add('wheelEnd', this.wheelEnd.bind(this))
|
---|
19 | }
|
---|
20 |
|
---|
21 | wheelChange(event: WheelEvent) {
|
---|
22 | const state = this.state
|
---|
23 | state._delta = wheelValues(event)
|
---|
24 | V.addTo(state._movement, state._delta)
|
---|
25 |
|
---|
26 | // _movement rolls back to when it passed the bounds.
|
---|
27 | clampStateInternalMovementToBounds(state)
|
---|
28 |
|
---|
29 | this.compute(event)
|
---|
30 | this.emit()
|
---|
31 | }
|
---|
32 |
|
---|
33 | wheelEnd() {
|
---|
34 | if (!this.state._active) return
|
---|
35 | this.state._active = false
|
---|
36 | this.compute()
|
---|
37 | this.emit()
|
---|
38 | }
|
---|
39 |
|
---|
40 | bind(bindFunction: any) {
|
---|
41 | bindFunction('wheel', '', this.wheel.bind(this))
|
---|
42 | }
|
---|
43 | }
|
---|