Video Fade to Black and Fade to White #9
Chapter 2, Color Effects
|
47
HACK
In all honesty, applying a transition over a set number of frames doesn’t
make the most sense. Videos are time-based media, so we should rightly per-
form the color transformation over multiple steps using a time-based inter-
val (created with setInterval( )). Time-based intervals make it easier to use
the effect with a video and make the transition duration independent of the
timeline’s frame rate.
So the frame-based technique really is more useful for applying color trans-
formations to movie clips that don’t contain videos. We demonstrated the
time-based method earlier
[Hack #8], so let’s examine the frame-based tech-
nique more closely here.
The onEnterFrame( ) handler defined within trans( ) adds
diffTrans to the
current transform object each frame until the effect is completed:
for (var i in diffTrans) {
getTrans[i] += diffTrans[i];
}
transCol.setTransform(getTrans);
At the end of the transition, the transform may not have reached its target
due to rounding errors, so we need to explicitly set it just to make sure. After
we have done that, all we need to do to finish is clean up.
frames--;
if (frames == 0) {
// Explicitly set the target transform just in case the
// target numbers were not exactly divisible by the
// number of frames, then clean up.
transCol.setTransform(targetTrans);
delete this.onEnterFrame;
delete transCol;
}
Try running the script with other transformations, such as those earlier in
this hack and the preceding hack.
Final Thoughts
The cool thing about the preceding two hacks is that they can target any
movie clip, not just those containing video. For example, targeting
_root
allows you to apply transitions to the entire SWF file at runtime (although
this might be too processor-intensive to be practical in some situations). As
well as producing some cool effects, color transforms also have practical
uses: inverting the color of all content on the Stage for 100 milliseconds
might signify an incorrect response (Windows changes the screen color
briefly to alert users of errors if you enable accessibility options, because a
beep noise isn’t audible to hearing-impaired users).

Get Flash Hacks now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.