xxxxxxxxxx
45
class Particle{
constructor(vector, center, speed, osc){
this.pos = vector;
this.center = center;
this.speed = speed;
this.osc = osc;
this.osc.amp(0.1);
this.osc.start();
}
}
let particles
function setup() {
createCanvas(400, 400);
particles = [];
for(let i = 0; i < 5; i++){
let v = createVector(random(0,width),random(0,height));
let c = createVector(random(0,width),random(0,height));
let speed = random(5,10);
let osc = new p5.SinOsc(); // set frequency and type
let fft = new p5.FFT();
particles.push(new Particle(v, c, speed, osc));
}
}
function draw() {
background(220);
for(let i = 0; i < particles.length; i++){
particles[i].pos.x = width/2 + Math.sin(frameCount/particles[i].speed) * 190 * random();
particles[i].pos.y = height/2 + Math.cos(frameCount/particles[i].speed) * 190
// sound
let freq = map(particles[i].pos.x, 0, width, 420, 460);
particles[i].osc.freq(freq);
let amp = map(particles[i].pos.y, 0, height, 1, 0.01);
particles[i].osc.amp(amp);
// display
circle(particles[i].pos.x,particles[i].pos.y,10);
}
}