How can you possibly extend the API? Read on.
Callbacks
After you have the animation object all set to animate in an agnostic way, you might want to
create custom events that fire at certain times, enabling other code to tie into the animation to
perform related tasks. In any animation, there are usually only three interesting moments:
??? The start of the animation: Tying into the start of an animation can be helpful if something
you want to do is tied into the start of a particular animation. For example, you
might have a text label that needs to change based on the state of an animation.
??? Each step of the animation: You generally won??™t care about each step because there
would simply be too much noise. However, if building a game, it might be useful to
know whether an element is close to or intersecting another element on the page.
(Yes, you can build Pong by using JavaScript.)
??? The end of the animation: This is the moment you??™ll want to tie into to remove elements
on the page, add new elements, or perform an Ajax call.
Looking at the API, you need to add the new code to the animate() function because it
knows when the animation is at the beginning, when it??™s at the end, and when it makes each
step in the animation. Why not add the start and end callbacks in the start() and stop()
methods? Because the object can be stopped and restarted mid-animation, and adding it in
those places doesn??™t properly account for that capability.
Pages:
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208