Skip to content

Instantly share code, notes, and snippets.

@poezn
Last active August 29, 2015 14: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 poezn/cbab887df0f84be35056 to your computer and use it in GitHub Desktop.
Save poezn/cbab887df0f84be35056 to your computer and use it in GitHub Desktop.
Tributary inlet
{"description":"Tributary inlet","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"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}},"fullscreen":false,"play":true,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"pingpong","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"ajax-caching":true,"inline-console":true,"thumbnail":"http://i.imgur.com/rJpZA8f.png"}
var width = 960,
height = 600,
interval = 1000;
var nodes = [],
foci = [{x: 150, y: 150}, {x: 350, y: 150}, {x: 550, y: 150}],
start = [{x: 200, y: height}, {x: 370, y: height}, {x: 600, y: height}];
var fill = d3.scale.category10()
var timeFired = new Date(),
hasFired = false;
tributary.init = function(g) {
var node = g.selectAll("circle");
node
.attr("cx", function(d) { return d.x; })
.attr("cy", function(d) { return d.y; });
var force = d3.layout.force()
.nodes(nodes)
.links([])
.friction(0.5)
.gravity(0)
.size([width, height])
.on("tick", tick);
function tick(e) {
var kX = 0.024 * e.alpha;
var kY = 0.1 * e.alpha;
// Push nodes toward their designated focus.
nodes.forEach(function(o, i) {
o.y += (foci[o.id].y - o.y) * kY;
o.x += (foci[o.id].x - o.x) * kX
});
}
}
tributary.run = function(g, t) {
var dt = new Date()
if (hasFired || dt - timeFired < interval) {
return;
} else {
timeFired = dt;
hasFired = true;
}
console.log(dt);
var idx = Math.floor((Math.random() * foci.length));
console.log(idx);
nodes.push({id: idx, x: start[idx].x, y: height, idx: i});
console.log(nodes.length)
if (nodes.length > 10) {
nodes.shift();
}
force.start();
node = node.data(nodes);
node.exit().remove();
node.enter().append("circle")
.attr("class", "node")
.attr("cx", function(d) { return start[d.id].x; })
.attr("cy", function(d) { return height; })
.attr("r", 8)
.style("fill", function(d) { return fill(d.id); });
console.log(idx);
hasFired = false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment