Roam2DControl.js
4.5 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import Base from 'claygl/src/core/Base';
import retrieve from './retrieve';
/**
* @alias module:echarts-gl/util/Roam2DControl
*/
var Roam2DControl = Base.extend(function () {
return {
/**
* @type {module:zrender~ZRender}
*/
zr: null,
/**
* @type {module:echarts-gl/core/ViewGL}
*/
viewGL: null,
minZoom: 0.2,
maxZoom: 5,
_needsUpdate: false,
_dx: 0,
_dy: 0,
_zoom: 1
};
}, function () {
// Each Roam2DControl has it's own handler
this._mouseDownHandler = this._mouseDownHandler.bind(this);
this._mouseWheelHandler = this._mouseWheelHandler.bind(this);
this._mouseMoveHandler = this._mouseMoveHandler.bind(this);
this._mouseUpHandler = this._mouseUpHandler.bind(this);
this._update = this._update.bind(this);
}, {
init: function () {
var zr = this.zr;
zr.on('mousedown', this._mouseDownHandler);
zr.on('mousewheel', this._mouseWheelHandler);
zr.on('globalout', this._mouseUpHandler);
zr.animation.on('frame', this._update);
},
setTarget: function (target) {
this._target = target;
},
setZoom: function (zoom) {
this._zoom = Math.max(Math.min(
zoom, this.maxZoom
), this.minZoom);
this._needsUpdate = true;
},
setOffset: function (offset) {
this._dx = offset[0];
this._dy = offset[1];
this._needsUpdate = true;
},
getZoom: function () {
return this._zoom;
},
getOffset: function () {
return [this._dx, this._dy];
},
_update: function () {
if (!this._target) {
return;
}
if (!this._needsUpdate) {
return;
}
var target = this._target;
var scale = this._zoom;
target.position.x = this._dx;
target.position.y = this._dy;
target.scale.set(scale, scale, scale);
this.zr.refresh();
this._needsUpdate = false;
this.trigger('update');
},
_mouseDownHandler: function (e) {
if (e.target) {
return;
}
var x = e.offsetX;
var y = e.offsetY;
if (this.viewGL && !this.viewGL.containPoint(x, y)) {
return;
}
this.zr.on('mousemove', this._mouseMoveHandler);
this.zr.on('mouseup', this._mouseUpHandler);
var pos = this._convertPos(x, y);
this._x = pos.x;
this._y = pos.y;
},
// Convert pos from screen space to viewspace.
_convertPos: function (x, y) {
var camera = this.viewGL.camera;
var viewport = this.viewGL.viewport;
// PENDING
return {
x: (x - viewport.x) / viewport.width * (camera.right - camera.left) + camera.left,
y: (y - viewport.y) / viewport.height * (camera.bottom - camera.top) + camera.top
};
},
_mouseMoveHandler: function (e) {
var pos = this._convertPos(e.offsetX, e.offsetY);
this._dx += pos.x - this._x;
this._dy += pos.y - this._y;
this._x = pos.x;
this._y = pos.y;
this._needsUpdate = true;
},
_mouseUpHandler: function (e) {
this.zr.off('mousemove', this._mouseMoveHandler);
this.zr.off('mouseup', this._mouseUpHandler);
},
_mouseWheelHandler: function (e) {
e = e.event;
var delta = e.wheelDelta // Webkit
|| -e.detail; // Firefox
if (delta === 0) {
return;
}
var x = e.offsetX;
var y = e.offsetY;
if (this.viewGL && !this.viewGL.containPoint(x, y)) {
return;
}
var zoomScale = delta > 0 ? 1.1 : 0.9;
var newZoom = Math.max(Math.min(
this._zoom * zoomScale, this.maxZoom
), this.minZoom);
zoomScale = newZoom / this._zoom;
var pos = this._convertPos(x, y);
var fixX = (pos.x - this._dx) * (zoomScale - 1);
var fixY = (pos.y - this._dy) * (zoomScale - 1);
this._dx -= fixX;
this._dy -= fixY;
this._zoom = newZoom;
this._needsUpdate = true;
},
dispose: function () {
var zr = this.zr;
zr.off('mousedown', this._mouseDownHandler);
zr.off('mousemove', this._mouseMoveHandler);
zr.off('mouseup', this._mouseUpHandler);
zr.off('mousewheel', this._mouseWheelHandler);
zr.off('globalout', this._mouseUpHandler);
zr.animation.off('frame', this._update);
}
});
export default Roam2DControl;