Skip to content

Process data uri images synchronously #4105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 28, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 34 additions & 29 deletions src/components/images/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,49 +74,54 @@ module.exports = function draw(gd) {
function setImage(d) {
var thisImage = d3.select(this);

if(this.img && this.img.src === d.source) {
if(this._imgSrc === d.source) {
return;
}

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

var imagePromise = new Promise(function(resolve) {
var img = new Image();
this.img = img;
if(d.source && d.source.slice(0, 5) === 'data:') {
thisImage.attr('xlink:href', d.source);
this._imgSrc = d.source;
} else {
var imagePromise = new Promise(function(resolve) {
var img = new Image();
this.img = img;

// If not set, a `tainted canvas` error is thrown
img.setAttribute('crossOrigin', 'anonymous');
img.onerror = errorHandler;
img.onload = function() {
var canvas = document.createElement('canvas');
canvas.width = this.width;
canvas.height = this.height;
// If not set, a `tainted canvas` error is thrown
img.setAttribute('crossOrigin', 'anonymous');
img.onerror = errorHandler;
img.onload = function() {
var canvas = document.createElement('canvas');
canvas.width = this.width;
canvas.height = this.height;

var ctx = canvas.getContext('2d');
ctx.drawImage(this, 0, 0);
var ctx = canvas.getContext('2d');
ctx.drawImage(this, 0, 0);

var dataURL = canvas.toDataURL('image/png');
var dataURL = canvas.toDataURL('image/png');

thisImage.attr('xlink:href', dataURL);
thisImage.attr('xlink:href', dataURL);

// resolve promise in onload handler instead of on 'load' to support IE11
// see https://github.com/plotly/plotly.js/issues/1685
// for more details
resolve();
};
// resolve promise in onload handler instead of on 'load' to support IE11
// see https://github.com/plotly/plotly.js/issues/1685
// for more details
resolve();
};

thisImage.on('error', errorHandler);

thisImage.on('error', errorHandler);
img.src = d.source;
this._imgSrc = d.source;

img.src = d.source;

function errorHandler() {
thisImage.remove();
resolve();
}
}.bind(this));
function errorHandler() {
thisImage.remove();
resolve();
}
}.bind(this));

gd._promises.push(imagePromise);
gd._promises.push(imagePromise);
}
}

function applyAttributes(d) {
Expand Down
29 changes: 29 additions & 0 deletions test/jasmine/tests/layout_images_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ var mouseEvent = require('../assets/mouse_event');
var jsLogo = 'https://images.plot.ly/language-icons/api-home/js-logo.png';
var pythonLogo = 'https://images.plot.ly/language-icons/api-home/python-logo.png';

// Single red pixel png generated with http://png-pixel.com/
var dataUriImage = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfF' +
'cSJAAAADUlEQVR42mP8z8DwHwAFBQIAX8jx0gAAAABJRU5ErkJggg==';

describe('Layout images', function() {
describe('supplyLayoutDefaults', function() {
var layoutIn,
Expand Down Expand Up @@ -290,6 +294,31 @@ describe('Layout images', function() {

afterEach(destroyGraphDiv);

it('should only create canvas if url image', function(done) {
var originalCreateElement = document.createElement;
var newCanvasElement;
spyOn(document, 'createElement').and.callFake(function(elementType) {
var element = originalCreateElement.call(document, elementType);
if(elementType === 'canvas') {
newCanvasElement = element;
spyOn(newCanvasElement, 'toDataURL');
}
return element;
});

Plotly.relayout(gd, 'images[0].source', dataUriImage).then(function() {
setTimeout(function() {
expect(newCanvasElement).toBeUndefined();
Plotly.relayout(gd, 'images[0].source', pythonLogo).then(function() {
expect(newCanvasElement).eventually.toBeDefined();
expect(newCanvasElement.toDataURL).toHaveBeenCalledTimes(1);
});

done();
}, 500);
});
});

it('should update the image if changed', function(done) {
var img = Plotly.d3.select('image');
var url = img.attr('xlink:href');
Expand Down