A loop returns to its starting state after a fixed duration. A convincing loop also preserves the direction and speed of motion at that boundary.
Time is relative to the code layer
In a code layer, motion.time is elapsed time in seconds from the start of its clip, and motion.duration is the clip length in seconds. Read these values inside the frame callback. A clip starting at composition time two seconds reads motion.time = 1 when the playhead reaches three seconds.
When a clip starts at zero and spans the full composition, layer time and composition time coincide. The tutorials use that arrangement. Moving a clip moves the animation with it; trimming the clip changes the duration used by duration-relative formulas.
Calculating a frame directly from its time makes the result reproducible when you scrub the timeline. Incrementing a position on every callback instead can make the result depend on which frames ran previously.
Turn time into a phase
For one turn over the whole clip, convert time into an angle:
const phase = (motion.time / Math.max(0.001, motion.duration)) * Math.PI * 2;
At the start, the phase is zero. One duration later, it is a full turn. Sine and cosine return to the same values after a full turn, making them useful for repeating motion.
Multiplying the phase by a whole number creates that many cycles per clip. A fractional multiplier may leave the animation at a different state when playback restarts.
Separate duration, speed, and frame rate
For a six-second code clip, the phase above completes one turn in six seconds. Three seconds into the clip it is halfway around. Extending the clip to twelve seconds makes that same turn take twice as long. Changing only the composition duration does not extend the clip automatically.
This is duration-relative motion: useful when the animation must complete a specific number of cycles in the available time. A formula such as motion.time * Math.PI * 2 instead completes one cycle per second, regardless of duration. That can be useful for a fixed rate, but the clip must end at a compatible point in the cycle to loop.
Frame rate determines how often that continuous motion is sampled into video frames. Moving from 30 to 60 FPS adds samples; it should not double the animation's speed. If speed depends on the number of callbacks rather than elapsed time, scrubbing and export may behave differently.
Coordinate several movements
Use a shared base phase and integer cycle counts for properties that must repeat together. For example, an orbit can turn once while its radius expands and contracts twice:
const phase = (motion.time / Math.max(0.001, motion.duration)) * Math.PI * 2;
const angle = phase;
const radius = 0.28 + Math.sin(phase * 2) * 0.03;
These calculations belong inside the frame callback. The radius remains between 0.25 and 0.31, making the motion's bounds predictable.
A fixed phase offset changes when a movement reaches its peak without changing its period. Math.sin(phase - Math.PI / 2) repeats just as reliably as Math.sin(phase). Apply a different constant offset to each element to create a traveling wave, as in Stagger a grid of pulsing dots.
Do not confuse a phase offset with an extra fraction of a cycle per clip. The first is constant; the second changes how far the animation travels before restarting.
Position is only part of the loop
Check every changing property: size, rotation, opacity, color, and effects. A looping position cannot hide a sudden change in another property. Layer clips must also remain visible for the intended interval.
The looping animation tutorial combines rotation with two cycles of breathing motion. Both return to their initial state together.
A straight-line movement from left to right can end at the same position after a reset yet still jump visibly across the frame. A back-and-forth path can meet at the same position but reverse direction abruptly. Smooth periodic functions help match both position and the rate of change; inspect the seam rather than assuming matching endpoints are sufficient.
Preview and export boundaries
A rendered video contains discrete frames. The final frame normally occurs one frame interval before the next cycle, rather than duplicating the first frame. The transition from that final frame back to the first should look like an ordinary frame step.
Whether the downloaded file repeats automatically depends on the player. Enable repeat when evaluating a loop. See Export an animation for the rendering workflow.
Diagnose a seam methodically
- Isolate the main movement and temporarily remove secondary changes.
- Inspect the start, quarter, half, and three-quarter points of the code clip.
- Check that every time multiplier completes a compatible number of cycles.
- Restore secondary properties one at a time, including effects and visibility.
- Inspect repeating playback of the exported file at the intended frame rate.
This separates a mathematical discontinuity from a clip ending early or a player pausing between repeats. For problems beyond the loop boundary, use Troubleshoot a composition.