FRAMERATE = 15
MAX_CROSS_TIME = 3
MAX_STEP_DIV = FRAMERATE*MAX_CROSS_TIME
LINES = 20
WANDER_JITTER = 10
RGB_MAX = [255]

Shoes.app do
	bg_color = ColorWanderer.new
	bg = background bg_color.wander
			
	size = [width, height]
	endpoints = (0..1).collect do
		[Slider.new(size, 0), Slider.new(size, 1), Slider.new(size, 0), Slider.new(size, 1)]
	end
	linecolors = [ColorWanderer.new, ColorWanderer.new]

	lines = [[], []]

#	dot_pos = [Wanderer.new(size, 0), Wanderer.new(size, 1)]
#	dot = oval :left => dot_pos[0].wander, :top => dot_pos[1].wander, :radius => 10, :center => true
	
	
	
	animate FRAMERATE do
		size[0] = width
		size[1] = height

		(0..1).each do |i|
			after(lines[i].last) do
				stroke linecolors[i].wander
				lines[i].push(line *(endpoints[i].collect {|s| s.slide }))
			end

			if(lines[i].length>LINES)
				junk = lines[i].shift
				junk.remove
			end
		end
		
#		dot.move *(dot_pos.collect { |w| w.wander })
	end
end

# A slider is a real-line component that maintains it's own velocity
# and bounces against 0 or a given max extent.
# "max" is defined herewith as "size[cardinal]" .. since it is normally a component of an array.
#The array gets saved, so that as exterior forces change the array we are kept up to date.
class Slider
	def initialize(size, cardinal)
		@size = size
		@cardinal = cardinal
		@pos = rand*@size[@cardinal]
		@vel = @size[@cardinal] / ( rand * MAX_STEP_DIV + MAX_STEP_DIV/2 ) * (rand(2)*2-1)
	end

	def slide
		@pos += @vel
		sign = 0
		sign=-1 if(@pos>@size[@cardinal])
		sign= 1 if(@pos<0)

		if sign==0
#			@vel *= 1.01
		else
			@pos -= @vel
			@vel= sign * @size[@cardinal] / (rand * MAX_STEP_DIV + MAX_STEP_DIV)
			@pos += @vel
		end

		@pos
	end
end

# Wanderer is a real-line component that wanders between 0 and max. It tries to dance around max/2.
# Useful for the meandering dot, and also for the color effects used in this demo.
#max = size[cardinal] like for Slider.
class Wanderer
	def initialize(size, cardinal)
		@size = size
		@cardinal = cardinal
		@pos = rand*@size[@cardinal]
		@vel = 0.0
	end
	
	def wander
		@pos += @vel
		@pos = @size[@cardinal] if(@pos>@size[@cardinal])
		@pos = 0 if(@pos<0)
		@vel += rand * WANDER_JITTER - @pos/@size[@cardinal]*WANDER_JITTER
		@pos
	end
end

# Just an RGB triplet of wanderers.
class ColorWanderer
	def initialize
		@r = Wanderer.new(RGB_MAX, 0);
		@g = Wanderer.new(RGB_MAX, 0);
		@b = Wanderer.new(RGB_MAX, 0);
	end
	
	def wander
		sprintf("#%02X%02X%02X", @r.wander, @g.wander, @b.wander)
	end
end