Andrew Meadowsarchive remaining JS scriptsc0b446c10 years agoHistory
878 lines · 16.8 KB · JavaScript
RawDownload
/** * Tween.js - Licensed under the MIT license * https://github.com/tweenjs/tween.js * ---------------------------------------------- * * See https://github.com/tweenjs/tween.js/graphs/contributors for the full list of contributors. * Thank you all, you're awesome! */ // Include a performance.now polyfill(function () { window= {}    if ('performance' in window === false) {        window.performance = {};    }     // IE 8    Date.now = (Date.now || function () {        return new Date().getTime();    });     if ('now' in window.performance === false) {        var offset = window.performance.timing && window.performance.timing.navigationStart ? window.performance.timing.navigationStart                                                                                            : Date.now();         window.performance.now = function () {            return Date.now() - offset;        };    } })(); var TWEEN = TWEEN || (function () {     var _tweens = [];     return {         getAll: function () {             return _tweens;         },         removeAll: function () {             _tweens = [];         },         add: function (tween) {             _tweens.push(tween);         },         remove: function (tween) {             var i = _tweens.indexOf(tween);             if (i !== -1) {                _tweens.splice(i, 1);            }         },         update: function (time) {             if (_tweens.length === 0) {                return false;            }             var i = 0;             time = time !== undefined ? time : window.performance.now();             while (i < _tweens.length) {                 if (_tweens[i].update(time)) {                    i++;                } else {                    _tweens.splice(i, 1);                }             }             return true;         }    }; })(); TWEEN.Tween = function (object) {     var _object = object;    var _valuesStart = {};    var _valuesEnd = {};    var _valuesStartRepeat = {};    var _duration = 1000;    var _repeat = 0;    var _yoyo = false;    var _isPlaying = false;    var _reversed = false;    var _delayTime = 0;    var _startTime = null;    var _easingFunction = TWEEN.Easing.Linear.None;    var _interpolationFunction = TWEEN.Interpolation.Linear;    var _chainedTweens = [];    var _onStartCallback = null;    var _onStartCallbackFired = false;    var _onUpdateCallback = null;    var _onCompleteCallback = null;    var _onStopCallback = null;     // Set all starting values present on the target object    for (var field in object) {        _valuesStart[field] = parseFloat(object[field], 10);    }     this.to = function (properties, duration) {         if (duration !== undefined) {            _duration = duration;        }         _valuesEnd = properties;         return this;     };     this.start = function (time) {         TWEEN.add(this);         _isPlaying = true;         _onStartCallbackFired = false;         _startTime = time !== undefined ? time : window.performance.now();        _startTime += _delayTime;         for (var property in _valuesEnd) {             // Check if an Array was provided as property value            if (_valuesEnd[property] instanceof Array) {                 if (_valuesEnd[property].length === 0) {                    continue;                }                 // Create a local copy of the Array with the start value at the front                _valuesEnd[property] = [_object[property]].concat(_valuesEnd[property]);             }             _valuesStart[property] = _object[property];             if ((_valuesStart[property] instanceof Array) === false) {                _valuesStart[property] *= 1.0; // Ensures we're using numbers, not strings            }             _valuesStartRepeat[property] = _valuesStart[property] || 0;         }         return this;     };     this.stop = function () {         if (!_isPlaying) {            return this;        }         TWEEN.remove(this);        _isPlaying = false;         if (_onStopCallback !== null) {            _onStopCallback.call(_object);        }         this.stopChainedTweens();        return this;     };     this.stopChainedTweens = function () {         for (var i = 0, numChainedTweens = _chainedTweens.length; i < numChainedTweens; i++) {            _chainedTweens[i].stop();        }     };     this.delay = function (amount) {         _delayTime = amount;        return this;     };     this.repeat = function (times) {         _repeat = times;        return this;     };     this.yoyo = function (yoyo) {         _yoyo = yoyo;        return this;     };      this.easing = function (easing) {         _easingFunction = easing;        return this;     };     this.interpolation = function (interpolation) {         _interpolationFunction = interpolation;        return this;     };     this.chain = function () {         _chainedTweens = arguments;        return this;     };     this.onStart = function (callback) {         _onStartCallback = callback;        return this;     };     this.onUpdate = function (callback) {         _onUpdateCallback = callback;        return this;     };     this.onComplete = function (callback) {         _onCompleteCallback = callback;        return this;     };     this.onStop = function (callback) {         _onStopCallback = callback;        return this;     };     this.update = function (time) {         var property;        var elapsed;        var value;         if (time < _startTime) {            return true;        }         if (_onStartCallbackFired === false) {             if (_onStartCallback !== null) {                _onStartCallback.call(_object);            }             _onStartCallbackFired = true;         }         elapsed = (time - _startTime) / _duration;        elapsed = elapsed > 1 ? 1 : elapsed;         value = _easingFunction(elapsed);         for (property in _valuesEnd) {             var start = _valuesStart[property] || 0;            var end = _valuesEnd[property];             if (end instanceof Array) {                 _object[property] = _interpolationFunction(end, value);             } else {                 // Parses relative end values with start as base (e.g.: +10, -3)                if (typeof (end) === 'string') {                    end = start + parseFloat(end, 10);                }                 // Protect against non numeric properties.                if (typeof (end) === 'number') {                    _object[property] = start + (end - start) * value;                }             }         }         if (_onUpdateCallback !== null) {            _onUpdateCallback.call(_object, value);        }         if (elapsed === 1) {             if (_repeat > 0) {                 if (isFinite(_repeat)) {                    _repeat--;                }                 // Reassign starting values, restart by making startTime = now                for (property in _valuesStartRepeat) {                     if (typeof (_valuesEnd[property]) === 'string') {                        _valuesStartRepeat[property] = _valuesStartRepeat[property] + parseFloat(_valuesEnd[property], 10);                    }                     if (_yoyo) {                        var tmp = _valuesStartRepeat[property];                         _valuesStartRepeat[property] = _valuesEnd[property];                        _valuesEnd[property] = tmp;                    }                     _valuesStart[property] = _valuesStartRepeat[property];                 }                 if (_yoyo) {                    _reversed = !_reversed;                }                 _startTime = time + _delayTime;                 return true;             } else {                 if (_onCompleteCallback !== null) {                    _onCompleteCallback.call(_object);                }                 for (var i = 0, numChainedTweens = _chainedTweens.length; i < numChainedTweens; i++) {                    // Make the chained tweens start exactly at the time they should,                    // even if the `update()` method was called way past the duration of the tween                    _chainedTweens[i].start(_startTime + _duration);                }                 return false;             }         }         return true;     }; };  TWEEN.Easing = {     Linear: {         None: function (k) {             return k;         }     },     Quadratic: {         In: function (k) {             return k * k;         },         Out: function (k) {             return k * (2 - k);         },         InOut: function (k) {             if ((k *= 2) < 1) {                return 0.5 * k * k;            }             return - 0.5 * (--k * (k - 2) - 1);         }     },     Cubic: {         In: function (k) {             return k * k * k;         },         Out: function (k) {             return --k * k * k + 1;         },         InOut: function (k) {             if ((k *= 2) < 1) {                return 0.5 * k * k * k;            }             return 0.5 * ((k -= 2) * k * k + 2);         }     },     Quartic: {         In: function (k) {             return k * k * k * k;         },         Out: function (k) {             return 1 - (--k * k * k * k);         },         InOut: function (k) {             if ((k *= 2) < 1) {                return 0.5 * k * k * k * k;            }             return - 0.5 * ((k -= 2) * k * k * k - 2);         }     },     Quintic: {         In: function (k) {             return k * k * k * k * k;         },         Out: function (k) {             return --k * k * k * k * k + 1;         },         InOut: function (k) {             if ((k *= 2) < 1) {                return 0.5 * k * k * k * k * k;            }             return 0.5 * ((k -= 2) * k * k * k * k + 2);         }     },     Sinusoidal: {         In: function (k) {

Lines 1–500 of 878.