study/p5js/动态数组。js

31 lines
560 B
Plaintext
Raw Permalink Normal View History

2022-12-18 07:56:32 +00:00
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)))
});
}