Skip to content

Commit 800355d

Browse files
authored
Merge pull request #7 from processing/Clock
Clock
2 parents 659c08d + 57070fd commit 800355d

2 files changed

Lines changed: 49 additions & 28 deletions

File tree

examples/curated/22_Clock.js

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,84 @@
1-
/*
1+
/**
22
* @name Clock
3-
* @arialabel Functioning pink clock on a grey background
4-
* @description The current time can be read with the second(),
5-
* minute(), and hour() functions. In this example, sin() and
6-
* cos() values are used to set the position of the hands.
3+
* @description The current time can be read with the
4+
* <a href="https://p5js.org/reference/#/p5/second" target="_blank">second()</a>,
5+
* <a href="https://p5js.org/reference/#/p5/minute" target="_blank">minute()</a>,
6+
* and <a href="https://p5js.org/reference/#/p5/minute" target="_blank">hour()</a>
7+
* functions. This example uses
8+
* <a href="https://p5js.org/reference/#/p5/map" target="_blank">map()</a>
9+
* to calculate the angle of the hands. It then uses
10+
* <a href="https://p5js.org/reference/#group-Transform" target="_blank">transformations</a>
11+
* to set their position.
712
*/
8-
let cx, cy;
13+
14+
// Declare variables for shape radii
915
let secondsRadius;
1016
let minutesRadius;
1117
let hoursRadius;
1218
let clockDiameter;
1319

1420
function setup() {
15-
createCanvas(720, 400);
21+
createCanvas(710, 400);
1622
stroke(255);
23+
angleMode(DEGREES);
1724

25+
// Set radius for each shape based on canvas dimensions
1826
let radius = min(width, height) / 2;
1927
secondsRadius = radius * 0.71;
2028
minutesRadius = radius * 0.6;
2129
hoursRadius = radius * 0.5;
2230
clockDiameter = radius * 1.7;
2331

24-
cx = width / 2;
25-
cy = height / 2;
32+
describe('Functioning pink clock on a grey background.');
2633
}
2734

2835
function draw() {
2936
background(230);
3037

38+
// Move origin to center of canvas
39+
translate(width / 2, height / 2);
40+
3141
// Draw the clock background
3242
noStroke();
3343
fill(244, 122, 158);
34-
ellipse(cx, cy, clockDiameter + 25, clockDiameter + 25);
44+
ellipse(0, 0, clockDiameter + 25, clockDiameter + 25);
3545
fill(237, 34, 93);
36-
ellipse(cx, cy, clockDiameter, clockDiameter);
46+
ellipse(0, 0, clockDiameter, clockDiameter);
3747

38-
// Angles for sin() and cos() start at 3 o'clock;
39-
// subtract HALF_PI to make them start at the top
40-
let s = map(second(), 0, 60, 0, TWO_PI) - HALF_PI;
41-
let m = map(minute() + norm(second(), 0, 60), 0, 60, 0, TWO_PI) - HALF_PI;
42-
let h = map(hour() + norm(minute(), 0, 60), 0, 24, 0, TWO_PI * 2) - HALF_PI;
48+
// Calculate angle for each hand
49+
let secondAngle = map(second(), 0, 60, 0, 360);
50+
let minuteAngle = map(minute(), 0, 60, 0, 360);
51+
let hourAngle = map(hour(), 0, 12, 0, 360);
4352

44-
// Draw the hands of the clock
4553
stroke(255);
54+
55+
// Second hand
56+
push();
57+
rotate(secondAngle);
4658
strokeWeight(1);
47-
line(cx, cy, cx + cos(s) * secondsRadius, cy + sin(s) * secondsRadius);
59+
line(0, 0, 0, -secondsRadius);
60+
pop();
61+
62+
// Minute hand
63+
push();
4864
strokeWeight(2);
49-
line(cx, cy, cx + cos(m) * minutesRadius, cy + sin(m) * minutesRadius);
65+
rotate(minuteAngle);
66+
line(0, 0, 0, -minutesRadius);
67+
pop();
68+
69+
// Hour hand
70+
push();
5071
strokeWeight(4);
51-
line(cx, cy, cx + cos(h) * hoursRadius, cy + sin(h) * hoursRadius);
72+
rotate(hourAngle);
73+
line(0, 0, 0, -hoursRadius);
74+
pop();
5275

53-
// Draw the minute ticks
76+
// Tick markers around perimeter of clock
77+
push();
5478
strokeWeight(2);
55-
beginShape(POINTS);
56-
for (let a = 0; a < 360; a += 6) {
57-
let angle = radians(a);
58-
let x = cx + cos(angle) * secondsRadius;
59-
let y = cy + sin(angle) * secondsRadius;
60-
vertex(x, y);
79+
for (let ticks = 0; ticks < 60; ticks += 1) {
80+
point(0, -secondsRadius);
81+
rotate(6);
6182
}
62-
endShape();
83+
pop();
6384
}

examples/curated/22_Clock.png

22 KB
Loading

0 commit comments

Comments
 (0)