const ctxWidth = 600;
const ctxHeight = 400;
const bubble = [];
function setup() {
createCanvas(ctxWidth, ctxHeight);
}
function mouseClicked() {
bubble.push(new Bubble(mouseX, mouseY));
}
function draw() {
background(0);
for (let i = bubble.length - 1; i >= 0; i--) {
const el = bubble[i];
el.run();
if (el.isFinished) {
bubble.splice(i, 1);
}
}
}
class Bubble {
constructor(x, y) {
const r = 40;
const alpha = 255;
Object.assign(this, {
x,
y,
r,
alpha,
});
}
run() {
this.show();
this.update();
}
show() {
noStroke();
fill(255, this.alpha);
ellipse(this.x, this.y, this.r);
}
update() {
this.x += random(1, -1);
this.y += random(1, -1);
this.alpha -= 1;
}
get isFinished() {
return !this.alpha;
}
}
console