xxxxxxxxxx
77
let fft
let Particle = function(position) {
this.position = position
this.oldPosition = position
this.speed = createVector(random(0, 2) - 1, random(0, 2) - 1)
this.color = [random(0, 255), random(0, 255), random(0, 255), ]
this.draw = function() {
circle(this.position.x, this.position.y, this.diameter)
fill(this.color)
}
this.update = function(energy) {
if (Math.abs(this.position.x - this.oldPosition.x) < .1) {
this.speed.x *= 1.0005
}
if (Math.abs(this.position.y - this.oldPosition.y) < .1) {
this.speed.y *= 1.0005
}
this.oldPosition = this.position
this.position.x += 1
this.position.y += 1
// if (this.speed.x > 0) {
// this.position.x += 1
// }
// else {
// this.position.x += 2
// }
// if (this.speed.y > 0) {
// this.position.y += 1
// }
// else {
// this.position.y += 2
// }
this.diameter = random(5, 7) + energy * 100
this.position.x += this.speed.x * energy * 10
this.position.y += this.speed.y * energy * 10
if (energy < .02) {
this.speed.x *= 1.0035
this.speed.y *= 1.0035
}
if (this.position.x < 0) {
this.speed.x *= -1
}
if (this.position.y < 0) {
this.speed.y *= -1
}
if (this.position.x > width || this.position.y > height) {
this.speed.x = random(0, 2) - 1
this.position.x = random(0, width / random(1, 3))
this.speed.y = random(0, 2) - 1
this.position.y = random(0, height / random(1, 3))
}
}
}
function setup() {
createCanvas(windowWidth, windowHeight)
noStroke()
let mic = new p5.AudioIn()
mic.start()
fft = new p5.FFT()
fft.setInput(mic)
positionParticles();
}
function draw() {
background(0, 0, 0)
let spectrum = fft.analyze()
updateParticles(spectrum)
}