loop
A composition node that repeats a child animation a specified number of times, or forever.
A loop node is a composition that repeats a single child animation node a set number of times, one after another, or forever with Infinity.
Example
import * as a from "@freestylejs/ani-core";
// 1. Define an animation that pulses an element's scale.
const pulse = a.sequence([
a.ani({ to: { scale: 1.2 }, duration: 0.5 }),
a.ani({ to: { scale: 1 }, duration: 0.5 }),
]);
// 2. Create a loop to repeat the pulse animation 3 times.
const loopedPulse = a.loop(pulse, 3);
// 3. Create and play the timeline.
const myTimeline = a.rafTimeline(loopedPulse);
myTimeline.play({ from: { scale: 1 } });Endless:
import * as a from "@freestylejs/ani-core";
const pulse = a.sequence([
a.ani({ to: { scale: 1.2 }, duration: 0.5 }),
a.ani({ to: { scale: 1 }, duration: 0.5 }),
]);
const heartbeat = a.rafTimeline(a.loop(pulse, Infinity));
heartbeat.play({ from: { scale: 1 } }); // pending until reset() / dispose()Usage & Concepts
Overview
loop is a composition node that takes a single child animation and repeats it a set number of times. The total duration of the loop node is the duration of its child multiplied by the loop count. This is a convenient alternative to manually creating a long sequence of identical animations.
An optional third argument is an options object, { timing, id }. Its timing applies a timing function to the looped child segments (only where the child does not define its own timing). A bare timing function in that position throws a TypeError.
import * as a from "@freestylejs/ani-core";
const pulse = a.ani({ to: { scale: 1.2 }, duration: 0.5 });
// Loop with an ease applied to every repetition.
const easedLoop = a.loop(pulse, 3, { timing: a.timing.ease() });Validation
loopCount must be an integer >= 0 or Infinity. Another number (-1, 1.5, NaN) throws a RangeError; a value that is not a number ('3', undefined) throws a TypeError.
The count does not cost anything
A loop is represented once, not count times. The first iteration starts from the value the animated properties have where the loop starts; every later iteration starts from the end state of the previous one, which is the same for all of them. So seeking anywhere inside loop(child, 1_000_000) costs the same as seeking in the first iteration, building it takes as long as loop(child, 2), and the values are those of the same tree written as a sequence of copies (dynamic springs restart in every iteration), to 1e-9 (absolute, scaled by the distance travelled), whatever the timing, bezier / ease* included.
A finite loop that shares a property with another segment running at the same time (parallel([loop(a, 3), b]) where both animate x) is written out as copies instead. The values are the same; the plan grows with the count.
Endless loops
loop(child, Infinity) repeats forever.
durationisInfinity, for the loop and for every node that contains it in time: asequencesums, aparalleltakes the maximum.loop(endless, n)is just the endless loop.- Unreachable: a
sequencechild placed after an endless child can never run. Constructing thatsequencethrows aRangeError(unreachable). Put the endless child last, or run it in aparallel. - Shared properties: an endless loop owns its properties from its start on.
parallel([loop(a, Infinity), b])where both animatexthrows aRangeErrorthat names the property. The same tree with a finite count works. - A timeline with an infinite duration never ends: there is no
ENDEDnotification,play()resolves only when the run is interrupted,reset()or disposed, andrepeatis ignored.seek(t)accepts any finitet >= 0;drive()needs a finite duration and throws aRangeError. - Reduced motion:
'jump'shows every finite part completed and each endless loop at the end of its first iteration, and holds that state (PAUSED).{ maxDuration }does not apply to an infinite run. - WAAPI: only a root
loop(child, Infinity)is supported; the child is compiled once and played withiterations: Infinity. Any other tree with an endless loop throws aRangeErrorfromwaapiTimeline(...).play(); use the RAF engine (rafTimeline) for it. rafGroupoffsets work as usual.
Overrides: one slot per node as written
The keyframes and durations options of play() hold one entry per ani / delay node as written in the tree. The child of a loop counts once, whatever the count (also 0 and Infinity), and its entry applies to every iteration:
import * as a from "@freestylejs/ani-core";
const looped = a.loop(a.ani({ to: { x: 100 }, duration: 1 }), 3);
const timeline = a.rafTimeline(looped);
timeline.play({ from: { x: 0 }, keyframes: [{ x: 50 }] }); // 50 in every iterationA durations entry that makes a segment run past the end of its iteration cuts it off at the next one; a finite loop is then written out, and an endless one throws a RangeError from play().
Best Practices
- Do use
loopfor repetitive animations like pulsing or bouncing, finite or endless. - Do use
loop(child, Infinity)instead ofrepeat: Infinitywhen only a part of the tree repeats. - Don't animate the properties of an endless loop elsewhere; split them between the children of a
parallel.
API Reference
| Name | Type | Description |
|---|---|---|
child | AnimationNode<G> | The node to repeat. |
loopCount | number | Integer >= 0, or Infinity. |
options.timing | TimingFunction | (Optional) Applied to child segments without their own timing. |
options.id | string | (Optional) Identifier of the node. |
Related Components
rafTimeline- Theplaymethod'srepeatoption repeats the whole timeline.sequence- Can be used to manually create loops, thoughloopis more concise.