xxxxxxxxxx
83
let orbs = [];
function setup() {
createCanvas(600, 500);
background('black'); // Set initial background color to black
frameRate(80)
}
function draw() {
background(0, 20); // Fading background effect for trails
for (let i = 0; i < orbs.length; i++) {
let currentOrb = orbs[i];
currentOrb.move(); // Update orb position
currentOrb.display(); // Draw orb
for (let j = 0; j < orbs.length; j++) {
if (i !== j && currentOrb.isCloseTo(orbs[j])) {
drawConnection(currentOrb, orbs[j]); // Draw connecting line between orbs if they are close
}
}
}
}
//Makes sure orbs are created when the mouse is dragged
function mouseDragged() {
let newOrb = new Orb(mouseX, mouseY); // Create new orb at mouse position
orbs.push(newOrb);
// Removes orb after 15 seconds
setTimeout(function() {
let index = orbs.indexOf(newOrb);
if (index !== -1) {
orbs.splice(index, 1);
}
}, 15000);
}
// Assigns all the attributes of the orb
class Orb {
constructor(x, y) {
this.pos = createVector(x, y); // Position vector
this.vel = p5.Vector.random2D(); // Assign velocity
this.vel.mult(random(1, 3)); // Assign magnitude
this.size = random(8, 20); // Random size for each orb
this.color = color(random(100, 255), random(100, 255), random(255), 150); // Random semi-transparent color
}
move() {
this.pos.add(this.vel); // Update position
// Bounce off edges
if (this.pos.x <= 0 || this.pos.x >= width) {
this.vel.x *= -1;
}
if (this.pos.y <= 0 || this.pos.y >= height) {
this.vel.y *= -1;
}
}
display() {
noStroke();
fill(this.color);
ellipse(this.pos.x, this.pos.y, this.size); // Draws orb
}
// Determines if orbs are within 100 pixels of each other
isCloseTo(other) {
let distance = this.pos.dist(other.pos);
if (distance < 100) {
return true;
} else {
return false;
}
}
}
// create the connection line between two close orbs
function drawConnection(orb1, orb2) {
let col = orb1.color; // Assign color
stroke(col); // Use orb1's color for the connection line
strokeWeight(2);
line(orb1.pos.x, orb1.pos.y, orb2.pos.x, orb2.pos.y); // Draw the line
}