Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Creative Coding
- * Week 5, 02 - Digital Clock
- * by Indae Hwang and Jon McCormack
- * Copyright (c) 2014 Monash University
- *
- * This sketch shows how to use text in Processing
- * The sketch creates a digital clock that shows the current time in hours, minutes and seconds
- * Use the 'h', 'm' and 's' keys to enlarge the hours, minutes or seconds in the display.
- *
- * Adapted by Giles Cartmel
- * Gives 3D effect by drawing a scale of grey up to the ratio position of the number
- * based on it's min/max values within the scale of 20 to 200 size, drawing the actual number
- * at it's relative position in white, and all further sizes up to 200 are drawn in very transparent grey
- * Numbers are all shown in 2 digit numbers with leading 0's
- * FrameRate set to 2 so that the "pips" can invert every half a second
- *
- */
- PFont myFont; // font data
- int gap; // gap between digits
- void setup() {
- size(1024, 600);
- myFont = createFont("Arial", 200);
- textFont(myFont); // set the current font to myFont
- gap = 300;
- ellipseMode(RADIUS);
- frameRate(2);
- }
- void draw() {
- background(0);
- fill(255);
- // draw h, m, s
- drawNumber(hour(), -gap, 0, 0, 23);
- drawNumber(minute(), 0, 0, 0, 59);
- drawNumber(second(), gap, 0, 0, 59);
- if (frameCount%2 == 0) {
- stroke(100);
- fill(0);
- } else {
- stroke(0);
- fill(255);
- }
- ellipse(width/2-gap/2,height/2-20,10,10);
- ellipse(width/2-gap/2,height/2+20,10,10);
- ellipse(width/2+gap/2,height/2-20,10,10);
- ellipse(width/2+gap/2,height/2+20,10,10);
- }
- /*
- * drawNumber
- * takes an integer and draws it offset from the centre of the screen by
- * offsetX and offsetY. If big is true then use a big size for the type.
- *
- */
- void drawNumber(int number, float offsetX, float offsetY, int minVal, int maxVal) {
- String theText = nf(number,2); // convert number to 2 digit string with leading 0 if necessary
- int pos = (int) map(number, minVal, maxVal, 20, 200);
- for (int i = 20; i <= 200; i++) {
- if (i < pos) {
- stroke((int) map(i,20,200,20,pos));
- fill((int) map(i,20,200,20,pos),50);
- } else if (i == pos) {
- stroke(255);
- fill(255);
- } else {
- stroke(128,25);
- fill(30,5);
- }
- textSize(i);
- float tWidth = textWidth(theText) * 0.5;
- float tAscent = textAscent() * 0.375;
- // draw text offset from the centre of the screen
- text(theText, width/2 - tWidth + offsetX, height/2 + tAscent + offsetY);
- }
- }
- void keyPressed() {
- if (key=='s') {
- saveFrame("Giles_5_02_a_"+String.valueOf(year())+"-"+String.valueOf(month())+"-"+String.valueOf(day())+"-"+String.valueOf(hour())+"-"+String.valueOf(minute())+"-"+String.valueOf(second())+".jpg");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement