xxxxxxxxxx
73
let mic;
let particles = [];
function setup() {
createCanvas(800, 600);
mic = new p5.AudioIn();
mic.start();
// Create initial particles
for (let i = 0; i < 100; i++) {
particles.push(new Particle(random(width), random(height)));
}
}
function draw() {
let vol = mic.getLevel();
background(0, 10);
for (let particle of particles) {
particle.update(vol);
particle.display();
}
}
class Particle {
constructor(x, y) {
this.x = x;
this.y = y;
this.angle = random(TWO_PI);
this.length = random(10, 20); // Length of the trails
this.timeOffset = random(TWO_PI);
}
update(vol) {
let harmonicX = cos(this.angle) * 200 * sin(frameCount * 0.02 + this.timeOffset); // Horizontal oscillation
let harmonicY = sin(this.angle * 2) * 200 * cos(frameCount * 0.03 + this.timeOffset); // Vertical oscillation
// Update positions based on combined harmonic motion
this.x += harmonicX * vol * 0.5;
this.y += harmonicY * vol * 0.5;
this.x += cos(this.angle) * vol * 100;
this.y += sin(this.angle) * vol * 100;
this.x = constrain(this.x, 0, width);
this.y = constrain(this.y, 0, height);
this.angle += 0.05; // Smooth rotation
}
display() {
noStroke();
fill(255, 150, 0, 150);
let xEnd = this.x + cos(this.angle) * this.length;
let yEnd = this.y + sin(this.angle) * this.length;
line(this.x, this.y, xEnd, yEnd);
// Add a glowing effect
for (let i = 0; i < 5; i++) {
stroke(255, 150, 0, 150 - i * 30);
strokeWeight(1 + i * 0.5);
line(this.x, this.y, xEnd, yEnd);
}
}
}