|
1 | | -/* |
| 1 | +/** |
2 | 2 | * @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. |
7 | 12 | */ |
8 | | -let cx, cy; |
| 13 | + |
| 14 | +// Declare variables for shape radii |
9 | 15 | let secondsRadius; |
10 | 16 | let minutesRadius; |
11 | 17 | let hoursRadius; |
12 | 18 | let clockDiameter; |
13 | 19 |
|
14 | 20 | function setup() { |
15 | | - createCanvas(720, 400); |
| 21 | + createCanvas(710, 400); |
16 | 22 | stroke(255); |
| 23 | + angleMode(DEGREES); |
17 | 24 |
|
| 25 | + // Set radius for each shape based on canvas dimensions |
18 | 26 | let radius = min(width, height) / 2; |
19 | 27 | secondsRadius = radius * 0.71; |
20 | 28 | minutesRadius = radius * 0.6; |
21 | 29 | hoursRadius = radius * 0.5; |
22 | 30 | clockDiameter = radius * 1.7; |
23 | 31 |
|
24 | | - cx = width / 2; |
25 | | - cy = height / 2; |
| 32 | + describe('Functioning pink clock on a grey background.'); |
26 | 33 | } |
27 | 34 |
|
28 | 35 | function draw() { |
29 | 36 | background(230); |
30 | 37 |
|
| 38 | + // Move origin to center of canvas |
| 39 | + translate(width / 2, height / 2); |
| 40 | + |
31 | 41 | // Draw the clock background |
32 | 42 | noStroke(); |
33 | 43 | fill(244, 122, 158); |
34 | | - ellipse(cx, cy, clockDiameter + 25, clockDiameter + 25); |
| 44 | + ellipse(0, 0, clockDiameter + 25, clockDiameter + 25); |
35 | 45 | fill(237, 34, 93); |
36 | | - ellipse(cx, cy, clockDiameter, clockDiameter); |
| 46 | + ellipse(0, 0, clockDiameter, clockDiameter); |
37 | 47 |
|
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); |
43 | 52 |
|
44 | | - // Draw the hands of the clock |
45 | 53 | stroke(255); |
| 54 | + |
| 55 | + // Second hand |
| 56 | + push(); |
| 57 | + rotate(secondAngle); |
46 | 58 | 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(); |
48 | 64 | 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(); |
50 | 71 | 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(); |
52 | 75 |
|
53 | | - // Draw the minute ticks |
| 76 | + // Tick markers around perimeter of clock |
| 77 | + push(); |
54 | 78 | 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); |
61 | 82 | } |
62 | | - endShape(); |
| 83 | + pop(); |
63 | 84 | } |
0 commit comments