Skip to content

Instantly share code, notes, and snippets.

@laxmikanta415
Created January 26, 2018 09:35
Show Gist options
  • Save laxmikanta415/ee07e97d22c08731fc69e1872783d64e to your computer and use it in GitHub Desktop.
Save laxmikanta415/ee07e97d22c08731fc69e1872783d64e to your computer and use it in GitHub Desktop.
fresh block
license: mit
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<script src="https://d3js.org/d3.v4.min.js"></script>
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<script>
var width = 300;
var height = 300;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
function checkForRange(randomNumber){
while(randomNumber > height){
randomNumber = randomNumber-height;
// checkForRange(randomNumber);
}
return randomNumber;
}
function getRandomNumber(i){
var randomNumber = Math.random()*i*100;
return randomNumber > height ?
checkForRange(randomNumber-height) : randomNumber;
}
var nodes = [];
for(var i=0;i<100;i++){
nodes.push(
{x: getRandomNumber(i), y:getRandomNumber(i)}
);
}
var simulation = d3.forceSimulation(nodes)
.force('charge',
d3.forceManyBody().strength(-10))
.force('center',
d3.forceCenter().x(width/2).y(height/2))
.on('tick',ticked);
function ticked(){
var u = svg.selectAll('circle.particle')
.data(nodes);
u.enter()
.append('circle')
.classed('particle',true)
.attr('r',4)
.merge(u)
.attr('cx',d => d.x)
.attr('cy',d => d.y);
u.exit().remove();
}
</script>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment