/
ruby-squares.html
66 lines (60 loc) · 1.76 KB
/
ruby-squares.html
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
61
62
63
64
65
66
<html>
<head>
<title>Ruby - Squares</title>
<style type="text/css">
html, body { height: 100%; overflow: hidden; }
body { padding: 0; margin: 0; }
#squares { height: 100%; }
</style>
<script type="text/javascript">
window.DLR = {settings: {id: 'squares', width:'100%', height:'100%'}}
</script>
<script src="ironruby/dlr.js" type="text/javascript"></script>
</head>
<body>
<script type="text/ruby" src="repl.rb"></script>
<script type="text/ruby" src="wpf_ext.rb"></script>
<script type="text/ruby" src="bouncer.rb"></script>
<script type="text/ruby" src="sketch.rb"></script>
<script type="text/ruby">
class Squares < Sketch
def setup
200.times do |i|
container.dispatcher.begin_invoke do |s,e|
my_square(15)
end
end
end
def draw
container.children.each do |obj|
obj.tag.update(obj) if obj.tag
end if @bounce_on
end
def my_square(length)
size = rand(dimensions.max / length)
container.children.add(square(
:size => size,
:fill => rand_brush,
:stroke => rand_brush,
:stroke_thickness => rand(dimensions.max / (length * 7)) + 4,
:left => rand(dimensions.first - size),
:top => rand(dimensions.last - size),
:tag => Bouncer.new(rand(10)-5, rand(10)-5, container)
))
end
def brushes
@brushes ||= %W(red orange yellow lime blue purple violet).map do |c|
SolidColorBrush.new(Colors.send(c))
end
end
def rand_brush
brushes[rand(brushes.size)]
end
def toggle_bounce
@bounce_on = !@bounce_on
end
end
Squares.create_sketch
</script>
</body>
</html>