Skip to content

Commit c310c92

Browse files
committed
Don't create promise or invoke onload for data uri images
- Stash prior input image src string as the this._imgSrc property - Pull datauti code outside of the promise, so that we skip creating the promise and skip creating an Image() object, and skip running the onload function.
1 parent 86a3fc0 commit c310c92

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

Diff for: src/components/images/draw.js

+16-17
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,20 @@ module.exports = function draw(gd) {
7474
function setImage(d) {
7575
var thisImage = d3.select(this);
7676

77-
if(this.img && this.img.src === d.source) {
77+
if(this._imgSrc === d.source) {
7878
return;
7979
}
8080

8181
thisImage.attr('xmlns', xmlnsNamespaces.svg);
8282

83-
var imagePromise = new Promise(function(resolve) {
84-
var img = new Image();
85-
this.img = img;
83+
if(d.source && d.source.slice(0, 5) === 'data:') {
84+
thisImage.attr('xlink:href', d.source);
85+
this._imgSrc = d.source;
86+
} else {
87+
var imagePromise = new Promise(function(resolve) {
88+
var img = new Image();
89+
this.img = img;
8690

87-
if(d.source && d.source.slice(0, 5) === 'data:') {
88-
img.src = d.source;
89-
thisImage.attr('xlink:href', d.source);
90-
resolve();
91-
} else {
9291
// If not set, a `tainted canvas` error is thrown
9392
img.setAttribute('crossOrigin', 'anonymous');
9493
img.onerror = errorHandler;
@@ -110,19 +109,19 @@ module.exports = function draw(gd) {
110109
resolve();
111110
};
112111

113-
114112
thisImage.on('error', errorHandler);
115113

116114
img.src = d.source;
117-
}
115+
this._imgSrc = d.source;
118116

119-
function errorHandler() {
120-
thisImage.remove();
121-
resolve();
122-
}
123-
}.bind(this));
117+
function errorHandler() {
118+
thisImage.remove();
119+
resolve();
120+
}
121+
}.bind(this));
124122

125-
gd._promises.push(imagePromise);
123+
gd._promises.push(imagePromise);
124+
}
126125
}
127126

128127
function applyAttributes(d) {

0 commit comments

Comments
 (0)