Skip to content

Instantly share code, notes, and snippets.

@zmaril
Created July 2, 2012 16:06
Show Gist options
  • Save zmaril/3033990 to your computer and use it in GitHub Desktop.
Save zmaril/3033990 to your computer and use it in GitHub Desktop.
Cardinal distortion

This uses cardinal distortion to explore the United States. Suggested by Panos Ipeirotis.

(function() {
d3.fisheye = {
scale: function(scaleType) {
return d3_fisheye_scale(scaleType(), 3, 0);
},
circular: function() {
var radius = 200,
distortion = 2,
k0,
k1,
focus = [0, 0];
function fisheye(d) {
var dx = d.x - focus[0],
dy = d.y - focus[1],
dd = Math.sqrt(dx * dx + dy * dy);
if (!dd || dd >= radius) return {x: d.x, y: d.y, z: 1};
var k = k0 * (1 - Math.exp(-dd * k1)) / dd * .75 + .25;
return {x: focus[0] + dx * k, y: focus[1] + dy * k, z: Math.min(k, 10)};
}
function rescale() {
k0 = Math.exp(distortion);
k0 = k0 / (k0 - 1) * radius;
k1 = distortion / radius;
return fisheye;
}
fisheye.radius = function(_) {
if (!arguments.length) return radius;
radius = +_;
return rescale();
};
fisheye.distortion = function(_) {
if (!arguments.length) return distortion;
distortion = +_;
return rescale();
};
fisheye.focus = function(_) {
if (!arguments.length) return focus;
focus = _;
return fisheye;
};
return rescale();
}
};
function d3_fisheye_scale(scale, d, a) {
function fisheye(_) {
var x = scale(_),
left = x < a,
v,
range = d3.extent(scale.range()),
min = range[0],
max = range[1],
m = left ? a - min : max - a;
if (m == 0) m = max - min;
return (left ? -1 : 1) * m * (d + 1) / (d + (m / Math.abs(x - a))) + a;
}
fisheye.distortion = function(_) {
if (!arguments.length) return d;
d = +_;
return fisheye;
};
fisheye.focus = function(_) {
if (!arguments.length) return a;
a = +_;
return fisheye;
};
fisheye.copy = function() {
return d3_fisheye_scale(scale.copy(), d, a);
};
fisheye.nice = scale.nice;
fisheye.ticks = scale.ticks;
fisheye.tickFormat = scale.tickFormat;
return d3.rebind(fisheye, scale, "domain", "range");
}
})();
<!DOCTYPE html>
<meta charset="utf-8">
<script src="http://d3js.org/d3.v2.min.js"></script>
<script src="fisheye.js"></script>
<script src="http://underscorejs.org/underscore-min.js"></script>
<style>
.background {
fill: none;
pointer-events: all;
}
#states {
fill: #aaa;
stroke: #fff;
stroke-width: 1.5px;
}
#states path:hover {
stroke: white;
}
</style>
<body>
<script src="index.js"></script>
</body>
</html>
var width = 960,
height = 500,
centered;
var projection = d3.geo.mercator()
.scale(width)
.translate([0, 0]);
var path = d3.geo.path()
.projection(projection);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
svg.append("rect")
.attr("class", "background")
.attr("width", width)
.attr("height", height);
var states = svg.append("g")
.attr("transform", "translate(" + width / 2 + "," + height / 2 + ")")
.append("g")
.attr("id", "states");
d3.json("world-countries.json", function(json) {
states.selectAll("path")
.data(json.features)
.enter().append("path")
.attr("d", path)
.each(function(d,i){
d.org = d.geometry.coordinates;
});
});
svg
.on("mousemove", refish)
.on("mousein", refish)
.on("mouseout", refish);
var xFisheye = d3.fisheye.scale(d3.scale.identity)
.domain([-width/2,width/2]).focus(10);
var yFisheye = d3.fisheye.scale(d3.scale.identity)
.domain([-height/2,height/2]).focus(10);
function fishPolygon(polygon){
return _.map(polygon, function(list){
return _.map(list, function(tuple){
var p = projection(tuple);
var c = [xFisheye(p[0]),yFisheye(p[1])];
return projection.invert(c);
}); });
}
function refish(d) {
var m = d3.mouse(this);
m = [m[0]-width/2,m[1]-height/2];
xFisheye.focus(m[0]);
yFisheye.focus(m[1]);
states.selectAll("path")
.attr("d", function(d){
var clone = _.extend({},d);
var type = clone.geometry.type;
var processed = null;
if (type === "Polygon")
processed = fishPolygon(d.org);
else
processed = _.map(d.org, fishPolygon);
clone.geometry.coordinates = processed;
return path(clone);
});
}
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment