Skip to content

Instantly share code, notes, and snippets.

@nkint
Created October 25, 2016 17:26
Show Gist options
  • Save nkint/55a7f53ae6bcdb4362e8232270a365da to your computer and use it in GitHub Desktop.
Save nkint/55a7f53ae6bcdb4362e8232270a365da to your computer and use it in GitHub Desktop.
Not working zoom buttons
<!DOCTYPE html>
<meta charset="utf-8">
<svg width="960" height="500"></svg>
<div id="gui"></div>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script>
var svg = d3.select("svg"),
width = +svg.attr("width"),
height = +svg.attr("height"),
transform = d3.zoomIdentity;
svg.style("border", "solid 1px black");
var points = d3.range(2000).map(phyllotaxis(10));
var g = svg.append("g");
var _zoom = d3.zoom()
.scaleExtent([1 / 2, 8])
.on("zoom", zoomed);
var container = g.selectAll("circle")
.data(points)
.enter().append("circle")
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.attr("r", 2.5)
svg.call(_zoom);
function zoomed() {
g.attr("transform", d3.event.transform);
}
function dragged(d) {
d3.select(this).attr("cx", d.x = d3.event.x).attr("cy", d.y = d3.event.y);
}
function phyllotaxis(radius) {
var theta = Math.PI * (3 - Math.sqrt(5));
return function(i) {
var r = radius * Math.sqrt(i), a = theta * i;
return {
x: width / 2 + r * Math.cos(a),
y: height / 2 + r * Math.sin(a)
};
};
}
var gui = d3.select("#gui")
gui.append("span")
.classed("zoom in", true)
.text("+")
.on("click", function() { _zoom.scaleBy(container, 2); })
gui.append("span")
.classed("zoom out", true)
.text("-")
.on("click", function(){ _zoom.scaleBy(container, 0.5); })
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment