d3-request
d3-request copied to clipboard
How about adding d3.image for loading images?
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);
});
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?