Skip to content

Instantly share code, notes, and snippets.

@bollig
Created September 10, 2013 21:18
Show Gist options
  • Save bollig/6515837 to your computer and use it in GitHub Desktop.
Save bollig/6515837 to your computer and use it in GitHub Desktop.
dots more colors
{"description":"dots more colors","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/R9bNLmk.gif","inline-console":false}
//nbody code from http://mbostock.github.io/protovis/ex/nbody.html
Math.seedrandom("seeds");
var charge = 100; // try more for more chaos
var n = 200;
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: 5 + 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", "#13121A");
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("#1B0A77","#B92626")(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