-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.mouse-movement.ts
44 lines (37 loc) · 1008 Bytes
/
plugin.mouse-movement.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
/**
* Internal mouse movement plugin to add mouse movement functionality to the core.
* Scope:
* - Capture the mouse movement
* @extends PluginBuilder
*/
import { BaseOptions, BindResult } from '../../types'
import { PluginBuilder } from '../core'
export default class MouseMovementPlugin extends PluginBuilder {
override key: string = 'mouse-movement'
override bind(_: BaseOptions): BindResult[] {
return [
{
name: 'mouse-movement',
target: document,
event: 'mousemove',
callback: (event: MouseEvent) => {
return this.captureEvent(event)
},
throttle: 100,
condition: () => {
// Skip the event if the touch is active
return !('ontouchstart' in window)
}
}
]
}
/**
* A function to capture the mouse movement events on your site.
*/
private captureEvent(event: MouseEvent): Record<string, any> {
return {
x: event.clientX,
y: event.clientY
}
}
}