iwmlib/lib/3rdparty/greensock/src/esm/TimelineMax.js

523 lines
25 KiB
JavaScript

/*!
* VERSION: 2.1.0
* DATE: 2019-02-15
* UPDATES AND DOCS AT: http://greensock.com
*
* @license Copyright (c) 2008-2019, GreenSock. All rights reserved.
* This work is subject to the terms at http://greensock.com/standard-license or for
* Club GreenSock members, the software agreement that was issued with your membership.
*
* @author: Jack Doyle, jack@greensock.com
*/
/* eslint-disable */
import TweenLite, { _gsScope, globals, Ease, Animation } from "./TweenLite.js";
import TimelineLite from "./TimelineLite.js";
_gsScope._gsDefine("TimelineMax", ["TimelineLite","TweenLite","easing.Ease"], function() {
var TimelineMax = function(vars) {
TimelineLite.call(this, vars);
this._repeat = this.vars.repeat || 0;
this._repeatDelay = this.vars.repeatDelay || 0;
this._cycle = 0;
this._yoyo = !!this.vars.yoyo;
this._dirty = true;
},
_tinyNum = 0.00000001,
TweenLiteInternals = TweenLite._internals,
_lazyTweens = TweenLiteInternals.lazyTweens,
_lazyRender = TweenLiteInternals.lazyRender,
_globals = _gsScope._gsDefine.globals,
_easeNone = new Ease(null, null, 1, 0),
p = TimelineMax.prototype = new TimelineLite();
p.constructor = TimelineMax;
p.kill()._gc = false;
TimelineMax.version = "2.1.0";
p.invalidate = function() {
this._yoyo = !!this.vars.yoyo;
this._repeat = this.vars.repeat || 0;
this._repeatDelay = this.vars.repeatDelay || 0;
this._uncache(true);
return TimelineLite.prototype.invalidate.call(this);
};
p.addCallback = function(callback, position, params, scope) {
return this.add( TweenLite.delayedCall(0, callback, params, scope), position);
};
p.removeCallback = function(callback, position) {
if (callback) {
if (position == null) {
this._kill(null, callback);
} else {
var a = this.getTweensOf(callback, false),
i = a.length,
time = this._parseTimeOrLabel(position);
while (--i > -1) {
if (a[i]._startTime === time) {
a[i]._enabled(false, false);
}
}
}
}
return this;
};
p.removePause = function(position) {
return this.removeCallback(TimelineLite._internals.pauseCallback, position);
};
p.tweenTo = function(position, vars) {
vars = vars || {};
var copy = {ease:_easeNone, useFrames:this.usesFrames(), immediateRender:false, lazy:false},
Engine = (vars.repeat && _globals.TweenMax) || TweenLite,
duration, p, t;
for (p in vars) {
copy[p] = vars[p];
}
copy.time = this._parseTimeOrLabel(position);
duration = (Math.abs(Number(copy.time) - this._time) / this._timeScale) || 0.001;
t = new Engine(this, duration, copy);
copy.onStart = function() {
t.target.paused(true);
if (t.vars.time !== t.target.time() && duration === t.duration() && !t.isFromTo) { //don't make the duration zero - if it's supposed to be zero, don't worry because it's already initting the tween and will complete immediately, effectively making the duration zero anyway. If we make duration zero, the tween won't run at all.
t.duration( Math.abs( t.vars.time - t.target.time()) / t.target._timeScale ).render(t.time(), true, true); //render() right away to ensure that things look right, especially in the case of .tweenTo(0).
}
if (vars.onStart) { //in case the user had an onStart in the vars - we don't want to overwrite it.
vars.onStart.apply(vars.onStartScope || vars.callbackScope || t, vars.onStartParams || []); //don't use t._callback("onStart") or it'll point to the copy.onStart and we'll get a recursion error.
}
};
return t;
};
p.tweenFromTo = function(fromPosition, toPosition, vars) {
vars = vars || {};
fromPosition = this._parseTimeOrLabel(fromPosition);
vars.startAt = {onComplete:this.seek, onCompleteParams:[fromPosition], callbackScope:this};
vars.immediateRender = (vars.immediateRender !== false);
var t = this.tweenTo(toPosition, vars);
t.isFromTo = 1; //to ensure we don't mess with the duration in the onStart (we've got the start and end values here, so lock it in)
return t.duration((Math.abs( t.vars.time - fromPosition) / this._timeScale) || 0.001);
};
p.render = function(time, suppressEvents, force) {
if (this._gc) {
this._enabled(true, false);
}
var self = this,
prevTime = self._time,
totalDur = (!self._dirty) ? self._totalDuration : self.totalDuration(),
dur = self._duration,
prevTotalTime = self._totalTime,
prevStart = self._startTime,
prevTimeScale = self._timeScale,
prevRawPrevTime = self._rawPrevTime,
prevPaused = self._paused,
prevCycle = self._cycle,
tween, isComplete, next, callback, internalForce, cycleDuration, pauseTween, curTime, pauseTime;
if (prevTime !== self._time) { //if totalDuration() finds a child with a negative startTime and smoothChildTiming is true, things get shifted around internally so we need to adjust the time accordingly. For example, if a tween starts at -30 we must shift EVERYTHING forward 30 seconds and move this timeline's startTime backward by 30 seconds so that things align with the playhead (no jump).
time += self._time - prevTime;
}
if (time >= totalDur - _tinyNum && time >= 0) { //to work around occasional floating point math artifacts.
if (!self._locked) {
self._totalTime = totalDur;
self._cycle = self._repeat;
}
if (!self._reversed) if (!self._hasPausedChild()) {
isComplete = true;
callback = "onComplete";
internalForce = !!self._timeline.autoRemoveChildren; //otherwise, if the animation is unpaused/activated after it's already finished, it doesn't get removed from the parent timeline.
if (self._duration === 0) if ((time <= 0 && time >= -_tinyNum) || prevRawPrevTime < 0 || prevRawPrevTime === _tinyNum) if (prevRawPrevTime !== time && self._first) {
internalForce = true;
if (prevRawPrevTime > _tinyNum) {
callback = "onReverseComplete";
}
}
}
self._rawPrevTime = (self._duration || !suppressEvents || time || self._rawPrevTime === time) ? time : _tinyNum; //when the playhead arrives at EXACTLY time 0 (right on top) of a zero-duration timeline or tween, we need to discern if events are suppressed so that when the playhead moves again (next time), it'll trigger the callback. If events are NOT suppressed, obviously the callback would be triggered in this render. Basically, the callback should fire either when the playhead ARRIVES or LEAVES this exact spot, not both. Imagine doing a timeline.seek(0) and there's a callback that sits at 0. Since events are suppressed on that seek() by default, nothing will fire, but when the playhead moves off of that position, the callback should fire. This behavior is what people intuitively expect. We set the _rawPrevTime to be a precise tiny number to indicate this scenario rather than using another property/variable which would increase memory usage. This technique is less readable, but more efficient.
if (self._yoyo && (self._cycle & 1)) {
self._time = time = 0;
} else {
self._time = dur;
time = dur + 0.0001; //to avoid occasional floating point rounding errors - sometimes child tweens/timelines were not being fully completed (their progress might be 0.999999999999998 instead of 1 because when _time - tween._startTime is performed, floating point errors would return a value that was SLIGHTLY off). Try (999999999999.7 - 999999999999) * 1 = 0.699951171875 instead of 0.7. We cannot do less then 0.0001 because the same issue can occur when the duration is extremely large like 999999999999 in which case adding 0.00000001, for example, causes it to act like nothing was added.
}
} else if (time < _tinyNum) { //to work around occasional floating point math artifacts, round super small values to 0.
if (!self._locked) {
self._totalTime = self._cycle = 0;
}
self._time = 0;
if (time > -_tinyNum) {
time = 0;
}
if (prevTime !== 0 || (dur === 0 && prevRawPrevTime !== _tinyNum && (prevRawPrevTime > 0 || (time < 0 && prevRawPrevTime >= 0)) && !self._locked)) { //edge case for checking time < 0 && prevRawPrevTime >= 0: a zero-duration fromTo() tween inside a zero-duration timeline (yeah, very rare)
callback = "onReverseComplete";
isComplete = self._reversed;
}
if (time < 0) {
self._active = false;
if (self._timeline.autoRemoveChildren && self._reversed) {
internalForce = isComplete = true;
callback = "onReverseComplete";
} else if (prevRawPrevTime >= 0 && self._first) { //when going back beyond the start, force a render so that zero-duration tweens that sit at the very beginning render their start values properly. Otherwise, if the parent timeline's playhead lands exactly at this timeline's startTime, and then moves backwards, the zero-duration tweens at the beginning would still be at their end state.
internalForce = true;
}
self._rawPrevTime = time;
} else {
self._rawPrevTime = (dur || !suppressEvents || time || self._rawPrevTime === time) ? time : _tinyNum; //when the playhead arrives at EXACTLY time 0 (right on top) of a zero-duration timeline or tween, we need to discern if events are suppressed so that when the playhead moves again (next time), it'll trigger the callback. If events are NOT suppressed, obviously the callback would be triggered in this render. Basically, the callback should fire either when the playhead ARRIVES or LEAVES this exact spot, not both. Imagine doing a timeline.seek(0) and there's a callback that sits at 0. Since events are suppressed on that seek() by default, nothing will fire, but when the playhead moves off of that position, the callback should fire. This behavior is what people intuitively expect. We set the _rawPrevTime to be a precise tiny number to indicate this scenario rather than using another property/variable which would increase memory usage. This technique is less readable, but more efficient.
if (time === 0 && isComplete) { //if there's a zero-duration tween at the very beginning of a timeline and the playhead lands EXACTLY at time 0, that tween will correctly render its end values, but we need to keep the timeline alive for one more render so that the beginning values render properly as the parent's playhead keeps moving beyond the begining. Imagine obj.x starts at 0 and then we do tl.set(obj, {x:100}).to(obj, 1, {x:200}) and then later we tl.reverse()...the goal is to have obj.x revert to 0. If the playhead happens to land on exactly 0, without this chunk of code, it'd complete the timeline and remove it from the rendering queue (not good).
tween = self._first;
while (tween && tween._startTime === 0) {
if (!tween._duration) {
isComplete = false;
}
tween = tween._next;
}
}
time = 0; //to avoid occasional floating point rounding errors (could cause problems especially with zero-duration tweens at the very beginning of the timeline)
if (!self._initted) {
internalForce = true;
}
}
} else {
if (dur === 0 && prevRawPrevTime < 0) { //without this, zero-duration repeating timelines (like with a simple callback nested at the very beginning and a repeatDelay) wouldn't render the first time through.
internalForce = true;
}
self._time = self._rawPrevTime = time;
if (!self._locked) {
self._totalTime = time;
if (self._repeat !== 0) {
cycleDuration = dur + self._repeatDelay;
self._cycle = (self._totalTime / cycleDuration) >> 0; //originally _totalTime % cycleDuration but floating point errors caused problems, so I normalized it. (4 % 0.8 should be 0 but it gets reported as 0.79999999!)
if (self._cycle) if (self._cycle === self._totalTime / cycleDuration && prevTotalTime <= time) {
self._cycle--; //otherwise when rendered exactly at the end time, it will act as though it is repeating (at the beginning)
}
self._time = self._totalTime - (self._cycle * cycleDuration);
if (self._yoyo) if (self._cycle & 1) {
self._time = dur - self._time;
}
if (self._time > dur) {
self._time = dur;
time = dur + 0.0001; //to avoid occasional floating point rounding error
} else if (self._time < 0) {
self._time = time = 0;
} else {
time = self._time;
}
}
}
if (self._hasPause && !self._forcingPlayhead && !suppressEvents) {
time = self._time;
if (time >= prevTime || (self._repeat && prevCycle !== self._cycle)) {
tween = self._first;
while (tween && tween._startTime <= time && !pauseTween) {
if (!tween._duration) if (tween.data === "isPause" && !tween.ratio && !(tween._startTime === 0 && self._rawPrevTime === 0)) {
pauseTween = tween;
}
tween = tween._next;
}
} else {
tween = self._last;
while (tween && tween._startTime >= time && !pauseTween) {
if (!tween._duration) if (tween.data === "isPause" && tween._rawPrevTime > 0) {
pauseTween = tween;
}
tween = tween._prev;
}
}
if (pauseTween) {
pauseTime = self._startTime + (pauseTween._startTime / self._timeScale);
if (pauseTween._startTime < dur) {
self._time = self._rawPrevTime = time = pauseTween._startTime;
self._totalTime = time + (self._cycle * (self._totalDuration + self._repeatDelay));
}
}
}
}
if (self._cycle !== prevCycle) if (!self._locked) {
/*
make sure children at the end/beginning of the timeline are rendered properly. If, for example,
a 3-second long timeline rendered at 2.9 seconds previously, and now renders at 3.2 seconds (which
would get translated to 2.8 seconds if the timeline yoyos or 0.2 seconds if it just repeats), there
could be a callback or a short tween that's at 2.95 or 3 seconds in which wouldn't render. So
we need to push the timeline to the end (and/or beginning depending on its yoyo value). Also we must
ensure that zero-duration tweens at the very beginning or end of the TimelineMax work.
*/
var backwards = (self._yoyo && (prevCycle & 1) !== 0),
wrap = (backwards === (self._yoyo && (self._cycle & 1) !== 0)),
recTotalTime = self._totalTime,
recCycle = self._cycle,
recRawPrevTime = self._rawPrevTime,
recTime = self._time;
self._totalTime = prevCycle * dur;
if (self._cycle < prevCycle) {
backwards = !backwards;
} else {
self._totalTime += dur;
}
self._time = prevTime; //temporarily revert _time so that render() renders the children in the correct order. Without this, tweens won't rewind correctly. We could arhictect things in a "cleaner" way by splitting out the rendering queue into a separate method but for performance reasons, we kept it all inside this method.
self._rawPrevTime = (dur === 0) ? prevRawPrevTime - 0.0001 : prevRawPrevTime;
self._cycle = prevCycle;
self._locked = true; //prevents changes to totalTime and skips repeat/yoyo behavior when we recursively call render()
prevTime = (backwards) ? 0 : dur;
self.render(prevTime, suppressEvents, (dur === 0));
if (!suppressEvents) if (!self._gc) {
if (self.vars.onRepeat) {
self._cycle = recCycle; //in case the onRepeat alters the playhead or invalidates(), we shouldn't stay locked or use the previous cycle.
self._locked = false;
self._callback("onRepeat");
}
}
if (prevTime !== self._time) { //in case there's a callback like onComplete in a nested tween/timeline that changes the playhead position, like via seek(), we should just abort.
return;
}
if (wrap) {
self._cycle = prevCycle; //if there's an onRepeat, we reverted this above, so make sure it's set properly again. We also unlocked in that scenario, so reset that too.
self._locked = true;
prevTime = (backwards) ? dur + 0.0001 : -0.0001;
self.render(prevTime, true, false);
}
self._locked = false;
if (self._paused && !prevPaused) { //if the render() triggered callback that paused this timeline, we should abort (very rare, but possible)
return;
}
self._time = recTime;
self._totalTime = recTotalTime;
self._cycle = recCycle;
self._rawPrevTime = recRawPrevTime;
}
if ((self._time === prevTime || !self._first) && !force && !internalForce && !pauseTween) {
if (prevTotalTime !== self._totalTime) if (self._onUpdate) if (!suppressEvents) { //so that onUpdate fires even during the repeatDelay - as long as the totalTime changed, we should trigger onUpdate.
self._callback("onUpdate");
}
return;
} else if (!self._initted) {
self._initted = true;
}
if (!self._active) if (!self._paused && self._totalTime !== prevTotalTime && time > 0) {
self._active = true; //so that if the user renders the timeline (as opposed to the parent timeline rendering it), it is forced to re-render and align it with the proper time/frame on the next rendering cycle. Maybe the timeline already finished but the user manually re-renders it as halfway done, for example.
}
if (prevTotalTime === 0) if (self.vars.onStart) if (self._totalTime !== 0 || !self._totalDuration) if (!suppressEvents) {
self._callback("onStart");
}
curTime = self._time;
if (curTime >= prevTime) {
tween = self._first;
while (tween) {
next = tween._next; //record it here because the value could change after rendering...
if (curTime !== self._time || (self._paused && !prevPaused)) { //in case a tween pauses or seeks the timeline when rendering, like inside of an onUpdate/onComplete
break;
} else if (tween._active || (tween._startTime <= self._time && !tween._paused && !tween._gc)) {
if (pauseTween === tween) {
self.pause();
self._pauseTime = pauseTime; //so that when we resume(), it's starting from exactly the right spot (the pause() method uses the rawTime for the parent, but that may be a bit too far ahead)
}
if (!tween._reversed) {
tween.render((time - tween._startTime) * tween._timeScale, suppressEvents, force);
} else {
tween.render(((!tween._dirty) ? tween._totalDuration : tween.totalDuration()) - ((time - tween._startTime) * tween._timeScale), suppressEvents, force);
}
}
tween = next;
}
} else {
tween = self._last;
while (tween) {
next = tween._prev; //record it here because the value could change after rendering...
if (curTime !== self._time || (self._paused && !prevPaused)) { //in case a tween pauses or seeks the timeline when rendering, like inside of an onUpdate/onComplete
break;
} else if (tween._active || (tween._startTime <= prevTime && !tween._paused && !tween._gc)) {
if (pauseTween === tween) {
pauseTween = tween._prev; //the linked list is organized by _startTime, thus it's possible that a tween could start BEFORE the pause and end after it, in which case it would be positioned before the pause tween in the linked list, but we should render it before we pause() the timeline and cease rendering. This is only a concern when going in reverse.
while (pauseTween && pauseTween.endTime() > self._time) {
pauseTween.render( (pauseTween._reversed ? pauseTween.totalDuration() - ((time - pauseTween._startTime) * pauseTween._timeScale) : (time - pauseTween._startTime) * pauseTween._timeScale), suppressEvents, force);
pauseTween = pauseTween._prev;
}
pauseTween = null;
self.pause();
self._pauseTime = pauseTime; //so that when we resume(), it's starting from exactly the right spot (the pause() method uses the rawTime for the parent, but that may be a bit too far ahead)
}
if (!tween._reversed) {
tween.render((time - tween._startTime) * tween._timeScale, suppressEvents, force);
} else {
tween.render(((!tween._dirty) ? tween._totalDuration : tween.totalDuration()) - ((time - tween._startTime) * tween._timeScale), suppressEvents, force);
}
}
tween = next;
}
}
if (self._onUpdate) if (!suppressEvents) {
if (_lazyTweens.length) { //in case rendering caused any tweens to lazy-init, we should render them because typically when a timeline finishes, users expect things to have rendered fully. Imagine an onUpdate on a timeline that reports/checks tweened values.
_lazyRender();
}
self._callback("onUpdate");
}
if (callback) if (!self._locked) if (!self._gc) if (prevStart === self._startTime || prevTimeScale !== self._timeScale) if (self._time === 0 || totalDur >= self.totalDuration()) { //if one of the tweens that was rendered altered this timeline's startTime (like if an onComplete reversed the timeline), it probably isn't complete. If it is, don't worry, because whatever call altered the startTime would complete if it was necessary at the new time. The only exception is the timeScale property. Also check _gc because there's a chance that kill() could be called in an onUpdate
if (isComplete) {
if (_lazyTweens.length) { //in case rendering caused any tweens to lazy-init, we should render them because typically when a timeline finishes, users expect things to have rendered fully. Imagine an onComplete on a timeline that reports/checks tweened values.
_lazyRender();
}
if (self._timeline.autoRemoveChildren) {
self._enabled(false, false);
}
self._active = false;
}
if (!suppressEvents && self.vars[callback]) {
self._callback(callback);
}
}
};
p.getActive = function(nested, tweens, timelines) {
var a = [],
all = this.getChildren(nested || (nested == null), tweens || (nested == null), !!timelines),
cnt = 0,
l = all.length,
i, tween;
for (i = 0; i < l; i++) {
tween = all[i];
if (tween.isActive()) {
a[cnt++] = tween;
}
}
return a;
};
p.getLabelAfter = function(time) {
if (!time) if (time !== 0) { //faster than isNan()
time = this._time;
}
var labels = this.getLabelsArray(),
l = labels.length,
i;
for (i = 0; i < l; i++) {
if (labels[i].time > time) {
return labels[i].name;
}
}
return null;
};
p.getLabelBefore = function(time) {
if (time == null) {
time = this._time;
}
var labels = this.getLabelsArray(),
i = labels.length;
while (--i > -1) {
if (labels[i].time < time) {
return labels[i].name;
}
}
return null;
};
p.getLabelsArray = function() {
var a = [],
cnt = 0,
p;
for (p in this._labels) {
a[cnt++] = {time:this._labels[p], name:p};
}
a.sort(function(a,b) {
return a.time - b.time;
});
return a;
};
p.invalidate = function() {
this._locked = false; //unlock and set cycle in case invalidate() is called from inside an onRepeat
return TimelineLite.prototype.invalidate.call(this);
};
//---- GETTERS / SETTERS -------------------------------------------------------------------------------------------------------
p.progress = function(value, suppressEvents) {
return (!arguments.length) ? (this._time / this.duration()) || 0 : this.totalTime( this.duration() * ((this._yoyo && (this._cycle & 1) !== 0) ? 1 - value : value) + (this._cycle * (this._duration + this._repeatDelay)), suppressEvents);
};
p.totalProgress = function(value, suppressEvents) {
return (!arguments.length) ? (this._totalTime / this.totalDuration()) || 0 : this.totalTime( this.totalDuration() * value, suppressEvents);
};
p.totalDuration = function(value) {
if (!arguments.length) {
if (this._dirty) {
TimelineLite.prototype.totalDuration.call(this); //just forces refresh
//Instead of Infinity, we use 999999999999 so that we can accommodate reverses.
this._totalDuration = (this._repeat === -1) ? 999999999999 : this._duration * (this._repeat + 1) + (this._repeatDelay * this._repeat);
}
return this._totalDuration;
}
return (this._repeat === -1 || !value) ? this : this.timeScale( this.totalDuration() / value );
};
p.time = function(value, suppressEvents) {
if (!arguments.length) {
return this._time;
}
if (this._dirty) {
this.totalDuration();
}
var duration = this._duration,
cycle = this._cycle,
cycleDur = cycle * (duration * this._repeatDelay);
if (value > duration) {
value = duration;
}
return this.totalTime((this._yoyo && (cycle & 1)) ? duration - value + cycleDur : this._repeat ? value + cycleDur : value, suppressEvents);
};
p.repeat = function(value) {
if (!arguments.length) {
return this._repeat;
}
this._repeat = value;
return this._uncache(true);
};
p.repeatDelay = function(value) {
if (!arguments.length) {
return this._repeatDelay;
}
this._repeatDelay = value;
return this._uncache(true);
};
p.yoyo = function(value) {
if (!arguments.length) {
return this._yoyo;
}
this._yoyo = value;
return this;
};
p.currentLabel = function(value) {
if (!arguments.length) {
return this.getLabelBefore(this._time + _tinyNum);
}
return this.seek(value, true);
};
return TimelineMax;
}, true);
export var TimelineMax = globals.TimelineMax;
export { TimelineLite, TimelineMax as default };