@@ -55,17 +55,21 @@ module.exports = function draw(gd) {
55
55
function setImage ( d ) {
56
56
var thisImage = d3 . select ( this ) ;
57
57
58
+ if ( this . img && this . img . src === d . source ) {
59
+ return ;
60
+ }
61
+
58
62
thisImage . attr ( 'xmlns' , xmlnsNamespaces . svg ) ;
59
63
60
64
var imagePromise = new Promise ( function ( resolve ) {
61
65
62
66
var img = new Image ( ) ;
67
+ this . img = img ;
63
68
64
69
// If not set, a `tainted canvas` error is thrown
65
70
img . setAttribute ( 'crossOrigin' , 'anonymous' ) ;
66
71
img . onerror = errorHandler ;
67
72
img . onload = function ( ) {
68
-
69
73
var canvas = document . createElement ( 'canvas' ) ;
70
74
canvas . width = this . width ;
71
75
canvas . height = this . height ;
@@ -88,7 +92,7 @@ module.exports = function draw(gd) {
88
92
thisImage . remove ( ) ;
89
93
resolve ( ) ;
90
94
}
91
- } ) ;
95
+ } . bind ( this ) ) ;
92
96
93
97
gd . _promises . push ( imagePromise ) ;
94
98
}
@@ -146,29 +150,31 @@ module.exports = function draw(gd) {
146
150
}
147
151
}
148
152
149
-
150
- // Required for updating images
151
- function keyFunction ( d , i ) {
152
- return d . source + i ;
153
- }
154
-
155
-
156
153
var imagesBelow = fullLayout . _imageLowerLayer . selectAll ( 'image' )
157
- . data ( imageDataBelow , keyFunction ) ,
154
+ . data ( imageDataBelow ) ,
158
155
imagesSubplot = fullLayout . _imageSubplotLayer . selectAll ( 'image' )
159
- . data ( imageDataSubplot , keyFunction ) ,
156
+ . data ( imageDataSubplot ) ,
160
157
imagesAbove = fullLayout . _imageUpperLayer . selectAll ( 'image' )
161
- . data ( imageDataAbove , keyFunction ) ;
158
+ . data ( imageDataAbove ) ;
162
159
163
- imagesBelow . enter ( ) . append ( 'image' ) . each ( setImage ) ;
164
- imagesSubplot . enter ( ) . append ( 'image' ) . each ( setImage ) ;
165
- imagesAbove . enter ( ) . append ( 'image' ) . each ( setImage ) ;
160
+ imagesBelow . enter ( ) . append ( 'image' ) ;
161
+ imagesSubplot . enter ( ) . append ( 'image' ) ;
162
+ imagesAbove . enter ( ) . append ( 'image' ) ;
166
163
167
164
imagesBelow . exit ( ) . remove ( ) ;
168
165
imagesSubplot . exit ( ) . remove ( ) ;
169
166
imagesAbove . exit ( ) . remove ( ) ;
170
167
171
- imagesBelow . each ( applyAttributes ) ;
172
- imagesSubplot . each ( applyAttributes ) ;
173
- imagesAbove . each ( applyAttributes ) ;
168
+ imagesBelow . each ( function ( d ) {
169
+ setImage . bind ( this ) ( d ) ;
170
+ applyAttributes . bind ( this ) ( d ) ;
171
+ } ) ;
172
+ imagesSubplot . each ( function ( d ) {
173
+ setImage . bind ( this ) ( d ) ;
174
+ applyAttributes . bind ( this ) ( d ) ;
175
+ } ) ;
176
+ imagesAbove . each ( function ( d ) {
177
+ setImage . bind ( this ) ( d ) ;
178
+ applyAttributes . bind ( this ) ( d ) ;
179
+ } ) ;
174
180
} ;
0 commit comments