xxxxxxxxxx
137
let sunHeight = 400
let sunSpeed = 1
let skyRed = 0
let skyGreen = 0
let skyBlue = 0
let skyBrite = 0
let earthRed = 0
let earthGreen = 0
let earthBlue = 0
let lensTint = 0
let sunButton = false
let starLatitude
let starLongitude = 0
let starRed = 0
let starGreen = 0
let starBlue = 0
function setup() {
createCanvas(400, 400);
}
function draw() {
background(skyRed, skyGreen, skyBlue);
// map sky brightness to height of sun
skyBlue = map(sunHeight, height, 0, 0, 255);
// draw star field
for (starLatitude = 0; starLatitude < width; starLatitude += random(width)) {
for (starLongitude = 0; starLongitude < height; starLongitude += random(height)) {
noStroke();
fill(255);
ellipse(starLatitude, starLongitude, 5, 5);
}
}
// draw sun
noStroke();
fill(255, 255, 0);
ellipseMode(CENTER);
ellipse(50, sunHeight, 50, 50);
// move sun
sunHeight -= sunSpeed;
// reverse sun at edge of canvas
if (sunHeight < 0 || sunHeight > height) sunSpeed *= -1;
// TODO: slow sun speed in proportion to distance from canvas edge
// sunSpeed = sunHeight / 100
// draw earth
noStroke();
fill(0, earthGreen, 0);
rect(0, 320, height, 100)
// map earth brightness to height of sun
earthGreen = map(sunHeight, height, 0, 0, 255);
// draw mustache
noStroke();
fill(0);
triangle(150, 220, 200, 200, 250, 220);
// mustacheTip = map(mouseY, 400, 0, 250, 240)
// draw goatee
noStroke();
fill(0);
triangle(185, 250, 215, 250, 200, 225);
noStroke();
fill(0);
triangle(175, 250, 200, 290, 225, 250);
// draw left pupil
noStroke();
fill(0);
ellipse(170, 165, 10, 10);
// draw right pupil
noStroke();
fill(0);
ellipse(225, 165, 10, 10);
// draw glasses lens left eye
stroke(0);
strokeWeight(3);
fill(lensTint, 125);
rect(140, 145, 45, 30, 5, 5, 20, 20);
// draw glasses lens right eye
stroke(0);
strokeWeight(3)
fill(lensTint, 125);
rect(210, 145, 45, 30, 5, 5, 20, 20);
// transition lenses
push();
lensTint = map(sunHeight, 100, 0, 255, 0);
lensTint = lensTint + 1;
// button to reverse sunSpeed
// declare and assign button variables
let fastButtonX = 335;
let fastButtonY = 335;
let fastButtonWidth = 50;
let fastButtonHeight = 50;
push();
noStroke();
fill('red');
// make fast-forward button a rollover
if ((mouseX > fastButtonX) && (mouseX < fastButtonX + fastButtonWidth) && (mouseY > fastButtonY) && (mouseY < fastButtonY + fastButtonHeight)) {
fill(125);
if (mouseIsPressed) {
sunSpeed = sunSpeed * -1
}
}
// draw fast-forward button
rect(fastButtonX, fastButtonY, fastButtonWidth, fastButtonHeight);
pop();
}
// TODO: make sunButton have on/off state
// function mousePressed() {
// if ((mouseX > fastButtonX) && (mouseX < fastButtonX + fastButtonWidth) && (mouseY > fastButtonY) && (mouseY < fastButtonY + fastButtonHeight)) {
// sunButton = !sunButton
// }
// }
// TODO: incorporate a better random event
// if (mouseIsPressed) {
// if (mouseButton === LEFT) {
// sunHeight = random(400, 0);
// }
// }