Skip to content

Instantly share code, notes, and snippets.

@tilgovi
Forked from mbostock/.block
Created June 20, 2012 23:09
Show Gist options
  • Save tilgovi/2962818 to your computer and use it in GitHub Desktop.
Save tilgovi/2962818 to your computer and use it in GitHub Desktop.
Fisheye Grid

The above grid shows the effect of fisheye distortion. Move the mouse to change the focal point.

<!DOCTYPE html>
<meta charset="utf-8">
<style>
.background {
fill: none;
pointer-events: all;
}
path {
fill: none;
stroke: #333;
}
</style>
<body>
<script src="http://d3js.org/d3.v2.min.js?2.8.1"></script>
<script src="http://bost.ocks.org/mike/fisheye/fisheye.js?0.0.2"></script>
<script>
var width = 960,
height = 500,
xStepsBig = d3.range(10, width, 20),
yStepsBig = d3.range(10, height, 20),
xStepsSmall = d3.range(0, width + 6, 6),
yStepsSmall = d3.range(0, height + 6, 6);
var fisheye = d3.fisheye();
var line = d3.svg.line()
.x(function(d) { return d.x; })
.y(function(d) { return d.y; });
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height)
.append("g")
.attr("transform", "translate(-.5,-.5)");
svg.append("rect")
.attr("class", "background")
.attr("width", width)
.attr("height", height);
svg.selectAll(".x")
.data(xStepsBig)
.enter().append("path")
.attr("class", "x")
.datum(function(x) { return yStepsSmall.map(function(y) { return {x: x, y: y}; }); });
svg.selectAll(".y")
.data(yStepsBig)
.enter().append("path")
.attr("class", "y")
.datum(function(y) { return xStepsSmall.map(function(x) { return {x: x, y: y}; }); });
var path = svg.selectAll("path")
.attr("d", line);
svg.on("mousemove", function() {
fisheye.center(d3.mouse(this));
d3.event.preventDefault();
path.attr("d", function(d) { return line(d.map(fisheye)); });
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment