-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathcamera.js
171 lines (137 loc) · 5.13 KB
/
camera.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
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
/**
* Copyright 2012-2017, Plotly, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
var mouseChange = require('mouse-change');
var mouseWheel = require('mouse-wheel');
module.exports = createCamera;
function Camera2D(element, plot) {
this.element = element;
this.plot = plot;
this.mouseListener = null;
this.wheelListener = null;
this.lastInputTime = Date.now();
this.lastPos = [0, 0];
this.boxEnabled = false;
this.boxStart = [0, 0];
this.boxEnd = [0, 0];
}
function createCamera(scene) {
var element = scene.mouseContainer,
plot = scene.glplot,
result = new Camera2D(element, plot);
function unSetAutoRange() {
scene.xaxis.autorange = false;
scene.yaxis.autorange = false;
}
result.mouseListener = mouseChange(element, function(buttons, x, y) {
var dataBox = scene.calcDataBox(),
viewBox = plot.viewBox;
var lastX = result.lastPos[0],
lastY = result.lastPos[1];
x *= plot.pixelRatio;
y *= plot.pixelRatio;
// mouseChange gives y about top; convert to about bottom
y = (viewBox[3] - viewBox[1]) - y;
function updateRange(i0, start, end) {
var range0 = Math.min(start, end),
range1 = Math.max(start, end);
if(range0 !== range1) {
dataBox[i0] = range0;
dataBox[i0 + 2] = range1;
result.dataBox = dataBox;
scene.setRanges(dataBox);
}
else {
scene.selectBox.selectBox = [0, 0, 1, 1];
scene.glplot.setDirty();
}
}
switch(scene.fullLayout.dragmode) {
case 'zoom':
if(buttons) {
var dataX = x /
(viewBox[2] - viewBox[0]) * (dataBox[2] - dataBox[0]) +
dataBox[0];
var dataY = y /
(viewBox[3] - viewBox[1]) * (dataBox[3] - dataBox[1]) +
dataBox[1];
if(!result.boxEnabled) {
result.boxStart[0] = dataX;
result.boxStart[1] = dataY;
}
result.boxEnd[0] = dataX;
result.boxEnd[1] = dataY;
result.boxEnabled = true;
}
else if(result.boxEnabled) {
updateRange(0, result.boxStart[0], result.boxEnd[0]);
updateRange(1, result.boxStart[1], result.boxEnd[1]);
unSetAutoRange();
result.boxEnabled = false;
scene.relayoutCallback();
}
break;
case 'pan':
result.boxEnabled = false;
if(buttons) {
var dx = (lastX - x) * (dataBox[2] - dataBox[0]) /
(plot.viewBox[2] - plot.viewBox[0]);
var dy = (lastY - y) * (dataBox[3] - dataBox[1]) /
(plot.viewBox[3] - plot.viewBox[1]);
dataBox[0] += dx;
dataBox[2] += dx;
dataBox[1] += dy;
dataBox[3] += dy;
scene.setRanges(dataBox);
result.panning = true;
result.lastInputTime = Date.now();
unSetAutoRange();
scene.cameraChanged();
scene.handleAnnotations();
}
else if(result.panning) {
result.panning = false;
scene.relayoutCallback();
}
break;
}
result.lastPos[0] = x;
result.lastPos[1] = y;
});
result.wheelListener = mouseWheel(element, function(dx, dy) {
var dataBox = scene.calcDataBox(),
viewBox = plot.viewBox;
var lastX = result.lastPos[0],
lastY = result.lastPos[1];
switch(scene.fullLayout.dragmode) {
case 'zoom':
break;
case 'pan':
var scale = Math.exp(0.1 * dy / (viewBox[3] - viewBox[1]));
var cx = lastX /
(viewBox[2] - viewBox[0]) * (dataBox[2] - dataBox[0]) +
dataBox[0];
var cy = lastY /
(viewBox[3] - viewBox[1]) * (dataBox[3] - dataBox[1]) +
dataBox[1];
dataBox[0] = (dataBox[0] - cx) * scale + cx;
dataBox[2] = (dataBox[2] - cx) * scale + cx;
dataBox[1] = (dataBox[1] - cy) * scale + cy;
dataBox[3] = (dataBox[3] - cy) * scale + cy;
scene.setRanges(dataBox);
result.lastInputTime = Date.now();
unSetAutoRange();
scene.cameraChanged();
scene.handleAnnotations();
scene.relayoutCallback();
break;
}
return true;
});
return result;
}