Skip to content

Instantly share code, notes, and snippets.

@emeeks
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emeeks/11328778 to your computer and use it in GitHub Desktop.
Save emeeks/11328778 to your computer and use it in GitHub Desktop.
Dynamic Image Gallery

More D3 without SVG. Create a hundred circles with canvas and turn them into imgs using toDataUrl, and then make a gallery out of them, which zooms in on mouseover.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>D3 in Action Gallery Example 1</title>
<meta charset="utf-8" />
</head>
<script src="http://d3js.org/d3.v3.min.js" type="text/javascript"></script>
<script src="http://d3js.org/colorbrewer.v1.min.js"></script>
<style>
#gallery {
position: relative;
width:100%;
}
img.infinite {
position: absolute;
}
</style>
<body onload="galleryExample1()">
<div id="controls">
</div>
</body>
<footer>
<script>
function galleryExample1() {
imageArray = [];
d3.select("#controls").append("canvas").attr("height", 500).attr("width", 500);
var context = d3.select("canvas").node().getContext("2d");
context.textAlign = "center";
context.font="200px Georgia";
colorScale = d3.scale.quantize().domain([0,1]).range(colorbrewer.Reds[7]);
lineScale = d3.scale.quantize().domain([0,1]).range([10,40]);
for (var x=0;x<100;x++) {
context.clearRect(0,0,500,500);
context.strokeStyle = colorScale(Math.random());
context.lineWidth = lineScale(Math.random());
context.fillStyle = colorScale(Math.random());
context.beginPath();
context.arc(250,250,200,0,2*Math.PI);
context.fill();
context.stroke();
context.fillStyle = "black";
context.fillText(x,250,280);
var dataURL = d3.select("canvas").node().toDataURL();
imageArray.push({x: x, data: dataURL});
}
d3.select("canvas").remove();
d3.select("body")
.append("div").attr("class", "gallery")
.selectAll("img").data(imageArray).enter().append("img")
.attr("class", "infinite")
.attr("src", function(d) {return d.data})
.on("mouseover", highlightImage)
.on("mouseout", dehighlightImage)
redrawGallery();
function highlightImage(d) {
var newWidth = parseFloat(d3.select("div.gallery").node().clientWidth);
var imageSize = newWidth / 8;
d3.select(this).transition().duration(500).style("width", imageSize * 2)
this.parentNode.appendChild(this)
}
function dehighlightImage(d) {
var newWidth = parseFloat(d3.select("div.gallery").node().clientWidth);
var imageSize = newWidth / 8;
d3.select(this).transition().duration(500).style("width", imageSize)
}
function redrawGallery() {
var newHeight = parseFloat(d3.select("div.gallery").node().clientHeight);
var newWidth = parseFloat(d3.select("div.gallery").node().clientWidth);
var imageSize = newWidth / 8;
d3.selectAll("img")
.style("width", newWidth / 8)
.style("top", function(d) {return Math.floor(d.x / 8) * imageSize})
.style("left", function(d) {return d.x%8 * imageSize})
}
window.onresize = function(event) {
redrawGallery();
}
}
</script>
</footer>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment