Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
Animations are a sequence of images, known as frames, that are displayed at a rate known as the frame rate. It’s often necessary to calculate an animation’s frame rate; for example, you may use an animation’s frame rate to implement time-based motion, as discussed in Section 5.6, “Time-Based Motion,” on p. 367, or you may simply want to ensure that your animation’s frame rate is sufficient for smooth animation.
The application shown in Figure 5.3 calculates the animation’s frame rate and displays it in the canvas.
Figure 5.3. Calculating frames per second
The code that calculates the frame rate is listed in Example 5.11, along with the application’s animation loop. Using a simple equation, the application calculates frames per second, given the elapsed time for the most recent animation frame.