77*/
88
99
10- /*eslint block-scoped-var: 0*/
11- /*eslint no-redeclare: 0*/
12-
1310'use strict' ;
1411
1512var createPlot = require ( 'gl-plot3d' ) ;
@@ -34,6 +31,8 @@ var STATIC_CANVAS, STATIC_CONTEXT;
3431
3532function render ( scene ) {
3633
34+ var trace ;
35+
3736 // update size of svg container
3837 var svgContainer = scene . svgContainer ;
3938 var clientRect = scene . container . getBoundingClientRect ( ) ;
@@ -50,7 +49,7 @@ function render(scene) {
5049 var lastPicked = null ;
5150 var selection = scene . glplot . selection ;
5251 for ( var i = 0 ; i < keys . length ; ++ i ) {
53- var trace = scene . traces [ keys [ i ] ] ;
52+ trace = scene . traces [ keys [ i ] ] ;
5453 if ( trace . handlePick ( selection ) ) {
5554 lastPicked = trace ;
5655 }
@@ -68,9 +67,9 @@ function render(scene) {
6867 var oldEventData ;
6968
7069 if ( lastPicked !== null ) {
71- var pdata = project ( scene . glplot . cameraParams , selection . dataCoordinate ) ,
72- trace = lastPicked . data ,
73- hoverinfo = trace . hoverinfo ;
70+ var pdata = project ( scene . glplot . cameraParams , selection . dataCoordinate ) ;
71+ trace = lastPicked . data ;
72+ var hoverinfo = trace . hoverinfo ;
7473
7574 var xVal = formatter ( 'xaxis' , selection . traceCoordinate [ 0 ] ) ,
7675 yVal = formatter ( 'yaxis' , selection . traceCoordinate [ 1 ] ) ,
@@ -172,6 +171,16 @@ function initializeGLPlot(scene, fullLayout, canvas, gl) {
172171 showNoWebGlMsg ( scene ) ;
173172 }
174173
174+ var relayoutCallback = function ( scene ) {
175+ var update = { } ;
176+ update [ scene . id ] = getLayoutCamera ( scene . camera ) ;
177+ scene . saveCamera ( scene . graphDiv . layout ) ;
178+ scene . graphDiv . emit ( 'plotly_relayout' , update ) ;
179+ } ;
180+
181+ scene . glplot . canvas . addEventListener ( 'mouseup' , relayoutCallback . bind ( null , scene ) ) ;
182+ scene . glplot . canvas . addEventListener ( 'wheel' , relayoutCallback . bind ( null , scene ) ) ;
183+
175184 if ( ! scene . staticMode ) {
176185 scene . glplot . canvas . addEventListener ( 'webglcontextlost' , function ( ev ) {
177186 console . log ( 'lost context' ) ;
@@ -255,7 +264,7 @@ function Scene(options, fullLayout) {
255264
256265 this . contourLevels = [ [ ] , [ ] , [ ] ] ;
257266
258- if ( ! initializeGLPlot ( this , fullLayout ) ) return ;
267+ if ( ! initializeGLPlot ( this , fullLayout ) ) return ; // todo check the necessity for this line
259268}
260269
261270var proto = Scene . prototype ;
@@ -283,18 +292,19 @@ proto.recoverContext = function() {
283292var axisProperties = [ 'xaxis' , 'yaxis' , 'zaxis' ] ;
284293
285294function coordinateBound ( axis , coord , d , bounds ) {
295+ var x ;
286296 for ( var i = 0 ; i < coord . length ; ++ i ) {
287297 if ( Array . isArray ( coord [ i ] ) ) {
288298 for ( var j = 0 ; j < coord [ i ] . length ; ++ j ) {
289- var x = axis . d2l ( coord [ i ] [ j ] ) ;
299+ x = axis . d2l ( coord [ i ] [ j ] ) ;
290300 if ( ! isNaN ( x ) && isFinite ( x ) ) {
291301 bounds [ 0 ] [ d ] = Math . min ( bounds [ 0 ] [ d ] , x ) ;
292302 bounds [ 1 ] [ d ] = Math . max ( bounds [ 1 ] [ d ] , x ) ;
293303 }
294304 }
295305 }
296306 else {
297- var x = axis . d2l ( coord [ i ] ) ;
307+ x = axis . d2l ( coord [ i ] ) ;
298308 if ( ! isNaN ( x ) && isFinite ( x ) ) {
299309 bounds [ 0 ] [ d ] = Math . min ( bounds [ 0 ] [ d ] , x ) ;
300310 bounds [ 1 ] [ d ] = Math . max ( bounds [ 1 ] [ d ] , x ) ;
@@ -317,7 +327,7 @@ proto.plot = function(sceneData, fullLayout, layout) {
317327 if ( this . glplot . contextLost ) return ;
318328
319329 var data , trace ;
320- var i , j ;
330+ var i , j , axis , axisType ;
321331 var fullSceneLayout = fullLayout [ this . id ] ;
322332 var sceneLayout = layout [ this . id ] ;
323333
@@ -341,7 +351,7 @@ proto.plot = function(sceneData, fullLayout, layout) {
341351
342352 // Update axes functions BEFORE updating traces
343353 for ( i = 0 ; i < 3 ; ++ i ) {
344- var axis = fullSceneLayout [ axisProperties [ i ] ] ;
354+ axis = fullSceneLayout [ axisProperties [ i ] ] ;
345355 setConvert ( axis ) ;
346356 }
347357
@@ -354,14 +364,14 @@ proto.plot = function(sceneData, fullLayout, layout) {
354364 [ Infinity , Infinity , Infinity ] ,
355365 [ - Infinity , - Infinity , - Infinity ]
356366 ] ;
357- for ( var i = 0 ; i < sceneData . length ; ++ i ) {
358- var data = sceneData [ i ] ;
367+ for ( i = 0 ; i < sceneData . length ; ++ i ) {
368+ data = sceneData [ i ] ;
359369 if ( data . visible !== true ) continue ;
360370
361371 computeTraceBounds ( this , data , dataBounds ) ;
362372 }
363373 var dataScale = [ 1 , 1 , 1 ] ;
364- for ( var j = 0 ; j < 3 ; ++ j ) {
374+ for ( j = 0 ; j < 3 ; ++ j ) {
365375 if ( dataBounds [ 0 ] [ j ] > dataBounds [ 1 ] [ j ] ) {
366376 dataScale [ j ] = 1.0 ;
367377 }
@@ -379,7 +389,7 @@ proto.plot = function(sceneData, fullLayout, layout) {
379389 this . dataScale = dataScale ;
380390
381391 //Update traces
382- for ( var i = 0 ; i < sceneData . length ; ++ i ) {
392+ for ( i = 0 ; i < sceneData . length ; ++ i ) {
383393 data = sceneData [ i ] ;
384394 if ( data . visible !== true ) {
385395 continue ;
@@ -416,8 +426,8 @@ proto.plot = function(sceneData, fullLayout, layout) {
416426 axisTypeRatios = { } ;
417427
418428 for ( i = 0 ; i < 3 ; ++ i ) {
419- var axis = fullSceneLayout [ axisProperties [ i ] ] ;
420- var axisType = axis . type ;
429+ axis = fullSceneLayout [ axisProperties [ i ] ] ;
430+ axisType = axis . type ;
421431
422432 if ( axisType in axisTypeRatios ) {
423433 axisTypeRatios [ axisType ] . acc *= dataScale [ i ] ;
@@ -471,9 +481,9 @@ proto.plot = function(sceneData, fullLayout, layout) {
471481 var axesScaleRatio = [ 1 , 1 , 1 ] ;
472482
473483 //Compute axis scale per category
474- for ( var i = 0 ; i < 3 ; ++ i ) {
475- var axis = fullSceneLayout [ axisProperties [ i ] ] ;
476- var axisType = axis . type ;
484+ for ( i = 0 ; i < 3 ; ++ i ) {
485+ axis = fullSceneLayout [ axisProperties [ i ] ] ;
486+ axisType = axis . type ;
477487 var axisRatio = axisTypeRatios [ axisType ] ;
478488 axesScaleRatio [ i ] = Math . pow ( axisRatio . acc , 1.0 / axisRatio . count ) / dataScale [ i ] ;
479489 }
@@ -567,33 +577,35 @@ proto.setCameraToDefault = function setCameraToDefault() {
567577 } ) ;
568578} ;
569579
570- // get camera position in plotly coords from 'orbit-camera' coords
571- proto . getCamera = function getCamera ( ) {
572- this . glplot . camera . view . recalcMatrix ( this . camera . view . lastT ( ) ) ;
573-
574- var up = this . glplot . camera . up ;
575- var center = this . glplot . camera . center ;
576- var eye = this . glplot . camera . eye ;
580+ // getOrbitCamera :: plotly_coords -> orbit_camera_coords
581+ // inverse of getLayoutCamera
582+ function getOrbitCamera ( camera ) {
583+ return [
584+ [ camera . eye . x , camera . eye . y , camera . eye . z ] ,
585+ [ camera . center . x , camera . center . y , camera . center . z ] ,
586+ [ camera . up . x , camera . up . y , camera . up . z ]
587+ ] ;
588+ }
577589
590+ // getLayoutCamera :: orbit_camera_coords -> plotly_coords
591+ // inverse of getOrbitCamera
592+ function getLayoutCamera ( camera ) {
578593 return {
579- up : { x : up [ 0 ] , y : up [ 1 ] , z : up [ 2 ] } ,
580- center : { x : center [ 0 ] , y : center [ 1 ] , z : center [ 2 ] } ,
581- eye : { x : eye [ 0 ] , y : eye [ 1 ] , z : eye [ 2 ] }
594+ up : { x : camera . up [ 0 ] , y : camera . up [ 1 ] , z : camera . up [ 2 ] } ,
595+ center : { x : camera . center [ 0 ] , y : camera . center [ 1 ] , z : camera . center [ 2 ] } ,
596+ eye : { x : camera . eye [ 0 ] , y : camera . eye [ 1 ] , z : camera . eye [ 2 ] }
582597 } ;
598+ }
599+
600+ // get camera position in plotly coords from 'orbit-camera' coords
601+ proto . getCamera = function getCamera ( ) {
602+ this . glplot . camera . view . recalcMatrix ( this . camera . view . lastT ( ) ) ;
603+ return getLayoutCamera ( this . glplot . camera ) ;
583604} ;
584605
585606// set camera position with a set of plotly coords
586607proto . setCamera = function setCamera ( cameraData ) {
587608
588- // getOrbitCamera :: plotly_coords -> orbit_camera_coords
589- function getOrbitCamera ( camera ) {
590- return [
591- [ camera . eye . x , camera . eye . y , camera . eye . z ] ,
592- [ camera . center . x , camera . center . y , camera . center . z ] ,
593- [ camera . up . x , camera . up . y , camera . up . z ]
594- ] ;
595- }
596-
597609 var update = { } ;
598610
599611 update [ this . id ] = cameraData ;
@@ -612,7 +624,7 @@ proto.saveCamera = function saveCamera(layout) {
612624 function same ( x , y , i , j ) {
613625 var vectors = [ 'up' , 'center' , 'eye' ] ,
614626 components = [ 'x' , 'y' , 'z' ] ;
615- return x [ vectors [ i ] ] [ components [ j ] ] === y [ vectors [ i ] ] [ components [ j ] ] ;
627+ return y [ vectors [ i ] ] && ( x [ vectors [ i ] ] [ components [ j ] ] === y [ vectors [ i ] ] [ components [ j ] ] ) ;
616628 }
617629
618630 if ( cameraDataLastSave === undefined ) hasChanged = true ;
0 commit comments