Skip to content

Commit b674b21

Browse files
committed
switch cameras when projection type is changed
1 parent 5c2186c commit b674b21

File tree

2 files changed

+29
-17
lines changed

2 files changed

+29
-17
lines changed

src/plots/gl3d/layout/layout_attributes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ module.exports = {
7878
role: 'info',
7979
values: ['perspective', 'orthographic'],
8080
dflt: 'perspective',
81-
editType: 'camera',
81+
editType: 'plot',
8282
description: [
8383
'Sets the projection type. The projection type could be',
8484
'either *perspective* or *orthographic*. The default is',
8585
'*perspective*.'
8686
].join(' ')
8787
},
88-
editType: 'camera'
88+
editType: 'plot'
8989
},
9090
editType: 'camera'
9191
},

src/plots/gl3d/scene.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,7 @@ function render(scene) {
192192
scene.drawAnnotations(scene);
193193
}
194194

195-
function initializeGLPlot(scene, camera, canvas, gl) {
196-
var gd = scene.graphDiv;
195+
function tryCreatePlot(scene, camera, canvas, gl) {
197196

198197
var glplotOptions = {
199198
canvas: canvas,
@@ -240,12 +239,19 @@ function initializeGLPlot(scene, camera, canvas, gl) {
240239
*/
241240
return showNoWebGlMsg(scene);
242241
}
242+
}
243+
244+
function initializeGLPlot(scene, camera, canvas, gl) {
245+
246+
tryCreatePlot(scene, camera, canvas, gl);
247+
248+
var gd = scene.graphDiv;
243249

244250
var relayoutCallback = function(scene) {
245251
if(scene.fullSceneLayout.dragmode === false) return;
246252

247253
var update = {};
248-
update[scene.id + '.camera'] = getLayoutCamera(scene.camera);
254+
update[scene.id + '.camera'] = getLayoutCamera(scene.camera, scene.camera._ortho);
249255
scene.saveCamera(gd.layout);
250256
scene.graphDiv.emit('plotly_relayout', update);
251257
};
@@ -350,7 +356,7 @@ var proto = Scene.prototype;
350356
proto.initializeGLCamera = function() {
351357

352358
var cameraData = this.fullSceneLayout.camera;
353-
var isOrtho = (cameraData.projection && cameraData.projection.type === 'orthographic');
359+
var isOrtho = (cameraData.projection.type === 'orthographic');
354360

355361
this.camera = createCamera(this.container, {
356362
center: [cameraData.center.x, cameraData.center.y, cameraData.center.z],
@@ -746,32 +752,38 @@ function getOrbitCamera(camera) {
746752

747753
// getLayoutCamera :: orbit_camera_coords -> plotly_coords
748754
// inverse of getOrbitCamera
749-
function getLayoutCamera(camera) {
750-
var cameraProjectionType;
751-
if(camera._ortho === false) { cameraProjectionType = 'perspective'; }
752-
else if(camera._ortho === true) { cameraProjectionType = 'orthographic'; }
753-
else {
754-
cameraProjectionType = (camera.projection && camera.projection.type === 'orthographic') ?
755-
'orthographic' : 'perspective';
756-
}
757-
755+
function getLayoutCamera(camera, isOrtho) {
758756
return {
759757
up: {x: camera.up[0], y: camera.up[1], z: camera.up[2]},
760758
center: {x: camera.center[0], y: camera.center[1], z: camera.center[2]},
761759
eye: {x: camera.eye[0], y: camera.eye[1], z: camera.eye[2]},
762-
projection: {type: cameraProjectionType}
760+
projection: {type: (isOrtho === true) ? 'orthographic' : 'perspective'}
763761
};
764762
}
765763

766764
// get camera position in plotly coords from 'orbit-camera' coords
767765
proto.getCamera = function getCamera() {
768766
this.glplot.camera.view.recalcMatrix(this.camera.view.lastT());
769-
return getLayoutCamera(this.glplot.camera);
767+
return getLayoutCamera(this.glplot.camera, this.glplot.camera._ortho);
770768
};
771769

772770
// set camera position with a set of plotly coords
773771
proto.setCamera = function setCamera(cameraData) {
774772
this.glplot.camera.lookAt.apply(this, getOrbitCamera(cameraData));
773+
774+
var newOrtho = (cameraData.projection.type === 'orthographic');
775+
var oldOrtho = this.glplot.camera._ortho;
776+
777+
if(newOrtho !== oldOrtho) {
778+
779+
this.glplot.dispose();
780+
this.glplot = null;
781+
782+
// Need to clear gl-container here
783+
784+
initializeGLPlot(this, cameraData);
785+
this.glplot.camera._ortho = newOrtho;
786+
}
775787
};
776788

777789
// save camera to user layout (i.e. gd.layout)

0 commit comments

Comments
 (0)