All files / fx transitions.js

100% Statements 45/45
78.26% Branches 18/23
100% Functions 17/17
100% Lines 45/45

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136                                                            1x 1x   1x   1x 11x 11x   11x 8x   11x     8x     10x                 1x   1x       1x 1x 7x       1x   3x       3x       3x       5x       3x 3x         3x 3x 3x 2x 2x 2x   3x 3x       3x               1x 4x 3x       1x   2x 2x 1x 1x 1x 1x 1x     2x       1x  
/* eslint-disable no-mixed-operators */
/**
 * 动画运行方式库
 * - Pow
 * - Expo
 * - Circ
 * - Sine
 * - Back
 * - Bounce
 * - Elastic
 * - Quad
 * - Cubic
 * - Quart
 * - Quint
 * @module fx/transitions
 * @see [mootools/Fx.Transitions](https://mootools.net/core/docs/1.6.0/Fx/Fx.Transitions#Fx-Transitions)
 * @example
 * var $fx = require('@spore-ui/kit/packages/fx/fx');
 * var $transitions = require('@spore-ui/kit/packages/fx/transitions');
 * new $fx({
 *   transition : $transitions.Sine.easeInOut
 * });
 * new $fx({
 *   transition : 'Sine:In'
 * });
 * new $fx({
 *   transition : 'Sine:In:Out'
 * });
 */
 
var $type = require('../obj/type');
var $assign = require('../obj/assign');
 
var $fx = require('./fx');
 
$fx.Transition = function (transition, params) {
  Eif ($type(params) !== 'array') {
    params = [params];
  }
  var easeIn = function (pos) {
    return transition(pos, params);
  };
  return $assign(easeIn, {
    easeIn: easeIn,
    easeOut: function (pos) {
      return 1 - transition(1 - pos, params);
    },
    easeInOut: function (pos) {
      return (
        (pos <= 0.5
          ? transition(2 * pos, params)
          : 2 - transition(2 * (1 - pos), params)) / 2
      );
    },
  });
};
 
var Transitions = {
  linear: function (zero) {
    return zero;
  },
};
 
Transitions.extend = function (transitions) {
  Object.keys(transitions).forEach(function (transition) {
    Transitions[transition] = new $fx.Transition(transitions[transition]);
  });
};
 
Transitions.extend({
  Pow: function (p, x) {
    return Math.pow(p, (x && x[0]) || 6);
  },
 
  Expo: function (p) {
    return Math.pow(2, 8 * (p - 1));
  },
 
  Circ: function (p) {
    return 1 - Math.sin(Math.acos(p));
  },
 
  Sine: function (p) {
    return 1 - Math.cos(p * Math.PI / 2);
  },
 
  Back: function (p, x) {
    x = (x && x[0]) || 1.618;
    return Math.pow(p, 2) * ((x + 1) * p - x);
  },
 
  Bounce: function (p) {
    var value;
    var a = 0;
    var b = 1;
    while (p < (7 - 4 * a) / 11) {
      value = b * b - Math.pow((11 - 6 * a - 11 * p) / 4, 2);
      a += b;
      b /= 2;
    }
    value = b * b - Math.pow((11 - 6 * a - 11 * p) / 4, 2);
    return value;
  },
 
  Elastic: function (p, x) {
    return (
      // eslint-disable-next-line no-plusplus
      Math.pow(2, 10 * --p)
      * Math.cos(20 * p * Math.PI * ((x && x[0]) || 1) / 3)
    );
  },
});
 
['Quad', 'Cubic', 'Quart', 'Quint'].forEach(function (transition, i) {
  Transitions[transition] = new $fx.Transition(function (p) {
    return Math.pow(p, i + 2);
  });
});
 
$fx.statics({
  getTransition: function () {
    var trans = this.options.transition || Transitions.Sine.easeInOut;
    if (typeof trans === 'string') {
      var data = trans.split(':');
      trans = Transitions;
      trans = trans[data[0]] || trans[data[0]];
      Eif (data[1]) {
        trans = trans['ease' + data[1] + (data[2] ? data[2] : '')];
      }
    }
    return trans;
  },
});
 
module.exports = Transitions;