xxxxxxxxxx
let num = 1000;
let particles = [];
let colors = [ '#fac855', '#edb9ea']; // Parlak ve çarpıcı renkler
let explosionPhase = false; // Patlama aşamasını kontrol etmek için bir değişken
function setup() {
createCanvas(800, 800);
noStroke();
ellipseMode(RADIUS);
frameRate(30);
initParticles(); // İlk partikülleri oluştur
}
function draw() {
background(0, 10); // Yavaş yavaş silinen bir arka plan efekti
for (let i = particles.length - 1; i >= 0; i--) {
particles[i].run();
if (particles[i].isOffScreen() || particles[i].isTooFar()) { // Eğer partikül ekran dışındaysa veya çok uzaksa sil
particles.splice(i, 1);
}
}
// Eğer tüm partiküller kaybolduysa yeni bir patlama başlat
if (particles.length === 0) {
if (!explosionPhase) {
explosionPhase = true; // Merkeze toplandıktan sonra patlama aşamasını başlat
explode();
} else {
explosionPhase = false;
initParticles(); // Yeni partikül dalgasını başlat
}
}
}
function initParticles() {
let launchX = 400, launchY = 400;
let radius = 200; // Dairenin yarıçapı (çapın yarısı)
for (let i = 0; i < num; i++) {
let angle = random(TWO_PI);
let distance = random(0.8 * radius, radius); // Dış kenarlardan başlat
let x = launchX + distance * cos(angle);
let y = launchY + distance * sin(angle);
let dir = p5.Vector.sub(createVector(launchX, launchY), createVector(x, y));
dir.normalize().mult(random(2, 3)); // Merkeze doğru hafif bir hız
let col = random(colors);
particles.push(new Particle(createVector(x, y), dir, col, radius));
}
}
function explode() {
let launchX = 400, launchY = 400;
for (let i = 0; i < num; i++) {
let angle = random(TWO_PI);
let speed = random(5, 25); // Patlama hızı
let dir = p5.Vector.fromAngle(angle);
dir.mult(speed);
let col = random(colors);
particles.push(new Particle(createVector(launchX, launchY), dir, col, 200));
}
}
class Particle {
constructor(loc, dir, col, radius) {
this.loc = loc;
this.dir = dir;
this.col = col;
this.radius = radius;
this.size = random(1,3);
}
run() {
this.move();
this.display();
}
move() {
this.loc.add(this.dir);
this.checkEdges(); // Yansıma kontrolleri
}
checkEdges() {
let d = dist(this.loc.x, this.loc.y, 400, 400);
if (d > this.radius && explosionPhase) {
// Daire sınırını geçerse yansıma açısı hesapla ve yönü ayarla
let angleToCenter = atan2(400 - this.loc.y, 400 - this.loc.x) + PI;
this.dir.x = cos(angleToCenter);
this.dir.y = sin(angleToCenter);
this.dir.mult(-1);
}
}
display() {
fill(this.col);
noStroke();
ellipse(this.loc.x, this.loc.y, this.size, this.size);
}
isOffScreen() {
return (this.loc.x < 0 || this.loc.x > width || this.loc.y < 0 || this.loc.y > height);
}
isTooFar() {
return dist(this.loc.x, this.loc.y, 400, 400) > 200; // Partikül merkezden 200px'den daha uzaksa sil
}
}