Nazariglez / pixi-tween

pixi-tween is a plugin for Pixi.js v3.0.8 or higher to create tween animations.
MIT License
98 stars 39 forks source link

Redundant code in elastic easing functions #1

Closed OlsonDev closed 8 years ago

OlsonDev commented 8 years ago

As far as I can tell, your elastic easing functions could be optimized by removing some redundant code. Note my inserted comments below:

const Easing = {
// snip
  inElastic: function(){
    return function(k){
      let s, a = 0.1, p = 0.4;
      if ( k === 0 ) return 0;
      if ( k === 1 ) return 1;
      // a is always less than 1; just set this stuff above and remove this if/else
      if ( !a || a < 1 ) { a = 1; s = p / 4; }
      else s = p * Math.asin( 1 / a ) / ( 2 * Math.PI );
      return - ( a * Math.pow( 2, 10 * (k-1) ) * Math.sin( ( (k-1) - s ) * ( 2 * Math.PI ) / p ) );
    };
  },

  outElastic: function(){
    return function(k){
      let s, a = 0.1, p = 0.4;
      if ( k === 0 ) return 0;
      if ( k === 1 ) return 1;
      // a is always less than 1; just set this stuff above and remove this if/else
      if ( !a || a < 1 ) { a = 1; s = p / 4; }
      else s = p * Math.asin( 1 / a ) / ( 2 * Math.PI );
      return ( a * Math.pow( 2, - 10 * k) * Math.sin( ( k - s ) * ( 2 * Math.PI ) / p ) + 1 );
    };
  },

  inOutElastic: function(){
    return function(k){
      let s, a = 0.1, p = 0.4;
      if ( k === 0 ) return 0;
      if ( k === 1 ) return 1;
      // a is always less than 1; just set this stuff above and remove this if/else
      if ( !a || a < 1 ) { a = 1; s = p / 4; }
      else s = p * Math.asin( 1 / a ) / ( 2 * Math.PI );
      k *= 2;
      if ( k < 1 ) return - 0.5 * ( a * Math.pow( 2, 10 * ( k - 1 ) ) * Math.sin( ( (k-1) - s ) * ( 2 * Math.PI ) / p ) );
      return a * Math.pow( 2, -10 * ( k - 1 ) ) * Math.sin( ( (k-1) - s ) * ( 2 * Math.PI ) / p ) * 0.5 + 1;
    };
  }
// snip
};
Nazariglez commented 8 years ago

@OlsonDev Good catch! I will fix this tomorrow, thanks!

Nazariglez commented 8 years ago

https://github.com/Nazariglez/pixi-tween/commit/dd8dea5eb4c1f12c4315196eaf075f52b915445c

Closed