Related examples:
- SVG geometric zooming
- SVG semantic zooming
Related examples:
<!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> |