xxxxxxxxxx
/*
Razer node network
Trying to recreate a cool wallpaper I saw in a computer store.
Framerate takes a bit of a hit since it's not very optimized.
Controls:
- Move the mouse along its width to change the distance threshold.
Author:
Jason Labbe
Site:
jasonlabbe3d.com
*/
var bobs = [];
function setup() {
createCanvas(1000, 600);
colorMode(HSB, 255);
mouseX = width / 2;
}
function draw() {
spawn();
background(0);
let threshold = map(mouseX, 0, width, 50, 200);
for (let i = bobs.length - 1; i > -1; i--) {
let bob = bobs[i];
bob.pos.add(bob.vel);
if (bob.pos.y < -100 || bob.pos.x < -100 || bob.pos.x > width + 100) {
bobs.splice(i, 1);
}
}
for (let i = 0; i < bobs.length; i++) {
let bob = bobs[i];
let h = map(bob.pos.x, 0, width, 0, 255);
let d = dist(bob.pos.x, bob.pos.y, width / 2, 0);
let falloff = 50;
if (d < 900) {
falloff = map(d, 0, 900, -50, 50);
}
stroke(h, 255, 255, 125);
strokeWeight(3);
point(bob.pos.x, bob.pos.y);
if (falloff < 0) {
continue;
}
stroke(h, 255, 255, falloff);
strokeWeight(0.5);
fill(h, 255, 255, falloff);
let foundCount = 0;
beginShape();
vertex(bob.pos.x, bob.pos.y);
for (let j = 0; j < bobs.length; j++) {
if (foundCount > 5) {
break;
}
if (i == j) {
continue;
}
let otherBob = bobs[j];
let d = dist(bob.pos.x, bob.pos.y, otherBob.pos.x, otherBob.pos.y);
if (d > threshold) {
continue;
}
vertex(otherBob.pos.x, otherBob.pos.y);
foundCount++;
}
endShape(CLOSE);
}
noStroke();
fill(255);
text(frameRate(), 50, 50);
}
function Bob(x, y, vx, vy) {
this.pos = new p5.Vector(x, y);
this.vel = new p5.Vector(vx, vy);
}
function spawn() {
let choice = int(random(5));
if (choice == 0) {
// Spawn on the left.
bobs.push(new Bob(
random(-50, 0), random(height -50, height + 100),
random(0, 4), random(-4, -1)));
} else if (choice == 1) {
// Spawn on the right.
bobs.push(new Bob(
random(width, width + 50), random(height -50, height + 100),
random(-4, 0), random(-4, -1)));
} else if (choice == 2) {
// Spawn in the bottom-center.
bobs.push(new Bob(
random(width / 2 - 50, width / 2 + 50), height + 100,
random(-3, 3), random(-4, -1)));
} else if (choice == 3) {
// Spawn in the bottom-left.
bobs.push(new Bob(
random(width / 4 - 50, width / 4 + 50), height + 100,
random(-1, 2), random(-4, -1)));
} else {
// Spawn in the bottom-right.
bobs.push(new Bob(
random(width - width / 4 - 50, width - width / 4 + 50), height + 100,
random(-2, 1), random(-4, -1)));
}
}