@@ -55,17 +55,21 @@ module.exports = function draw(gd) {
5555 function setImage ( d ) {
5656 var thisImage = d3 . select ( this ) ;
5757
58+ if ( this . img && this . img . src === d . source ) {
59+ return ;
60+ }
61+
5862 thisImage . attr ( 'xmlns' , xmlnsNamespaces . svg ) ;
5963
6064 var imagePromise = new Promise ( function ( resolve ) {
6165
6266 var img = new Image ( ) ;
67+ this . img = img ;
6368
6469 // If not set, a `tainted canvas` error is thrown
6570 img . setAttribute ( 'crossOrigin' , 'anonymous' ) ;
6671 img . onerror = errorHandler ;
6772 img . onload = function ( ) {
68-
6973 var canvas = document . createElement ( 'canvas' ) ;
7074 canvas . width = this . width ;
7175 canvas . height = this . height ;
@@ -88,7 +92,7 @@ module.exports = function draw(gd) {
8892 thisImage . remove ( ) ;
8993 resolve ( ) ;
9094 }
91- } ) ;
95+ } . bind ( this ) ) ;
9296
9397 gd . _promises . push ( imagePromise ) ;
9498 }
@@ -146,29 +150,31 @@ module.exports = function draw(gd) {
146150 }
147151 }
148152
149-
150- // Required for updating images
151- function keyFunction ( d , i ) {
152- return d . source + i ;
153- }
154-
155-
156153 var imagesBelow = fullLayout . _imageLowerLayer . selectAll ( 'image' )
157- . data ( imageDataBelow , keyFunction ) ,
154+ . data ( imageDataBelow ) ,
158155 imagesSubplot = fullLayout . _imageSubplotLayer . selectAll ( 'image' )
159- . data ( imageDataSubplot , keyFunction ) ,
156+ . data ( imageDataSubplot ) ,
160157 imagesAbove = fullLayout . _imageUpperLayer . selectAll ( 'image' )
161- . data ( imageDataAbove , keyFunction ) ;
158+ . data ( imageDataAbove ) ;
162159
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' ) ;
166163
167164 imagesBelow . exit ( ) . remove ( ) ;
168165 imagesSubplot . exit ( ) . remove ( ) ;
169166 imagesAbove . exit ( ) . remove ( ) ;
170167
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+ } ) ;
174180} ;
0 commit comments