-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathevents.ts
87 lines (77 loc) · 1.98 KB
/
events.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
class Events {
redraw: Function;
isPan: boolean;
isDrag: boolean;
x: number;
y: number;
constructor(canvas, redraw) {
this.redraw = redraw;
document.addEventListener('wheel', this, { passive: false });
canvas.addEventListener('mousedown', this);
canvas.addEventListener('mousemove', this);
canvas.addEventListener('mouseup', this);
document.addEventListener('keyup', this);
document.addEventListener('keydown', this);
addEventListener('resize', this);
}
handleEvent(e) {
switch (e.type) {
case 'wheel':
this.zoom(e);
break;
case 'mousedown':
this.onStart(e);
break;
case 'mousemove':
this.onMove(e);
break;
case 'mouseup':
this.onEnd();
break;
case 'keyup':
this.onKeyUp();
break;
case 'keydown':
this.onKeyDown(e);
break;
case 'resize':
this.onResize();
break;
}
}
onResize() {
this.redraw('resize');
}
onKeyDown(e) {
if (e.shiftKey || e.ctrlKey) {
this.isPan = true;
}
}
onKeyUp() {
this.isPan = false;
}
onStart(e) {
this.x = e.clientX;
this.y = e.clientY;
this.isDrag = true;
}
onMove(e) {
if (this.isDrag) {
if (this.isPan) {
this.redraw('pan', [this.x, this.y], [e.clientX, e.clientY]);
} else {
this.redraw('rotate', [this.x, this.y], [e.clientX, e.clientY]);
}
this.x = e.clientX;
this.y = e.clientY;
}
}
onEnd() {
this.isDrag = false;
}
zoom(e) {
e.preventDefault();
this.redraw('zoom', e.deltaY);
}
}
export { Events };