-
Notifications
You must be signed in to change notification settings - Fork 0
/
pannable-mixin.html
67 lines (55 loc) · 1.75 KB
/
pannable-mixin.html
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
<script>
const PannableMixin = ((superClass) => class extends superClass {
constructor() {
super();
this._panningData = {
element: this,
isPanning: false,
startPanX: 0,
startPanY: 0,
netPanX: 0,
netPanY: 0
};
this.addEventListener('mousedown', this._panningStart.bind(this));
this.addEventListener('mouseup', this._panningEnd.bind(this));
this.addEventListener('mousemove', this._panningEvent.bind(this));
}
setPanningElement(element) {
if(element) {
this._panningData.element = element;
}
}
resetPanning() {
this._panningData.startPanX = 0;
this._panningData.startPanY = 0;
this._panningData.netPanX = 0;
this._panningData.netPanY = 0;
this._panElement();
}
_panElement() {
this._panningData.element.style.transform = `translate(${this._panningData.netPanX}px, ${this._panningData.netPanY}px)`;
}
_panningStart(e) {
this._panningData.isPanning = true;
this._panningData.startPanX = e.pageX;
this._panningData.startPanY = e.pageY;
}
_panningEnd(e) {
this._panningData.isPanning = false;
this._panningData.startPanX = 0;
this._panningData.startPanY = 0;
}
_panningEvent(e) {
if(!this._panningData.isPanning) {
return;
}
// Pan
this._panningData.netPanX += e.pageX - this._panningData.startPanX;
this._panningData.netPanY += e.pageY - this._panningData.startPanY;
this._panElement();
// Update starting position for next calculation
this._panningData.startPanX = e.pageX;
this._panningData.startPanY = e.pageY;
}
});
</script>