123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935 |
- /**
- * 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!
- */
- var _Group = function () {
- this._tweens = {};
- this._tweensAddedDuringUpdate = {};
- };
- _Group.prototype = {
- getAll: function () {
- return Object.keys(this._tweens).map(function (tweenId) {
- return this._tweens[tweenId];
- }.bind(this));
- },
- removeAll: function () {
- this._tweens = {};
- },
- add: function (tween) {
- this._tweens[tween.getId()] = tween;
- this._tweensAddedDuringUpdate[tween.getId()] = tween;
- },
- remove: function (tween) {
- delete this._tweens[tween.getId()];
- delete this._tweensAddedDuringUpdate[tween.getId()];
- },
- update: function (time, preserve) {
- var tweenIds = Object.keys(this._tweens);
- if (tweenIds.length === 0) {
- return false;
- }
- time = time !== undefined ? time : TWEEN.now();
- // Tweens are updated in "batches". If you add a new tween during an update, then the
- // new tween will be updated in the next batch.
- // If you remove a tween during an update, it may or may not be updated. However,
- // if the removed tween was added during the current batch, then it will not be updated.
- while (tweenIds.length > 0) {
- this._tweensAddedDuringUpdate = {};
- for (var i = 0; i < tweenIds.length; i++) {
- var tween = this._tweens[tweenIds[i]];
- if (tween && tween.update(time) === false) {
- tween._isPlaying = false;
- if (!preserve) {
- delete this._tweens[tweenIds[i]];
- }
- }
- }
- tweenIds = Object.keys(this._tweensAddedDuringUpdate);
- }
- return true;
- }
- };
- var TWEEN = new _Group();
- TWEEN.Group = _Group;
- TWEEN._nextId = 0;
- TWEEN.nextId = function () {
- return TWEEN._nextId++;
- };
- // Include a performance.now polyfill.
- // In node.js, use process.hrtime.
- if (typeof (self) === 'undefined' && typeof (process) !== 'undefined' && process.hrtime) {
- TWEEN.now = function () {
- var time = process.hrtime();
- // Convert [seconds, nanoseconds] to milliseconds.
- return time[0] * 1000 + time[1] / 1000000;
- };
- }
- // In a browser, use self.performance.now if it is available.
- else if (typeof (self) !== 'undefined' &&
- self.performance !== undefined &&
- self.performance.now !== undefined) {
- // This must be bound, because directly assigning this function
- // leads to an invocation exception in Chrome.
- TWEEN.now = self.performance.now.bind(self.performance);
- }
- // Use Date.now if it is available.
- else if (Date.now !== undefined) {
- TWEEN.now = Date.now;
- }
- // Otherwise, use 'new Date().getTime()'.
- else {
- TWEEN.now = function () {
- return new Date().getTime();
- };
- }
- TWEEN.Tween = function (object, group) {
- this._object = object;
- this._valuesStart = {};
- this._valuesEnd = {};
- this._valuesStartRepeat = {};
- this._duration = 1000;
- this._repeat = 0;
- this._repeatDelayTime = undefined;
- this._yoyo = false;
- this._isPlaying = false;
- this._reversed = false;
- this._delayTime = 0;
- this._startTime = null;
- this._easingFunction = TWEEN.Easing.Linear.None;
- this._interpolationFunction = TWEEN.Interpolation.Linear;
- this._chainedTweens = [];
- this._onStartCallback = null;
- this._onStartCallbackFired = false;
- this._onUpdateCallback = null;
- this._onRepeatCallback = null;
- this._onCompleteCallback = null;
- this._onStopCallback = null;
- this._group = group || TWEEN;
- this._id = TWEEN.nextId();
- };
- TWEEN.Tween.prototype = {
- getId: function () {
- return this._id;
- },
- isPlaying: function () {
- return this._isPlaying;
- },
- to: function (properties, duration) {
- this._valuesEnd = Object.create(properties);
- if (duration !== undefined) {
- this._duration = duration;
- }
- return this;
- },
- duration: function duration(d) {
- this._duration = d;
- return this;
- },
- start: function (time) {
- this._group.add(this);
- this._isPlaying = true;
- this._onStartCallbackFired = false;
- this._startTime = time !== undefined ? typeof time === 'string' ? TWEEN.now() + parseFloat(time) : time : TWEEN.now();
- this._startTime += this._delayTime;
- for (var property in this._valuesEnd) {
- // Check if an Array was provided as property value
- if (this._valuesEnd[property] instanceof Array) {
- if (this._valuesEnd[property].length === 0) {
- continue;
- }
- // Create a local copy of the Array with the start value at the front
- this._valuesEnd[property] = [this._object[property]].concat(this._valuesEnd[property]);
- }
- // If `to()` specifies a property that doesn't exist in the source object,
- // we should not set that property in the object
- if (this._object[property] === undefined) {
- continue;
- }
- // Save the starting value.
- this._valuesStart[property] = this._object[property];
- if ((this._valuesStart[property] instanceof Array) === false) {
- this._valuesStart[property] *= 1.0; // Ensures we're using numbers, not strings
- }
- this._valuesStartRepeat[property] = this._valuesStart[property] || 0;
- }
- return this;
- },
- stop: function () {
- if (!this._isPlaying) {
- return this;
- }
- this._group.remove(this);
- this._isPlaying = false;
- if (this._onStopCallback !== null) {
- this._onStopCallback(this._object);
- }
- this.stopChainedTweens();
- return this;
- },
- end: function () {
- this.update(Infinity);
- return this;
- },
- stopChainedTweens: function () {
- for (var i = 0, numChainedTweens = this._chainedTweens.length; i < numChainedTweens; i++) {
- this._chainedTweens[i].stop();
- }
- },
- group: function (group) {
- this._group = group;
- return this;
- },
- delay: function (amount) {
- this._delayTime = amount;
- return this;
- },
- repeat: function (times) {
- this._repeat = times;
- return this;
- },
- repeatDelay: function (amount) {
- this._repeatDelayTime = amount;
- return this;
- },
- yoyo: function (yoyo) {
- this._yoyo = yoyo;
- return this;
- },
- easing: function (easingFunction) {
- this._easingFunction = easingFunction;
- return this;
- },
- interpolation: function (interpolationFunction) {
- this._interpolationFunction = interpolationFunction;
- return this;
- },
- chain: function () {
- this._chainedTweens = arguments;
- return this;
- },
- onStart: function (callback) {
- this._onStartCallback = callback;
- return this;
- },
- onUpdate: function (callback) {
- this._onUpdateCallback = callback;
- return this;
- },
- onRepeat: function onRepeat(callback) {
- this._onRepeatCallback = callback;
- return this;
- },
- onComplete: function (callback) {
- this._onCompleteCallback = callback;
- return this;
- },
- onStop: function (callback) {
- this._onStopCallback = callback;
- return this;
- },
- update: function (time) {
- var property;
- var elapsed;
- var value;
- if (time < this._startTime) {
- return true;
- }
- if (this._onStartCallbackFired === false) {
- if (this._onStartCallback !== null) {
- this._onStartCallback(this._object);
- }
- this._onStartCallbackFired = true;
- }
- elapsed = (time - this._startTime) / this._duration;
- elapsed = (this._duration === 0 || elapsed > 1) ? 1 : elapsed;
- value = this._easingFunction(elapsed);
- for (property in this._valuesEnd) {
- // Don't update properties that do not exist in the source object
- if (this._valuesStart[property] === undefined) {
- continue;
- }
- var start = this._valuesStart[property] || 0;
- var end = this._valuesEnd[property];
- if (end instanceof Array) {
- this._object[property] = this._interpolationFunction(end, value);
- } else {
- // Parses relative end values with start as base (e.g.: +10, -3)
- if (typeof (end) === 'string') {
- if (end.charAt(0) === '+' || end.charAt(0) === '-') {
- end = start + parseFloat(end);
- } else {
- end = parseFloat(end);
- }
- }
- // Protect against non numeric properties.
- if (typeof (end) === 'number') {
- this._object[property] = start + (end - start) * value;
- }
- }
- }
- if (this._onUpdateCallback !== null) {
- this._onUpdateCallback(this._object, elapsed);
- }
- if (elapsed === 1) {
- if (this._repeat > 0) {
- if (isFinite(this._repeat)) {
- this._repeat--;
- }
- // Reassign starting values, restart by making startTime = now
- for (property in this._valuesStartRepeat) {
- if (typeof (this._valuesEnd[property]) === 'string') {
- this._valuesStartRepeat[property] = this._valuesStartRepeat[property] + parseFloat(this._valuesEnd[property]);
- }
- if (this._yoyo) {
- var tmp = this._valuesStartRepeat[property];
- this._valuesStartRepeat[property] = this._valuesEnd[property];
- this._valuesEnd[property] = tmp;
- }
- this._valuesStart[property] = this._valuesStartRepeat[property];
- }
- if (this._yoyo) {
- this._reversed = !this._reversed;
- }
- if (this._repeatDelayTime !== undefined) {
- this._startTime = time + this._repeatDelayTime;
- } else {
- this._startTime = time + this._delayTime;
- }
- if (this._onRepeatCallback !== null) {
- this._onRepeatCallback(this._object);
- }
- return true;
- } else {
- if (this._onCompleteCallback !== null) {
- this._onCompleteCallback(this._object);
- }
- for (var i = 0, numChainedTweens = this._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
- this._chainedTweens[i].start(this._startTime + this._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) {
- return 1 - Math.cos(k * Math.PI / 2);
- },
- Out: function (k) {
- return Math.sin(k * Math.PI / 2);
- },
- InOut: function (k) {
- return 0.5 * (1 - Math.cos(Math.PI * k));
- }
- },
- Exponential: {
- In: function (k) {
- return k === 0 ? 0 : Math.pow(1024, k - 1);
- },
- Out: function (k) {
- return k === 1 ? 1 : 1 - Math.pow(2, - 10 * k);
- },
- InOut: function (k) {
- if (k === 0) {
- return 0;
- }
- if (k === 1) {
- return 1;
- }
- if ((k *= 2) < 1) {
- return 0.5 * Math.pow(1024, k - 1);
- }
- return 0.5 * (- Math.pow(2, - 10 * (k - 1)) + 2);
- }
- },
- Circular: {
- In: function (k) {
- return 1 - Math.sqrt(1 - k * k);
- },
- Out: function (k) {
- return Math.sqrt(1 - (--k * k));
- },
- InOut: function (k) {
- if ((k *= 2) < 1) {
- return - 0.5 * (Math.sqrt(1 - k * k) - 1);
- }
- return 0.5 * (Math.sqrt(1 - (k -= 2) * k) + 1);
- }
- },
- Elastic: {
- In: function (k) {
- if (k === 0) {
- return 0;
- }
- if (k === 1) {
- return 1;
- }
- return -Math.pow(2, 10 * (k - 1)) * Math.sin((k - 1.1) * 5 * Math.PI);
- },
- Out: function (k) {
- if (k === 0) {
- return 0;
- }
- if (k === 1) {
- return 1;
- }
- return Math.pow(2, -10 * k) * Math.sin((k - 0.1) * 5 * Math.PI) + 1;
- },
- InOut: function (k) {
- if (k === 0) {
- return 0;
- }
- if (k === 1) {
- return 1;
- }
- k *= 2;
- if (k < 1) {
- return -0.5 * Math.pow(2, 10 * (k - 1)) * Math.sin((k - 1.1) * 5 * Math.PI);
- }
- return 0.5 * Math.pow(2, -10 * (k - 1)) * Math.sin((k - 1.1) * 5 * Math.PI) + 1;
- }
- },
- Back: {
- In: function (k) {
- var s = 1.70158;
- return k * k * ((s + 1) * k - s);
- },
- Out: function (k) {
- var s = 1.70158;
- return --k * k * ((s + 1) * k + s) + 1;
- },
- InOut: function (k) {
- var s = 1.70158 * 1.525;
- if ((k *= 2) < 1) {
- return 0.5 * (k * k * ((s + 1) * k - s));
- }
- return 0.5 * ((k -= 2) * k * ((s + 1) * k + s) + 2);
- }
- },
- Bounce: {
- In: function (k) {
- return 1 - TWEEN.Easing.Bounce.Out(1 - k);
- },
- Out: function (k) {
- if (k < (1 / 2.75)) {
- return 7.5625 * k * k;
- } else if (k < (2 / 2.75)) {
- return 7.5625 * (k -= (1.5 / 2.75)) * k + 0.75;
- } else if (k < (2.5 / 2.75)) {
- return 7.5625 * (k -= (2.25 / 2.75)) * k + 0.9375;
- } else {
- return 7.5625 * (k -= (2.625 / 2.75)) * k + 0.984375;
- }
- },
- InOut: function (k) {
- if (k < 0.5) {
- return TWEEN.Easing.Bounce.In(k * 2) * 0.5;
- }
- return TWEEN.Easing.Bounce.Out(k * 2 - 1) * 0.5 + 0.5;
- }
- }
- };
- TWEEN.Interpolation = {
- Linear: function (v, k) {
- var m = v.length - 1;
- var f = m * k;
- var i = Math.floor(f);
- var fn = TWEEN.Interpolation.Utils.Linear;
- if (k < 0) {
- return fn(v[0], v[1], f);
- }
- if (k > 1) {
- return fn(v[m], v[m - 1], m - f);
- }
- return fn(v[i], v[i + 1 > m ? m : i + 1], f - i);
- },
- Bezier: function (v, k) {
- var b = 0;
- var n = v.length - 1;
- var pw = Math.pow;
- var bn = TWEEN.Interpolation.Utils.Bernstein;
- for (var i = 0; i <= n; i++) {
- b += pw(1 - k, n - i) * pw(k, i) * v[i] * bn(n, i);
- }
- return b;
- },
- CatmullRom: function (v, k) {
- var m = v.length - 1;
- var f = m * k;
- var i = Math.floor(f);
- var fn = TWEEN.Interpolation.Utils.CatmullRom;
- if (v[0] === v[m]) {
- if (k < 0) {
- i = Math.floor(f = m * (1 + k));
- }
- return fn(v[(i - 1 + m) % m], v[i], v[(i + 1) % m], v[(i + 2) % m], f - i);
- } else {
- if (k < 0) {
- return v[0] - (fn(v[0], v[0], v[1], v[1], -f) - v[0]);
- }
- if (k > 1) {
- return v[m] - (fn(v[m], v[m], v[m - 1], v[m - 1], f - m) - v[m]);
- }
- return fn(v[i ? i - 1 : 0], v[i], v[m < i + 1 ? m : i + 1], v[m < i + 2 ? m : i + 2], f - i);
- }
- },
- Utils: {
- Linear: function (p0, p1, t) {
- return (p1 - p0) * t + p0;
- },
- Bernstein: function (n, i) {
- var fc = TWEEN.Interpolation.Utils.Factorial;
- return fc(n) / fc(i) / fc(n - i);
- },
- Factorial: (function () {
- var a = [1];
- return function (n) {
- var s = 1;
- if (a[n]) {
- return a[n];
- }
- for (var i = n; i > 1; i--) {
- s *= i;
- }
- a[n] = s;
- return s;
- };
- })(),
- CatmullRom: function (p0, p1, p2, p3, t) {
- var v0 = (p2 - p0) * 0.5;
- var v1 = (p3 - p1) * 0.5;
- var t2 = t * t;
- var t3 = t * t2;
- return (2 * p1 - 2 * p2 + v0 + v1) * t3 + (- 3 * p1 + 3 * p2 - 2 * v0 - v1) * t2 + v0 * t + p1;
- }
- }
- };
- // UMD (Universal Module Definition)
- (function (root) {
- if (typeof define === 'function' && define.amd) {
- // AMD
- define([], function () {
- return TWEEN;
- });
- } else if (typeof module !== 'undefined' && typeof exports === 'object') {
- // Node.js
- module.exports = TWEEN;
- } else if (root !== undefined) {
- // Global variable
- root.TWEEN = TWEEN;
- }
- })(this);
|