xxxxxxxxxx
113
const m = 1;
const pxs = [];
const N = 256;
var amt_spawns = 3;
const SPAWNLIMIT = 450;
const MAXLIFE = 765;
var fromcolor;
var tocolor ;
var rotation_slider;
function setup() {
fromcolor = color('yellow');
tocolor = color('red') // color(255,0,0,50);
createCanvas(500, 500);
background(0);
rotation_slider = createSlider(1, 10, 3, 0.05);
push()
stroke('green')
strokeWeight(12)
fill('green')
line(width/2, height/2, width/2, height)
pop()
for (var i = 0; i < N; i++) {
spawnParticle(i);
}
}
function mouseClicked() {
resetStuff();
}
function resetStuff(){
background(0);
push()
stroke('green')
strokeWeight(12)
fill('green')
line(width/2, height/2, width/2, height)
pop()
pxs.splice(0, pxs.length);
amt_spawns = 0;
for (var i = 0; i < N; i++) {
spawnParticle(i);
}
loop()
}
function spawnParticle(i) {
if (amt_spawns > SPAWNLIMIT && pxs.length < 1) {print('finised');noLoop();}
if (amt_spawns > SPAWNLIMIT) {return;}
cx = (width/2) + random(-m, m);
cy = (height/2) + random(-m, m);
pxs.push(
{
pos: createVector(cx, cy),
speed: p5.Vector.random2D(),
rot: int(random(2))*QUARTER_PI,
rotd: random(-1,1) * map(rotation_slider.value(), 1, 10, .00001, .0001),
life: random() * MAXLIFE,
size: random(10)
});
pxs[i].speed.setMag(random()*150);
amt_spawns++;
}
function draw() {
for (var i = pxs.length - 1; i > -1; i--) {
var o = pxs[i];
if (o.life < 1) {
pxs.splice(i, 1);
continue;
}
const p = o.pos;
if (p.x < 0 || p.x > width || p.y < 0 || p.y > height) {
pxs.splice(i, 1);
continue;
}
push();
strokeWeight(o.size);
translate(p.x,p.y);
var col = lerpColor(fromcolor, tocolor, o.life/MAXLIFE);
stroke(col);
// stroke(100 + o.life, 150 +(o.life/MAXLIFE)*100, 0);
point(0,0);
pop();
o.speed.rotate(o.rot)
o.speed.setMag(o.life/MAXLIFE)
o.rot+=o.rotd;
o.life--;
o.size-=0.005;
o.pos.add(o.speed);
}
for (var i = 0; i < N - pxs.length; i++) {
spawnParticle(i);
}
}