xxxxxxxxxx
246
let xoff = 0;
let yoff = 0;
let zoff = 0;
let xscale_i = 0.05;
let yscale_i = 0.05;
let zscale_i = 0.0005;
let xscale = xscale_i;
let yscale = yscale_i;
let zscale = zscale_i;
let n_PIs_i = 2;
let n_PIs = n_PIs_i;
let vMag_i = 0.5;
let vMag = vMag_i;
let xoff_i;
let yoff_i;
let xoff_r;
let yoff_r;
let xscale_r = 0.01;
let yscale_r = 0.01;
let counter_x = 0;
let counter_y = 0;
let counter_speed = 0;
let scl = 30;
let cols, rows;
let background_a = 0.5;
let background_sub = 0;
let strokeWidthAddOn = 0;
let fr;
let controlRandom = "p";
let controlRandom_p = "p";
var particles = [];
let nParticlesToAdd_i = 10;
let nParticlesToAdd = 200;
let flowfield = [];
function setup() {
createCanvas(windowWidth, windowHeight);
//createCanvas(400, 400);
cols = floor(width / scl);
rows = floor(height / scl);
pixelDensity(1);
xoff_i = random(100);
yoff_i = random(100);
xoff_r = xoff_i;
yoff_r = yoff_i;
fr = createP("");
flowfield = new Array(cols * rows);
if (particles.length < 2500) {
for (i = 0; i < 200; i++) {
particles.push(new Particle());
}
nParticlesToAdd_i = nParticlesToAdd;
}
background(255 - background_sub);
}
function draw() {
if (frameCount % 5 == 0) background(255 - background_sub, background_a);
if (0) {
xscale_r = random(0.005, xscale_r);
yscale_r = random(0.005, yscale_r);
}
xoff = 0 + counter_x;
xoff_r = xoff_i + counter_x;
for (var x = 0; x < cols; x++) {
yoff = 0 + counter_y;
yoff_r = yoff_i + counter_y;
for (var y = 0; y < rows + 1; y++) {
var index = (x + y * cols) * 4;
var index_vector = x + y * cols;
var angle = noise(xoff, yoff, zoff) * TWO_PI * n_PIs;
if (controlRandom === "d") {
angle = noise(xoff, yoff, zoff) * TWO_PI * random(-n_PIs, n_PIs);
}
var v = p5.Vector.fromAngle(angle);
v.setMag(vMag);
flowfield[index_vector] = v;
stroke(0, 50);
strokeWeight(1);
push();
translate(x * scl, y * scl);
rotate(v.heading());
//line(0, 0, scl, 0);
pop();
//rect(x*scl,y*scl,scl,scl)
yoff += yscale;
yoff_r += yscale_r;
}
xoff += xscale;
xoff_r += xscale_r;
zoff += zscale;
}
// counter_x += random(counter_speed);
// counter_y += random(counter_speed);
for (let i = 0; i < particles.length; i++) {
particles[i].follow(flowfield, controlRandom_p);
particles[i].update();
particles[i].edges();
particles[i].show(background_sub);
}
//fr.html(floor(frameRate()));
}
function keyTyped() {
let controlKeys = [
"P",
"p",
"C",
"c",
"Q",
"q",
"W",
"w",
"A",
"a",
"D",
"d",
"S",
"s",
"R",
"r",
"+",
"=",
"-",
"_",
];
if (controlKeys.includes(key)) {
controlRandom = key;
checkControlChanges();
}
}
function checkControlChanges() {
if (controlRandom == "p" || controlRandom == "P") {
background(255 - background_sub, 10);
controlRandom_p = "p";
background_a = 5;
xscale = xscale_i;
yscale = yscale_i;
zscale = zscale_i;
vMag = vMag_i;
n_PIs = n_PIs_i;
}
if (controlRandom == "c" || controlRandom == "C") {
controlRandom_p = "c";
background_a = 10;
xscale = 0.15;
yscale = 0.15;
zscale = 0.009;
vMag = 20;
n_PIs = 6;
}
if (controlRandom == "r" || controlRandom == "R") {
controlRandom_p = "r";
background_a = 10;
zscale = zscale_i;
vMag = vMag_i;
}
if (controlRandom == "d" || controlRandom == "D") {
background(255 - background_sub, 10);
//controlRandom = "d";
for (let i = 0; i < particles.length; i++) {
particles[i].updateSpeed(1);
}
}
if (controlRandom == "a" || controlRandom == "A") {
background(255 - background_sub, 10);
//controlRandom = "a";
for (let i = 0; i < particles.length; i++) {
particles[i].updateSpeed(-1);
}
}
if (controlRandom == "s" || controlRandom == "S") {
background(255 - background_sub, 10);
controlRandom_p = "s";
background_a = 5;
xscale = xscale_i;
yscale = yscale_i;
zscale = zscale_i;
vMag = vMag_i;
n_PIs = 0.5;
}
if (controlRandom == "w" || controlRandom == "W") {
background(255 - background_sub, 10);
controlRandom_p = "p";
background_a = 5;
xscale = xscale_i;
yscale = yscale_i;
zscale = zscale_i;
vMag = vMag_i;
n_PIs = -0.5;
}
if (controlRandom === "q" || controlRandom === "Q") {
background_sub = abs(background_sub - 255);
background(255 - background_sub);
}
if (controlRandom == "+" || controlRandom == "=") {
addParticles();
}
if (controlRandom == "-" || controlRandom == "_") {
removeParticles();
}
}
function addParticles() {
if (particles.length < 2500) {
for (i = 0; i < nParticlesToAdd_i; i++) {
particles.push(new Particle());
}
nParticlesToAdd_i = nParticlesToAdd;
}
}
function removeParticles() {
particles.splice(particles.length - nParticlesToAdd_i, nParticlesToAdd_i);
background(255 - background_sub, 10);
}