Timeline
An animation timeline system that manages keyframe-based animations.
Timeline provides:
- Frame-based positioning and playback
- Multiple animation tracks for different properties
- Label system for marking important positions
- Looping and one-shot playback modes
- Auto-update integration with the game loop
- Complete callbacks for animation sequences
Timelines are commonly used in:
- Fragment animations
- Complex UI transitions
- Cutscenes and scripted sequences
- Any multi-property animations that need synchronization
Example usage:
var timeline = new Timeline();
timeline.fps = 30;
timeline.size = 120; // 4 seconds at 30 fps
// Add animation tracks
var track = new TimelineFloatTrack();
track.add(new TimelineFloatKeyframe(0, 100));
track.add(new TimelineFloatKeyframe(60, 200));
timeline.add(track);
// Add labels for important positions
timeline.setLabel(0, "start");
timeline.setLabel(60, "middle");
timeline.setLabel(120, "end");
// Play animation from a label
timeline.animate("start", () -> trace("Animation complete!"));
Instance Members
size: Int
The total length of the timeline in frames.
- Default is 0 (timeline won't play)
- When
autoFitSize
is true (default), automatically adjusts to match the longest track - Set to -1 for an infinite timeline that never finishes
The actual duration in seconds = size / fps
autoFitSize: Bool
Whether the timeline should automatically adjust its size to match the longest track. When true (default), you don't need to manually set the timeline size.
loop: Bool
Whether the timeline should loop back to the beginning when it reaches the end. Ignored if size is -1 (infinite timeline). Default is true.
autoUpdate: Bool
Whether the timeline automatically updates each frame. When true (default), the timeline advances based on frame delta time. Set to false to manually control timeline playback with seek() or update().
fps: Int
Timeline playback speed in frames per second. This defines how many timeline frames pass per second of real time.
Note: Timeline values are interpolated between frames, so using 30 fps still provides smooth animation even on 60+ fps displays.
Default is 30 fps.
position: Float
Current playback position in frames.
- Starts at 0
- Can be fractional for smooth interpolation between frames
- Wraps back to 0 when looping is enabled and size is reached
- Use seek() to jump to specific positions
tracks: ReadOnlyArray<TimelineTrack<TimelineKeyframe>>
Array of animation tracks managed by this timeline. Each track animates a specific property of an entity. Tracks are updated automatically as the timeline plays.
paused: Bool
Whether the timeline playback is paused. Setting to true stops all animation while preserving the current position.
labels: ReadOnlyArray<String>
Array of label names in the timeline. Labels mark important positions for seeking and animation control. Sorted by position (frame index).
startPosition: Int
Optional starting position for timeline playback.
- If >= 0, timeline starts from this frame index
- When looping, timeline resets to this position instead of 0
- Default is -1 (use position 0)
endPosition: Int
Optional ending position for timeline playback.
- If >= 0, timeline stops at this frame index
- When looping, timeline resets to startPosition (or 0)
- Default is -1 (use timeline size)
entity: Entity
initializerName: String
Update the timeline position based on elapsed time. Called automatically each frame when autoUpdate is true.
Name | Type | Description |
---|---|---|
delta |
Float | Time elapsed since last frame in seconds |
Jump to a specific position in the timeline. Handles looping and clamping based on timeline settings. Updates all tracks to reflect the new position.
Name | Type | Description |
---|---|---|
targetPosition |
Float | The frame index to seek to |
Play an animation sequence from a labeled position. The animation plays until reaching the next label or timeline end.
If the animation is interrupted (by seeking or playing another animation), the complete callback won't be called.
Name | Type | Description |
---|---|---|
name |
String | The label name to start from |
complete |
Function | Callback fired when the animation completes |
Jump to the position of a named label.
Name | Type | Description |
---|---|---|
name |
String | The label name to seek to |
Returns | Description |
---|---|
Int | The frame index of the label, or -1 if not found |
resetStartAndEndPositions(): Void
Reset the timeline's start and end positions to their defaults. After calling this, the timeline will play from 0 to its full size.
Set up the timeline to loop within a labeled section.
The timeline will:
- Jump to the label position
- Set startPosition to the label's frame
- Set endPosition to the next label (or timeline end)
- Loop within this range
Name | Type | Description |
---|---|---|
name |
String | The label name marking the start of the loop section |
Returns | Description |
---|---|
Int | The frame index of the label, or -1 if not found |
Apply all timeline tracks at the current position. Useful for ensuring all animated properties are up to date.
Name | Type | Default | Description |
---|---|---|---|
forceChange |
Bool | false |
If true, forces track updates even if values haven't changed |
add(track: TimelineTrack<TimelineKeyframe>): Void
Add an animation track to this timeline. If the track was previously added to another timeline, it's removed first. If autoFitSize is true, the timeline size adjusts to accommodate the track.
Name | Type | Description |
---|---|---|
track |
TimelineTrack<TimelineKeyframe> | The animation track to add |
get(trackId: String): TimelineTrack<TimelineKeyframe>
Get a track by its ID.
Name | Type | Description |
---|---|---|
trackId |
String | The track identifier |
Returns | Description |
---|---|
TimelineTrack<TimelineKeyframe> | The track with the given ID, or null if not found |
remove(track: TimelineTrack<TimelineKeyframe>): Void
Remove an animation track from this timeline. If autoFitSize is true, the timeline size adjusts after removal.
Name | Type | Description |
---|---|---|
track |
TimelineTrack<TimelineKeyframe> | The animation track to remove |
fitSize(): Void
Adjust the timeline size to match the longest track. Called automatically when autoFitSize is true and tracks are added/removed.
Find the last label before a given position.
Name | Type | Description |
---|---|---|
index |
Int | The frame index to search before |
Returns | Description |
---|---|
Int | The frame index of the previous label, or -1 if none exists |
Get the label name at a specific frame index.
Name | Type | Description |
---|---|---|
index |
Int | The frame index to check |
Returns | Description |
---|---|
String | The label name at that position, or null if no label exists |
Get the frame index of a named label.
Name | Type | Description |
---|---|---|
name |
String | The label name to find |
Returns | Description |
---|---|
Int | The frame index of the label, or -1 if not found |
Create or update a label at a specific position. If a label with the same name exists, it's moved to the new position. Labels are automatically sorted by position.
Name | Type | Description |
---|---|---|
index |
Int | The frame index for the label |
name |
String | The label name |
Remove any label at the specified frame index.
Name | Type | Description |
---|---|---|
index |
Int | The frame index where the label should be removed |
Returns | Description |
---|---|
Bool | True if a label was removed, false if no label existed |
Remove a label by name.
Name | Type | Description |
---|---|---|
name |
String | The label name to remove |
Returns | Description |
---|---|
Bool | True if the label was removed, false if it didn't exist |
new(): Void
Create a new timeline instance. The timeline starts paused at position 0 with no tracks.
Private Members
Used in pair with labels
to manage timeline labels
completeHandlers: Array<Function>
Internal array of complete handlers
Internal array of complete handler label indexes
Event triggered when the timeline position reaches a label. Useful for triggering actions at specific points in the animation.
Name | Type | Description |
---|---|---|
index |
Int | The frame index (position) of the label |
name |
String | The name of the label that was reached |
Event triggered when the timeline leaves a labeled section. This happens when reaching the next label or the end of the timeline. Useful for cleaning up effects or transitioning to new states.
Name | Type | Description |
---|---|---|
index |
Int | The frame index (position) of the label being left |
name |
String | The name of the label being left |
bindAsComponent(): Void
bindOrUnbindUpdateIfNeeded(): Void
Internal function to bind or update to app update event depending on current settings
Name | Type | Default |
---|---|---|
targetPosition |
Float | |
forceSeek |
Bool | false |
forceChange |
Bool | false |
clearCompleteHandlers(): Void
Name | Type |
---|---|
index |
Int |
name |
String |
sortLabels(): Void
Name | Type |
---|---|
a |
Int |
b |
Int |
Returns |
---|
Int |
Name | Type |
---|---|
nameA |
String |
nameB |
String |
Returns |
---|
Int |
Name | Type |
---|---|
entity |
Entity |
getEntity(): Entity
Returns |
---|
Entity |
Metadata
Name | Parameters |
---|---|
:build |
ceramic.macros.ComponentMacro.build() |
:autoBuild |
ceramic.macros.ComponentMacro.build() |
:build |
ceramic.macros.EntityMacro.buildForCompletion() |
:autoBuild |
ceramic.macros.EntityMacro.buildForCompletion() |
:build |
tracker.macros.EventsMacro.build() |
:autoBuild |
tracker.macros.EventsMacro.build() |