Tap to toggle mouse repel/attract. Press any key to save screenshot.
A fork of GPT project 4 by Felix Bade
xxxxxxxxxx
p5.disableFriendlyErrors = true;
class Particle {
constructor() {
this.x = width / 2;
this.y = height;
this.vx = random(-3, 3); // Vary initial velocity
this.vy = random(-8, -2);
this.ax = random(-0.2, 0.2); // Vary acceleration
this.ay = random(0.05, 0.2);
this.fx = 0;
this.fy = 0;
this.timeOffsetX = random(1000);
this.timeOffsetY = random(1000, 2000);
this.alpha = 255;
this.size = random(5, 30);
this.color = color(random(180, 300), 100, random(60, 90), 100);
this.shape = random(['ellipse', 'rect']);
}
finished() {
return this.alpha < 0;
}
applyForce(fx, fy) {
this.fx = fx;
this.fy = fy;
}
update() {
this.vx += this.ax + this.fx;
this.vy += this.ay + this.fy;
this.x += this.vx + map(noise(this.timeOffsetX), 0, 1, -2, 2);
this.y += this.vy;
this.alpha -= 5;
this.size -= 0.1;
this.timeOffsetX += 0.01;
this.timeOffsetY += 0.01;
this.checkCanvasEdges(); // Check for canvas edge collisions
}
checkCanvasEdges() {
if (this.x <= 0 || this.x >= width) {
this.vx *= -1;
}
if (this.y <= 0 || this.y >= height) {
this.vy *= -1;
}
}
show() {
noStroke();
fill(this.color, this.alpha);
if (this.shape === 'ellipse') {
ellipse(this.x, this.y, this.size);
} else {
rect(this.x - this.size / 2, this.y - this.size / 2, this.size, this.size);
}
}
}
let particles = [];
let repel = true;
function setup() {
createCanvas(920, 690);
colorMode(HSL, 360, 100, 100, 100);
background(0, 0, 10);
mouseX = width/2;
}
function draw() {
mouseX = random(width)
background(0, 0, 10, 25);
for (let i = 0; i < 5; i++) {
particles.push(new Particle());
}
for (let p of particles) {
const d = dist(p.x, p.y, mouseX, mouseY);
const force = map(d, 0, 200, repel ? p.size / 5 : -p.size / 5, 0); // Vary force by particle size
const angle = atan2(p.y - mouseY, p.x - mouseX);
const fx = force * cos(angle);
const fy = force * sin(angle);
p.applyForce(fx, fy);
p.update();
p.show();
}
particles = particles.filter(p => !p.finished());
}
let lapse = 0; // mouse timer
function mousePressed(){
// prevents mouse press from registering twice
if (millis() - lapse > 400){
repel = !repel;
lapse = millis();
}
}
function keyPressed(){
save("img_" + month() + '-' + day() + '_' + hour() + '-' + minute() + '-' + second() + ".jpg");
}