xxxxxxxxxx
var agents = [];
var numAgents = 50; // agent number
var t = 0; // Time Variable
function setup() {
createCanvas(800, 800); // canvas size
noStroke();
// create agents
for (let i = 0; i < numAgents; i++) {
agents.push(new Agent(random(width), random(height))); // create agent at random location
}
}
function draw() {
background(20, 20, 50, 25); //tranceparant background
t += 0.01; // time
// Move and draw all agents
for (let agent of agents) {
agent.update(t);
agent.display();
}
}
// agent class
class Agent {
constructor(x, y) {
this.pos = createVector(x, y); // position
this.vel = p5.Vector.random2D(); // random speed
this.size = random(5, 15); // dimension
this.colorOffset = random(TWO_PI); // for color
}
update(t) {
// cyclic notion
let angle = noise(this.pos.x * 0.01, this.pos.y * 0.01, t) * TWO_PI * 2;
this.vel = p5.Vector.fromAngle(angle).setMag(2); // new speed
this.pos.add(this.vel); // update position
// if iy crosses the borders wrap it to the other side
this.pos.x = (this.pos.x + width) % width;
this.pos.y = (this.pos.y + height) % height;
}
display() {
// color
let r = map(sin(t + this.colorOffset), -1, 1, 100, 255);
let g = map(cos(t + this.colorOffset), -1, 1, 50, 150);
let b = map(sin(t + this.colorOffset * 1.5), -1, 1, 150, 255);
fill(r, g, b, 150); // Hafif transparan renk
// draw ellipse
ellipse(this.pos.x, this.pos.y, this.size);
}
}