Skip to content

Commit b92230a

Browse files
committed
Save camera and aspectratio only if they change before redraw the scene
- consider aspectratio changes in modebar home and initial views - bump gl-plot3d 2.3.0
1 parent fe19e47 commit b92230a

File tree

5 files changed

+68
-65
lines changed

5 files changed

+68
-65
lines changed

package-lock.json

+3-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
"gl-mat4": "^1.2.0",
8282
"gl-mesh3d": "^2.1.1",
8383
"gl-plot2d": "^1.4.2",
84-
"gl-plot3d": "git://github.com/gl-vis/gl-plot3d.git#83e9c9406976e5ee92bf1f1851e4366e516c06f7",
84+
"gl-plot3d": "^2.3.0",
8585
"gl-pointcloud2d": "^1.0.2",
8686
"gl-scatter3d": "^1.2.2",
8787
"gl-select-box": "^1.0.3",

src/components/modebar/buttons.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -344,17 +344,27 @@ function handleCamera3d(gd, ev) {
344344

345345
for(var i = 0; i < sceneIds.length; i++) {
346346
var sceneId = sceneIds[i];
347-
var key = sceneId + '.camera';
347+
var camera = sceneId + '.camera';
348+
var aspectratio = sceneId + '.aspectratio';
348349
var scene = fullLayout[sceneId]._scene;
350+
var didUpdate;
349351

350352
if(attr === 'resetLastSave') {
351-
aobj[key + '.up'] = scene.viewInitial.up;
352-
aobj[key + '.eye'] = scene.viewInitial.eye;
353-
aobj[key + '.center'] = scene.viewInitial.center;
353+
aobj[camera + '.up'] = scene.viewInitial.up;
354+
aobj[camera + '.eye'] = scene.viewInitial.eye;
355+
aobj[camera + '.center'] = scene.viewInitial.center;
356+
didUpdate = true;
354357
} else if(attr === 'resetDefault') {
355-
aobj[key + '.up'] = null;
356-
aobj[key + '.eye'] = null;
357-
aobj[key + '.center'] = null;
358+
aobj[camera + '.up'] = null;
359+
aobj[camera + '.eye'] = null;
360+
aobj[camera + '.center'] = null;
361+
didUpdate = true;
362+
}
363+
364+
if(didUpdate) {
365+
aobj[aspectratio + '.x'] = scene.viewInitial.aspectratio.x;
366+
aobj[aspectratio + '.y'] = scene.viewInitial.aspectratio.y;
367+
aobj[aspectratio + '.z'] = scene.viewInitial.aspectratio.z;
358368
}
359369
}
360370

src/plots/gl3d/scene.js

+47-37
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,10 @@ function initializeGLPlot(scene, pixelRatio, canvas, gl) {
268268
// camera updates
269269
update[scene.id + '.camera'] = getLayoutCamera(scene.camera);
270270

271-
// scene updates
272-
update[scene.id + '.aspectratio'] = scene.glplot.getAspectratio();
271+
if(scene.camera._ortho === true) {
272+
// scene updates
273+
update[scene.id + '.aspectratio'] = scene.glplot.getAspectratio();
274+
}
273275

274276
return update;
275277
};
@@ -290,15 +292,12 @@ function initializeGLPlot(scene, pixelRatio, canvas, gl) {
290292
if(gd._context._scrollZoom.gl3d) {
291293
if(scene.glplot.camera._ortho) {
292294
var s = (e.deltaX > e.deltaY) ? 1.1 : 1.0 / 1.1;
293-
294-
var aspectratio = scene.fullSceneLayout.aspectratio;
295-
296-
aspectratio.x = scene.glplot.aspect[0] *= s;
297-
aspectratio.y = scene.glplot.aspect[1] *= s;
298-
aspectratio.z = scene.glplot.aspect[2] *= s;
299-
300-
scene.glplot.setAspectratio(aspectratio);
301-
scene.glplot.redraw();
295+
var o = scene.glplot.getAspectratio();
296+
scene.glplot.setAspectratio({
297+
x: s * o.x,
298+
y: s * o.y,
299+
z: s * o.z
300+
});
302301
}
303302

304303
relayoutCallback(scene);
@@ -747,6 +746,15 @@ proto.plot = function(sceneData, fullLayout, layout) {
747746
*/
748747
this.glplot.setAspectratio(fullSceneLayout.aspectratio);
749748

749+
// save 'initial' camera view settings for modebar button
750+
if(!this.viewInitial.aspectratio) {
751+
this.viewInitial.aspectratio = {
752+
x: fullSceneLayout.aspectratio.x,
753+
y: fullSceneLayout.aspectratio.y,
754+
z: fullSceneLayout.aspectratio.z
755+
};
756+
}
757+
750758
// Update frame position for multi plots
751759
var domain = fullSceneLayout.domain || null;
752760
var size = fullLayout._size || null;
@@ -841,25 +849,25 @@ proto.saveLayout = function saveLayout(layout) {
841849
var cameraNestedProp = Lib.nestedProperty(layout, this.id + '.camera');
842850
var cameraDataLastSave = cameraNestedProp.get();
843851

852+
844853
var aspectData = this.glplot.getAspectratio();
845-
var aspectNestedProp = Lib.nestedProperty(layout, this.id + '.camera');
854+
var aspectNestedProp = Lib.nestedProperty(layout, this.id + '.aspectratio');
846855
var aspectDataLastSave = aspectNestedProp.get();
847856

848-
var hasChanged = false;
849-
850857
function same(x, y, i, j) {
851858
var vectors = ['up', 'center', 'eye'];
852859
var components = ['x', 'y', 'z'];
853860
return y[vectors[i]] && (x[vectors[i]][components[j]] === y[vectors[i]][components[j]]);
854861
}
855862

863+
var cameraChanged = false;
856864
if(cameraDataLastSave === undefined) {
857-
hasChanged = true;
865+
cameraChanged = true;
858866
} else {
859867
for(var i = 0; i < 3; i++) {
860868
for(var j = 0; j < 3; j++) {
861869
if(!same(cameraData, cameraDataLastSave, i, j)) {
862-
hasChanged = true;
870+
cameraChanged = true;
863871
break;
864872
}
865873
}
@@ -868,37 +876,39 @@ proto.saveLayout = function saveLayout(layout) {
868876
if(!cameraDataLastSave.projection || (
869877
cameraData.projection &&
870878
cameraData.projection.type !== cameraDataLastSave.projection.type)) {
871-
hasChanged = true;
879+
cameraChanged = true;
872880
}
873881
}
874882

875-
if(!hasChanged) {
876-
if(aspectDataLastSave === undefined) {
877-
hasChanged = true;
878-
} else {
879-
if(
880-
aspectDataLastSave.x !== aspectData.x ||
881-
aspectDataLastSave.y !== aspectData.y ||
882-
aspectDataLastSave.z !== aspectData.z
883-
) {
884-
hasChanged = true;
885-
}
886-
}
887-
}
883+
var aspectChanged = (
884+
aspectDataLastSave === undefined || (
885+
aspectDataLastSave.x !== aspectData.x ||
886+
aspectDataLastSave.y !== aspectData.y ||
887+
aspectDataLastSave.z !== aspectData.z
888+
));
888889

890+
var hasChanged = cameraChanged || aspectChanged;
889891
if(hasChanged) {
890892
var preGUI = {};
891-
preGUI[this.id + '.camera'] = cameraDataLastSave;
892-
preGUI[this.id + '.aspectratio'] = aspectDataLastSave;
893+
if(cameraChanged) preGUI[this.id + '.camera'] = cameraDataLastSave;
894+
if(aspectChanged) preGUI[this.id + '.aspectratio'] = aspectDataLastSave;
893895
Registry.call('_storeDirectGUIEdit', layout, fullLayout._preGUI, preGUI);
894896

895-
cameraNestedProp.set(cameraData);
897+
if(cameraChanged) {
898+
cameraNestedProp.set(cameraData);
896899

897-
var cameraFullNP = Lib.nestedProperty(fullLayout, this.id + '.camera');
898-
cameraFullNP.set(cameraData);
900+
var cameraFullNP = Lib.nestedProperty(fullLayout, this.id + '.camera');
901+
cameraFullNP.set(cameraData);
902+
}
903+
904+
if(aspectChanged) {
905+
aspectNestedProp.set(aspectData);
899906

900-
var aspectFullNP = Lib.nestedProperty(fullLayout, this.id + '.aspectratio');
901-
aspectFullNP.set(aspectData);
907+
var aspectFullNP = Lib.nestedProperty(fullLayout, this.id + '.aspectratio');
908+
aspectFullNP.set(aspectData);
909+
910+
this.glplot.redraw();
911+
}
902912
}
903913

904914
return hasChanged;

test/jasmine/tests/gl3d_plot_interact_test.js

-18
Original file line numberDiff line numberDiff line change
@@ -906,24 +906,6 @@ describe('Test gl3d drag and wheel interactions', function() {
906906
return scroll(sceneTarget2);
907907
})
908908
.then(function() {
909-
expect(
910-
gd._fullLayout.scene2.aspectratio.x
911-
).toEqual(
912-
gd._fullLayout.scene2._scene.glplot.aspect[0]
913-
);
914-
915-
expect(
916-
gd._fullLayout.scene2.aspectratio.y
917-
).toEqual(
918-
gd._fullLayout.scene2._scene.glplot.aspect[1]
919-
);
920-
921-
expect(
922-
gd._fullLayout.scene2.aspectratio.z
923-
).toEqual(
924-
gd._fullLayout.scene2._scene.glplot.aspect[2]
925-
);
926-
927909
_assertAndReset(2);
928910
})
929911
.catch(failTest)

0 commit comments

Comments
 (0)