xxxxxxxxxx
62
let NUM_OF_PARTICLES = 3;
let particles = [];
function setup() {
createCanvas(500, 400);
for (let i = 0; i < NUM_OF_PARTICLES; i++) {
particles.push( new Particle(random(width), random(height)) );
}
}
function draw() {
background(220);
for (let i = 0; i < particles.length; i++) {
let p = particles[i];
p.move();
p.reappear(); // ***
p.display();
}
}
//
class Particle {
constructor(x, y) {
// properties
this.x = x;
this.y = y;
this.xSpd = random(-5, 5);
this.ySpd = random(-5, 5);
this.dia = 30;
}
// methods
reappear() {
if (this.x < 0) {
this.x = width;
}
else if (this.x > width) {
this.x = 0;
}
if (this.y < 0) {
this.y = height;
}
else if (this.y > height) {
this.y = 0;
}
}
move() {
this.x += this.xSpd;
this.y += this.ySpd;
}
display() {
push();
translate(this.x, this.y);
circle(0, 0, this.dia);
pop();
}
}