/
clock.rb
60 lines (56 loc) · 1.71 KB
/
clock.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env jruby -v -W2
# frozen_string_literal: true
require 'propane'
# The current local time can be read with the Time.now
# the seconds Time.now.sec, minutes Time.now.min etc...
# functions. In this example, DegLut.sin() and DegLut.cos() values are used to
# set the position of the hands, perfect for degree precision Lookup Table.
class Clock < Propane::App
def setup
sketch_title 'Clock'
stroke 255
@font = create_font 'Roman', 20
text_font @font
end
def draw
background 0
fill 80
no_stroke
# adj factor to map 0-60 to 0-360 (sec/min) & 0-12 to 0-360 (hours)
# since angles for DegLut.sin() and DegLut.cos() start at 3 o'clock we
# subtract 90 degrees to make them start at the top.
clock_x = lambda do |val, adj, length|
DegLut.cos((val * adj).to_i - 90) * length + width / 2
end
clock_y = lambda do |val, adj, length|
DegLut.sin((val * adj).to_i - 90) * length + height / 2
end
ellipse 100, 100, 160, 160
stroke 220
stroke_weight 6
t = Time.now
line(100, 100, clock_x.call(t.hour % 12 + (t.min / 60.0), 30, 50),
clock_y.call(t.hour % 12 + (t.min / 60.0), 30, 50))
stroke_weight 3
line(100, 100, clock_x.call(t.min + (t.sec / 60.0), 6, 60),
clock_y.call(t.min + (t.sec / 60.0), 6, 60))
stroke 255, 0, 0
stroke_weight 1
line(100, 100, clock_x.call(t.sec, 6, 72), clock_y.call(t.sec, 6, 72))
# Draw the minute ticks
stroke_weight 2
stroke 255
(0..360).step(6) do |a|
x = 100 + DegLut.cos(a) * 72
y = 100 + DegLut.sin(a) * 72
point x, y
end
fill 200
text t.strftime('%H:%M:%S'), 50, 200
end
def settings
size 200, 220, P2D
smooth 8
end
end
Clock.new