colorMode(HSL,360,100,100);
n_col=int(random(50,100));
for(let i=0;i<n_col;i++){
colori.push(color(h,s,l))
griglia(width / 50, width / 50, width - width / 50 * 2, height - width / 50 * 2, 10);
let val = map(t, 0, 1, 0.01, 0.1);
for (let particle of particles) {
particle.follow(flowField,val);
function griglia(posX, posY, wid, hei, depth) {
griglia(posX, posY, wid, hei / 2, depth - 1);
griglia(posX, posY + hei / 2, wid, hei / 2, depth - 1);
griglia(posX, posY, wid / 2, hei, depth - 1);
griglia(posX + wid / 2, posY, wid / 2, hei, depth - 1);
let cI=constrain(floor(map(noise(posX*0.1,posY*0.1),0,1,0,colori.length-1)),0,colori.length-1)
flowField = new FlowField(posX, posY, resolution, wid, hei);
let n_pa=int(random(1,11));
for (let i = 0; i < n_pa; i++) {
particles.push(new Particle(posX, posY, wid, hei, pal));
constructor(rectWidth, rectHeight, resolution, offsetX, offsetY) {
this.rectWidth = rectWidth;
this.rectHeight = rectHeight;
this.resolution = resolution;
this.cols = rectWidth / resolution;
this.rows = rectHeight / resolution;
this.field = this.generateField();
for (let y = 0; y < this.rows; y++) {
for (let x = 0; x < this.cols; x++) {
let angle = map(noise((x + this.offsetX) * 0.05, (y + this.offsetY) * 0.05), 0, 0.5, 0, TWO_PI);
let vector = p5.Vector.fromAngle(angle);
let col = floor(constrain((position.x - this.offsetX) / this.resolution, 0, this.cols - 1));
let row = floor(constrain((position.y - this.offsetY) / this.resolution, 0, this.rows - 1));
return this.field[row][col].copy();
constructor(rectX, rectY, rectWidth, rectHeight, pal) {
this.rectWidth = rectWidth;
this.rectHeight = rectHeight;
this.position = createVector(random(rectX, rectX + rectWidth), random(rectY, rectY + rectHeight));
this.velocity = createVector();
this.acceleration = createVector();
this.maxSpeed = random(1, 4);
let desired = flowField.lookup(this.position);
desired.setMag(this.maxSpeed);
let steering = p5.Vector.sub(desired, this.velocity);
steering.limit(this.maxForce);
this.applyForce(steering);
this.acceleration.add(force);
this.velocity.add(this.acceleration);
this.velocity.limit(this.maxSpeed);
this.position.add(this.velocity);
this.acceleration.mult(0);
if (this.position.x > this.rectX + this.rectWidth) this.position.x = this.rectX;
if (this.position.x < this.rectX) this.position.x = this.rectX + this.rectWidth;
if (this.position.y > this.rectY + this.rectHeight) this.position.y = this.rectY;
if (this.position.y < this.rectY) this.position.y = this.rectY + this.rectHeight;
let n=noise(this.position.x*0.001,this.position.y*0.001);
let cI=constrain(floor(map(n,0,1,0,colori.length-1)),0,colori.length-1);
let prevPos = this.position.copy().sub(this.velocity.copy().normalize().mult(3));
line(prevPos.x, prevPos.y, this.position.x, this.position.y);