7
7
*/
8
8
9
9
10
- /*eslint block-scoped-var: 0*/
11
- /*eslint no-redeclare: 0*/
12
-
13
10
'use strict' ;
14
11
15
12
var createPlot = require ( 'gl-plot3d' ) ;
@@ -34,6 +31,8 @@ var STATIC_CANVAS, STATIC_CONTEXT;
34
31
35
32
function render ( scene ) {
36
33
34
+ var trace ;
35
+
37
36
// update size of svg container
38
37
var svgContainer = scene . svgContainer ;
39
38
var clientRect = scene . container . getBoundingClientRect ( ) ;
@@ -50,7 +49,7 @@ function render(scene) {
50
49
var lastPicked = null ;
51
50
var selection = scene . glplot . selection ;
52
51
for ( var i = 0 ; i < keys . length ; ++ i ) {
53
- var trace = scene . traces [ keys [ i ] ] ;
52
+ trace = scene . traces [ keys [ i ] ] ;
54
53
if ( trace . handlePick ( selection ) ) {
55
54
lastPicked = trace ;
56
55
}
@@ -68,9 +67,9 @@ function render(scene) {
68
67
var oldEventData ;
69
68
70
69
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 ;
74
73
75
74
var xVal = formatter ( 'xaxis' , selection . traceCoordinate [ 0 ] ) ,
76
75
yVal = formatter ( 'yaxis' , selection . traceCoordinate [ 1 ] ) ,
@@ -172,6 +171,16 @@ function initializeGLPlot(scene, fullLayout, canvas, gl) {
172
171
showNoWebGlMsg ( scene ) ;
173
172
}
174
173
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
+
175
184
if ( ! scene . staticMode ) {
176
185
scene . glplot . canvas . addEventListener ( 'webglcontextlost' , function ( ev ) {
177
186
console . log ( 'lost context' ) ;
@@ -255,7 +264,7 @@ function Scene(options, fullLayout) {
255
264
256
265
this . contourLevels = [ [ ] , [ ] , [ ] ] ;
257
266
258
- if ( ! initializeGLPlot ( this , fullLayout ) ) return ;
267
+ if ( ! initializeGLPlot ( this , fullLayout ) ) return ; // todo check the necessity for this line
259
268
}
260
269
261
270
var proto = Scene . prototype ;
@@ -283,18 +292,19 @@ proto.recoverContext = function() {
283
292
var axisProperties = [ 'xaxis' , 'yaxis' , 'zaxis' ] ;
284
293
285
294
function coordinateBound ( axis , coord , d , bounds ) {
295
+ var x ;
286
296
for ( var i = 0 ; i < coord . length ; ++ i ) {
287
297
if ( Array . isArray ( coord [ i ] ) ) {
288
298
for ( var j = 0 ; j < coord [ i ] . length ; ++ j ) {
289
- var x = axis . d2l ( coord [ i ] [ j ] ) ;
299
+ x = axis . d2l ( coord [ i ] [ j ] ) ;
290
300
if ( ! isNaN ( x ) && isFinite ( x ) ) {
291
301
bounds [ 0 ] [ d ] = Math . min ( bounds [ 0 ] [ d ] , x ) ;
292
302
bounds [ 1 ] [ d ] = Math . max ( bounds [ 1 ] [ d ] , x ) ;
293
303
}
294
304
}
295
305
}
296
306
else {
297
- var x = axis . d2l ( coord [ i ] ) ;
307
+ x = axis . d2l ( coord [ i ] ) ;
298
308
if ( ! isNaN ( x ) && isFinite ( x ) ) {
299
309
bounds [ 0 ] [ d ] = Math . min ( bounds [ 0 ] [ d ] , x ) ;
300
310
bounds [ 1 ] [ d ] = Math . max ( bounds [ 1 ] [ d ] , x ) ;
@@ -317,7 +327,7 @@ proto.plot = function(sceneData, fullLayout, layout) {
317
327
if ( this . glplot . contextLost ) return ;
318
328
319
329
var data , trace ;
320
- var i , j ;
330
+ var i , j , axis , axisType ;
321
331
var fullSceneLayout = fullLayout [ this . id ] ;
322
332
var sceneLayout = layout [ this . id ] ;
323
333
@@ -341,7 +351,7 @@ proto.plot = function(sceneData, fullLayout, layout) {
341
351
342
352
// Update axes functions BEFORE updating traces
343
353
for ( i = 0 ; i < 3 ; ++ i ) {
344
- var axis = fullSceneLayout [ axisProperties [ i ] ] ;
354
+ axis = fullSceneLayout [ axisProperties [ i ] ] ;
345
355
setConvert ( axis ) ;
346
356
}
347
357
@@ -354,14 +364,14 @@ proto.plot = function(sceneData, fullLayout, layout) {
354
364
[ Infinity , Infinity , Infinity ] ,
355
365
[ - Infinity , - Infinity , - Infinity ]
356
366
] ;
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 ] ;
359
369
if ( data . visible !== true ) continue ;
360
370
361
371
computeTraceBounds ( this , data , dataBounds ) ;
362
372
}
363
373
var dataScale = [ 1 , 1 , 1 ] ;
364
- for ( var j = 0 ; j < 3 ; ++ j ) {
374
+ for ( j = 0 ; j < 3 ; ++ j ) {
365
375
if ( dataBounds [ 0 ] [ j ] > dataBounds [ 1 ] [ j ] ) {
366
376
dataScale [ j ] = 1.0 ;
367
377
}
@@ -379,7 +389,7 @@ proto.plot = function(sceneData, fullLayout, layout) {
379
389
this . dataScale = dataScale ;
380
390
381
391
//Update traces
382
- for ( var i = 0 ; i < sceneData . length ; ++ i ) {
392
+ for ( i = 0 ; i < sceneData . length ; ++ i ) {
383
393
data = sceneData [ i ] ;
384
394
if ( data . visible !== true ) {
385
395
continue ;
@@ -416,8 +426,8 @@ proto.plot = function(sceneData, fullLayout, layout) {
416
426
axisTypeRatios = { } ;
417
427
418
428
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 ;
421
431
422
432
if ( axisType in axisTypeRatios ) {
423
433
axisTypeRatios [ axisType ] . acc *= dataScale [ i ] ;
@@ -471,9 +481,9 @@ proto.plot = function(sceneData, fullLayout, layout) {
471
481
var axesScaleRatio = [ 1 , 1 , 1 ] ;
472
482
473
483
//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 ;
477
487
var axisRatio = axisTypeRatios [ axisType ] ;
478
488
axesScaleRatio [ i ] = Math . pow ( axisRatio . acc , 1.0 / axisRatio . count ) / dataScale [ i ] ;
479
489
}
@@ -567,33 +577,35 @@ proto.setCameraToDefault = function setCameraToDefault() {
567
577
} ) ;
568
578
} ;
569
579
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
+ }
577
589
590
+ // getLayoutCamera :: orbit_camera_coords -> plotly_coords
591
+ // inverse of getOrbitCamera
592
+ function getLayoutCamera ( camera ) {
578
593
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 ] }
582
597
} ;
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 ) ;
583
604
} ;
584
605
585
606
// set camera position with a set of plotly coords
586
607
proto . setCamera = function setCamera ( cameraData ) {
587
608
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
-
597
609
var update = { } ;
598
610
599
611
update [ this . id ] = cameraData ;
@@ -612,7 +624,7 @@ proto.saveCamera = function saveCamera(layout) {
612
624
function same ( x , y , i , j ) {
613
625
var vectors = [ 'up' , 'center' , 'eye' ] ,
614
626
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 ] ] ) ;
616
628
}
617
629
618
630
if ( cameraDataLastSave === undefined ) hasChanged = true ;
0 commit comments