p5((s) => {
let points = [];
let step = 6;
let mic;
s.setup = function () {
s.createCanvas(width, 500);
mic = new pSound.AudioIn();
mic.start();
for (let i = 0; i < 50; i++) {
points.push(s.createVector(s.random(s.width), s.random(s.height)));
}
s.noFill();
s.noStroke();
s.frameRate(30);
};
s.draw = function () {
let micLevel = mic.getLevel();
let micMapped = s.map(micLevel, 0, 1, 0, 2);
s.background(0);
for (let i = 0; i < points.length; i++) {
points[i].x += s.cos(s.frameCount * micMapped + i);
points[i].y += s.sin(s.frameCount * micMapped + i);
}
drawVoronoi();
};
function drawVoronoi() {
for (let x = 0; x < s.width; x += step) {
for (let y = 0; y < s.height; y += step) {
let minDist = s.dist(0, 0, s.width, s.height);
let j = -1;
for (let i = 0; i < points.length; i++) {
let d = s.dist(x, y, points[i].x, points[i].y);
if (d < minDist) {
minDist = d;
j = i;
}
}
let c = minDist * 2;
s.fill(c);
s.rect(x, y, step, step);
}
}
}
})