Skip to content

Commit 604f7a0

Browse files
committed
refactor modebar gl3d camera handler to use Plotly.relayout
- instead of hacky setCamera call - save 'initial' camera setting during scene creation - use that + relayout to set camera to last save - no need to emit plotly_relayout in setCamera no more!
1 parent 99ad3cb commit 604f7a0

File tree

3 files changed

+19
-26
lines changed

3 files changed

+19
-26
lines changed

src/components/modebar/buttons.js

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -306,20 +306,23 @@ function handleCamera3d(gd, ev) {
306306
var button = ev.currentTarget,
307307
attr = button.getAttribute('data-attr'),
308308
fullLayout = gd._fullLayout,
309-
sceneIds = Plots.getSubplotIds(fullLayout, 'gl3d');
309+
sceneIds = Plots.getSubplotIds(fullLayout, 'gl3d'),
310+
aobj = {};
310311

311312
for(var i = 0; i < sceneIds.length; i++) {
312313
var sceneId = sceneIds[i],
313-
fullSceneLayout = fullLayout[sceneId],
314-
scene = fullSceneLayout._scene;
314+
key = sceneId + '.camera',
315+
scene = fullLayout[sceneId]._scene;
315316

316-
if(attr === 'resetDefault') scene.setCameraToDefault();
317+
if(attr === 'resetDefault') {
318+
aobj[key] = null;
319+
}
317320
else if(attr === 'resetLastSave') {
318-
// This handler looks in the un-updated fullLayout.scene.camera object to reset the camera
319-
// to the last saved position.
320-
scene.setCamera(fullSceneLayout.camera);
321+
aobj[key] = Lib.extendDeep({}, scene.cameraInitial);
321322
}
322323
}
324+
325+
Plotly.relayout(gd, aobj);
323326
}
324327

325328
modeBarButtons.hoverClosest3d = {

src/plots/gl3d/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
var Scene = require('./scene');
1313
var Plots = require('../plots');
14+
var Lib = require('../../lib');
1415
var xmlnsNamespaces = require('../../constants/xmlns_namespaces');
1516

1617
var axesNames = ['xaxis', 'yaxis', 'zaxis'];
@@ -60,6 +61,11 @@ exports.plot = function plotGl3d(gd) {
6061
sceneLayout._scene = scene;
6162
}
6263

64+
// save 'initial' camera settings for modebar button
65+
if(!scene.cameraInitial) {
66+
scene.cameraInitial = Lib.extendDeep({}, sceneLayout.camera);
67+
}
68+
6369
scene.plot(fullSceneData, fullLayout, gd.layout);
6470
}
6571
};

src/plots/gl3d/scene.js

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ function computeTraceBounds(scene, trace, bounds) {
321321
}
322322

323323
proto.plot = function(sceneData, fullLayout, layout) {
324+
324325
// Save parameters
325326
this.plotArgs = [sceneData, fullLayout, layout];
326327

@@ -343,7 +344,8 @@ proto.plot = function(sceneData, fullLayout, layout) {
343344
this.axesOptions.merge(fullSceneLayout);
344345
this.spikeOptions.merge(fullSceneLayout);
345346

346-
// Update camera mode
347+
// Update camera and camera mode
348+
this.setCamera(fullSceneLayout.camera);
347349
this.updateFx(fullSceneLayout.dragmode, fullSceneLayout.hovermode);
348350

349351
// Update scene
@@ -564,18 +566,6 @@ proto.destroy = function() {
564566
this.glplot = null;
565567
};
566568

567-
568-
// for reset camera button in mode bar
569-
proto.setCameraToDefault = function setCameraToDefault() {
570-
// as in Gl3d.layoutAttributes
571-
572-
this.setCamera({
573-
eye: { x: 1.25, y: 1.25, z: 1.25 },
574-
center: { x: 0, y: 0, z: 0 },
575-
up: { x: 0, y: 0, z: 1 }
576-
});
577-
};
578-
579569
// getOrbitCamera :: plotly_coords -> orbit_camera_coords
580570
// inverse of getLayoutCamera
581571
function getOrbitCamera(camera) {
@@ -604,13 +594,7 @@ proto.getCamera = function getCamera() {
604594

605595
// set camera position with a set of plotly coords
606596
proto.setCamera = function setCamera(cameraData) {
607-
608-
var update = {};
609-
610-
update[this.id] = cameraData;
611-
612597
this.glplot.camera.lookAt.apply(this, getOrbitCamera(cameraData));
613-
this.graphDiv.emit('plotly_relayout', update);
614598
};
615599

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

0 commit comments

Comments
 (0)