Each of those ovals and backgrounds you draw is a separate item in memory, which means they bog it down after a while. If you just want to show the last frame you drew, then you need to clear the app each time:
Shoes.app ( :title => 'Circles', :width => 500, :height => 500, :resizable => false ) do
# Animation loop
animate ( 24 ) do |i|
app.clear
# Variables For Randomized Colours
randomCol = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
randomCol2 = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
randomCol3 = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
randomCol4 = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
background randomCol..randomCol2
fill randomCol3
stroke randomCol4
strokewidth ( 0..5 ).rand
# Generate 10 circles per loop cycle
10.times do |i|
i += 1
oval :left => ( -5..self.width ).rand,
:top => ( -5..self.height ).rand,
:radius => ( 1..100 ).rand
end
end
end
That's not as cool as your original one (other than the fact that it'll run indefinitely), because you don't have the layering effect any more. In that case, we can let it run through a few times before clearing. In this example, it'll clear every sixth time through:
Shoes.app ( :title => 'Circles', :width => 500, :height => 500, :resizable => false ) do
# Animation loop
animate ( 24 ) do |i|
app.clear if (i % 6 == 0)
# Variables For Randomized Colours
randomCol = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
randomCol2 = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
randomCol3 = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
randomCol4 = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
background randomCol..randomCol2
fill randomCol3
stroke randomCol4
strokewidth ( 0..5 ).rand
# Generate 10 circles per loop cycle
10.times do |i|
i += 1
oval :left => ( -5..self.width ).rand,
:top => ( -5..self.height ).rand,
:radius => ( 1..100 ).rand
end
end
end
Now an even more interesting strategy would be to keep the last n
passes and clear out the oldest, so that we always have, say, 6 layers on-screen (I find 6 to be a good cutoff point, but your opinion (and computer's performance!) may vary):
Shoes.app ( :title => 'Circles', :width => 500, :height => 500, :resizable => false ) do
n = 6
@layers = []
n.times { @layers << [] }
# Animation loop
animate ( 24 ) do |i|
oldest = i % n
# Clear out oldest frame
@layers[oldest].each {|x| x.remove}
@layers[oldest] = []
# Variables For Randomized Colours
randomCol = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
randomCol2 = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
randomCol3 = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
randomCol4 = rgb( ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand, ( 0..255 ).rand )
@layers[oldest] << background(randomCol..randomCol2)
fill randomCol3
stroke randomCol4
strokewidth ( 0..5 ).rand
# Generate 10 circles per loop cycle
10.times do |i|
@layers[oldest] << oval (:left => ( -5..self.width ).rand,
:top => ( -5..self.height ).rand,
:radius => ( 1..100 ).rand)
end
end
end