In previous chapters you learned how to draw simple geometric shapes in a graphics window. Although you also learned how to manipulate these pictures somewhat using the mouse, they were otherwise static. In this chapter I will explain how to make these pictures dynamic; that is, how to program animations. In doing so you will learn several new ideas, such as how to use timers and how to use polymorphism and type classes in unique ways.
module Animation where
import Shape
import Draw
import Picture
import SOEGraphics hiding (Region)
import qualified SOEGraphics as G (Region)
import Win32Misc (timeGetTime)
import Word (word32ToInt)
What is an Animation?
An animation is a continuous, time-varying image. However, the animations that you see in the movies, on TV, or on your computer screen only seem to be moving continuously. In reality they are created by a sequence of static images - called frames that are displayed in such rapid succession that they appear to be continuous to the human eye. The rate at which this phenomenon occurs is somewhere between 20 and 30 times per second, depending on lighting conditions and other factors. I will use the rate of 30 times per second, or 30 Hertz, abbreviated 30 Hz. Televisions, computers, and motion pictures all use this fundamental physiological phenomenon to give the illusion of continuous movement.
So the question is, how do we program an animation? The only graphics 10 operation that we have discussed so far is drawInWindow, but note that drawing things using this operation is accumulative. That is, we never erase anything, nor even do so much as clear the screen. Even the user interaction given in Chapter 10 does not clear the screen; each successive rendition of the stack of regions is drawn right over the previous rendition. This works because each new picture completely hides the old one.
This will not work, however, if we are drawing a person walking across the screen, or a ball bouncing in a box.
Review the options below to login to check your access.
Log in with your Cambridge Aspire website account to check access.
If you believe you should have access to this content, please contact your institutional librarian or consult our FAQ page for further information about accessing our content.