Untitled
unknown
plain_text
2 years ago
1.6 kB
18
Indexable
let x = [];
let y = [];
let r;
let g;
let b;
let mic;
let amp;
let centerX;
let centerY;
let formResolution = 100;
let initRadius = 150;
let stepSize = 10;
/**
* Setup the canvas and initialize variables.
*/
function setup() {
frameRate(30);
createCanvas(windowWidth, windowHeight);
centerX = width / 2;
centerY = height / 2;
r = random();
g = random(255);
b = random(255);
// Initialize microphone and amplitude objects
mic = new p5.AudioIn();
mic.start();
amp = new p5.Amplitude();
amp.setInput(mic);
let angle = radians(360 / formResolution);
for (let i = 0; i < formResolution; i++) {
x.push(cos(angle * i) * initRadius);
y.push(sin(angle * i) * initRadius);
}
}
/**
* Main draw loop.
*/
function draw() {
let soundLevel = amp.getLevel(); // Get the amplitude level in a 0.0 - 1.0 range
let dynamicStepSize = 2*map(soundLevel, 0, 1, 1, 20); // Map the step size based on soundLevel
centerX += random(windowWidth/2 - centerX, windowWidth/2 - centerX);
centerY += random(windowWidth/2 - centerX, windowWidth/2 - centerX);
for (let i = 0; i < formResolution; i++) {
x[i] += random(-dynamicStepSize, dynamicStepSize);
y[i] += random(-dynamicStepSize, dynamicStepSize);
}
stroke(r*soundLevel, g*soundLevel, b*(1-soundLevel));
noFill();
beginShape();
curveVertex(x[formResolution - 1] + centerX, y[formResolution - 1] + centerY);
for (let i = 0; i < formResolution; i++) {
curveVertex(x[i] + centerX, y[i] + centerY);
}
curveVertex(x[0] + centerX, y[0] + centerY);
curveVertex(x[1] + centerX, y[1] + centerY);
endShape();
background(255, 10);
}
Editor is loading...