-
Notifications
You must be signed in to change notification settings - Fork 59
/
event.js
38 lines (35 loc) · 955 Bytes
/
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
/**
* @author muwoo
* Date: 2018/7/11
*/
import {constants} from './utils'
export class Event {
constructor (ctx) {
this.ctx = ctx || null
this.stack = []
this.eventHandler = this.eventHandler || this.emit.bind(this)
}
init (canvas) {
if (constants.IN_BROWSER) {
canvas.removeEventListener('click', this.eventHandler)
canvas.removeEventListener('mousedown', this.eventHandler)
canvas.removeEventListener('mouseup', this.eventHandler)
canvas.addEventListener('click', this.eventHandler)
canvas.addEventListener('mousedown', this.eventHandler)
canvas.addEventListener('mouseup', this.eventHandler)
}
}
addEvent (shape, fns) {
this.stack.push({
shape: shape,
handler: fns
})
}
emit (point) {
this.stack.forEach((item) => {
if (item.handler[point.type] && item.shape.isInPath(point)) {
item.handler[point.type](point, item)
}
})
}
}