xxxxxxxxxx
166
let mic;
let musicThreshold = 0.02;
let faceWidth = 180;
let faceHeight = 200;
let leftEarXPosition = 120, leftEarYPosition = 180;
let rightEarXPosition = 280, rightEarYPosition = 180;
let EarXSize = 50, EarYSize = 45;
let shirtWidth = 300;
let shirtHeigth = 500;
let shirtPos = 280;
let shoulder = 60;
let neckXPosition = 195, neckYPosition = 280;
let neckXSize = 60, neckYSize = 80;
let LeftEyeXPosition = 160, EyeYPosition = 180;
let RightEyeYPosition = 230;
let eyeXSize = 50, eyeYSize = 25;
function setup()
{
createCanvas(400, 500);
mic = new p5.AudioIn();
mic.start();//start mic
}
function draw()
{ //if there's no music, turn the background black
//else turn it white
if(mic.getLevel() <= musicThreshold)
{
//show bad mood
background(0);
}
else
{
background('white');
textAlign(CENTER);
textStyle(BOLDITALIC)
text("CLICK TO TURN THE DISCO LIGHTS ON", 190, 50);
if (mouseIsPressed)
{
discoLight();
}
}
//ears
fill(210, 180, 140);
ellipse(leftEarXPosition, leftEarYPosition, EarXSize, EarYSize);
fill(210, 180, 140);
ellipse(leftEarXPosition, leftEarYPosition, EarXSize*0.5, EarYSize*0.5);
fill(210, 180, 140);
ellipse(rightEarXPosition, rightEarYPosition, EarXSize, EarYSize);
fill(210, 180, 140);
ellipse(rightEarXPosition, rightEarYPosition, EarXSize*0.5, EarYSize*0.5);
//SHIRT
fill('');
shirt(shirtPos, shirtWidth, shirtHeigth, shoulder);
//neck
fill(210, 180, 140);
arc(neckXPosition, neckYPosition, neckXSize, neckYSize, 0, PI);
//face
fill(210, 180, 140);
ellipse(200, 200, faceWidth, faceHeight );
//eyes
fill(255);
ellipse(LeftEyeXPosition, EyeYPosition, eyeXSize, eyeYSize);
fill(255);
ellipse(RightEyeYPosition, EyeYPosition, eyeXSize, eyeYSize);
//pupils (they dance to the music)
fill('black');
ellipse(160+10*mic.getLevel(), 180+10*mic.getLevel(), 20 );
fill('black');
ellipse(230+10*mic.getLevel(), 180+10*mic.getLevel(), 20);
//mouth (moves with the music)
fill('white');
arc(195, 250, 50+40*mic.getLevel(), 100*mic.getLevel(), 0, PI);
//nose
fill(210, 180, 140);
nose(196, 240, 50)
//hair
fill("black")
ellipse(200, 130, 125, 30);
rect(135, 85, 130, 50, 10, 10);
rect(120, 85, 160, 45, 30, 30)
//heart (dances to the music)
heart(250, 360, mic.getLevel()*100);
}
//function to draw a nose
function nose(x,y, size)
{
beginShape();
vertex(x, y);
bezierVertex(
x - size * 0.4, y - size * 0.1,
x - size * 0.2, y - size * 0.2,
x - size * 0.1, y - size * 0.8
);
vertex(x, y);
bezierVertex(
x + size * 0.4, y - size * 0.1,
x + size * 0.2, y - size * 0.2,
x - size * 0.1, y - size * 0.8
);
endShape(CLOSE);
}
//function to draw a shirt
function shirt(y, shirtWidth, shirtHeigth, shoulder)
{
rect(width/2-shirtWidth/2,y,shirtWidth, shirtHeigth, shoulder, shoulder, 0, 0);
line(100, 345, 100, 500);
line(300, 345, 300, 500);
}
//function to draw heart
//https://editor.p5js.org/Mithru/sketches/Hk1N1mMQg
function heart(x, y, size) {
beginShape();
vertex(x, y);
fill('red');
bezierVertex(x - size / 2, y - size / 2, x - size, y + size / 3, x, y + size);
bezierVertex(x + size, y + size / 3, x + size / 2, y - size / 2, x, y);
endShape(CLOSE);
}
//function to turn on the disco light (One second cycle)
function discoLight() {
let level = map(mic.getLevel(), 0, 1, 50, 255);
if (frameCount % 60 < 10) {
background(level, 0, 0);
} else if (frameCount % 60 < 20) {
background(0, level, 0);
} else if (frameCount % 60 < 30) {
background(0, 0, level);
} else if (frameCount % 60 < 40) {
background(level,0 , level);
}
else if (frameCount % 60 < 50) {
background(0, level, level);
}
else if (frameCount % 60 < 60) {
background(level, level, level);
}
}