@@ -159,27 +159,41 @@ module.exports = function plot(gd, plotinfo, cdimage, imageLayer) {
159
159
if ( trace . _isFromZ ) {
160
160
resolve ( ) ;
161
161
} else if ( trace . _isFromSource ) {
162
- // Transfer image to a canvas to access pixel information
163
- trace . _canvas = trace . _canvas || document . createElement ( 'canvas' ) ;
164
- trace . _canvas . width = w ;
165
- trace . _canvas . height = h ;
166
- var context = trace . _canvas . getContext ( '2d' ) ;
167
-
168
- var sel ;
169
- if ( fastImage ) {
170
- // Use the displayed image
171
- sel = image3 ;
162
+ // Check if canvas already exists
163
+ if (
164
+ trace . _canvas &&
165
+ trace . _canvas . el . width === w &&
166
+ trace . _canvas . el . height === h &&
167
+ trace . _canvas . source === trace . source
168
+ ) {
169
+ resolve ( ) ;
172
170
} else {
173
- // Use the hidden image
174
- sel = d3 . select ( image3 [ 0 ] [ 1 ] ) ;
175
- }
171
+ // Create a canvas and transfer image onto it to access pixel information
172
+ var canvas = document . createElement ( 'canvas' ) ;
173
+ canvas . width = w ;
174
+ canvas . height = h ;
175
+ var context = canvas . getContext ( '2d' ) ;
176
+
177
+ var sel ;
178
+ if ( fastImage ) {
179
+ // Use the displayed image
180
+ sel = image3 ;
181
+ } else {
182
+ // Use the hidden image
183
+ sel = d3 . select ( image3 [ 0 ] [ 1 ] ) ;
184
+ }
176
185
177
- var image = sel . node ( ) ;
178
- image . onload = function ( ) {
179
- context . drawImage ( image , 0 , 0 ) ;
180
- resolve ( ) ;
181
- } ;
182
- sel . attr ( 'xlink:href' , trace . source ) ;
186
+ var image = sel . node ( ) ;
187
+ image . onload = function ( ) {
188
+ context . drawImage ( image , 0 , 0 ) ;
189
+ trace . _canvas = {
190
+ el : canvas ,
191
+ source : trace . source
192
+ } ;
193
+ resolve ( ) ;
194
+ } ;
195
+ sel . attr ( 'xlink:href' , trace . source ) ;
196
+ }
183
197
}
184
198
} )
185
199
. then ( function ( ) {
@@ -188,7 +202,7 @@ module.exports = function plot(gd, plotinfo, cdimage, imageLayer) {
188
202
if ( trace . _isFromZ ) {
189
203
canvas = drawMagnifiedPixelsOnCanvas ( function ( i , j ) { return z [ j ] [ i ] ; } ) ;
190
204
} else if ( trace . _isFromSource ) {
191
- var context = trace . _canvas . getContext ( '2d' ) ;
205
+ var context = trace . _canvas . el . getContext ( '2d' ) ;
192
206
var data = context . getImageData ( 0 , 0 , w , h ) . data ;
193
207
canvas = drawMagnifiedPixelsOnCanvas ( function ( i , j ) {
194
208
var index = 4 * ( j * w + i ) ;
0 commit comments