p5js/2.动态数组.js
2022-11-27 16:57:23 +08:00

30 lines
556 B
JavaScript

const circles = [];
function setup() {
createCanvas(600, 400);
strokeWeight(1);
stroke(0);
}
function draw() {
background(255);
circles.forEach((o, i, circles) => {
fill(o.color);
circle(o.x, o.y, o.size);
o.y += 5;
if (o.y > 420) {
circles.splice(i, 1);
}
});
text(circles.length, 40, 40);
}
function mousePressed() {
circles.push({
x: mouseX,
y: mouseY,
color: Math.floor(random(100, 201)),
size: Math.floor(random(20, 41)),
});
}