Skip to content

Commit

Permalink
Add Timeline.isFinished method (#1200)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsherif committed Aug 16, 2019
1 parent 53a60f1 commit ff374e8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions docs/api-reference/addons/animation/timeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ Add a new channel to the timeline. Returns a handle to the channel that can be u

Remove a channel from the timeline. `handle` should be a value that was returned by `addChannel`.

### isFinished(handle : Number) : Boolean

Returns whether the channel's time has completely elapsed.

### getTime([handle : Number]) : Number

Return the current time of the channel indicated by `handle`. If no handle is provided, return timeline time.
Expand Down
9 changes: 9 additions & 0 deletions modules/addons/src/animation/timeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ export class Timeline {
}
}

isFinished(handle) {
const channel = this.channels.get(handle);
if (channel === undefined) {
return false;
}

return this.time >= channel.delay + channel.duration * channel.repeat;
}

getTime(handle) {
if (handle === undefined) {
return this.time;
Expand Down
6 changes: 6 additions & 0 deletions modules/addons/test/animation/timeline.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ test('Animation#Timeline', t => {

timeline.setTime(4);
t.equals(timeline.getTime(channel1), 2 * CHANNEL1_RATE, 'Channel 1 set');
t.ok(!timeline.isFinished(channel1), 'Channel 1 is not finished');
t.equals(timeline.getTime(channel2), 1 * CHANNEL2_RATE, 'Channel 2 set');
t.ok(!timeline.isFinished(channel2), 'Channel 2 is not finished');
t.equals(animationBase.time, timeline.getTime(), 'Animation updated for base timeline');
t.equals(
animationChannel1.time,
Expand All @@ -74,7 +76,9 @@ test('Animation#Timeline', t => {

timeline.setTime(7);
t.equals(timeline.getTime(channel1), 4 * CHANNEL1_RATE, 'Channel 1 does not loop');
t.ok(timeline.isFinished(channel1), 'Channel 1 is finished');
t.equals(timeline.getTime(channel2), 1 * CHANNEL2_RATE, 'Channel 2 looped once');
t.ok(!timeline.isFinished(channel2), 'Channel 2 is not finished');
t.equals(animationBase.time, timeline.getTime(), 'Animation updated for base timeline');
t.equals(
animationChannel1.time,
Expand All @@ -84,7 +88,9 @@ test('Animation#Timeline', t => {

timeline.setTime(10);
t.equals(timeline.getTime(channel1), 4 * CHANNEL1_RATE, 'Channel 1 does not loop');
t.ok(timeline.isFinished(channel1), 'Channel 1 is finished');
t.equals(timeline.getTime(channel2), 3 * CHANNEL2_RATE, 'Channel 2 only looped once');
t.ok(timeline.isFinished(channel2), 'Channel 2 is finished');
t.equals(animationBase.time, timeline.getTime(), 'Animation updated for base timeline');
t.equals(
animationChannel1.time,
Expand Down

0 comments on commit ff374e8

Please sign in to comment.