Click on the above sketch for some fun fireworks. Link here to source files and editor.
let goal = [];
let cpos = [];
let inertia = [];
let k = [];
let pp = [];
let c = [];
let radius = [];
let particlecount = 0;
let pg;
function preload() {
img = loadImage("yes.jpg");
}
function setup() {
createCanvas(800,800);
imageMode(CENTER);
pg = createGraphics(width, height);
pg.image(img, 0, 0);
}
function draw() {
background(0);
let pix;
let x;
let y;
for(let i = 0; i < 5; i++){
x = floor(random(img.width));
y = floor(random(img.height));
pix = red(pg.get(x,y));
if(pix < 20){
i = 200;
}
}
if(particlecount < 1200){
if(pix < 20){
append(goal, createVector(x,y));
append(cpos, createVector(0,windowHeight + int(random(-20,20))));
append(radius, int(random(5,30)));
append(c,color(random(255),random(255),random(255)));
append(pp, createVector(0,0));
append(inertia, random(0.7,0.95));
append(k, random(0.01,0.09));
particlecount++;
}
}
for (var i = 0; i < goal.length; i++) {
if(accelerationX > 1){
cpos[i].x = cpos[i].x + (accelerationX*(random(15)+4));
}
if(accelerationY > 1){
cpos[i].y = cpos[i].y + (accelerationY*(random(15)+4));
}
var x1 = -1 * cpos[i].x + goal[i].x;
var y1 = -1 * cpos[i].y + goal[i].y;
pp[i].x = pp[i].x * inertia[i] + x1*k[i];
pp[i].y = pp[i].y * inertia[i] + y1*k[i];
cpos[i].x += pp[i].x;
cpos[i].y += pp[i].y;
fill(c[i]);
ellipse(cpos[i].x,cpos[i].y,radius[i],radius[i]);
}
}
function mouseClicked() {
for (var i = 0; i < goal.length; i++) {
var xdis = cpos[i].x - mouseX;
let ydis = cpos[i].y - mouseY;
let dis = sqrt((xdis*xdis) + (ydis*ydis));
if(dis < 100){
time = random()*10;
cpos[i].x = mouseX + 300 * cos(time);
cpos[i].y = mouseY + 300 * sin(time);
}
}
}