Skip to content

Make relayout work for scene.camera #1364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/components/modebar/buttons.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,20 +306,23 @@ function handleCamera3d(gd, ev) {
var button = ev.currentTarget,
attr = button.getAttribute('data-attr'),
fullLayout = gd._fullLayout,
sceneIds = Plots.getSubplotIds(fullLayout, 'gl3d');
sceneIds = Plots.getSubplotIds(fullLayout, 'gl3d'),
aobj = {};

for(var i = 0; i < sceneIds.length; i++) {
var sceneId = sceneIds[i],
fullSceneLayout = fullLayout[sceneId],
scene = fullSceneLayout._scene;
key = sceneId + '.camera',
scene = fullLayout[sceneId]._scene;

if(attr === 'resetDefault') scene.setCameraToDefault();
if(attr === 'resetDefault') {
aobj[key] = null;
}
else if(attr === 'resetLastSave') {
// This handler looks in the un-updated fullLayout.scene.camera object to reset the camera
// to the last saved position.
scene.setCamera(fullSceneLayout.camera);
aobj[key] = Lib.extendDeep({}, scene.cameraInitial);
}
}

Plotly.relayout(gd, aobj);
}

modeBarButtons.hoverClosest3d = {
Expand Down
8 changes: 7 additions & 1 deletion src/plot_api/plot_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,7 @@ Plotly.relayout = function relayout(gd, astr, val) {
if(flags.dolayoutstyle) seq.push(subroutines.layoutStyles);
if(flags.doticks) seq.push(subroutines.doTicksRelayout);
if(flags.domodebar) seq.push(subroutines.doModeBar);
if(flags.docamera) seq.push(subroutines.doCamera);
}

Queue.add(gd,
Expand Down Expand Up @@ -1771,6 +1772,7 @@ function _relayout(gd, aobj) {
doplot: false,
docalc: false,
domodebar: false,
docamera: false,
layoutReplot: false
};

Expand Down Expand Up @@ -1967,7 +1969,10 @@ function _relayout(gd, aobj) {
var pp1 = String(p.parts[1] || '');
// check whether we can short-circuit a full redraw
// 3d or geo at this point just needs to redraw.
if(p.parts[0].indexOf('scene') === 0) flags.doplot = true;
if(p.parts[0].indexOf('scene') === 0) {
if(p.parts[1] === 'camera') flags.docamera = true;
else flags.doplot = true;
}
else if(p.parts[0].indexOf('geo') === 0) flags.doplot = true;
else if(p.parts[0].indexOf('ternary') === 0) flags.doplot = true;
else if(ai === 'paper_bgcolor') flags.doplot = true;
Expand Down Expand Up @@ -2119,6 +2124,7 @@ Plotly.update = function update(gd, traceUpdate, layoutUpdate, traces) {
if(relayoutFlags.dolayoutstyle) seq.push(subroutines.layoutStyles);
if(relayoutFlags.doticks) seq.push(subroutines.doTicksRelayout);
if(relayoutFlags.domodebar) seq.push(subroutines.doModeBar);
if(relayoutFlags.doCamera) seq.push(subroutines.doCamera);
}

Queue.add(gd,
Expand Down
12 changes: 12 additions & 0 deletions src/plot_api/subroutines.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,3 +314,15 @@ exports.doModeBar = function(gd) {

return Plots.previousPromises(gd);
};

exports.doCamera = function(gd) {
var fullLayout = gd._fullLayout,
sceneIds = Plots.getSubplotIds(fullLayout, 'gl3d');

for(var i = 0; i < sceneIds.length; i++) {
var sceneLayout = fullLayout[sceneIds[i]],
scene = sceneLayout._scene;

scene.setCamera(sceneLayout.camera);
}
};
16 changes: 7 additions & 9 deletions src/plots/gl3d/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

var Scene = require('./scene');
var Plots = require('../plots');
var Lib = require('../../lib');
var xmlnsNamespaces = require('../../constants/xmlns_namespaces');

var axesNames = ['xaxis', 'yaxis', 'zaxis'];
Expand All @@ -37,21 +38,13 @@ exports.plot = function plotGl3d(gd) {
fullData = gd._fullData,
sceneIds = Plots.getSubplotIds(fullLayout, 'gl3d');

fullLayout._paperdiv.style({
width: fullLayout.width + 'px',
height: fullLayout.height + 'px'
});

gd._context.setBackground(gd, fullLayout.paper_bgcolor);

for(var i = 0; i < sceneIds.length; i++) {
var sceneId = sceneIds[i],
fullSceneData = Plots.getSubplotData(fullData, 'gl3d', sceneId),
sceneLayout = fullLayout[sceneId],
scene = sceneLayout._scene;

// If Scene is not instantiated, create one!
if(scene === undefined) {
if(!scene) {
initAxes(gd, sceneLayout);

scene = new Scene({
Expand All @@ -68,6 +61,11 @@ exports.plot = function plotGl3d(gd) {
sceneLayout._scene = scene;
}

// save 'initial' camera settings for modebar button
if(!scene.cameraInitial) {
scene.cameraInitial = Lib.extendDeep({}, sceneLayout.camera);
}

scene.plot(fullSceneData, fullLayout, gd.layout);
}
};
Expand Down
22 changes: 3 additions & 19 deletions src/plots/gl3d/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ function computeTraceBounds(scene, trace, bounds) {
}

proto.plot = function(sceneData, fullLayout, layout) {

// Save parameters
this.plotArgs = [sceneData, fullLayout, layout];

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

// Update camera mode
// Update camera and camera mode
this.setCamera(fullSceneLayout.camera);
this.updateFx(fullSceneLayout.dragmode, fullSceneLayout.hovermode);

// Update scene
Expand Down Expand Up @@ -564,18 +566,6 @@ proto.destroy = function() {
this.glplot = null;
};


// for reset camera button in mode bar
proto.setCameraToDefault = function setCameraToDefault() {
// as in Gl3d.layoutAttributes

this.setCamera({
eye: { x: 1.25, y: 1.25, z: 1.25 },
center: { x: 0, y: 0, z: 0 },
up: { x: 0, y: 0, z: 1 }
});
};

// getOrbitCamera :: plotly_coords -> orbit_camera_coords
// inverse of getLayoutCamera
function getOrbitCamera(camera) {
Expand Down Expand Up @@ -604,13 +594,7 @@ proto.getCamera = function getCamera() {

// set camera position with a set of plotly coords
proto.setCamera = function setCamera(cameraData) {

var update = {};

update[this.id] = cameraData;

this.glplot.camera.lookAt.apply(this, getOrbitCamera(cameraData));
this.graphDiv.emit('plotly_relayout', update);
};

// save camera to user layout (i.e. gd.layout)
Expand Down
Loading