Gtk2Hs allows to set up "timers" that call functions at regular intervals. So I would do the following:
- Because most of Gtk2Hs happens in the IO monad, use an IORef or an MVar to store the state of the animation and change it anywhere.
- Add a call to
timeoutAdd
before mainGUI, like this:timeoutAdd (update can lines) 100
to set up a timer that will run every 100 milliseconds and call a new function "update" with the DrawingArea and the IORef/MVar with the animation state as parameters. - In the function "update" change the animation state and call
widgetQueueDraw can
so that the drawing area is re-exposed. That will call "drawCanvas" automatically because it is connected to the expose event. "update" has to return an IO Bool. Returning False will stop the timer. - To update the animation state, I would use a tuple. The first element would store the line to be drawn, and the second element would store the list of the other lines.
I find it meaningful to update the animation's state and the drawing in separate functions.