Skip to content

Instantly share code, notes, and snippets.

@georules
Created September 12, 2013 19:49
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save georules/6542901 to your computer and use it in GitHub Desktop.
antisocial dots 2
{"description":"antisocial dots 2","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/knLP7VF.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")
svg.append("rect").attr({width: "100%", height: "100%"}).style("fill", "#655BAA");
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)
if (d.fy >0) color = "#00ff00"
if (d.fy <=0) color = "#1F00FF"
color = d3.interpolateRgb("#DA1E1E","#000000")(d.vy)
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