block by emeeks 11328778

Dynamic Image Gallery

Full Screen

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.

index.html

<html xmlns="//www.w3.org/1999/xhtml">
<head>
  <title>D3 in Action Gallery Example 1</title>
  <meta charset="utf-8" />
</head>
<script src="//d3js.org/d3.v3.min.js" type="text/javascript"></script>
<script src="//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>