Java Reference
In-Depth Information
Interpolator
One of the constraints of the SimpleInterpolator is that target and start values
need to differ by a certain amount to actually see any movement. This is because
the curve() value is fractional from 0.0 to 1.0 and is multiplied by this differ-
ence to obtain the actual change in value. Obviously, if the start value is the same
as the target value, the fractional result would always be multiplied by the zero
difference, so there would be no movement at all.
What if you want a shaking effect, without the object actually ending at a new
location? In this case, the start values and end values are the same, but during the
time interval, the object shakes back and forth. This is exactly what happens with
the eQuake Alert plug-in to the Firefox browser. With eQuake Alert, when an
earthquake of a certain magnitude occurs, the entire Firefox browser shakes with
an amplitude based on the magnitude of the earthquake. You cannot use Simple-
Interpolator for this kind of animation. You must create a custom interpolator
that extends javafx.animation.Interpolator and implements the function
interpolate() .
public function interpolate(startValue:Object,
endValue:Object, fraction:Number):Object
The parameter startValue is the starting value for the target variable at the
beginning of the time slice represented by the KeyFrame , endValue is the ending
value, and fraction is a number from 0.0 to 1.0, which represents the fraction
of time within the time period. This function returns the interpolated value for
that instant. Actually, SimpleInterpolator , itself, extends Interpolator and
implements its own version of interpolate() that ends up calling the abstract
curve() function. Let's examine an example for shaking.
A perfect mathematical formula for doing a shake is a Bessel function. Figure
7.7 shows the graph of the Bessel function, where the shaking effect is greatest
at the beginning of the animation and calms down as the animation progresses.
Basically, we need to call the Bessel function when the animation runtime calls
the interpolate() method. However, it is not that straightforward, and there is a
little plumbing that needs to be created. The following code snippet in Listing 7.4
illustrates this. (The full listing is on the topic's Web site, http://jfxbook.com.)
Search WWH ::




Custom Search