Skip to content

Instantly share code, notes, and snippets.

@georules georules/_.md

Created Sep 10, 2013
Embed
What would you like to do?
antisocial dots
{"description":"antisocial dots","endpoint":"","display":"svg","public":true,"require":[{"name":"seedrandom","url":"http://davidbau.com/encode/seedrandom-min.js"},{"name":"protovis","url":"http://mbostock.github.io/protovis/protovis-r3.2.js"}],"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},"dots.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,"thumbnail":"http://i.imgur.com/S3PtwvR.gif","inline-console":false}
//nbody code from http://mbostock.github.io/protovis/ex/nbody.html
Math.seedrandom("djhkjsdhfksdf");
var charge = -100; // try more for more chaos
var n = 500;
tributary.delay = 40;
var w = tributary.sw,
h = tributary.sh,
nodes = pv.range(n).map(function(i) {
return {x: w * Math.random(),
y: h * Math.random(),
r: 10 + Math.random() * 8};
});
var sim = pv.simulation(nodes)
.force(pv.Force.charge(charge))
.constraint(pv.Constraint.collision(function(d) {return d.r}))
.stabilize();
var svg = d3.select("svg")
var render = function(data) {
var circle = svg.selectAll("circle").data(data).attr(
{
"cx":function(d) {
if (d.x > w) d.x = w
if (d.x < 0) d.x = 0
return d.x.toFixed(5);
},
"cy":function(d) {
if (d.y > h) d.y = h
if (d.y < 0) d.y = 0
return d.y.toFixed(5)
},
"r":function(d) {return d.r}
})
.style(
{
fill: function(d) {
heat = Math.sqrt(d.vx * d.vx + d.vy * d.vy)/10
color = d3.interpolateHsl("#4C24EB","#34EE44")(heat)
return color;
}
})
}
var tlast = 0
tributary.run = function(g,t) {
tdiff = t-tlast
if (tdiff < 10) {
sim.step();
render(nodes);
}
tlast = t
}
var circle = svg.selectAll("circle").data(nodes).enter().append("circle").attr(
{
"cx":function(d) {return d.x},
"cy":function(d) {return d.y},
"r":function(d) {return d.r}
})
.style(
{
fill: function(d) {return d.c}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.