9
9
10
10
'use strict' ;
11
11
12
- var createCamera = require ( 'gl-plot3d' ) . createCamera ;
13
- var createPlot = require ( 'gl-plot3d' ) . createScene ;
14
- var mouseWheel = require ( 'mouse-wheel' ) ;
12
+ var glPlot3d = require ( 'gl-plot3d' ) ;
13
+ var createCamera = glPlot3d . createCamera ;
14
+ var createPlot = glPlot3d . createScene ;
15
+
15
16
var getContext = require ( 'webgl-context' ) ;
16
17
var passiveSupported = require ( 'has-passive-events' ) ;
17
18
@@ -244,26 +245,7 @@ function tryCreatePlot(scene, cameraObject, pixelRatio, canvas, gl) {
244
245
}
245
246
}
246
247
247
- if ( failed < 2 ) {
248
- scene . wheelListener = mouseWheel ( scene . glplot . canvas , function ( dx , dy ) {
249
- // TODO remove now that we can disable scroll via scrollZoom?
250
- if ( scene . glplot . camera . keyBindingMode === false ) return ;
251
- if ( ! scene . glplot . camera . enableWheel ) return ;
252
-
253
- if ( scene . glplot . camera . _ortho ) {
254
- var s = ( dx > dy ) ? 1.1 : 1.0 / 1.1 ;
255
-
256
- scene . fullSceneLayout . aspectratio . x = scene . glplot . aspect [ 0 ] *= s ;
257
- scene . fullSceneLayout . aspectratio . y = scene . glplot . aspect [ 1 ] *= s ;
258
- scene . fullSceneLayout . aspectratio . z = scene . glplot . aspect [ 2 ] *= s ;
259
-
260
- scene . glplot . redraw ( ) ;
261
- }
262
- } , true ) ;
263
-
264
- return true ;
265
- }
266
- return false ;
248
+ return failed < 2 ;
267
249
}
268
250
269
251
function initializeGLPlot ( scene , pixelRatio , canvas , gl ) {
@@ -280,21 +262,45 @@ function initializeGLPlot(scene, pixelRatio, canvas, gl) {
280
262
281
263
var gd = scene . graphDiv ;
282
264
265
+ var makeUpdate = function ( ) {
266
+ var update = { } ;
267
+
268
+ // camera updates
269
+ update [ scene . id + '.camera' ] = getLayoutCamera ( scene . camera ) ;
270
+
271
+ // scene updates
272
+ update [ scene . id + '.aspectratio' ] = scene . glplot . getAspectratio ( ) ;
273
+
274
+ return update ;
275
+ } ;
276
+
283
277
var relayoutCallback = function ( scene ) {
284
278
if ( scene . fullSceneLayout . dragmode === false ) return ;
285
279
286
- var update = { } ;
287
- update [ scene . id + '.camera' ] = getLayoutCamera ( scene . camera ) ;
288
- scene . saveCamera ( gd . layout ) ;
280
+ var update = makeUpdate ( ) ;
281
+ scene . saveLayout ( gd . layout ) ;
289
282
scene . graphDiv . emit ( 'plotly_relayout' , update ) ;
290
283
} ;
291
284
292
285
scene . glplot . canvas . addEventListener ( 'mouseup' , function ( ) {
293
286
relayoutCallback ( scene ) ;
294
287
} ) ;
295
288
296
- scene . glplot . canvas . addEventListener ( 'wheel' , function ( ) {
289
+ scene . glplot . canvas . addEventListener ( 'wheel' , function ( e ) {
297
290
if ( gd . _context . _scrollZoom . gl3d ) {
291
+ if ( scene . glplot . camera . _ortho ) {
292
+ 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 ( ) ;
302
+ }
303
+
298
304
relayoutCallback ( scene ) ;
299
305
}
300
306
} , passiveSupported ? { passive : false } : false ) ;
@@ -303,8 +309,7 @@ function initializeGLPlot(scene, pixelRatio, canvas, gl) {
303
309
if ( scene . fullSceneLayout . dragmode === false ) return ;
304
310
if ( scene . camera . mouseListener . buttons === 0 ) return ;
305
311
306
- var update = { } ;
307
- update [ scene . id + '.camera' ] = getLayoutCamera ( scene . camera ) ;
312
+ var update = makeUpdate ( ) ;
308
313
scene . graphDiv . emit ( 'plotly_relayouting' , update ) ;
309
314
} ) ;
310
315
@@ -516,7 +521,7 @@ proto.plot = function(sceneData, fullLayout, layout) {
516
521
this . spikeOptions . merge ( fullSceneLayout ) ;
517
522
518
523
// Update camera and camera mode
519
- this . setCamera ( fullSceneLayout . camera ) ;
524
+ this . setViewport ( fullSceneLayout ) ;
520
525
this . updateFx ( fullSceneLayout . dragmode , fullSceneLayout . hovermode ) ;
521
526
this . camera . enableWheel = this . graphDiv . _context . _scrollZoom . gl3d ;
522
527
@@ -740,8 +745,7 @@ proto.plot = function(sceneData, fullLayout, layout) {
740
745
* Finally assign the computed aspecratio to the glplot module. This will have an effect
741
746
* on the next render cycle.
742
747
*/
743
- this . glplot . aspect = aspectRatio ;
744
-
748
+ this . glplot . setAspectratio ( fullSceneLayout . aspectratio ) ;
745
749
746
750
// Update frame position for multi plots
747
751
var domain = fullSceneLayout . domain || null ;
@@ -771,9 +775,9 @@ proto.destroy = function() {
771
775
this . glplot = null ;
772
776
} ;
773
777
774
- // getOrbitCamera :: plotly_coords -> orbit_camera_coords
778
+ // getCameraArrays :: plotly_coords -> orbit_camera_coords
775
779
// inverse of getLayoutCamera
776
- function getOrbitCamera ( camera ) {
780
+ function getCameraArrays ( camera ) {
777
781
return [
778
782
[ camera . eye . x , camera . eye . y , camera . eye . z ] ,
779
783
[ camera . center . x , camera . center . y , camera . center . z ] ,
@@ -782,7 +786,7 @@ function getOrbitCamera(camera) {
782
786
}
783
787
784
788
// getLayoutCamera :: orbit_camera_coords -> plotly_coords
785
- // inverse of getOrbitCamera
789
+ // inverse of getCameraArrays
786
790
function getLayoutCamera ( camera ) {
787
791
return {
788
792
up : { x : camera . up [ 0 ] , y : camera . up [ 1 ] , z : camera . up [ 2 ] } ,
@@ -798,9 +802,12 @@ proto.getCamera = function getCamera() {
798
802
return getLayoutCamera ( this . glplot . camera ) ;
799
803
} ;
800
804
801
- // set camera position with a set of plotly coords
802
- proto . setCamera = function setCamera ( cameraData ) {
803
- this . glplot . camera . lookAt . apply ( this , getOrbitCamera ( cameraData ) ) ;
805
+ // set gl-plot3d camera position and scene aspects with a set of plotly coords
806
+ proto . setViewport = function setViewport ( sceneLayout ) {
807
+ var cameraData = sceneLayout . camera ;
808
+
809
+ this . glplot . camera . lookAt . apply ( this , getCameraArrays ( cameraData ) ) ;
810
+ this . glplot . setAspectratio ( sceneLayout . aspectratio ) ;
804
811
805
812
var newOrtho = ( cameraData . projection . type === 'orthographic' ) ;
806
813
var oldOrtho = this . glplot . camera . _ortho ;
@@ -827,11 +834,17 @@ proto.setCamera = function setCamera(cameraData) {
827
834
} ;
828
835
829
836
// save camera to user layout (i.e. gd.layout)
830
- proto . saveCamera = function saveCamera ( layout ) {
837
+ proto . saveLayout = function saveLayout ( layout ) {
831
838
var fullLayout = this . fullLayout ;
839
+
832
840
var cameraData = this . getCamera ( ) ;
833
841
var cameraNestedProp = Lib . nestedProperty ( layout , this . id + '.camera' ) ;
834
842
var cameraDataLastSave = cameraNestedProp . get ( ) ;
843
+
844
+ var aspectData = this . glplot . getAspectratio ( ) ;
845
+ var aspectNestedProp = Lib . nestedProperty ( layout , this . id + '.camera' ) ;
846
+ var aspectDataLastSave = aspectNestedProp . get ( ) ;
847
+
835
848
var hasChanged = false ;
836
849
837
850
function same ( x , y , i , j ) {
@@ -859,15 +872,33 @@ proto.saveCamera = function saveCamera(layout) {
859
872
}
860
873
}
861
874
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
+ }
888
+
862
889
if ( hasChanged ) {
863
890
var preGUI = { } ;
864
891
preGUI [ this . id + '.camera' ] = cameraDataLastSave ;
892
+ preGUI [ this . id + '.aspectratio' ] = aspectDataLastSave ;
865
893
Registry . call ( '_storeDirectGUIEdit' , layout , fullLayout . _preGUI , preGUI ) ;
866
894
867
895
cameraNestedProp . set ( cameraData ) ;
868
896
869
897
var cameraFullNP = Lib . nestedProperty ( fullLayout , this . id + '.camera' ) ;
870
898
cameraFullNP . set ( cameraData ) ;
899
+
900
+ var aspectFullNP = Lib . nestedProperty ( fullLayout , this . id + '.aspectratio' ) ;
901
+ aspectFullNP . set ( aspectData ) ;
871
902
}
872
903
873
904
return hasChanged ;
0 commit comments