Skip to content

Instantly share code, notes, and snippets.

@eweitnauer
Last active December 25, 2015 07:29
Show Gist options
  • Save eweitnauer/6939999 to your computer and use it in GitHub Desktop.
Save eweitnauer/6939999 to your computer and use it in GitHub Desktop.
SVG Geometric Zooming
<!DOCTYPE html>
<meta charset="utf-8">
<title>Zoom + Pan</title>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="http://graspingmath.com/libs/css-fpsmeter.js"></script>
<style>
.overlay {
fill: none;
pointer-events: all;
}
</style>
<body>
<script>
var width = 960,
height = 500;
var randomX = d3.random.normal(width / 2, 80),
randomY = d3.random.normal(height / 2, 80);
var data = d3.range(100).map(function() {
return [
randomX(),
randomY()
];
});
var svg = d3.select("body").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("text")
.data(data)
.enter().append("text")
.text(function() {return Math.round(Math.random()*10) } )
.style('font-size', '30px')
.style('font-style', 'italic')
.style('font-family', 'serif')
.attr("transform", function(d) { return "translate(" + d + ")"; });
function zoom() {
svg.attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment