xxxxxxxxxx
139
function make2DArray(cols, rows) {
let arr = new Array(cols);
for(let i = 0; i < arr.length; i++){
arr[i] = new Array(rows);
for(let j = 0; j < arr[i].length; j++){
arr[i][j] = 0;
}
}
return arr;
}
let w;
let rows, cols;
let dens;
let nzoom;
let dots;
let t1, t2;
let center;
let aim;
let here;
let speed;
let col;
let c;
function setup() {
createCanvas(900, 900);
w = 10;
cols = width / w;
rows = height / w;
grid = make2DArray(cols, rows);
dens = 10;
// nzoom = 200;
dots = [];
colors = ["#012a4a","#013a63","#01497c","#014f86","#2a6f97","#2c7da0","#468faf","#61a5c2","#89c2d9","#a9d6e5"];
col = color(colors[0]);
t1 = 0;
t2 = 3;
t3 = 5;
c = 0;
speed = 14;
nzoom = sin(t3) * 250;
center = createVector(width / 2, height / 2);
here = createVector(0, 0);
there = createVector(0, 0);
// col = color(colors[floor(random(colors.length))]);
col = color("#070D27");
count = width / dens * height / dens;
for(i = 0; i < cols; i++){
for(j = 0; j < rows; j++){
grid[i][j] = createVector(1, 0);
grid[i][j].setHeading(noise(i / nzoom, j / nzoom) * TWO_PI * 2);
}
}
for(i = 0; i < width / dens; i++){
for(j = 0; j < height / dens; j++){
dots[dots.length] = new Dot(i * dens + random(-dens, dens), j * dens + random(-dens, dens), dots.length);
}
}
strokeWeight (1);
// stroke(30)
}
function draw() {
// col.setAlpha(20);
background(col);
t1 = t1 + 0.01;
t2 = t2 + 0.015;
t3 = t3 + 0.0015;
nzoom = sin(t3) * 25
if(frameCount % 250 == true){
for(i = 0; i < cols; i++){
for(j = 0; j < rows; j++){
here = createVector(i * w, j * w);
aim = p5.Vector.sub(center, here);
grid[i][j].setHeading(aim.heading() + HALF_PI + noise(i / nzoom + t1, j / nzoom + t2) * 5 + 2);
}
}
}
for(i = 0; i < dots.length; i++){
dots[i].update();
}
}
class Dot {
constructor(x, y, index){
this.pos = createVector(x, y);
this.length = floor(random(5) + 1);
this.p = [];
this.p1 = [];
for(let a = 0; a <= this.length; a++){
this.p[a] = createVector(x, y);
}
this.index = index;
this.vel = createVector(0, 0);
this.c = floor(colors.length / count * this.index);
this.speed = 5 / ((width / dens * height / dens)) * this.index + 9;
}
update(){
stroke(color(colors[this.c]));
if(this.pos.x > 0 && this.pos.x < width && this.pos.y > 0 && this.pos.y < height){
if(grid[floor(this.pos.x / w)][floor(this.pos.y / w)] !== undefined){
this.vel.add(grid[floor(this.pos.x / w)][floor(this.pos.y / w)]);
}
} else {
this.vel.setHeading(this.vel.heading() + HALF_PI)
}
this.vel.setHeading(this.vel.heading() + sin(frameCount / 2 + this.index / (this.length * 1)) * this.length * 0.05)
for(let a = this.length; a > 1; a--){
strokeWeight(this.length - a);
this.p[a -1] = this.p[a - 2];
line(this.p[a].x, this.p[a].y, this.p[a - 1].x, this.p[a - 1].y);
}
this.p[this.length] = this.p[this.length - 1];
this.pos.add(this.vel);
this.p[0] = createVector(this.pos.x, this.pos.y);
if(this.vel.mag() > this.speed){
this.vel.setMag(this.speed)
}
}
}