Skip to content

Commit 968257e

Browse files
committed
put gl2d modules in src/plots/gl2d/
1 parent 865476a commit 968257e

File tree

4 files changed

+249
-9
lines changed

4 files changed

+249
-9
lines changed
File renamed without changes.

src/gl2d/lib/gl2daxes.js renamed to src/plots/gl2d/convert.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
'use strict';
22

33
var Plotly = require('../../plotly');
4-
var htmlToUnicode = require('../../gl3d/lib/html2unicode');
5-
var str2RGBArray = require('../../gl3d/lib/str2rgbarray');
4+
5+
var htmlToUnicode = require('../../lib/html2unicode');
6+
var str2RGBArray = require('../../lib/str2rgbarray');
67

78
function Axes2DOptions(scene) {
89
this.scene = scene;

src/gl2d/scene2d.js renamed to src/plots/gl2d/scene2d.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
'use strict';
22

3-
var Plotly = require('../plotly');
3+
var Plotly = require('../../plotly');
44
var createPlot2D = require('gl-plot2d');
55
var createSpikes = require('gl-spikes2d');
66
var createSelectBox = require('gl-select-box');
7-
var createLineWithMarkers = require('./scattergl/convert/');
8-
var createOptions = require('./lib/gl2daxes');
9-
var createCamera = require('./lib/camera');
10-
var htmlToUnicode = require('../gl3d/lib/html2unicode');
11-
var showNoWebGlMsg = require('../gl3d/lib/show_no_webgl_msg');
7+
8+
var createOptions = require('./convert');
9+
var createCamera = require('./camera');
10+
11+
var createLineWithMarkers = require('../../traces/scattergl/convert');
12+
13+
var htmlToUnicode = require('../../lib/html2unicode');
14+
var showNoWebGlMsg = require('../../lib/show_no_webgl_msg');
1215

1316
var AXES = ['xaxis', 'yaxis'];
1417
var STATIC_CANVAS, STATIC_CONTEXT;
1518

16-
1719
Plotly.Plots.registerSubplot('gl2d', ['xaxis', 'yaxis'], ['x', 'y'],
1820
Plotly.Axes.traceAttributes);
1921

src/plots/gl3d/camera.js

Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
/* jshint shadow: true */
2+
3+
'use strict';
4+
5+
module.exports = createCamera;
6+
7+
var now = require('right-now');
8+
var createView = require('3d-view');
9+
var mouseChange = require('mouse-change');
10+
var mouseWheel = require('mouse-wheel');
11+
12+
function createCamera(element, options) {
13+
element = element || document.body;
14+
options = options || {};
15+
16+
var limits = [ 0.01, Infinity ];
17+
if('distanceLimits' in options) {
18+
limits[0] = options.distanceLimits[0];
19+
limits[1] = options.distanceLimits[1];
20+
}
21+
if('zoomMin' in options) {
22+
limits[0] = options.zoomMin;
23+
}
24+
if('zoomMax' in options) {
25+
limits[1] = options.zoomMax;
26+
}
27+
28+
var view = createView({
29+
center: options.center || [0, 0, 0],
30+
up: options.up || [0, 1, 0],
31+
eye: options.eye || [0, 0, 10],
32+
mode: options.mode || 'orbit',
33+
distanceLimits: limits
34+
});
35+
36+
var pmatrix = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
37+
var distance = 0.0;
38+
var width = element.clientWidth;
39+
var height = element.clientHeight;
40+
41+
var camera = {
42+
keyBindingMode: 'rotate',
43+
view: view,
44+
element: element,
45+
delay: options.delay || 16,
46+
rotateSpeed: options.rotateSpeed || 1,
47+
zoomSpeed: options.zoomSpeed || 1,
48+
translateSpeed: options.translateSpeed || 1,
49+
flipX: !!options.flipX,
50+
flipY: !!options.flipY,
51+
modes: view.modes,
52+
tick: function() {
53+
var t = now();
54+
var delay = this.delay;
55+
var ctime = t - 2 * delay;
56+
view.idle(t-delay);
57+
view.recalcMatrix(ctime);
58+
view.flush(t - (100+delay * 2));
59+
var allEqual = true;
60+
var matrix = view.computedMatrix;
61+
for(var i = 0; i < 16; ++i) {
62+
allEqual = allEqual && (pmatrix[i] === matrix[i]);
63+
pmatrix[i] = matrix[i];
64+
}
65+
var sizeChanged =
66+
element.clientWidth === width &&
67+
element.clientHeight === height;
68+
width = element.clientWidth;
69+
height = element.clientHeight;
70+
if(allEqual) {
71+
return !sizeChanged;
72+
}
73+
distance = Math.exp(view.computedRadius[0]);
74+
return true;
75+
},
76+
lookAt: function(center, eye, up) {
77+
view.lookAt(view.lastT(), center, eye, up);
78+
},
79+
rotate: function(pitch, yaw, roll) {
80+
view.rotate(view.lastT(), pitch, yaw, roll);
81+
},
82+
pan: function(dx, dy, dz) {
83+
view.pan(view.lastT(), dx, dy, dz);
84+
},
85+
translate: function(dx, dy, dz) {
86+
view.translate(view.lastT(), dx, dy, dz);
87+
}
88+
};
89+
90+
Object.defineProperties(camera, {
91+
matrix: {
92+
get: function() {
93+
return view.computedMatrix;
94+
},
95+
set: function(mat) {
96+
view.setMatrix(view.lastT(), mat);
97+
return view.computedMatrix;
98+
},
99+
enumerable: true
100+
},
101+
mode: {
102+
get: function() {
103+
return view.getMode();
104+
},
105+
set: function(mode) {
106+
var curUp = view.computedUp.slice();
107+
var curEye = view.computedEye.slice();
108+
var curCenter = view.computedCenter.slice();
109+
view.setMode(mode);
110+
if(mode === 'turntable') {
111+
//Hacky time warping stuff to generate smooth animation
112+
var t0 = now();
113+
view._active.lookAt(t0, curEye, curCenter, curUp);
114+
view._active.lookAt(t0 + 500, curEye, curCenter, [0,0,1]);
115+
view._active.flush(t0);
116+
}
117+
return view.getMode();
118+
},
119+
enumerable: true
120+
},
121+
center: {
122+
get: function() {
123+
return view.computedCenter;
124+
},
125+
set: function(ncenter) {
126+
view.lookAt(view.lastT(), null, ncenter);
127+
return view.computedCenter;
128+
},
129+
enumerable: true
130+
},
131+
eye: {
132+
get: function() {
133+
return view.computedEye;
134+
},
135+
set: function(neye) {
136+
view.lookAt(view.lastT(), neye);
137+
return view.computedEye;
138+
},
139+
enumerable: true
140+
},
141+
up: {
142+
get: function() {
143+
return view.computedUp;
144+
},
145+
set: function(nup) {
146+
view.lookAt(view.lastT(), null, null, nup);
147+
return view.computedUp;
148+
},
149+
enumerable: true
150+
},
151+
distance: {
152+
get: function() {
153+
return distance;
154+
},
155+
set: function(d) {
156+
view.setDistance(view.lastT(), d);
157+
return d;
158+
},
159+
enumerable: true
160+
},
161+
distanceLimits: {
162+
get: function() {
163+
return view.getDistanceLimits(limits);
164+
},
165+
set: function(v) {
166+
view.setDistanceLimits(v);
167+
return v;
168+
},
169+
enumerable: true
170+
}
171+
});
172+
173+
element.addEventListener('contextmenu', function(ev) {
174+
ev.preventDefault();
175+
return false;
176+
});
177+
178+
var lastX = 0, lastY = 0;
179+
mouseChange(element, function(buttons, x, y, mods) {
180+
var rotate = camera.keyBindingMode === 'rotate';
181+
var pan = camera.keyBindingMode === 'pan';
182+
var zoom = camera.keyBindingMode === 'zoom';
183+
184+
var ctrl = !!mods.control;
185+
var alt = !!mods.alt;
186+
var shift = !!mods.shift;
187+
var left = !!(buttons&1);
188+
var right = !!(buttons&2);
189+
var middle = !!(buttons&4);
190+
191+
var scale = 1.0 / element.clientHeight;
192+
var dx = scale * (x - lastX);
193+
var dy = scale * (y - lastY);
194+
195+
var flipX = camera.flipX ? 1 : -1;
196+
var flipY = camera.flipY ? 1 : -1;
197+
198+
var t = now();
199+
200+
var drot = Math.PI * camera.rotateSpeed;
201+
202+
if( (rotate && left && !ctrl && !alt && !shift) || (left && !ctrl && !alt && shift)) {
203+
//Rotate
204+
view.rotate(t, flipX * drot * dx, -flipY * drot * dy, 0);
205+
}
206+
207+
if( (pan && left && !ctrl && !alt && !shift) || right || (left && ctrl && !alt && !shift)) {
208+
//Pan
209+
view.pan(t, -camera.translateSpeed * dx * distance, camera.translateSpeed * dy * distance, 0);
210+
}
211+
212+
if( (zoom && left && !ctrl && !alt && !shift) || middle || (left && !ctrl && alt && !shift)) {
213+
//Zoom
214+
var kzoom = -camera.zoomSpeed * dy / window.innerHeight * (t - view.lastT()) * 100;
215+
view.pan(t, 0, 0, distance * (Math.exp(kzoom) - 1));
216+
}
217+
218+
lastX = x;
219+
lastY = y;
220+
221+
return true;
222+
});
223+
224+
mouseWheel(element, function(dx, dy, dz) {
225+
var flipX = camera.flipX ? 1 : -1;
226+
var flipY = camera.flipY ? 1 : -1;
227+
var t = now();
228+
if(Math.abs(dx) > Math.abs(dy)) {
229+
view.rotate(t, 0, 0, -dx * flipX * Math.PI * camera.rotateSpeed / window.innerWidth);
230+
} else {
231+
var kzoom = -camera.zoomSpeed * flipY * dy / window.innerHeight * (t - view.lastT()) / 100.0;
232+
view.pan(t, 0, 0, distance * (Math.exp(kzoom) - 1));
233+
}
234+
}, true);
235+
236+
return camera;
237+
}

0 commit comments

Comments
 (0)