You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The [javafx.animation](javafxdoc:AnimationPackageSummary) package in JavaFX offers a simple framework for creating animations and transitions in a JavaFX application.
16
+
The [javafx.animation](javafxdoc:AnimationPackageSummary) package offers a simple framework for creating animations and transitions in a JavaFX application.
17
17
It operates on the principle of [WritableValue\<T\>](javafxdoc:WritableValue) which are used across JavaFX.
18
18
It additionally provides a variety of built-in transitions for common effects, support for parallel and sequential transitions, and the ability to handle events upon animation completion.
19
19
I will go through all types of animations, starting with `Animation` and its subclasses `Transition` and `Timeline`, before representing a lower level animation with `AnimationTimer`.
20
20
While `Transition` provides a simpler and more user-friendly way to create animations, `Timeline` offers greater flexibility and is suitable for more complex animations.
21
-
In contrast, `AnimationTimer` is designed for frame-by-frame updates and does not take use of `WritableValue<T>`.
21
+
In contrast, `AnimationTimer` is designed for frame-by-frame updates and does not make use of `WritableValue<T>`.
22
+
23
+
## WritableValue
24
+
`WritableValue<T>` is an interface that wraps a value that can be read and set. It is commonly used for storing properties in JavaFX UI elements, like
25
+
`width` or `height` in the [Rectangle](javafxdoc:Rectangle) shape.
22
26
23
27
## Animation
24
28
25
29
The [Animation](javafxdoc:Animation) abstract class provides the core functionality for `Transition` and `Timeline` animations.
26
30
27
31
An `Animation` consists of multiple properties:
28
-
- The `targetFramerate` is the maximum framerate (frames per s) at which this `Animation` will run.
29
-
- The `currentTime` is the current point in time as a `Duration` of the `Animation`.
32
+
- The `targetFramerate` is the maximum framerate (frames per second) at which this `Animation` will run.
33
+
- The `currentTime` is the current point in time in the `Animation` as a [Duration](javafx:Duration).
30
34
- The `rate` defines the direction and speed at which the `Animation` is expected to be played. It supports both positive and negative numbers.
31
35
- The `cycleCount` defines the number of cycles of this `Animation`. It can't be changed while running and must be positive.
32
36
- The `cycleDuration` is the `Duration` of one cycle of this `Animation`. It is the time it takes to play from start to end of the `Animation`**at the default rate of 1.0**.
@@ -43,10 +47,8 @@ A quick look into the JavaDocs provides a great overview of its functionalities.
43
47
The [Transition](javafxdoc:Transition) abstract class serves as the foundational class for all transitions, presenting a common form of `Animation`.
44
48
JavaFX provides a variety of built-in transitions for common [Node](javafxdoc:Node) and [Shape](javafxdoc:Shape) properties.
45
49
46
-
**Note:** By default, all transitions, excluding `ParallelTransition` and `SequentialTransition`, utilize the `Interpolator#EASE_BOTH`.
47
-
48
50
### Fade Transition
49
-
The [FadeTransition](javafxdoc:FadeTransition) creates a fade effect over a `duration`.
51
+
The [FadeTransition](javafxdoc:FadeTransition) creates a fade effect.
50
52
This is done by updating the `opacity` property of the `Node` at regular intervals.
The [PathTransition](javafxdoc:PathTransition) creates a path animation that spans its `duration`.
99
+
The [PathTransition](javafxdoc:PathTransition) creates a move animation using a complex predefined path specified by a sequence of shapes.
82
100
The translation along the path is done by updating the `translateX` and `translateY` properties of the `Node`, and the `rotate` variable will get updated if `orientation` is set to `OrientationType.ORTHOGONAL_TO_TANGENT`, at regular interval.
The [PauseTransition](javafxdoc:PauseTransition) executes an `Animation.onFinished` at the end of its `duration`.
167
-
168
-
### Parallel Transition
169
-
The [ParallelTransition](javafxdoc:ParallelTransition) plays a list of animations in parallel.
170
-
171
191
### Sequential Transition
172
192
The [SequentialTransition](javafxdoc:SequentialTransition) plays a list of animations in sequential order.
173
193
It is not recommended to contain an `Animation`, which is not the last one, with `Duration.INDEFINITE` as this will block all later animations in the sequence.
174
194
175
195
## Timeline
176
-
A [Timeline](javafxdoc:Timeline) is used to define a free form `Animation`of any `WritableValue<T>`. It is helpful if none of the built-in transitions operate on the required properties.
196
+
A [Timeline](javafxdoc:Timeline) is used to define a free form `Animation`on any `WritableValue<T>`. It is helpful if none of the built-in transitions operate on the required properties.
177
197
178
-
It consists of a sequential series of `KeyFrame`.
198
+
It consists of a sequential series of `KeyFrame`s.
179
199
Each `KeyFrame` encapsulates a moment in time (**Cue Point**), and collectively specify how target properties evolve over the entire duration.
180
200
181
201
> **Warning:** A running Timeline is being referenced from the FX runtime. Infinite Timeline might result in a memory leak if not stopped properly. All the objects with animated properties would not be garbage collected.
@@ -232,10 +252,10 @@ Here is a visualization of the Interpolator using the example from [Timeline](#e
232
252
## AnimationTimer
233
253
234
254
The [AnimationTimer](javafxdoc:AnimationTimer) abstract class provides the lowest level option to create an animation.
235
-
The `handle(long now)` method gets called in each frame while it is active. The timestamp `long now` is the nanoseconds time of the current frame and will be the same for all `AnimationTimer` called during that frame.
236
-
Additionally, the `AnimationTimer` adds the `start()` and `stop()` to handle the lifetime of the animation.
255
+
The `handle(long now)` method gets called in each frame while it is active. The timestamp `now` is the time of the current frame in nanoseconds and will be the same for all `AnimationTimer`s called during that frame.
256
+
Additionally, the `AnimationTimer` adds `start()` and `stop()` methods to handle the lifetime of the animation.
237
257
238
-
**Note:** The handle method will be called on the **JavaFX Application Thread** and thus shouldn't do heavy computations.
258
+
**Note:** The `handle` method will be called in the **JavaFX Application Thread** and thus shouldn't do heavy computations.
0 commit comments