summaryrefslogtreecommitdiffstats
path: root/src/animation/Timeline.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/animation/Timeline.js')
-rw-r--r--src/animation/Timeline.js179
1 files changed, 90 insertions, 89 deletions
diff --git a/src/animation/Timeline.js b/src/animation/Timeline.js
index d175ae6..a29d683 100644
--- a/src/animation/Timeline.js
+++ b/src/animation/Timeline.js
@@ -44,6 +44,62 @@ export default class Timeline extends EventTarget {
this._stepImmediate = this._stepFn.bind(this, true)
}
+ active () {
+ return !!this._nextFrame
+ }
+
+ finish () {
+ // Go to end and pause
+ this.time(this.getEndTimeOfTimeline() + 1)
+ return this.pause()
+ }
+
+ // Calculates the end of the timeline
+ getEndTime () {
+ var lastRunnerInfo = this.getLastRunnerInfo()
+ var lastDuration = lastRunnerInfo ? lastRunnerInfo.runner.duration() : 0
+ var lastStartTime = lastRunnerInfo ? lastRunnerInfo.start : this._time
+ return lastStartTime + lastDuration
+ }
+
+ getEndTimeOfTimeline () {
+ const endTimes = this._runners.map((i) => i.start + i.runner.duration())
+ return Math.max(0, ...endTimes)
+ }
+
+ getLastRunnerInfo () {
+ return this.getRunnerInfoById(this._lastRunnerId)
+ }
+
+ getRunnerInfoById (id) {
+ return this._runners[this._runnerIds.indexOf(id)] || null
+ }
+
+ pause () {
+ this._paused = true
+ return this._continue()
+ }
+
+ persist (dtOrForever) {
+ if (dtOrForever == null) return this._persist
+ this._persist = dtOrForever
+ return this
+ }
+
+ play () {
+ // Now make sure we are not paused and continue the animation
+ this._paused = false
+ return this.updateTime()._continue()
+ }
+
+ reverse (yes) {
+ var currentSpeed = this.speed()
+ if (yes == null) return this.speed(-currentSpeed)
+
+ var positive = Math.abs(currentSpeed)
+ return this.speed(yes ? -positive : positive)
+ }
+
// schedules a runner on the timeline
schedule (runner, delay, when) {
if (runner == null) {
@@ -102,56 +158,20 @@ export default class Timeline extends EventTarget {
return this
}
- // Remove the runner from this timeline
- unschedule (runner) {
- var index = this._runnerIds.indexOf(runner.id)
- if (index < 0) return this
-
- this._runners.splice(index, 1)
- this._runnerIds.splice(index, 1)
-
- runner.timeline(null)
- return this
- }
-
- getRunnerInfoById (id) {
- return this._runners[this._runnerIds.indexOf(id)] || null
- }
-
- getLastRunnerInfo () {
- return this.getRunnerInfoById(this._lastRunnerId)
- }
-
- // Calculates the end of the timeline
- getEndTime () {
- var lastRunnerInfo = this.getLastRunnerInfo()
- var lastDuration = lastRunnerInfo ? lastRunnerInfo.runner.duration() : 0
- var lastStartTime = lastRunnerInfo ? lastRunnerInfo.start : this._time
- return lastStartTime + lastDuration
- }
-
- getEndTimeOfTimeline () {
- const endTimes = this._runners.map((i) => i.start + i.runner.duration())
- return Math.max(0, ...endTimes)
+ seek (dt) {
+ return this.time(this._time + dt)
}
- // Makes sure, that after pausing the time doesn't jump
- updateTime () {
- if (!this.active()) {
- this._lastSourceTime = this._timeSource()
- }
+ source (fn) {
+ if (fn == null) return this._timeSource
+ this._timeSource = fn
return this
}
- play () {
- // Now make sure we are not paused and continue the animation
- this._paused = false
- return this.updateTime()._continue()
- }
-
- pause () {
- this._paused = true
- return this._continue()
+ speed (speed) {
+ if (speed == null) return this._speed
+ this._speed = speed
+ return this
}
stop () {
@@ -160,45 +180,41 @@ export default class Timeline extends EventTarget {
return this.pause()
}
- finish () {
- // Go to end and pause
- this.time(this.getEndTimeOfTimeline() + 1)
- return this.pause()
+ time (time) {
+ if (time == null) return this._time
+ this._time = time
+ return this._continue(true)
}
- speed (speed) {
- if (speed == null) return this._speed
- this._speed = speed
- return this
- }
+ // Remove the runner from this timeline
+ unschedule (runner) {
+ var index = this._runnerIds.indexOf(runner.id)
+ if (index < 0) return this
- reverse (yes) {
- var currentSpeed = this.speed()
- if (yes == null) return this.speed(-currentSpeed)
+ this._runners.splice(index, 1)
+ this._runnerIds.splice(index, 1)
- var positive = Math.abs(currentSpeed)
- return this.speed(yes ? -positive : positive)
+ runner.timeline(null)
+ return this
}
- seek (dt) {
- return this.time(this._time + dt)
+ // Makes sure, that after pausing the time doesn't jump
+ updateTime () {
+ if (!this.active()) {
+ this._lastSourceTime = this._timeSource()
+ }
+ return this
}
- time (time) {
- if (time == null) return this._time
- this._time = time
- return this._continue(true)
- }
+ // Checks if we are running and continues the animation
+ _continue (immediateStep = false) {
+ Animator.cancelFrame(this._nextFrame)
+ this._nextFrame = null
- persist (dtOrForever) {
- if (dtOrForever == null) return this._persist
- this._persist = dtOrForever
- return this
- }
+ if (immediateStep) return this._stepImmediate()
+ if (this._paused) return this
- source (fn) {
- if (fn == null) return this._timeSource
- this._timeSource = fn
+ this._nextFrame = Animator.frame(this._step)
return this
}
@@ -303,21 +319,6 @@ export default class Timeline extends EventTarget {
return this
}
- // Checks if we are running and continues the animation
- _continue (immediateStep = false) {
- Animator.cancelFrame(this._nextFrame)
- this._nextFrame = null
-
- if (immediateStep) return this._stepImmediate()
- if (this._paused) return this
-
- this._nextFrame = Animator.frame(this._step)
- return this
- }
-
- active () {
- return !!this._nextFrame
- }
}
registerMethods({