Skip to content

Instantly share code, notes, and snippets.

@enjalot
Created May 8, 2013 05:06
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/5538328 to your computer and use it in GitHub Desktop.
Save enjalot/5538328 to your computer and use it in GitHub Desktop.
force layout no collision
{"description":"force layout no collision","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"members.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":true,"loop":false,"restart":true,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"thumbnail":"http://i.imgur.com/FnBYaVH.png"}
/*
*
* working from:
* http://bl.ocks.org/mbostock/1748247
*/
var width = tributary.sw;
var height = tributary.sh;
var radius = d3.scale.sqrt().range([4, 23])
var padding = 6;
var nx = 8;
//var color = d3.scale.category10().domain(d3.range(nx));
var colors = ["#585858", "#000000"]
var color = d3.scale.ordinal()
.domain(d3.range(colors.length))
.range(colors);
var xyGravity = [0, 1.68];
if(tributary.force) {
tributary.force
.gravity(0.0968)
.charge(0.87024)
.friction(0.041762304)
}
tributary.init = function(g) {
var n = tributary.n = 100;
m = 10;
tributary.nodes = d3.range(n).map(function() {
var i = Math.floor(Math.random() * m);
var v = (i + 1) / m * -Math.log(Math.random());
var nx = 9
var y = Math.floor(arguments[1]/nx) * height/nx;
var x = (arguments[1] % nx) * width/nx;
return {
px: x,
py: y,
x: x,
y: y,
radius: radius(v),
color: color(i)
};
});
tributary.force = d3.layout.force()
.size([width, height])
.nodes(tributary.nodes)
.start()
//.stop();
var circle = g.selectAll("circle")
.data(tributary.nodes)
.enter().append("circle")
.attr("r", function(d) { return d.radius; })
.style("fill", function(d) { return d.color; })
.call(tributary.force.drag)
.on("click", function(d) {
d.rsvp = !d.rsvp
})
}
var time = 0;
tributary.run = function(g,t) {
//force.alpha(0.01);
tributary.force.resume();
tributary.force.tick()
if(Math.floor(time) % 4 === 0) {
var i = Math.cos(time*time * Math.PI) * (tributary.n-1);
i = Math.abs(Math.floor(i));
if(i < tributary.nodes.length)
tributary.nodes[i].rsvp = !tributary.nodes[i].rsvp;
}
g.selectAll("circle")
.each(gravity(xyGravity))
//.each(collide(0.1))
.each(eventer)
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; })
.style("fill", function(d,i) { return color(i); })
tributary.force.stop();
time += t;
}
function eventer(d,i) {
if(d.rsvp) {
d.y -= 3.6
}
}
function gravity(g) {
return function(d) {
d.x += g[0]
d.y += g[1]
}
}
// Resolves collisions between d and all other circles.
function collide(alpha) {
var quadtree = d3.geom.quadtree(tributary.nodes);
return function(d) {
var r = d.radius + radius.domain()[1] + padding,
nx1 = d.x - r,
nx2 = d.x + r,
ny1 = d.y - r,
ny2 = d.y + r;
quadtree.visit(function(quad, x1, y1, x2, y2) {
if (quad.point && (quad.point !== d)) {
var x = d.x - quad.point.x,
y = d.y - quad.point.y,
l = Math.sqrt(x * x + y * y),
r = d.radius + quad.point.radius + (d.color !== quad.point.color) * padding;
if (l < r) {
l = (l - r) / l * alpha;
d.x -= x *= l;
d.y -= y *= l;
quad.point.x += x;
quad.point.y += y;
}
}
return x1 > nx2
|| x2 < nx1
|| y1 > ny2
|| y2 < ny1;
});
};
}
#display {
background: black
}
circle {
stroke: #ffffff;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment