Skip to content

Instantly share code, notes, and snippets.

@DinoJay
Created March 11, 2016 10:18
Show Gist options
  • Save DinoJay/683d2626ae1e6cfd0902 to your computer and use it in GitHub Desktop.
Save DinoJay/683d2626ae1e6cfd0902 to your computer and use it in GitHub Desktop.
SVG Semantic Zooming
license: gpl-3.0
<!DOCTYPE html>
<meta charset="utf-8">
<title>Zoom + Pan</title>
<style>
.divLay {
position: absolute
}
.recto {
position: absolute;
background: red
}
svg {
font: 10px sans-serif;
}
.overlay {
fill: none;
pointer-events: all;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<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(2000).map(function() {
return [
randomX(),
randomY()
];
});
var x = d3.scale.linear()
.domain([0, width])
.range([0, width]);
var y = d3.scale.linear()
.domain([0, height])
.range([height, 0]);
var div = d3.select("body").append("div")
.attr("class", "divLay")
.style("width", width)
.attr("style", height)
.call(d3.behavior.zoom().x(x).y(y).scaleExtent([1, 12])
.on("zoom", zoom));
//var svg = d3.select("body").append("svg")
// .attr("width", width)
// .attr("height", height)
// .append("g")
// .call(d3.behavior.zoom().x(x).y(y).scaleExtent([1, 37]).on("zoom", zoom));
//svg.append("rect")
// .attr("class", "overlay")
// .attr("width", width)
// .attr("height", height);
//var circle = svg.selectAll("rect.rectangle")
// .data(data)
// .enter().append("rect")
// .attr("class", "rectangle")
// .attr("width", 20)
// .attr("height", 20)
// .attr("transform", transform);
var drect = div.selectAll("div.recto")
.data(data)
.enter().append("div")
.attr("class", "recto")
.style("width", 20)
.style("height", 20)
.style("opacity", 0.552)
.style("transform", transformHtml)
function zoom() {
//circle.attr("transform", transform);
drect.style("transform", transformHtml);
}
function transformHtml(d) {
return "translate(" + x(d[0]) + "px," + y(d[1]) + "px)";
}
function transform(d) {
return "translate(" + x(d[0]) + "," + y(d[1]) + ")";
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment