d3-request icon indicating copy to clipboard operation
d3-request copied to clipboard

How about adding d3.image for loading images?

Open gka opened this issue 9 years ago • 2 comments

If you want to pre-load images for use in canvas, you have to do this

var imageObj = new Image();
imageObj.onload = function() {
     // do sth, e.g.
     context.drawImage(this, 0, 0);
};
imageObj.src = url;

d3-request could abstract this, so I can load images in a queue etc

d3.image(url, function(err, img) {
    context.drawImage(img, 0,0);
});

gka avatar Apr 21 '16 18:04 gka

See mbostock/d3#1923 for earlier discussion.

One problem is that Image does not support the same API as XMLHttpRequest (for example, setting custom headers on the request, or aborting the request). There’s a way to do with the blob responseType and URL.createObjectURL.

Alternatively, d3.image could be an unrelated, though similar API to d3-request. Maybe it would only support the simple case you mention. That might be a little confusing, but, maybe it’s still useful?

mbostock avatar Apr 21 '16 18:04 mbostock

FWIW, I’ve added d3.image to d3-fetch.

mbostock avatar Oct 24 '16 21:10 mbostock