xxxxxxxxxx
65
const bubbleAmount = 100;
let bubbles = [];
function setup() {
createCanvas(600, 400);
for (let i = 0; i < bubbleAmount; i++) {
bubbles.push(
new Bubble(random(0, width), random(0, height), 10)
);
}
}
function draw() {
background(0);
for (let bubble of bubbles) {
bubble.move();
bubble.show();
let overlapping = false;
for (let other of bubbles) {
if (other !== bubble && bubble.contains(other)) {
overlapping = true;
}
}
if (overlapping) {
bubble.changeColor(255);
} else {
bubble.changeColor(0);
}
}
}
class Bubble {
constructor(x, y, r = 50) {
this.x = x;
this.y = y;
this.r = r;
this.brightness = 0;
}
changeColor(bright) {
this.brightness = bright;
}
contains(other) {
let d = dist(this.x, this.y, other.x, other.y);
return d < this.r + other.r;
}
move() {
this.x = this.x + random(-2, 2);
this.y = this.y + random(-2, 2);
}
show() {
stroke(255);
strokeWeight(4);
fill(this.brightness, 125);
ellipse(this.x, this.y, this.r * 2);
}
}