parallel
A composition node that plays all of its child animations at the same time.
A parallel node is a composition that plays all of its child animations at the same time.
Example
import * as a from "@freestylejs/ani-core";
// 1. Define two animations with different durations.
const fadeIn = a.ani({ to: { opacity: 1, scale: 1.2 }, duration: 0.5 });
const move = a.ani({ to: { x: 100, y: 50 }, duration: 0.8 });
// 2. Create a parallel block. Both animations will start at the same time.
const popIn = a.parallel([fadeIn, move]);
// 3. Create a timeline. The total duration will be that of the longest child (0.8s).
const myTimeline = a.rafTimeline(popIn);
// 4. Play the animation.
myTimeline.play({ from: { opacity: 0, scale: 1, x: 0, y: 0 } });Usage & Concepts
Overview
parallel is a "branch" composition node that groups animations to run concurrently. This is useful for creating effects where multiple properties change at once, like an element fading in while scaling up. The total duration of a parallel block is determined by the duration of its longest child animation.
Type Safety
Like sequence, parallel enforces that all children operate on a compatible data shape G. This allows you to safely compose animations for different properties.
Animating the same property twice
When two children animate the same property, the later child wins for the whole overlap, because they start together.
- Record-shaped direct
anichildren are resolved when the node is created: the overridden property is removed from the earlier child. - A child left with no properties is kept as a hold. It still contributes its duration (and its position in
keyframes/durationsoverrides), exactly likedelay. A trailingdelaycounts toward the block duration. - Array shapes and nested trees follow the timeline's general overlap rule: the most recently started segment produces the value, and each segment's
fromis the value at its start. Times within 1e-9 s of each other are one instant: sums taken in different orders (a loop'si * period, a sequence's durations added one by one) leave the same instant a few ulps apart, and the plan stores one value per instant. A zero-length segment applies at its start time, so a time sampled at that instant shows its value.
Best Practices
- Do use
parallelwhenever you want two or more animations to happen simultaneously. - Don't nest
parallelblocks if a single one will suffice.parallel([a, parallel([b, c])])is the same asparallel([a, b, c]).
API Reference
| Name | Type | Description |
|---|---|---|
children | AnimationNode[] | Nodes that all start together. |
options.timing | TimingFunction | (Optional) Inherited by child segments without their own timing. |
options.id | string | (Optional) Identifier of the node. |
The second argument is an options object: a.parallel(children, { timing, id }). A bare timing function or any extra argument throws a TypeError.