xxxxxxxxxx
let img;
let particles = [];
function preload() {
img = loadImage('https://deckard.openprocessing.org/user418107/visual2157141/hdf04e1b644afc84ab3ff2e27b05426a5/gm_l.png');
}
function setup() {
createCanvas(1080, 1080);
background('#01d801');
for (let i = 0; i < 500; i++) {
particles.push(new Particle(random(width), random(height)));
}
}
function draw() {
background('#01d801');
for (let i = 0; i < particles.length; i++) {
particles[i].update();
particles[i].show();
}
}
class Particle {
constructor(x, y) {
this.pos = createVector(x, y);
this.vel = p5.Vector.random2D().mult(random(2, 5));
this.acc = createVector(0, 0);
this.target = createVector(width / 2, height / 2);
this.maxSpeed = 2;
this.maxForce = 1;
}
update() {
let desired = p5.Vector.sub(this.target, this.pos);
let d = desired.mag();
let speed = this.maxSpeed;
if (d < 100) {
speed = map(d, 0, 100, 0, this.maxSpeed);
}
desired.setMag(speed);
let steer = p5.Vector.sub(desired, this.vel);
steer.limit(this.maxForce);
this.acc.add(steer);
this.vel.add(this.acc);
this.pos.add(this.vel);
this.acc.mult(0);
}
show() {
let scl = 0.2;
imageMode(CENTER);
translate(this.pos.x, this.pos.y);
rotate(frameCount * 0.01);
image(img, 0, 0, img.width * scl, img.height * scl);
resetMatrix();
}
}