Skip to content

Commit 9b8a843

Browse files
committed
Add a workaround to image's onload event not firing when image is cached
1 parent 09eb1d0 commit 9b8a843

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

src/traces/image/hover.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module.exports = function hoverPoints(pointData, xval, yval) {
3232
if(trace._isFromZ) {
3333
pixel = cd0.z[ny][nx];
3434
} else if(trace._isFromSource) {
35-
pixel = trace._canvas.getContext('2d').getImageData(nx, ny, 1, 1).data;
35+
pixel = trace._canvas.el.getContext('2d').getImageData(nx, ny, 1, 1).data;
3636
}
3737

3838
// return early if pixel is undefined

src/traces/image/plot.js

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -159,27 +159,41 @@ module.exports = function plot(gd, plotinfo, cdimage, imageLayer) {
159159
if(trace._isFromZ) {
160160
resolve();
161161
} else if(trace._isFromSource) {
162-
// Transfer image to a canvas to access pixel information
163-
trace._canvas = trace._canvas || document.createElement('canvas');
164-
trace._canvas.width = w;
165-
trace._canvas.height = h;
166-
var context = trace._canvas.getContext('2d');
167-
168-
var sel;
169-
if(fastImage) {
170-
// Use the displayed image
171-
sel = image3;
162+
// Check if canvas already exists
163+
if(
164+
trace._canvas &&
165+
trace._canvas.el.width === w &&
166+
trace._canvas.el.height === h &&
167+
trace._canvas.source === trace.source
168+
) {
169+
resolve();
172170
} else {
173-
// Use the hidden image
174-
sel = d3.select(image3[0][1]);
175-
}
171+
// Create a canvas and transfer image onto it to access pixel information
172+
var canvas = document.createElement('canvas');
173+
canvas.width = w;
174+
canvas.height = h;
175+
var context = canvas.getContext('2d');
176+
177+
var sel;
178+
if(fastImage) {
179+
// Use the displayed image
180+
sel = image3;
181+
} else {
182+
// Use the hidden image
183+
sel = d3.select(image3[0][1]);
184+
}
176185

177-
var image = sel.node();
178-
image.onload = function() {
179-
context.drawImage(image, 0, 0);
180-
resolve();
181-
};
182-
sel.attr('xlink:href', trace.source);
186+
var image = sel.node();
187+
image.onload = function() {
188+
context.drawImage(image, 0, 0);
189+
trace._canvas = {
190+
el: canvas,
191+
source: trace.source
192+
};
193+
resolve();
194+
};
195+
sel.attr('xlink:href', trace.source);
196+
}
183197
}
184198
})
185199
.then(function() {
@@ -188,7 +202,7 @@ module.exports = function plot(gd, plotinfo, cdimage, imageLayer) {
188202
if(trace._isFromZ) {
189203
canvas = drawMagnifiedPixelsOnCanvas(function(i, j) {return z[j][i];});
190204
} else if(trace._isFromSource) {
191-
var context = trace._canvas.getContext('2d');
205+
var context = trace._canvas.el.getContext('2d');
192206
var data = context.getImageData(0, 0, w, h).data;
193207
canvas = drawMagnifiedPixelsOnCanvas(function(i, j) {
194208
var index = 4 * (j * w + i);

0 commit comments

Comments
 (0)