Skip to content

Use URLs directly for layout images #7199

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 3 commits into from
Oct 8, 2024
Merged
Changes from all 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
1 change: 1 addition & 0 deletions draftlogs/7199_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Do not convert url-sourced layout images to data uri unless we're in staticPlot mode, to improve interactivity when images are changed with zoom/pan [[#7199](https://github.com/plotly/plotly.js/pull/7199)]
2 changes: 1 addition & 1 deletion src/components/images/draw.js
Original file line number Diff line number Diff line change
@@ -73,7 +73,7 @@ module.exports = function draw(gd) {

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

if(d.source && d.source.slice(0, 5) === 'data:') {
if(!gd._context.staticPlot || (d.source && d.source.slice(0, 5) === 'data:')) {
thisImage.attr('xlink:href', d.source);
this._imgSrc = d.source;
} else {
42 changes: 35 additions & 7 deletions test/jasmine/tests/layout_images_test.js
Original file line number Diff line number Diff line change
@@ -319,9 +319,8 @@ describe('Layout images', function() {
var gd;
var data = [{ x: [1, 2, 3], y: [1, 2, 3] }];

beforeEach(function(done) {
gd = createGraphDiv();
Plotly.newPlot(gd, data, {
var layoutFn = function() {
return {
images: [{
source: jsLogo,
x: 2,
@@ -331,12 +330,17 @@ describe('Layout images', function() {
}],
width: 500,
height: 400
}).then(done);
};
}

beforeEach(function(done) {
gd = createGraphDiv();
Plotly.newPlot(gd, data, layoutFn()).then(done);
});

afterEach(destroyGraphDiv);

it('should only create canvas if url image', function(done) {
it('should only create canvas if url image and staticPlot', function(done) {
var originalCreateElement = document.createElement;
var newCanvasElement;
spyOn(document, 'createElement').and.callFake(function(elementType) {
@@ -350,7 +354,21 @@ describe('Layout images', function() {

Plotly.relayout(gd, 'images[0].source', dataUriImage)
.then(function() {
expect(newCanvasElement).toBeUndefined();
expect(newCanvasElement).withContext('non-static data uri').toBeUndefined();

return Plotly.relayout(gd, 'images[0].source', jsLogo);
})
.then(function() {
expect(newCanvasElement).withContext('non-static url').toBeUndefined();

return Plotly.newPlot(gd, data, layoutFn(), {staticPlot: true});
})
.then(function() {
newCanvasElement = undefined;
return Plotly.relayout(gd, 'images[0].source', dataUriImage);
})
.then(function() {
expect(newCanvasElement).withContext('static data uri').toBeUndefined();

return Plotly.relayout(gd, 'images[0].source', jsLogo);
})
@@ -392,11 +410,21 @@ describe('Layout images', function() {
.then(done, done.fail);
});

it('should remove the image tag if an invalid source', function(done) {
it('should remove the image tag if an invalid source and staticPlot', function(done) {
var selection = d3Select('image');
expect(selection.size()).toBe(1);

Plotly.relayout(gd, 'images[0].source', 'invalidUrl')
.then(function() {
var newSelection = d3Select('image');
expect(newSelection.size()).toBe(1);
})
.then(function() {
return Plotly.newPlot(gd, data, layoutFn(), {staticPlot: true});
})
.then(function() {
return Plotly.relayout(gd, 'images[0].source', 'invalidUrl');
})
.then(function() {
var newSelection = d3Select('image');
expect(newSelection.size()).toBe(0);