Path: blob/master/webroot/rsrc/externals/javelin/ext/fx/FX.js
12242 views
/**1* @provides javelin-fx2* @requires javelin-color javelin-install javelin-util3* @javelin4*5* Based on moo.fx (moofx.mad4milk.net).6*/78JX.install('FX', {910events: ['start', 'complete'],1112construct: function(element) {13this._config = {};14this.setElement(element);15this.setTransition(JX.FX.Transitions.sine);16},1718properties: {19fps: 50,20wait: true,21duration: 500,22element: null,23property: null,24transition: null25},2627members: {28_to: null,29_now: null,30_from: null,31_start: null,32_config: null,33_interval: null,3435start: function(config) {36if (__DEV__) {37if (!config) {38throw new Error('What styles do you want to animate?');39}40if (!this.getElement()) {41throw new Error('What element do you want to animate?');42}43}44if (this._interval && this.getWait()) {45return;46}47var from = {};48var to = {};49for (var prop in config) {50from[prop] = config[prop][0];51to[prop] = config[prop][1];52if (/color/i.test(prop)) {53from[prop] = JX.Color.hexToRgb(from[prop], true);54to[prop] = JX.Color.hexToRgb(to[prop], true);55}56}57this._animate(from, to);58return this;59},6061stop: function() {62clearInterval(this._interval);63this._interval = null;64return this;65},6667then: function(func) {68var token = this.listen('complete', function() {69token.remove();70func();71});72return this;73},7475_animate: function(from, to) {76if (!this.getWait()) {77this.stop();78}79if (this._interval) {80return;81}82setTimeout(JX.bind(this, this.invoke, 'start'), 10);83this._from = from;84this._to = to;85this._start = JX.now();86this._interval = setInterval(87JX.bind(this, this._tween),88Math.round(1000 / this.getFps()));8990// Immediately update to the initial values.91this._tween();92},9394_tween: function() {95var now = JX.now();96var prop;97if (now < this._start + this.getDuration()) {98this._now = now - this._start;99for (prop in this._from) {100this._config[prop] = this._compute(this._from[prop], this._to[prop]);101}102} else {103setTimeout(JX.bind(this, this.invoke, 'complete'), 10);104105// Compute the final position using the transition function, in case106// the function applies transformations.107this._now = this.getDuration();108for (prop in this._from) {109this._config[prop] = this._compute(this._from[prop], this._to[prop]);110}111this.stop();112}113this._render();114},115116_compute: function(from, to) {117if (JX.isArray(from)) {118return from.map(function(value, ii) {119return Math.round(this._compute(value, to[ii]));120}, this);121}122var delta = to - from;123return this.getTransition()(this._now, from, delta, this.getDuration());124},125126_render: function() {127var style = this.getElement().style;128for (var prop in this._config) {129var value = this._config[prop];130if (prop == 'opacity') {131value = parseInt(100 * value, 10);132if (window.ActiveXObject) {133style.filter = 'alpha(opacity=' + value + ')';134} else {135style.opacity = value / 100;136}137} else if (/color/i.test(prop)) {138style[prop] = 'rgb(' + value + ')';139} else {140style[prop] = value + 'px';141}142}143}144},145146statics: {147fade: function(element, visible) {148return new JX.FX(element).setDuration(250).start({149opacity: visible ? [0, 1] : [1, 0]150});151},152153highlight: function(element, color) {154color = color || '#fff8dd';155return new JX.FX(element).setDuration(1000).start({156backgroundColor: [color, '#fff']157});158},159160/**161* Easing equations based on work by Robert Penner162* http://www.robertpenner.com/easing/163*/164Transitions: {165linear: function(t, b, c, d) {166return c * t / d + b;167},168169sine: function(t, b, c, d) {170return -c / 2 * (Math.cos(Math.PI * t / d) - 1) + b;171},172173sineIn: function(t, b, c, d) {174if (t == d) {175return c + b;176}177return -c * Math.cos(t / d * (Math.PI / 2)) + c + b;178},179180sineOut: function(t, b, c, d) {181if (t == d) {182return c + b;183}184return c * Math.sin(t / d * (Math.PI / 2)) + b;185},186187elastic: function(t, b, c, d, a, p) {188if (t === 0) { return b; }189if ((t /= d) == 1) { return b + c; }190if (!p) { p = d * 0.3; }191if (!a) { a = 1; }192var s;193if (a < Math.abs(c)) {194a = c;195s = p / 4;196} else {197s = p / (2 * Math.PI) * Math.asin(c / a);198}199return a * Math.pow(2, -10 * t) *200Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b;201},202203bounce: function(t, b, c, d) {204if ((t /= d) < (1 / 2.75)) {205return c * (7.5625 * t * t) + b;206} else if (t < (2 / 2.75)) {207return c * (7.5625 * (t -= (1.5 / 2.75)) * t + 0.75) + b;208} else if (t < (2.5 / 2.75)) {209return c * (7.5625 * (t -= (2.25 / 2.75)) * t + 0.9375) + b;210} else {211return c * (7.5625 * (t -= (2.625 / 2.75)) * t + 0.984375) + b;212}213}214}215}216});217218219