Skip to content

Instantly share code, notes, and snippets.

@enjalot
Last active December 22, 2015 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enjalot/6473448 to your computer and use it in GitHub Desktop.
Save enjalot/6473448 to your computer and use it in GitHub Desktop.
fisheye
{"description":"fisheye","endpoint":"","display":"svg","public":true,"require":[{"name":"fisheye","url":"https://raw.github.com/d3/d3-plugins/master/fisheye/fisheye.js"}],"tab":"edit","display_percent":0.6066666666666666,"play":true,"loop":true,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"fileconfigs":{"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"eye.svg":{"default":true,"vim":false,"emacs":false,"fontSize":12},"fish.svg":{"default":true,"vim":false,"emacs":false,"fontSize":12},"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"fisheye.js":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"thumbnail":"http://i.imgur.com/9nlPXUd.gif","ajax-caching":true}
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
// https://raw.githubusercontent.com/d3/d3-plugins/master/fisheye/fisheye.js
(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: dd >= radius ? 1 : 10};
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,
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");
}
})();
var fishD = d3.select("#fish").attr("d");
d3.select("#fish").style("display", "none");
var eye = d3.select("#eye").style("display", "none");
var fisheye = d3.fisheye.circular()
.radius(tributary.anim(573, 300))
.distortion(tributary.anim(2, 5.94985));
tributary.loop_type = "pingpong";
tributary.duration = 2000;
var data = _.map(d3.range(100), function(d) {
return {
i: d,
x: d % 10 * tributary.anim(84, 60),
y: parseInt(d/10) * 77
}
});
var svg = d3.select("svg");
var fishies = svg.selectAll("g.fish")
.data(data)
.enter()
.append("g")
.classed("fish", true);
fishies.attr("transform", function(d) {
return "translate(" + [d.x, d.y] + ")";
})
fishies.append("path")
.attr("d", fishD)
.style({
fill: "#8709AF",
"fill-opacity": 0.4,
stroke: "#125EBE",
"stroke-opacity": 0.8,
"stroke-width": 3
});
fisheye.focus([tributary.sw/2-50, tributary.sh/2 - 50]);
fishies.attr("transform", function(d) {
var fe = fisheye(d);
return "translate(" + [fe.x, fe.y] + ")"
+ "scale(" + fe.z + ")";
})
svg.on("mousemove", function() {
var mouse = d3.mouse(this);
eye.attr("transform", "translate(" + [mouse[0] - 50, mouse[1] - 30] + ")")
fisheye.focus(mouse);
fishies.attr("transform", function(d) {
var fe = fisheye(d); //{x: ..., y: ...
return "translate(" + [fe.x, fe.y] + ")"
+ "scale(" + fe.z + ")";
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment