Skip to content

Instantly share code, notes, and snippets.

@Fil
Last active February 3, 2017 09:01
Show Gist options
  • Save Fil/0f541f8d9995738f2ad6143cc372cded to your computer and use it in GitHub Desktop.
Save Fil/0f541f8d9995738f2ad6143cc372cded to your computer and use it in GitHub Desktop.
d3.queue image loader
license: mit
Text contents
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<script>
d3.imageload = function(src, cb) {
image = new Image();
image.src = src;
image.onload = function() { cb(null, image); };
image.onerror = cb;
}
d3.queue()
.defer(d3.text, 'hola.txt')
.defer(d3.imageload, 'thumbnail.png')
.await(function(err, text, image) {
let msg = (err) ? "Error: " + err : `Image and text loaded (${text})`;
d3.select('body').append('p')
.html(msg)
.style("font-size", 14)
.style("font-family", "monospace");
if(err) return;
var canvas = d3.select('body').append('canvas')
.attr('width', image.width)
.attr('height', image.height),
context = canvas.node().getContext('2d');
context.drawImage(image,0,0);
});
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment