<meta charset="utf-8">
<title>Zoom + Pan</title>
<style>
svg {
font: 10px sans-serif;
shape-rendering: crispEdges;
}
.bg {
fill: #ddd;
}
.axis path, .axis line {
fill: none;
stroke: #fff;
}
#labels text {
font: 11px sans-serif
stroke: #000;
}
</style>
<body>
<button id="add">Add</button>
<button id="reset">Reset</button>
<script src="http://d3js.org/d3.v3.min.js"></script>
<script>
var color = d3.scale.category20();
var position = [0, 1, 2, 3, 4, 5, 6, 7];
var unit = 10,
boardWidth = 100, //in units
boardHeight = 50,
tileWidth = 10,
tileHeight = 10,
labelPadding = 1;
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = boardWidth * unit,
height = boardHeight * unit;
var tileX = function(i) { return position[i] % boardWidth * tileWidth; }
var tileY = function(i) { return Math.floor(position[i] / boardHeight) * tileHeight;}
var x = d3.scale.linear()
.domain([-50, 50])
.range([0, width]);
var y = d3.scale.linear()
.domain([-50, 50])
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickSize(-height);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(5)
.tickSize(-width);
var zoom = d3.behavior.zoom()
.x(x)
.y(y)
.scaleExtent([1, 2])
.on("zoom", zoomed);
var svg = d3.select("body").append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
.call(zoom);
svg.append("rect")
.attr("class", "bg")
.attr("width", width)
.attr("height", height);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
var dataset = [];
var tiles = svg.append("g")
.attr("clip-path", "url(#clip)")
.attr("id", "blocks")
.selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("fill", function(d, i) { return color(i) })
.attr("x", function(d, i) { return x(tileX(i))})
.attr("y", function(d, i) { return y(tileY(i)+tileHeight)} )
.attr("width", tileWidth * unit)
.attr("height", tileHeight * unit);
function zoomed() {
svg.select(".x.axis").call(xAxis);
svg.select(".y.axis").call(yAxis);
svg.select("#blocks").attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
}
d3.select("#add").on("click", function(){
dataset.push(Math.random());
d3.select("#blocks").selectAll("rect")
.data(dataset)
.enter()
.append("rect")
.attr("fill", function(d, i){ return color(i) })
.attr("x", function(d, i) { return x(tileX(i)) })
.attr("y", function(d, i) { return y(tileY(i)) } )
.attr("width", tileWidth * unit * .01)
.attr("height", tileHeight * unit)
.attr("transform", "translate(" + zoom.translate().map(function(a) { return -a })+ ")")
.transition(1000)
.attr("width", tileWidth * unit).call(zoom);
});
d3.select("#reset").on("click", function(){
dataset = [];
d3.select("#blocks").selectAll("rect").data(dataset).exit().remove();
});
</script>
</body>
<html>
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |