xxxxxxxxxx
138
let particles;
let paused = false;
let seed = 1;
let colors;
function keyPressed() {
if (key === " ") paused = !paused;
}
class Particle {
constructor() {
this.radius = random(0.5, 2);
this.diameter = this.radius * 2;
this.velocity = createVector(10, 10); //random(-2,2), random(-2,2));
this.alpha = 100;
this.position = createVector(
random(this.radius, width - this.radius),
random(this.radius, height - this.radius)
);
let _r = random();
if (_r > 0.66)
this.color = colors[2];
// this.color = color(255, 0, 0, this.alpha);
else if (_r > 0.33)
this.color = colors[3];
// this.color = color(0, 0, 255, this.alpha);
else
this.color = colors[4];
// this.color = color(0, 255, 0, this.alpha);
this.color._array[3] = this.alpha/255;
}
update() {
this.position.x += this.velocity.x;
this.position.y += this.velocity.y;
let change = false;
if (
this.position.x < this.radius ||
this.position.x > width - this.radius
) {
this.velocity.x *= -1;
change = true;
}
if (
this.position.y < this.radius ||
this.position.y > height - this.radius
) {
this.velocity.y *= -1;
change = true;
}
if (random() > 0.99) {
this.alpha = constrain(this.alpha, 0, 25 );
if (random() > 0.5) {
this.velocity.x = random(-2, 2);
this.velocity.y = 0;
} else {
this.velocity.x = 0;
this.velocity.y = random(-2, 2);
}
this.alpha -= 1;//0.5;
this.color = colors[1];//color(255,255,255,this.alpha);
this.color._array[3] = this.alpha/255;
}
if (change) {
this.alpha -= 5;
}
if (this.alpha <= 0) {
this.alpha = 0;
this.dead = true;
}
this.color._array[3] = this.alpha / 255;
}
draw() {
noStroke();
fill(this.color);
circle(this.position.x, this.position.y, this.diameter);
}
}
function setup() {
randomSeed(seed);
// https://www.colourlovers.com/palette/4834108/All_bark_no_bite
// colors = [
// color("#9CB2A0"),
// color("#A27C70"),
// color("#815E53"),
// color("#DB703E"),
// color("#CFAD94")
// ];
colors = [
color(0,0,0),
color(255,0,0),
color(0,255,0),
color(0,0,255),
color(255,0,255)
]
particles = [];
let numParticles = 1250; //50;
// createCanvas(600, 600);
createCanvas(7200,7200);
// createCanvas(3840, 2160);
for (let i = 0; i < numParticles; i++) {
particles.push(new Particle());
}
// background(0);
background(colors[0]);
}
function draw() {
if (!paused) {
// background(0);
for (let i = particles.length - 1; i >= 0; i--) {
let p = particles[i];
p.update();
p.draw();
if (p.dead) particles.splice(i, 1);
}
if (particles.length == 0) {
console.log("done");
noLoop();
}
}
}