xxxxxxxxxx
195
const DRAW_VECTORS = 0;
const DRAW_COLORS = 1;
const cols = 50;
const rows = 50;
const THE_RADIUS = 172;
// particles container & the lifespans
const ps = [];
const lifespans = [];
const MAX_PARTICLE = 100;
const MIN_LIFE = 50;
const MAX_LIFE = 250;
const psSIZE = 1;
const SIZEFLURRY = 15; // idk .
const MIN_SPEED = 1;
const MAX_SPEED = 5; // this is actually added so uhh.
const ALPHA_LEVEL = 76;
var colz = 510;
var cold = 0.005;
var speedVector;
var centreVector;
// time and its increment
t = 0;
dt = 0.004; // 0.002254287623488794242;
function setup() {
createCanvas(400, 400);
noiseSeed(1);
randomSeed(1);
centreVector = createVector(width / 2, height / 2);
speedVector = createVector(1, 1);
speedVector.setMag(MIN_SPEED + noise(t) * MAX_SPEED);
}
function mouseClicked() {
if (!(mouseX < 0 || mouseX > width || mouseY < 0 || mouseY > height)) {
// set new randomish seed
var s =
random() * 1000 * randomGaussian(42, 2) * random() * 1000 * random();
print(`new seed: ${s}`);
noiseSeed(s);
randomSeed(s);
// reset values
background(220);
ps.splice(0, ps.length);
t = 0;
}
}
function epow(x) {
return Math.pow(Math.E, x);
}
function suggestedDirectionAt(args) {
const [x, y, d] = args;
var a = sin(x/40 + noise(t));
return a;
const d2 = dist(x, y, 150, 225);
// angle reference at point, influenced by time and constants
const [xx, yy] = [x / 35, y / 35];
const a_ref = noise(xx + t, yy + t) * TWO_PI;
// angle modifier to alternate angles at certain distances from CX, CY
// more towards right or left
// example: CX, CY = 200, 200
// point A at 250,200 is at 50 distance from CX, CY
// lets arbitrarily choose that we want to shift the direction left/right
// every 25 units distance
// lets say the reference angle at A was originally 180 degrees
// 50 % 25 = 0
// lets say 24 is left, 0 is right
// map(0, 0, 24, radians(-45), radians(45))
const Alt = 25;
const shift = radians(1);
const a_mod = map(d2 % Alt, 0, Alt, -shift, shift);
return a_ref + a_mod;
}
function drawVectors() {
for (var x = 0; x < width; x += width / cols) {
for (var y = 0; y < height; y += height / rows) {
const d = dist(x, y, 200, 200);
if (d < THE_RADIUS) {
const a = suggestedDirectionAt(x, y, d);
px = x + cos(a) * 5;
py = y + sin(a) * 5;
push();
translate(x, y);
rotate(a);
line(0, 0, 5, 0);
line(4, -2, 6, 0);
line(4, +2, 6, 0);
pop();
}
}
}
}
function spawnMore() {
if (ps.length < MAX_PARTICLE) {
if (random() < 0.2) {
var tt = random(-TWO_PI, TWO_PI);
var x = width / 2 + cos(tt) * THE_RADIUS;
var y = height / 2 + sin(tt) * THE_RADIUS;
ps.push(createVector(x, y));
} else {
tt = random() * TWO_PI;
x = width / 2 + random(-1, 1) * THE_RADIUS;
y = height / 2 + random(-1, 1) * THE_RADIUS;
ps.push(createVector(x, y));
}
lifespans.push(createVector(MIN_LIFE + randomGaussian(1, 0.05) * MAX_LIFE));
}
}
function updateAndDrawParticles() {
for (var i = ps.length - 1; i > -1; i--) {
var v = ps[i];
// var [v2,v3,v4] = [ps[(i+1)%ps.length],ps[(i+3)%ps.length],ps[(i+5)%ps.length]]
lifespans[i].x--;
var l = lifespans[i].x;
const d = dist(v.x, v.y, 200, 200);
if (l > 0) {
var a_sug = suggestedDirectionAt(v.x, v.y, d);
// change even more from suggested
// ... TO DO ....
var a = randomGaussian(a_sug, 1);
// idk how to do this nicely/smoothely yet
speedVector.setHeading(a);
v.add(speedVector);
if (v.dist(centreVector) < THE_RADIUS - psSIZE / 2 - 1) {
var cr = 25+noise(v.x, v.y)*225;
var cg = 25+noise(colz+t)*225;
var cb = 26+noise(colz)*225;
if (psSIZE > 1) {
stroke(cr, cg, cb, ALPHA_LEVEL*noise(t));
fill(cr, cg, cb, ALPHA_LEVEL*noise(t));
circle(
v.x,
v.y,
1 + noise(t * v.x * v.y) * SIZEFLURRY * psSIZE
);
} else {
push();
translate(v.x, v.y);
stroke(cr, cg, cb, ALPHA_LEVEL);
point(0,0)
pop();
}
} else if (v.dist(centreVector) > THE_RADIUS + THE_RADIUS * 0.1) {
ps.splice(i, 1);
lifespans.splice(i, 1);
}
} else {
ps.splice(i, 1);
lifespans.splice(i, 1);
}
}
}
function draw() {
if (DRAW_VECTORS) drawVectors();
if (DRAW_COLORS) {
updateAndDrawParticles();
spawnMore();
}
t += dt;
speedVector.setMag(MIN_SPEED + noise(t) * MAX_SPEED);
fill(255,255,255,255);rect(0,0,50,50);fill(0);
text(`spd: ${speedVector.mag().toFixed(1)}`, 0, 10);
text(`t: ${t.toFixed(1)}`, 0, 20);
text(`n: ${noise(t).toFixed(1)}`, 0, 30);
}