/* Scalable Smiley Face. In this version we demonstrate use of a function for drawing the face, and with a user interface modeled on the "growing circle" script from class. Processing Sketch by Michael Goldwasser */ // configuration settings color faceColor = color(255, 255, 0); // white color irisColor = color(56, 183, 240); color mouthColor = color(243, 23, 56); // parameters for a growing face int cx, cy, r; void setup() { size(500, 400); background(255); r = 0; } // required for interactive script, even though empty body void draw() { if (r > 0) { // mouse currently pressed r++; drawFace(cx, cy, 2*r); } } void mousePressed() { // record parameters for new smiley r = 1; cx = mouseX; cy = mouseY; } void mouseReleased() { drawFaceMessage("Have a nice day!", cx, cy, 2*r); r = 0; // causes smiley to stop growing } // Draw a face centered at (x,y) having width w void drawFace(float x, float y, float w) { stroke(0); strokeWeight(1); // general face fill(faceColor); ellipse(x, y, w, w); // eyes float eyeWidth = 0.2*w; float eyeGap = 0.15*w; float eyeY = y - 0.15*w; fill(255); ellipse(x - eyeGap, eyeY, eyeWidth, eyeWidth); ellipse(x + eyeGap, eyeY, eyeWidth, eyeWidth); noStroke(); fill(irisColor); ellipse(x - eyeGap, eyeY, 0.6*eyeWidth, 0.6*eyeWidth); ellipse(x + eyeGap, eyeY, 0.6*eyeWidth, 0.6*eyeWidth); fill(0); ellipse(x - eyeGap, eyeY, 0.35*eyeWidth, 0.35*eyeWidth); ellipse(x + eyeGap, eyeY, 0.35*eyeWidth, 0.35*eyeWidth); // mouth noFill(); stroke(mouthColor); strokeWeight(0.05*w); arc(x, y, 0.6*w, 0.6*w, 0.25*PI, 0.75*PI); } // draw a message immediately below a previously drawn face void drawFaceMessage(String msg, float x, float y, float w) { textSize(12); float temp = textWidth(msg); textSize(12*w/temp); textAlign(CENTER, TOP); fill(0); text(msg, x, y + 0.5*w); }