Java Reference
In-Depth Information
id = window.requestAnimationFrame(rotate);
This is similar to the earlier code, but this time we place the rotation code inside a
function called
rotate
. The last line of this function uses the
win-
dow.requestAnimationFrame()
method and takes the
rotate()
function as an
argument. This will then call the
rotate()
function recursively. The frame rate cannot
be set using
requestAnimationFrame()
; it's usually 60 frames per second, although
it's optimized for the device being used.
To start the animation, we need to call the
window.requestAnimationFrame()
method, giving the
rotate()
function as an argument. This will return a unique ID
that
can
be
employed
to
stop
the
animation
using
the
win-
dow.cancelAnimationFrame()
method:
window.cancelAnimationFrame(id);
Refresh the animation.htm page and you should notice that the animation is much faster
and smoother, as shown in
Figure 9.1
.
