@@ -8,78 +8,25 @@ var createLineWithMarkers = require('./scattergl/convert/');
8
8
var createOptions = require ( './lib/gl2daxes' ) ;
9
9
var createCamera = require ( './lib/camera' ) ;
10
10
var htmlToUnicode = require ( '../gl3d/lib/html2unicode' ) ;
11
+ var showNoWebGlMsg = require ( '../gl3d/lib/show_no_webgl_msg' ) ;
12
+
13
+ var AXES = [ 'xaxis' , 'yaxis' ] ;
14
+ var STATIC_CANVAS , STATIC_CONTEXT ;
11
15
12
16
13
17
Plotly . Plots . registerSubplot ( 'gl2d' , [ 'xaxis' , 'yaxis' ] , [ 'x' , 'y' ] ,
14
18
Plotly . Axes . traceAttributes ) ;
15
19
16
20
function Scene2D ( options , fullLayout ) {
17
- var container = this . container = options . container ;
18
-
21
+ this . container = options . container ;
19
22
this . pixelRatio = options . plotGlPixelRatio || window . devicePixelRatio ;
20
23
this . id = options . id ;
21
24
this . staticPlot = ! ! options . staticPlot ;
25
+
22
26
this . fullLayout = fullLayout ;
23
27
this . updateAxes ( fullLayout ) ;
24
28
25
- var width = fullLayout . width ;
26
- var height = fullLayout . height ;
27
-
28
- // get pixel ratio
29
- // var pixelRatio = this.pixelRatio = options.pixelRatio || window.devicePixelRatio;
30
- var pixelRatio = this . pixelRatio = 2 ;
31
-
32
- // create canvas and append to container
33
- var canvas = this . canvas = document . createElement ( 'canvas' ) ;
34
- canvas . width = Math . ceil ( pixelRatio * width ) | 0 ;
35
- canvas . height = Math . ceil ( pixelRatio * height ) | 0 ;
36
- canvas . style . width = '100%' ;
37
- canvas . style . height = '100%' ;
38
- canvas . style . position = 'absolute' ;
39
- canvas . style . top = '0px' ;
40
- canvas . style . left = '0px' ;
41
- canvas . style [ 'pointer-events' ] = 'none' ;
42
-
43
- // create SVG container for hover text
44
- var svgContainer = this . svgContainer = document . createElementNS (
45
- 'http://www.w3.org/2000/svg' ,
46
- 'svg' ) ;
47
- svgContainer . style . position = 'absolute' ;
48
- svgContainer . style . top = svgContainer . style . left = '0px' ;
49
- svgContainer . style . width = svgContainer . style . height = '100%' ;
50
- svgContainer . style [ 'z-index' ] = 20 ;
51
- svgContainer . style [ 'pointer-events' ] = 'none' ;
52
-
53
- // create div to catch the mouse event
54
- var mouseContainer = this . mouseContainer = document . createElement ( 'div' ) ;
55
- mouseContainer . style . position = 'absolute' ;
56
-
57
- // get webgl context
58
- var glopts = options . glopts || { premultipliedAlpha : true } ;
59
- var gl ;
60
-
61
- glopts . preserveDrawingBuffer = true ;
62
-
63
- try {
64
- gl = canvas . getContext ( 'webgl' , glopts ) ;
65
- } catch ( e ) { }
66
-
67
- if ( ! gl ) {
68
- try {
69
- gl = canvas . getContext ( 'experimental-webgl' , glopts ) ;
70
- } catch ( e ) { }
71
- }
72
-
73
- if ( ! gl ) {
74
- throw new Error ( 'webgl not supported!' ) ;
75
- }
76
-
77
- this . gl = gl ;
78
-
79
- // append canvas to container
80
- container . appendChild ( canvas ) ;
81
- container . appendChild ( svgContainer ) ;
82
- container . appendChild ( mouseContainer ) ;
29
+ this . makeFramework ( ) ;
83
30
84
31
// update options
85
32
this . glplotOptions = createOptions ( this ) ;
@@ -119,12 +66,88 @@ module.exports = Scene2D;
119
66
120
67
var proto = Scene2D . prototype ;
121
68
122
- var AXES = [ 'xaxis' , 'yaxis' ] ;
69
+ proto . makeFramework = function ( ) {
70
+ // create canvas and gl context
71
+ if ( this . staticPlot ) {
72
+ if ( ! STATIC_CONTEXT ) {
73
+ STATIC_CANVAS = document . createElement ( 'canvas' ) ;
74
+
75
+ try {
76
+ STATIC_CONTEXT = STATIC_CANVAS . getContext ( 'webgl' , {
77
+ preserveDrawingBuffer : true ,
78
+ premultipliedAlpha : true
79
+ } ) ;
80
+ } catch ( e ) {
81
+ throw new Error ( [
82
+ 'Error creating static canvas/context for image server'
83
+ ] . join ( ' ' ) ) ;
84
+ }
85
+ }
86
+
87
+ this . canvas = STATIC_CANVAS ;
88
+ this . gl = STATIC_CONTEXT ;
89
+ }
90
+ else {
91
+ var liveCanvas = document . createElement ( 'canvas' ) ,
92
+ glOpts = { premultipliedAlpha : true } ;
93
+ var gl ;
94
+
95
+ try {
96
+ gl = liveCanvas . getContext ( 'webgl' , glOpts ) ;
97
+ } catch ( e ) { }
98
+
99
+ if ( ! gl ) {
100
+ try {
101
+ gl = liveCanvas . getContext ( 'experimental-webgl' , glOpts ) ;
102
+ } catch ( e ) { }
103
+ }
104
+
105
+ if ( ! gl ) showNoWebGlMsg ( this ) ;
106
+
107
+ this . canvas = liveCanvas ;
108
+ this . gl = gl ;
109
+ }
110
+
111
+ // position the canvas
112
+ var canvas = this . canvas ,
113
+ pixelRatio = this . pixelRatio ,
114
+ fullLayout = this . fullLayout ;
115
+
116
+ canvas . width = Math . ceil ( pixelRatio * fullLayout . width ) | 0 ;
117
+ canvas . height = Math . ceil ( pixelRatio * fullLayout . height ) | 0 ;
118
+ canvas . style . width = '100%' ;
119
+ canvas . style . height = '100%' ;
120
+ canvas . style . position = 'absolute' ;
121
+ canvas . style . top = '0px' ;
122
+ canvas . style . left = '0px' ;
123
+ canvas . style [ 'pointer-events' ] = 'none' ;
124
+
125
+ // create SVG container for hover text
126
+ var svgContainer = this . svgContainer = document . createElementNS (
127
+ 'http://www.w3.org/2000/svg' ,
128
+ 'svg' ) ;
129
+ svgContainer . style . position = 'absolute' ;
130
+ svgContainer . style . top = svgContainer . style . left = '0px' ;
131
+ svgContainer . style . width = svgContainer . style . height = '100%' ;
132
+ svgContainer . style [ 'z-index' ] = 20 ;
133
+ svgContainer . style [ 'pointer-events' ] = 'none' ;
134
+
135
+ // create div to catch the mouse event
136
+ var mouseContainer = this . mouseContainer = document . createElement ( 'div' ) ;
137
+ mouseContainer . style . position = 'absolute' ;
138
+
139
+ // append canvas, hover svg and mouse div to container
140
+ var container = this . container ;
141
+ container . appendChild ( canvas ) ;
142
+ container . appendChild ( svgContainer ) ;
143
+ container . appendChild ( mouseContainer ) ;
144
+ } ;
123
145
124
146
proto . toImage = function ( format ) {
125
147
if ( ! format ) format = 'png' ;
126
148
127
149
this . stopped = true ;
150
+ if ( this . staticPlot ) this . container . appendChild ( STATIC_CANVAS ) ;
128
151
129
152
// force redraw
130
153
this . glplot . setDirty ( true ) ;
@@ -173,6 +196,8 @@ proto.toImage = function(format) {
173
196
dataURL = canvas . toDataURL ( 'image/png' ) ;
174
197
}
175
198
199
+ if ( this . staticPlot ) this . container . removeChild ( STATIC_CANVAS ) ;
200
+
176
201
return dataURL ;
177
202
} ;
178
203
@@ -361,7 +386,6 @@ trace_id_loop:
361
386
options . dataBox = [ xrange [ 0 ] , yrange [ 0 ] , xrange [ 1 ] , yrange [ 1 ] ] ;
362
387
363
388
options . merge ( fullLayout ) ;
364
-
365
389
glplot . update ( options ) ;
366
390
} ;
367
391
0 commit comments