Skip to content

Instantly share code, notes, and snippets.

@takeru
Forked from mbostock/.block
Last active August 29, 2015 14:03
Show Gist options
  • Save takeru/458ae91166fc9133fe29 to your computer and use it in GitHub Desktop.
Save takeru/458ae91166fc9133fe29 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<meta charset="utf-8">
<title>Zoom + Pan</title>
<style>
.overlay {
fill: none;
pointer-events: all;
}
div{border:1px solid black; margin: 10px;}
svg{border:1px solid red; margin: 10px;}
</style>
<body>
<!--
<script src="http://d3js.org/d3.v3.min.js"></script>
-->
using https://raw.githubusercontent.com/mbostock/d3/086b5fc57845105db8adc393c74cbbbce588d30d/d3.js
<script src="https://raw.githubusercontent.com/mbostock/d3/086b5fc57845105db8adc393c74cbbbce588d30d/d3.js"></script>
<div id="div1"></div>
<div id="div2"></div>
<div id="div3"></div>
<div id="div4"></div>
<script>
function make(selector)
{
var width = 500,
height = 500;
var randomX = d3.random.normal(width / 2, 80),
randomY = d3.random.normal(height / 2, 80);
var data = d3.range(2000).map(function() {
return [
randomX(),
randomY()
];
});
var svg = d3.select(selector).append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.call(d3.behavior.zoom().scaleExtent([1, 8]).on("zoom", zoom))
.append("g");
svg.append("rect")
.attr("class", "overlay")
.attr("width", width)
.attr("height", height);
svg.selectAll("circle")
.data(data)
.enter().append("circle")
.attr("r", 2.5)
.attr("transform", function(d) { return "translate(" + d + ")"; });
function zoom() {
svg.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
}
}
make("#div1");
make("#div2");
make("#div3");
make("#div4");
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment