root / trunk / samples / clock.rb

Revision 373, 1.6 kB (checked in by why, 8 months ago)
  • shoes/ruby.c: mental's fix to draw the stroke after the fill.
Line 
1#
2# Shoes Clock by Thomas Bell
3# posted to the Shoes mailing list on 04 Dec 2007
4#
5Shoes.app :height => 260, :width => 250 do
6  @radius, @centerx, @centery = 90, 126, 140
7  stack :margin => 10 do
8    animate(8) do
9      @time = Time.now
10      clear do
11        draw_background
12        stack do
13          background black
14          para @time.strftime("%a"),
15            span(@time.strftime(" %b %d, %Y "), :stroke => "#ccc"),
16            strong(@time.strftime("%I:%M"), :stroke => white),
17            @time.strftime(".%S"), :align => "center", :stroke => "#666"
18        end
19        clock_hand @time.sec + (@time.usec * 0.000001),2,30,red
20        clock_hand @time.min + (@time.sec / 60.0),5
21        clock_hand @time.hour + (@time.min / 60.0),8,6
22      end
23    end
24  end
25  def draw_background
26    background rgb(230, 240, 200)
27
28    fill white
29    stroke black
30    strokewidth 4
31    oval @centerx - 102, @centery - 102, 204, 204
32
33    fill black
34    nostroke
35    oval @centerx - 5, @centery - 5, 10, 10
36
37    stroke black
38    strokewidth 1
39    line(@centerx, @centery - 102, @centerx, @centery - 95)
40    line(@centerx - 102, @centery, @centerx - 95, @centery)
41    line(@centerx + 95, @centery, @centerx + 102, @centery)
42    line(@centerx, @centery + 95, @centerx, @centery + 102)
43  end
44  def clock_hand(time, sw, unit=30, color=black)
45    radius_local = unit == 30 ? @radius : @radius - 15
46    _x = radius_local * Math.sin( time * Math::PI / unit )
47    _y = radius_local * Math.cos( time * Math::PI / unit )
48    stroke color
49    strokewidth sw
50    line(@centerx, @centery, @centerx + _x, @centery - _y)
51  end
52end
Note: See TracBrowser for help on using the browser.