10
10
11
11
'use strict' ;
12
12
13
- var EventEmitter = require ( 'events' ) . EventEmitter ;
14
13
var Plotly = require ( '../plotly' ) ;
15
14
16
15
/**
@@ -19,64 +18,73 @@ var Plotly = require('../plotly');
19
18
* @param opts.format 'jpeg' | 'png' | 'webp' | 'svg'
20
19
*/
21
20
function toImage ( gd , opts ) {
22
-
23
- // first clone the GD so we can operate in a clean environment
24
- var Snapshot = Plotly . Snapshot ;
25
- var ev = new EventEmitter ( ) ;
26
-
27
- var clone = Snapshot . clone ( gd , { format : 'png' , height : opts . height , width : opts . width } ) ;
28
- var clonedGd = clone . td ;
29
-
30
- // put the cloned div somewhere off screen before attaching to DOM
31
- clonedGd . style . position = 'absolute' ;
32
- clonedGd . style . left = '-5000px' ;
33
- document . body . appendChild ( clonedGd ) ;
34
-
35
- function wait ( ) {
36
- var delay = Snapshot . getDelay ( clonedGd . _fullLayout ) ;
37
-
38
- setTimeout ( function ( ) {
39
- var svg = Plotly . Snapshot . toSVG ( clonedGd ) ;
40
-
41
- var canvasContainer = window . document . createElement ( 'div' ) ;
42
- var canvas = window . document . createElement ( 'canvas' ) ;
43
-
44
- // window.document.body.appendChild(canvasContainer);
45
- canvasContainer . appendChild ( canvas ) ;
46
-
47
- canvasContainer . id = Plotly . Lib . randstr ( ) ;
48
- canvas . id = Plotly . Lib . randstr ( ) ;
49
-
50
- ev = Plotly . Snapshot . svgToImg ( {
51
- format : opts . format ,
52
- width : clonedGd . _fullLayout . width ,
53
- height : clonedGd . _fullLayout . height ,
54
- canvas : canvas ,
55
- emitter : ev ,
56
- svg : svg
21
+ var promise = new Promise ( function ( resolve , reject ) {
22
+ // check for undefined opts
23
+ opts = ( opts ) ? opts : { } ;
24
+ // default to png
25
+ opts . format = ( opts . format ) ? opts . format : 'png' ;
26
+
27
+ // first clone the GD so we can operate in a clean environment
28
+ var Snapshot = Plotly . Snapshot ;
29
+
30
+ var clone = Snapshot . clone ( gd , { format : 'png' , height : opts . height , width : opts . width } ) ;
31
+ var clonedGd = clone . td ;
32
+
33
+ // put the cloned div somewhere off screen before attaching to DOM
34
+ clonedGd . style . position = 'absolute' ;
35
+ clonedGd . style . left = '-5000px' ;
36
+ document . body . appendChild ( clonedGd ) ;
37
+
38
+ function wait ( ) {
39
+ var delay = Snapshot . getDelay ( clonedGd . _fullLayout ) ;
40
+
41
+ return new Promise ( function ( resolve , reject ) {
42
+ setTimeout ( function ( ) {
43
+ var svg = Plotly . Snapshot . toSVG ( clonedGd ) ;
44
+
45
+ var canvasContainer = window . document . createElement ( 'div' ) ;
46
+ var canvas = window . document . createElement ( 'canvas' ) ;
47
+
48
+ // window.document.body.appendChild(canvasContainer);
49
+ canvasContainer . appendChild ( canvas ) ;
50
+
51
+ canvasContainer . id = Plotly . Lib . randstr ( ) ;
52
+ canvas . id = Plotly . Lib . randstr ( ) ;
53
+
54
+ Plotly . Snapshot . svgToImg ( {
55
+ format : opts . format ,
56
+ width : clonedGd . _fullLayout . width ,
57
+ height : clonedGd . _fullLayout . height ,
58
+ canvas : canvas ,
59
+ svg : svg
60
+ } ) . then ( function ( url ) {
61
+ if ( clonedGd ) clonedGd . remove ( ) ;
62
+ resolve ( url ) ;
63
+ } ) . catch ( function ( err ) {
64
+ reject ( err ) ;
65
+ } ) ;
66
+ } , delay ) ;
57
67
} ) ;
68
+ }
69
+
70
+ var redrawFunc = Snapshot . getRedrawFunc ( clonedGd ) ;
71
+
72
+ Plotly . plot ( clonedGd , clone . data , clone . layout , clone . config )
73
+ // TODO: the following is Plotly.Plots.redrawText but without the waiting.
74
+ // we shouldn't need to do this, but in *occasional* cases we do. Figure
75
+ // out why and take it out.
76
+
77
+ // not sure the above TODO makes sense anymore since
78
+ // we have converted to promises
79
+ . then ( redrawFunc )
80
+ . then ( wait )
81
+ . then ( function ( url ) { resolve ( url ) ; } )
82
+ . catch ( function ( err ) {
83
+ reject ( err ) ;
84
+ } ) ;
85
+ } ) ;
58
86
59
- ev . clean = function ( ) {
60
- if ( clonedGd ) clonedGd . remove ( ) ;
61
- } ;
62
-
63
- } , delay ) ;
64
- }
65
-
66
- var redrawFunc = Snapshot . getRedrawFunc ( clonedGd ) ;
67
-
68
- Plotly . plot ( clonedGd , clone . data , clone . layout , clone . config )
69
- // TODO: the following is Plotly.Plots.redrawText but without the waiting.
70
- // we shouldn't need to do this, but in *occasional* cases we do. Figure
71
- // out why and take it out.
72
- . then ( redrawFunc )
73
- . then ( wait )
74
- . catch ( function ( err ) {
75
- ev . emit ( 'error' , err ) ;
76
- } ) ;
77
-
78
-
79
- return ev ;
87
+ return promise ;
80
88
}
81
89
82
90
module . exports = toImage ;
0 commit comments