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