xxxxxxxxxx
248
let canvas;
let backgroundColor;
let skinColor;
let skinColor2;
let lipColor;
let hairColor;
let corneaColor;
let irisColor;
let glassesColor;
let shirtColor;
let time = "day";
let textY;
function setup() {
canvas = createCanvas(400, 400);
//for text movement
textY = 1.5*height/10;
textIncrementY = 0.01*height/10
}
function draw() {
//colors and other variable declaration
skinColor = color("#CDA888");
skinColor2 = color("#B99A82");
lipColor = color("#b26458")
hairColor = color("black");
corneaColor = color("#F3ECEC");
irisColor = color("#49322E");
glassesColor = color("#423E3E");
shirtColor = color("chocolate");
//check whether it is "day" or "night" based on mouseClicked() function
if (time == "day"){
backgroundColor = color("#FDF4D8");
background(backgroundColor);
fill("gold");
noStroke();
arc(5*width/10,0,4*width/10,1.5*height/10,0,PI);
fill("black")
textSize(16);
text("Hit the lamp above to put me to sleep", 2*width/10, textY); //text remains stationary when awake
} else{
backgroundColor = color("#353D6D");
background(backgroundColor);
fill("grey");
noStroke();
arc(5*width/10,0,4*width/10,1.5*height/10,0,PI);
fill("rgba(255,255,255,0.6)")
textSize(16);
//to make text bounce between two heights
if (textY > 1.6*height/10){
textIncrementY = -0.01*height/10
} else if (textY < 1.4*height/10){
textIncrementY = 0.01*height/10
}
if (frameCount % 4 == 0){
textY = textY + textIncrementY
}
text("Hit the lamp above to wake me up", 2*width/10, textY);
}
rectMode(CENTER);
drawShirt();
drawHead();
drawNose();
drawLips();
drawHair();
if (time == "day"){
drawDayEyes();
drawGlasses();
} else{
drawNightEyes();
}
}
//head and neck
function drawHead() {
noStroke();
//neck
fill(skinColor2);
rect(5*width/10, 7.4*height/10, 3*width/10, 2*height/10);
arc(5*width/10, 8.4*height/10, 3*width/10, 1.5*height/10, 2*PI, PI);
//head
fill(skinColor);
arc(3.25*width/10, 5*height/10, width/10, 5*height/10, PI/2, 3*PI/2); //face right side
arc(6.75*width/10, 5*height/10, width/10, 5*height/10, 3*PI/2, PI/2); //face left side
rect(5*width/10, 5*height/10, 3.5*width/10, 5*height/10); //face center
arc(5*width/10, 7.4*height/10, 3.75*width/10, 1.25*height/10, 2*PI, PI); //jaw
arc(5*width/10, 7.85*height/10, 1.75*width/10, 0.7*height/10, 2*PI, PI); //chin
//right ear
beginShape();
vertex(2.85*width/10, 3.8*height/10);
bezierVertex(2.5*width/10, 3.6*height/10, 2.4*width/10, 4.025*height/10, 2.55*width/10, 4.55*height/10);
bezierVertex(2.625*width/10, 4.65*height/10, 2.4*width/10, 5.05*height/10, 2.8*width/10, 5*height/10)
endShape();
//left ear
beginShape();
vertex(7.15*width/10, 3.8*height/10);
bezierVertex(7.5*width/10, 3.6*height/10, 7.6*width/10, 4.025*height/10, 7.45*width/10, 4.55*height/10);
bezierVertex(7.375*width/10, 4.65*height/10, 7.6*width/10, 5.05*height/10, 7.2*width/10, 5*height/10)
endShape();
}
//hair and eyebrows
function drawHair(){
fill(hairColor);
noStroke();
//right
triangle(3.15*width/10,2.5*height/10,2.775*width/10,5.775*height/10,3.45*width/10,2.975*height/10);
arc(3*width/10,4.25*height/10,0.5*width/10,3.75*height/10, PI/2+0.2, 3*PI/2+0.1);
//top
rect(5*width/10,2.725*height/10,4*width/10,0.5*height/10);
arc(4.3*width/10,2.475*height/10, 2.6*width/10,0.95*height/10, PI, 0);
arc(5.6*width/10,2.55*height/10, 3*width/10,0.75*height/10, PI, 0);
arc(4.2*width/10,2.9*height/10, 2*width/10,0.55*height/10, 0, PI);
arc(6.6*width/10,2.95*height/10, 3*width/10,0.75*height/10, 1, PI);
//left
triangle(7.2*width/10,2.82*height/10,6.57*width/10,2.85*height/10,7.2*width/10,5.5*height/10);
arc(6.95*width/10,2.825*height/10,0.5*width/10,0.6*height/10, 3*PI/2, 0);
//eyebrows
strokeWeight(9);
stroke(hairColor);
noFill();
arc(4.13*width/10,3.9*height/10,1.5*width/10,0.7*height/10,PI+0.4,-0.5);
arc(5.87*width/10,3.9*height/10,1.5*width/10,0.7*height/10,PI+0.5,-0.4);
}
//mouth
function drawLips(){
noStroke();
fill(lipColor);
//upper lip
arc(5*width/10,6.85*height/10,1.8*width/10,0.3*height/10,PI,0);
triangle(4.1*width/10,6.825*height/10,4.925*width/10,6.63*height/10,5.1*width/10,6.725*height/10);
triangle(5.9*width/10,6.825*height/10,5.075*width/10,6.63*height/10,4.9*width/10,6.725*height/10);
arc(5*width/10,6.85*height/10,1.8*width/10,0.65*height/10,0-0.05,PI+0.05); //bottom lip
//lip separation
noFill();
stroke("#8A3A3A");
strokeWeight(1);
arc(4.28*width/10, 6.825*height/10, 0.34*width/10, 0.07*height/10, 0.5, PI);
arc(4.54*width/10, 6.875*height/10, 0.46*width/10, 0.07*height/10, PI+0.07, 0);
arc(5*width/10, 6.885*height/10, 0.44*width/10, 0.07*height/10, 0, PI);
arc(5.46*width/10, 6.875*height/10, 0.46*width/10, 0.07*height/10, PI, -0.07);
arc(5.72*width/10, 6.825*height/10, 0.34*width/10, 0.07*height/10, 0, PI-0.5);
}
//nose
function drawNose(){
//apex
noFill();
stroke("#7E6552");
strokeWeight(1.5);
arc(5*width/10, 5.88*height/10, 0.35*width/10, 0.45*height/10, 0.5, PI-0.5);
//ala (nose wings)
bezier(5.4*width/10,5.7*height/10,5.55*width/10, 5.825*height/10,5.9*width/10,6.15*height/10,5.4*width/10,6.2*height/10);
bezier(4.6*width/10,5.7*height/10,4.45*width/10, 5.825*height/10,4.1*width/10,6.15*height/10,4.6*width/10,6.2*height/10);
//dorsum
strokeWeight(1);
arc(4.5*width/10, 4.65*height/10, 0.6*width/10, 2*height/10, 3*PI/2+2,PI/2);
arc(5.5*width/10, 4.65*height/10, 0.6*width/10, 2*height/10, PI/2, 3*PI/2-2)
//nostrils
fill("black");
noStroke();
ellipse(4.7*width/10, 5.98*height/10,0.3*width/10, 0.2*height/10);
ellipse(5.3*width/10, 5.98*height/10,0.3*width/10, 0.2*height/10);
}
//Open eyes for awake
function drawDayEyes(){
//cornea
strokeWeight(2);
stroke(hairColor);
fill(corneaColor);
//right
arc(4.13*width/10,4.28*height/10,0.8*width/10,0.55*height/10, PI+0.1, -0.1, OPEN);
bezier(3.73*width/10,4.23*height/10,4*width/10,4.5*height/10,4.25*width/10,4.45*height/10,4.5*width/10,4.23*height/10);
//left
arc(5.87*width/10,4.28*height/10,0.8*width/10,0.55*height/10, PI+0.1, -0.1, OPEN);
bezier(5.47*width/10,4.23*height/10,5.77*width/10,4.45*height/10,6.07*width/10,4.5*height/10,6.27*width/10,4.23*height/10);
//irises
noStroke();
fill(irisColor);
ellipse(4.13*width/10,4.2*height/10,0.34*width/10);
ellipse(5.87*width/10,4.2*height/10,0.34*width/10);
//pupils
fill("black");
ellipse(4.13*width/10,4.2*height/10,0.18*width/10);
ellipse(5.87*width/10,4.2*height/10,0.18*width/10);
}
//Closed eyes for sleeping
function drawNightEyes(){
//cornea
strokeWeight(2);
stroke(hairColor);
fill(skinColor2);
//right
arc(4.13*width/10,4.18*height/10,width/10,0.45*height/10, -0.2, PI+0.2, OPEN);
//left
arc(5.87*width/10,4.18*height/10,width/10,0.45*height/10, -0.2, PI+0.2, OPEN);
}
//Glasses
function drawGlasses(){
strokeWeight(8);
stroke(glassesColor);
noFill();
quad(3.13*width/10,3.9*height/10,4.73*width/10,3.9*height/10,4.63*width/10,4.8*height/10,3.18*width/10,4.8*height/10); //right lens
quad(5.27*width/10,3.9*height/10,6.87*width/10,3.9*height/10,6.82*width/10,4.8*height/10,5.37*width/10,4.8*height/10); //left lens
line(3.33*width/10,3.9*height/10,6.67*width/10,3.9*height/10); //bridge
//frame
line(3.13*width/10,3.9*height/10,2.8*width/10,3.7*height/10);
line(6.87*width/10,3.9*height/10,7.2*width/10,3.7*height/10);
//optical lensing from high power
noStroke();
fill(backgroundColor);
rect(3.35*width/10, 4.35*height/10, 0.25*width/10, 0.7*height/10);
rect(6.65*width/10, 4.35*height/10, 0.25*width/10, 0.7*height/10);
fill(skinColor);
arc(3.475*width/10, 4.35*height/10, 0.07*width/10, 0.7*height/10, PI/2, 3*PI/2);
arc(6.525*width/10, 4.35*height/10, 0.07*width/10, 0.7*height/10, 3*PI/2, PI/2);
}
//T-Shirt
function drawShirt() {
noStroke();
fill(shirtColor);
quad(width/10, 9.5*height/10, 3.5*width/10, 8.5*height/10, 6.5*width/10, 8.5*height/10, 9*width/10, 9.5*height/10)
rect(5*width/10, 9.75*height/10, 8*width/10, 0.5*height/10)
//to make the shoulders rounded
arc(width/10, height, 0.5*width/10, height/10, PI/2, 3*PI/2);
arc(9*width/10, height, 0.5*width/10, height/10, 3*PI/2, PI/2);
}
//cycling between "wake" and "sleep"
function mouseClicked() {
//checks whether left mouse button was clicked within the lamp
//checks whether mouse is inside a circle with the radius of the major axis (width) and above the bottom of the lamp
if (mouseButton == LEFT && dist(5*width/10, 0, mouseX, mouseY) <= 2*width/10 && mouseY <= 0.75*height/10) {
if (time == "day"){
time = "night";
print("Lights out!")
} else{
time = "day";
print("Lights on!")
}
}
}