diff options
Diffstat (limited to 'src/animation')
-rw-r--r-- | src/animation/Controller.js | 23 | ||||
-rw-r--r-- | src/animation/Morphable.js | 8 | ||||
-rw-r--r-- | src/animation/Queue.js | 2 | ||||
-rw-r--r-- | src/animation/Timeline.js | 33 |
4 files changed, 33 insertions, 33 deletions
diff --git a/src/animation/Controller.js b/src/animation/Controller.js index 35fa1ae..972679e 100644 --- a/src/animation/Controller.js +++ b/src/animation/Controller.js @@ -9,7 +9,7 @@ The base stepper class that will be function makeSetterGetter (k, f) { return function (v) { - if (v == null) return this[v] + if (v == null) return this[k] this[k] = v if (f) f.call(this) return this @@ -104,9 +104,9 @@ Easing Functions ***/ export class Ease extends Stepper { - constructor (fn) { + constructor (fn = timeline.ease) { super() - this.ease = easing[fn || timeline.ease] || fn + this.ease = easing[fn] || fn } step (from, to, pos) { @@ -155,10 +155,10 @@ function recalculate () { } export class Spring extends Controller { - constructor (duration, overshoot) { + constructor (duration = 500, overshoot = 0) { super() - this.duration(duration || 500) - .overshoot(overshoot || 0) + this.duration(duration) + .overshoot(overshoot) } step (current, target, dt, c) { @@ -195,13 +195,8 @@ extend(Spring, { }) export class PID extends Controller { - constructor (p, i, d, windup) { + constructor (p = 0.1, i = 0.01, d = 0, windup = 1000) { super() - - p = p == null ? 0.1 : p - i = i == null ? 0.01 : i - d = d == null ? 0 : d - windup = windup == null ? 1000 : windup this.p(p).i(i).d(d).windup(windup) } @@ -215,7 +210,7 @@ export class PID extends Controller { var p = target - current var i = (c.integral || 0) + p * dt var d = (p - (c.error || 0)) / dt - var windup = this.windup + var windup = this._windup // antiwindup if (windup !== false) { @@ -232,7 +227,7 @@ export class PID extends Controller { } extend(PID, { - windup: makeSetterGetter('windup'), + windup: makeSetterGetter('_windup'), p: makeSetterGetter('P'), i: makeSetterGetter('I'), d: makeSetterGetter('D') diff --git a/src/animation/Morphable.js b/src/animation/Morphable.js index 93debe7..0a28e8e 100644 --- a/src/animation/Morphable.js +++ b/src/animation/Morphable.js @@ -195,6 +195,10 @@ TransformBag.defaults = { originY: 0 } +const sortByKey = (a, b) => { + return (a[0] < b[0] ? -1 : (a[0] > b[0] ? 1 : 0)) +} + export class ObjectBag { constructor (...args) { this.init(...args) @@ -215,9 +219,7 @@ export class ObjectBag { entries.push([ i, objOrArr[i] ]) } - entries.sort((a, b) => { - return a[0] - b[0] - }) + entries.sort(sortByKey) this.values = entries.reduce((last, curr) => last.concat(curr), []) return this diff --git a/src/animation/Queue.js b/src/animation/Queue.js index 0d3cdcd..1858b99 100644 --- a/src/animation/Queue.js +++ b/src/animation/Queue.js @@ -6,7 +6,7 @@ export default class Queue { push (value) { // An item stores an id and the provided value - var item = value.next ? value : { value: value, next: null, prev: null } + var item = typeof value.next !== 'undefined' ? value : { value: value, next: null, prev: null } // Deal with the queue being empty or populated if (this._last) { diff --git a/src/animation/Timeline.js b/src/animation/Timeline.js index 2137727..d175ae6 100644 --- a/src/animation/Timeline.js +++ b/src/animation/Timeline.js @@ -68,11 +68,15 @@ export default class Timeline extends EventTarget { } else if (when === 'now') { absoluteStartTime = this._time } else if (when === 'relative') { - const runnerInfo = this._runners[runner.id] + const runnerInfo = this.getRunnerInfoById(runner.id) if (runnerInfo) { absoluteStartTime = runnerInfo.start + delay delay = 0 } + } else if (when === 'with-last') { + const lastRunnerInfo = this.getLastRunnerInfo() + const lastStartTime = lastRunnerInfo ? lastRunnerInfo.start : this._time + absoluteStartTime = lastStartTime } else { throw new Error('Invalid value for the "when" parameter') } @@ -110,26 +114,25 @@ export default class Timeline extends EventTarget { 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._runners[this._runnerIds.indexOf(this._lastRunnerId)] + var lastRunnerInfo = this.getLastRunnerInfo() var lastDuration = lastRunnerInfo ? lastRunnerInfo.runner.duration() : 0 - var lastStartTime = lastRunnerInfo ? lastRunnerInfo.start : 0 + var lastStartTime = lastRunnerInfo ? lastRunnerInfo.start : this._time return lastStartTime + lastDuration } getEndTimeOfTimeline () { - let lastEndTime = 0 - for (var i = 0; i < this._runners.length; i++) { - const runnerInfo = this._runners[i] - var duration = runnerInfo ? runnerInfo.runner.duration() : 0 - var startTime = runnerInfo ? runnerInfo.start : 0 - const endTime = startTime + duration - if (endTime > lastEndTime) { - lastEndTime = endTime - } - } - return lastEndTime + const endTimes = this._runners.map((i) => i.start + i.runner.duration()) + return Math.max(0, ...endTimes) } // Makes sure, that after pausing the time doesn't jump @@ -174,7 +177,7 @@ export default class Timeline extends EventTarget { if (yes == null) return this.speed(-currentSpeed) var positive = Math.abs(currentSpeed) - return this.speed(yes ? positive : -positive) + return this.speed(yes ? -positive : positive) } seek (dt) { |