xxxxxxxxxx
var nodes = [];
var nodeCount = 200;
var maxDistance =120;
function setup() {
createCanvas(windowWidth, windowHeight);
//Create nodes
for(i =0; i<nodeCount; i++){
var b = new Ball(random(0,windowWidth),random(0,windowHeight));
nodes.push(b);
}
}
function windowResized(){
resizeCanvas(windowWidth, windowHeight);
}
function draw() {
background(255);
for(i = 0; i<nodes.length; i++){
nodes[i].display();
nodes[i].update();
drawConnection(i);
}
}
function drawConnection(theNode){
node1 = nodes[theNode];
stroke(node1.color);
for(j=theNode; j < nodes.length; j++){
node2 = nodes[j];
distance = dist(node1.x,node1.y,node2.x,node2.y);
if(distance < maxDistance){
if(j != theNode){
strokeWeight(3 - (distance/maxDistance) *3); // Distance/ max creates line thickness
line(node1.x,node1.y,node2.x,node2.y);
}
}
}
}
function Ball (x ,y){
this.size = 8;
this.x = x;
this.y = y;
this.speed = 1.5;
this.xSpeed = this.speed * random(-1,1);
this.ySpeed = this.speed * random(-1,1);
this.color = color(223, 8, 47);
this.display = function (){
noStroke();
fill(this.color);
ellipse(this.x,this.y,this.size ,this.size );
}
this.update = function(){
if(this.x + this.xSpeed > windowWidth || this.x + this.xSpeed <0){
this.xSpeed *= -1;
}else {
this.x += this.xSpeed;
}
if(this.y + this.ySpeed > windowHeight || this.y + this.ySpeed <0){
this.ySpeed *= -1;
}else {
this.y += this.ySpeed;
}
}
}