block by fil 402491c9a3fbeaf54e47179e90d1c7af

Pan & Zoom III + fix

Full Screen

This example demonstrates using d3-zoom to pan and zoom an SVG element by applying an SVG transform using transform.toString. The zoom behavior is applied to an invisible rect overlaying the SVG element; this ensures that it receives input, and that the pointer coordinates are not affected by the zoom behavior’s transform.

forked from mbostock‘s block: Pan & Zoom III

forked from Fil‘s block: Pan & Zoom III

index.html

<!DOCTYPE html>
<meta charset="utf-8">
<svg width="960" height="500"></svg>
<script src="https://unpkg.com/d3-selection@2"></script>
<script src="https://unpkg.com/d3-array@2"></script>
<script src="https://unpkg.com/d3-dispatch@2"></script>
<script src="https://unpkg.com/d3-drag@2"></script>
<script src="https://unpkg.com/d3-transition@2"></script>
<script src="https://files-eqsttefcg.vercel.app/d3-zoom.js"></script>

<script>

var svg = d3.select("svg"),
    width = +svg.attr("width"),
    height = +svg.attr("height");

var points = d3.range(2000).map(phyllotaxis(10));

var g = svg.append("g");

g.selectAll("circle")
    .data(points)
  .enter().append("circle")
    .attr("cx", function(d) { return d[0]; })
    .attr("cy", function(d) { return d[1]; })
    .attr("r", 2.5);

svg.append("rect")
    .attr("width", width)
    .attr("height", height)
    .style("fill", "none")
    .style("pointer-events", "all")
    .call(d3.zoom()
        .scaleExtent([1 / 2, 4])
        .on("zoom", zoomed));

function zoomed(event) {
  g.attr("transform", event.transform);
}

function phyllotaxis(radius) {
  var theta = Math.PI * (3 - Math.sqrt(5));
  return function(i) {
    var r = radius * Math.sqrt(i), a = theta * i;
    return [
      width / 2 + r * Math.cos(a),
      height / 2 + r * Math.sin(a)
    ];
  };
}

</script>