d3 / d3-request

A convenient alternative to XMLHttpRequest.
BSD 3-Clause "New" or "Revised" License
110 stars 54 forks source link

How about adding d3.image for loading images? #11

Open gka opened 8 years ago

gka commented 8 years ago

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);
});
mbostock commented 8 years ago

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 commented 8 years ago

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