fween
Version:
Fween animates things
848 lines (812 loc) • 36 kB
JavaScript
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Fween = {}));
})(this, (function (exports) { 'use strict';
var FweenTicker = /** @class */ (function () {
// ================================================================================================================
// CONSTRUCTOR ----------------------------------------------------------------------------------------------------
function FweenTicker() {
// Ticker class to control updates
// Properties
this.sequences = [];
this.time = 0.0;
this.update = this.update.bind(this);
this.update();
}
// ================================================================================================================
// PUBLIC INTERFACE -----------------------------------------------------------------------------------------------
FweenTicker.prototype.getTime = function () {
return this.time;
};
FweenTicker.prototype.add = function (sequence) {
this.sequences.push(sequence);
};
FweenTicker.prototype.remove = function (sequence) {
// Nullify first, remove later - otherwise it gets remove while doing Update(), which can cause the list to trip over itself
var idx = this.sequences.indexOf(sequence);
if (idx > -1)
this.sequences[idx] = null;
};
// ================================================================================================================
// PRIVATE INTERFACE ----------------------------------------------------------------------------------------------
FweenTicker.prototype.update = function () {
globalThis.requestAnimationFrame(this.update);
this.time = performance.now() / 1000;
for (var i = 0; i < this.sequences.length; i++) {
var sequence = this.sequences[i];
if (sequence) {
sequence.update();
}
else {
this.sequences.splice(i, 1);
i--;
}
}
};
return FweenTicker;
}());
/*! *****************************************************************************
Copyright (c) Microsoft Corporation.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
***************************************************************************** */
/* global Reflect, Promise */
var extendStatics = function(d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
function __extends(d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
/**
* @author Zeh Fernando
*/
// Vars
/**
* Maps a value from a range, determined by old minimum and maximum values, to a new range,
* determined by new minimum and maximum values. These minimum and maximum values are
* referential; the new value is not clamped by them.
*
* @param value The value to be re-mapped.
* @param oldMin The previous minimum value.
* @param oldMax The previous maximum value.
* @param newMin The new minimum value.
* @param newMax The new maximum value.
* @return The new value, mapped to the new range.
*/
function map(value, oldMin, oldMax, newMin, newMax, shouldClamp) {
if (newMin === void 0) { newMin = 0; }
if (newMax === void 0) { newMax = 1; }
if (shouldClamp === void 0) { shouldClamp = false; }
if (oldMin === oldMax)
return newMin;
var p = ((value - oldMin) / (oldMax - oldMin)) * (newMax - newMin) + newMin;
if (shouldClamp)
p = newMin < newMax ? clamp(p, newMin, newMax) : clamp(p, newMax, newMin);
return p;
}
/**
* Clamps a number to a range, by restricting it to a minimum and maximum values: if the passed
* value is lower than the minimum value, it's replaced by the minimum; if it's higher than the
* maximum value, it's replaced by the maximum; if neither, it's unchanged.
*
* @param value The value to be clamped.
* @param min Minimum value allowed.
* @param max Maximum value allowed.
* @return The newly clamped value.
*/
function clamp(value, min, max) {
if (min === void 0) { min = 0; }
if (max === void 0) { max = 1; }
return value < min ? min : value > max ? max : value;
}
/*
Disclaimer for Robert Penner's Easing Equations license:
TERMS OF USE - EASING EQUATIONS
Open source under the BSD License.
Copyright © 2001 Robert Penner
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* Based on Robert Penner's easing equations - remade from Tweener's equations, but simplified
* Not fully tested!
*/
var Easing = /** @class */ (function () {
function Easing() {
}
// ================================================================================================================
// EQUATIONS ------------------------------------------------------------------------------------------------------
/**
* Easing equation function for a simple linear tweening, with no easing.
*
* @param t Current time/phase (0-1).
* @return The new value/phase (0-1).
*/
Easing.none = function (t) {
return t;
};
/**
* Easing equation function for a quadratic (t^2) easing in: accelerating from zero velocity.
*
* @param t Current time/phase (0-1).
* @return The new value/phase (0-1).
*/
Easing.quadIn = function (t) {
return t * t;
};
/**
* Easing equation function for a quadratic (t^2) easing out: decelerating to zero velocity.
*
* @param t Current time/phase (0-1).
* @return The new value/phase (0-1).
*/
Easing.quadOut = function (t) {
return -t * (t - 2);
};
/**
* Easing equation function for a quadratic (t^2) easing in and then out: accelerating from zero velocity, then decelerating.
*
* @param t Current time/phase (0-1).
* @return The new value/phase (0-1).
*/
Easing.quadInOut = function (t) {
//return t < 0.5 ? quadIn(t*2) : quadOut((t-0.5)*2);
return (t *= 2) < 1 ? t * t * 0.5 : -0.5 * (--t * (t - 2) - 1);
};
/**
* Easing equation function for a cubic (t^3) easing in: accelerating from zero velocity.
*
* @param t Current time/phase (0-1).
* @return The new value/phase (0-1).
*/
Easing.cubicIn = function (t) {
return t * t * t;
};
/**
* Easing equation function for a cubic (t^3) easing out: decelerating from zero velocity.
*
* @param t Current time/phase (0-1).
* @return The new value/phase (0-1).
*/
Easing.cubicOut = function (t) {
return (t = t - 1) * t * t + 1;
};
Easing.cubicInOut = function (t) {
return (t *= 2) < 1 ? Easing.cubicIn(t) / 2 : Easing.cubicOut(t - 1) / 2 + 0.5; // TODO: redo with in-line calculation
};
/**
* Easing equation function for a quartic (t^4) easing in: accelerating from zero velocity.
*
* @param t Current time/phase (0-1).
* @return The new value/phase (0-1).
*/
Easing.quartIn = function (t) {
return t * t * t * t;
};
/**
* Easing equation function for a quartic (t^4) easing out: decelerating from zero velocity.
*
* @param t Current time/phase (0-1).
* @return The new value/phase (0-1).
*/
Easing.quartOut = function (t) {
t--;
return -1 * (t * t * t * t - 1);
};
Easing.quartInOut = function (t) {
return (t *= 2) < 1 ? Easing.quartIn(t) / 2 : Easing.quartOut(t - 1) / 2 + 0.5; // TODO: redo with in-line calculation
};
/**
* Easing equation function for a quintic (t^5) easing in: accelerating from zero velocity.
*
* @param t Current time/phase (0-1).
* @return The new value/phase (0-1).
*/
Easing.quintIn = function (t) {
return t * t * t * t * t;
};
/**
* Easing equation function for a quintic (t^5) easing out: decelerating from zero velocity.
*
* @param t Current time/phase (0-1).
* @return The new value/phase (0-1).
*/
Easing.quintOut = function (t) {
t--;
return t * t * t * t * t + 1;
};
Easing.quintInOut = function (t) {
return (t *= 2) < 1 ? Easing.quintIn(t) / 2 : Easing.quintOut(t - 1) / 2 + 0.5; // TODO: redo with in-line calculation
};
/**
* Easing equation function for a sinusoidal (sin(t)) easing in: accelerating from zero velocity.
*
* @param t Current time/phase (0-1).
* @return The new value/phase (0-1).
*/
Easing.sineIn = function (t) {
return -1 * Math.cos(t * Easing.HALF_PI) + 1;
};
/**
* Easing equation function for a sinusoidal (sin(t)) easing out: decelerating from zero velocity.
*
* @param t Current time/phase (0-1).
* @return The new value/phase (0-1).
*/
Easing.sineOut = function (t) {
return Math.sin(t * Easing.HALF_PI);
};
Easing.sineInOut = function (t) {
return (t *= 2) < 1 ? Easing.sineIn(t) / 2 : Easing.sineOut(t - 1) / 2 + 0.5; // TODO: redo with in-line calculation
};
/**
* Easing equation function for an exponential (2^t) easing in: accelerating from zero velocity.
*
* @param t Current time/phase (0-1).
* @return The new value/phase (0-1).
*/
Easing.expoIn = function (t) {
// return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b; // original
return t == 0 ? 0 : Math.pow(2, 10 * (t - 1)) - 0.001; // ztween fixed
};
/**
* Easing equation function for an exponential (2^t) easing out: decelerating from zero velocity.
*
* @param t Current time/phase (0-1).
* @return The new value/phase (0-1).
*/
Easing.expoOut = function (t) {
// return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b; // original
// return (t==1) ? 1 : (-Math.pow(2, -10 * t) + 1); // ztween
// return (t == d) ? b + c : c * 1.001 * (-Math.pow(2, -10 * t / d) + 1) + b; // tweener fixed
//log(">", t, (t==1) ? 1 : 1.001 * (-Math.pow(2, -10 * t) + 1))
//return (t==1) ? 1 : 1.001 * (-Math.pow(2, -10 * t) + 1); // ztween fixed
return t >= 0.999 ? 1 : 1.001 * (-Math.pow(2, -10 * t) + 1); // ztween fixed 2
};
Easing.expoInOut = function (t) {
return (t *= 2) < 1 ? Easing.expoIn(t) / 2 : Easing.expoOut(t - 1) / 2 + 0.5; // TODO: redo with in-line calculation
};
/**
* Easing equation function for a circular (sqrt(1-t^2)) easing in: accelerating from zero velocity.
*
* @param t Current time/phase (0-1).
* @return The new value/phase (0-1).
*/
Easing.circIn = function (t) {
return -1 * (Math.sqrt(1 - t * t) - 1);
};
/**
* Easing equation function for a circular (sqrt(1-t^2)) easing out: decelerating from zero velocity.
*
* @param t Current time/phase (0-1).
* @return The new value/phase (0-1).
*/
Easing.circOut = function (t) {
t--;
return Math.sqrt(1 - t * t);
};
Easing.circInOut = function (t) {
return (t *= 2) < 1 ? Easing.circIn(t) / 2 : Easing.circOut(t - 1) / 2 + 0.5; // TODO: redo with in-line calculation
};
/**
* Easing equation function for an elastic (exponentially decaying sine wave) easing in: accelerating from zero velocity.
*
* @param t Current time/phase (0-1).
* @param a Amplitude.
* @param p Period.
* @return The new value/phase (0-1).
*/
Easing.elasticIn = function (t, a, p) {
if (a === void 0) { a = 0; }
if (p === void 0) { p = 0.3; }
if (t == 0)
return 0;
if (t == 1)
return 1;
var s;
if (a < 1) {
a = 1;
s = p / 4;
}
else {
s = (p / Easing.TWO_PI) * Math.asin(1 / a);
}
return -(a * Math.pow(2, 10 * (t -= 1)) * Math.sin(((t - s) * Easing.TWO_PI) / p));
};
/**
* Easing equation function for an elastic (exponentially decaying sine wave) easing out: decelerating from zero velocity.
*
* @param t Current time/phase (0-1).
* @param a Amplitude.
* @param p Period.
*/
Easing.elasticOut = function (t, a, p) {
if (a === void 0) { a = 0; }
if (p === void 0) { p = 0.3; }
if (t == 0)
return 0;
if (t == 1)
return 1;
var s;
if (a < 1) {
a = 1;
s = p / 4;
}
else {
s = (p / Easing.TWO_PI) * Math.asin(1 / a);
}
return a * Math.pow(2, -10 * t) * Math.sin(((t - s) * Easing.TWO_PI) / p) + 1;
};
/**
* Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing in: accelerating from zero velocity.
*
* @param t Current time/phase (0-1).
* @param s Overshoot ammount: higher s means greater overshoot (0 produces cubic easing with no overshoot, and the default value of 1.70158 produces an overshoot of 10 percent).
* @param p Period.
*/
Easing.backIn = function (t, s) {
if (s === void 0) { s = 1.70158; }
return t * t * ((s + 1) * t - s);
};
/**
* Easing equation function for a back (overshooting cubic easing: (s+1)*t^3 - s*t^2) easing out: decelerating from zero velocity.
*
* @param t Current time/phase (0-1).
* @param s Overshoot ammount: higher s means greater overshoot (0 produces cubic easing with no overshoot, and the default value of 1.70158 produces an overshoot of 10 percent).
* @param p Period.
*/
Easing.backOut = function (t, s) {
if (s === void 0) { s = 1.70158; }
t--;
return t * t * ((s + 1) * t + s) + 1;
};
Easing.backOutWith = function (overshoot) {
var _this = this;
if (overshoot === void 0) { overshoot = 1.70158; }
return function (t) {
return _this.backOut(t, overshoot);
};
};
Easing.backInOut = function (t) {
return (t *= 2) < 1 ? Easing.backIn(t) / 2 : Easing.backOut(t - 1) / 2 + 0.5; // TODO: redo with in-line calculation
};
/**
* Easing equation function for a bounce (exponentially decaying parabolic bounce) easing in: accelerating from zero velocity.
*
* @param t Current time/phase (0-1).
* @param p Period.
*/
Easing.bounceIn = function (t) {
return 1 - Easing.bounceOut(1 - t);
};
/**
* Easing equation function for a bounce (exponentially decaying parabolic bounce) easing out: decelerating from zero velocity.
*
* @param t Current time/phase (0-1).
* @param p Period.
*/
Easing.bounceOut = function (t) {
if (t < 1 / 2.75) {
return 7.5625 * t * t;
}
else if (t < 2 / 2.75) {
return 7.5625 * (t -= 1.5 / 2.75) * t + 0.75;
}
else if (t < 2.5 / 2.75) {
return 7.5625 * (t -= 2.25 / 2.75) * t + 0.9375;
}
else {
return 7.5625 * (t -= 2.625 / 2.75) * t + 0.984375;
}
};
// ================================================================================================================
// COMBINATOR -----------------------------------------------------------------------------------------------------
Easing.combined = function (t, equations) {
var l = equations.length;
var eq = Math.floor(t * l);
if (eq === equations.length)
eq = l - 1;
return Number(equations[eq](t * l - eq));
};
// Constants
Easing.HALF_PI = Math.PI / 2;
Easing.TWO_PI = Math.PI * 2;
return Easing;
}());
var FweenStepMetadata = /** @class */ (function () {
// ================================================================================================================
// CONSTRUCTOR ----------------------------------------------------------------------------------------------------
function FweenStepMetadata() {
// Class to maintain metadata related to each step of a Fween sequence
// Properties
this.hasStarted = false;
this.hasCompleted = false;
this.timeStart = 0.0;
this.timeEnd = 0;
}
Object.defineProperty(FweenStepMetadata.prototype, "timeDuration", {
// ================================================================================================================
// ACCESSOR INTERFACE ---------------------------------------------------------------------------------------------
get: function () {
return this.timeEnd - this.timeStart;
},
enumerable: false,
configurable: true
});
return FweenStepMetadata;
}());
var FweenStepCall = /** @class */ (function () {
// ================================================================================================================
// CONSTRUCTOR ----------------------------------------------------------------------------------------------------
function FweenStepCall(func) {
this._action = func;
}
// ================================================================================================================
// PUBLIC INTERFACE -----------------------------------------------------------------------------------------------
FweenStepCall.prototype.start = function () { };
FweenStepCall.prototype.update = function (t) { };
FweenStepCall.prototype.end = function () {
this._action();
};
FweenStepCall.prototype.getDuration = function () {
return 0;
};
return FweenStepCall;
}());
var FweenStepWait = /** @class */ (function () {
// ================================================================================================================
// CONSTRUCTOR ----------------------------------------------------------------------------------------------------
function FweenStepWait(duration) {
this._duration = duration;
}
// ================================================================================================================
// PUBLIC INTERFACE -----------------------------------------------------------------------------------------------
FweenStepWait.prototype.start = function () { };
FweenStepWait.prototype.update = function (t) { };
FweenStepWait.prototype.end = function () { };
FweenStepWait.prototype.getDuration = function () {
return this._duration;
};
return FweenStepWait;
}());
var FweenSequence = /** @class */ (function () {
// ================================================================================================================
// CONSTRUCTOR ----------------------------------------------------------------------------------------------------
function FweenSequence(ticker) {
// One sequence of steps
// Properties
this._steps = [];
this._stepsMetadatas = [];
this._isPlaying = false;
this._currentStep = 0;
this._startTime = 0.0;
this._pauseTime = 0.0;
this._executedTime = 0.0;
this._duration = 0.0;
// Create a new Fween
this._ticker = ticker;
this._startTime = this._ticker.getTime();
}
// ================================================================================================================
// PUBLIC INTERFACE -----------------------------------------------------------------------------------------------
// Play control methods
/**
* Play (or resume) the sequence
*/
FweenSequence.prototype.play = function () {
if (!this._isPlaying) {
this._isPlaying = true;
var timePaused = this._ticker.getTime() - this._pauseTime;
this._startTime += timePaused;
this._ticker.add(this);
}
return this;
};
/**
* Pause the sequence at the current position
*/
FweenSequence.prototype.pause = function () {
if (this._isPlaying) {
this._isPlaying = false;
this._pauseTime = this._ticker.getTime();
this._ticker.remove(this);
}
return this;
};
/**
* Stop the sequence completely
*/
FweenSequence.prototype.stop = function () {
if (this._isPlaying) {
this.pause();
// TODO: do pause() and seek() instead
this._startTime = this._ticker.getTime();
this._executedTime = 0;
}
return this;
};
FweenSequence.prototype.isPlaying = function () {
return this._isPlaying;
};
// Utility methods
/**
* Call a function
*/
FweenSequence.prototype.call = function (func) {
this.addStep(new FweenStepCall(func));
return this;
};
/**
* Wait a number of seconds
*/
FweenSequence.prototype.wait = function (duration) {
this.addStep(new FweenStepWait(duration));
return this;
};
// ================================================================================================================
// PRIVATE INTERFACE ----------------------------------------------------------------------------------------------
// Core tween step control methods; reused by subclasses
FweenSequence.prototype.addStep = function (step) {
this._steps.push(step);
var tweenMetadata = new FweenStepMetadata();
tweenMetadata.timeStart = this._startTime + this._duration;
this._duration += step.getDuration();
tweenMetadata.timeEnd = this._startTime + this._duration;
this._stepsMetadatas.push(tweenMetadata);
};
FweenSequence.prototype.update = function () {
// Update current step(s) based on the time
// Check if finished
if (this._currentStep >= this._steps.length) {
this.destroy();
}
else {
var shouldUpdateOnce = this._isPlaying;
while (shouldUpdateOnce && this._currentStep < this._steps.length) {
shouldUpdateOnce = false;
if (this._ticker.getTime() >= this._stepsMetadatas[this._currentStep].timeStart) {
// Start the current tween step if necessary
if (!this._stepsMetadatas[this._currentStep].hasStarted) {
this._steps[this._currentStep].start();
this._stepsMetadatas[this._currentStep].hasStarted = true;
}
// Update the current tween step
this._steps[this._currentStep].update(map(this._ticker.getTime(), this._stepsMetadatas[this._currentStep].timeStart, this._stepsMetadatas[this._currentStep].timeEnd, 0, 1, true));
// Check if it's finished
if (this._ticker.getTime() >= this._stepsMetadatas[this._currentStep].timeEnd) {
if (!this._stepsMetadatas[this._currentStep].hasCompleted) {
this._steps[this._currentStep].end();
this._stepsMetadatas[this._currentStep].hasCompleted = true;
this._executedTime += this._stepsMetadatas[this._currentStep].timeDuration;
shouldUpdateOnce = true;
this._currentStep++;
}
}
}
}
}
};
FweenSequence.prototype.getTransition = function (transition) {
return transition ? transition : Easing.none;
};
FweenSequence.prototype.destroy = function () {
this._ticker.remove(this);
};
return FweenSequence;
}());
var FweenStepValuesTo = /** @class */ (function () {
// ================================================================================================================
// CONSTRUCTOR ----------------------------------------------------------------------------------------------------
function FweenStepValuesTo(targetGet, targetSet, targetValues, duration, transition) {
this._targetGet = targetGet;
this._targetSet = targetSet;
this._duration = duration;
this._startValues = {};
this._targetValues = targetValues;
this._transition = transition;
this._names = Object.keys(targetValues);
}
// ================================================================================================================
// PUBLIC INTERFACE -----------------------------------------------------------------------------------------------
FweenStepValuesTo.prototype.start = function () {
var _this = this;
this._startValues = {};
this._names.forEach(function (name) {
_this._startValues[name] = _this._targetGet(name);
});
};
FweenStepValuesTo.prototype.update = function (t) {
var _this = this;
this._names.forEach(function (name) {
_this._targetSet(name, map(_this._transition(t), 0, 1, _this._startValues[name], _this._targetValues[name]));
});
};
FweenStepValuesTo.prototype.end = function () {
var _this = this;
this._names.forEach(function (name) {
_this._targetSet(name, _this._targetValues[name]);
});
};
FweenStepValuesTo.prototype.getDuration = function () {
return this._duration;
};
return FweenStepValuesTo;
}());
var FweenObjectSequence = /** @class */ (function (_super) {
__extends(FweenObjectSequence, _super);
// ================================================================================================================
// CONSTRUCTOR ----------------------------------------------------------------------------------------------------
function FweenObjectSequence(object, ticker) {
var _this = _super.call(this, ticker) || this;
_this._targetObject = object;
return _this;
}
// ================================================================================================================
// PUBLIC INTERFACE -----------------------------------------------------------------------------------------------
FweenObjectSequence.prototype.to = function (values, duration, transition) {
if (duration === void 0) { duration = 0; }
this.addStep(new FweenStepValuesTo(this.getValue.bind(this), this.setValue.bind(this), values, duration, this.getTransition(transition)));
return this;
};
// ================================================================================================================
// PRIVATE INTERFACE ----------------------------------------------------------------------------------------------
FweenObjectSequence.prototype.setValue = function (name, t) {
this._targetObject[name] = t;
};
FweenObjectSequence.prototype.getValue = function (name) {
return this._targetObject[name];
};
return FweenObjectSequence;
}(FweenSequence));
var FweenStepValueFrom = /** @class */ (function () {
// ================================================================================================================
// CONSTRUCTOR ----------------------------------------------------------------------------------------------------
function FweenStepValueFrom(targetSet, targetValue) {
this._targetSet = targetSet;
this._targetValue = targetValue;
}
// ================================================================================================================
// PUBLIC INTERFACE -----------------------------------------------------------------------------------------------
FweenStepValueFrom.prototype.start = function () { };
FweenStepValueFrom.prototype.update = function (t) { };
FweenStepValueFrom.prototype.end = function () {
this._targetSet(typeof this._targetValue === "function" ? this._targetValue() : this._targetValue);
};
FweenStepValueFrom.prototype.getDuration = function () {
return 0;
};
return FweenStepValueFrom;
}());
var FweenStepValueTo = /** @class */ (function () {
// ================================================================================================================
// CONSTRUCTOR ----------------------------------------------------------------------------------------------------
function FweenStepValueTo(targetGet, targetSet, targetValue, duration, transition) {
this._targetGet = targetGet;
this._targetSet = targetSet;
this._duration = duration;
this._startValue = 0;
this._targetValue = targetValue;
this._transition = transition;
}
// ================================================================================================================
// PUBLIC INTERFACE -----------------------------------------------------------------------------------------------
FweenStepValueTo.prototype.start = function () {
this._startValue = this._targetGet();
};
FweenStepValueTo.prototype.update = function (t) {
this._targetSet(map(this._transition(t), 0, 1, this._startValue, this._targetValue));
};
FweenStepValueTo.prototype.end = function () {
this._targetSet(this._targetValue);
};
FweenStepValueTo.prototype.getDuration = function () {
return this._duration;
};
return FweenStepValueTo;
}());
var FweenSetterSequence = /** @class */ (function (_super) {
__extends(FweenSetterSequence, _super);
// ================================================================================================================
// CONSTRUCTOR ----------------------------------------------------------------------------------------------------
function FweenSetterSequence(targetSet, ticker) {
var _this = _super.call(this, ticker) || this;
_this._targetValue = 0;
_this._targetSet = targetSet;
return _this;
}
// ================================================================================================================
// PUBLIC INTERFACE -----------------------------------------------------------------------------------------------
FweenSetterSequence.prototype.from = function (value) {
this.addStep(new FweenStepValueFrom(this.setValue.bind(this), value));
return this;
};
FweenSetterSequence.prototype.to = function (value, duration, transition) {
if (duration === void 0) { duration = 0; }
this.addStep(new FweenStepValueTo(this.getValue.bind(this), this.setValue.bind(this), value, duration, this.getTransition(transition)));
return this;
};
// ================================================================================================================
// PRIVATE INTERFACE ----------------------------------------------------------------------------------------------
FweenSetterSequence.prototype.setValue = function (t) {
this._targetValue = t;
this._targetSet(t);
};
FweenSetterSequence.prototype.getValue = function () {
return this._targetValue;
};
return FweenSetterSequence;
}(FweenSequence));
/*
Ideas for tweening - from https://github.com/zeh/unity-tidbits/blob/master/transitions/ZTween.cs
DONE:
-- FweenSetterSequence:
Fween
.use(getter)
.from(value)
.to(value, t, transition)
-- FweenObjectSequence:
Fween
.use(object)
.to({name: value}, t, transition)
All:
.call(f)
.wait(t)
Also:
.play()
.pause()
.isPlaying()
TODO:
.stop()
.seek()
*/
var ticker;
function use(p1) {
if (typeof p1 === "object") {
// Object
return new FweenObjectSequence(p1, getTicker());
}
else if (typeof p1 === "function") {
// Setter sequence
return new FweenSetterSequence(p1, getTicker());
}
console.error("Tweening parameters were not understood.");
return null;
}
function resetTicker() {
ticker = new FweenTicker();
}
function getTicker() {
if (!ticker)
ticker = new FweenTicker();
return ticker;
}
var Fween = {
use: use,
getTicker: getTicker,
resetTicker: resetTicker,
};
exports.Easing = Easing;
exports.Fween = Fween;
exports.FweenObjectSequence = FweenObjectSequence;
exports.FweenSequence = FweenSequence;
exports.FweenSetterSequence = FweenSetterSequence;
exports.FweenTicker = FweenTicker;
}));
module.exports.default = module.exports; // Terrible injection just so it works regardless of how it's required