BezierEaser.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* eslint-disable */
  2. const BezierFactory = (function () {
  3. /**
  4. * BezierEasing - use bezier curve for transition easing function
  5. * by Gaëtan Renaudeau 2014 - 2015 – MIT License
  6. *
  7. * Credits: is based on Firefox's nsSMILKeySpline.cpp
  8. * Usage:
  9. * var spline = BezierEasing([ 0.25, 0.1, 0.25, 1.0 ])
  10. * spline.get(x) => returns the easing value | x must be in [0, 1] range
  11. *
  12. */
  13. var ob = {};
  14. ob.getBezierEasing = getBezierEasing;
  15. var beziers = {};
  16. function getBezierEasing(a, b, c, d, nm) {
  17. var str = nm || ('bez_' + a + '_' + b + '_' + c + '_' + d).replace(/\./g, 'p');
  18. if (beziers[str]) {
  19. return beziers[str];
  20. }
  21. var bezEasing = new BezierEasing([a, b, c, d]);
  22. beziers[str] = bezEasing;
  23. return bezEasing;
  24. }
  25. // These values are established by empiricism with tests (tradeoff: performance VS precision)
  26. var NEWTON_ITERATIONS = 4;
  27. var NEWTON_MIN_SLOPE = 0.001;
  28. var SUBDIVISION_PRECISION = 0.0000001;
  29. var SUBDIVISION_MAX_ITERATIONS = 10;
  30. var kSplineTableSize = 11;
  31. var kSampleStepSize = 1.0 / (kSplineTableSize - 1.0);
  32. var float32ArraySupported = typeof Float32Array === 'function';
  33. function A(aA1, aA2) { return 1.0 - 3.0 * aA2 + 3.0 * aA1; }
  34. function B(aA1, aA2) { return 3.0 * aA2 - 6.0 * aA1; }
  35. function C(aA1) { return 3.0 * aA1; }
  36. // Returns x(t) given t, x1, and x2, or y(t) given t, y1, and y2.
  37. function calcBezier(aT, aA1, aA2) {
  38. return ((A(aA1, aA2) * aT + B(aA1, aA2)) * aT + C(aA1)) * aT;
  39. }
  40. // Returns dx/dt given t, x1, and x2, or dy/dt given t, y1, and y2.
  41. function getSlope(aT, aA1, aA2) {
  42. return 3.0 * A(aA1, aA2) * aT * aT + 2.0 * B(aA1, aA2) * aT + C(aA1);
  43. }
  44. function binarySubdivide(aX, aA, aB, mX1, mX2) {
  45. var currentX,
  46. currentT,
  47. i = 0;
  48. do {
  49. currentT = aA + (aB - aA) / 2.0;
  50. currentX = calcBezier(currentT, mX1, mX2) - aX;
  51. if (currentX > 0.0) {
  52. aB = currentT;
  53. } else {
  54. aA = currentT;
  55. }
  56. } while (Math.abs(currentX) > SUBDIVISION_PRECISION && ++i < SUBDIVISION_MAX_ITERATIONS);
  57. return currentT;
  58. }
  59. function newtonRaphsonIterate(aX, aGuessT, mX1, mX2) {
  60. for (var i = 0; i < NEWTON_ITERATIONS; ++i) {
  61. var currentSlope = getSlope(aGuessT, mX1, mX2);
  62. if (currentSlope === 0.0) return aGuessT;
  63. var currentX = calcBezier(aGuessT, mX1, mX2) - aX;
  64. aGuessT -= currentX / currentSlope;
  65. }
  66. return aGuessT;
  67. }
  68. /**
  69. * points is an array of [ mX1, mY1, mX2, mY2 ]
  70. */
  71. function BezierEasing(points) {
  72. this._p = points;
  73. this._mSampleValues = float32ArraySupported ? new Float32Array(kSplineTableSize) : new Array(kSplineTableSize);
  74. this._precomputed = false;
  75. this.get = this.get.bind(this);
  76. }
  77. BezierEasing.prototype = {
  78. get: function (x) {
  79. var mX1 = this._p[0],
  80. mY1 = this._p[1],
  81. mX2 = this._p[2],
  82. mY2 = this._p[3];
  83. if (!this._precomputed) this._precompute();
  84. if (mX1 === mY1 && mX2 === mY2) return x; // linear
  85. // Because JavaScript number are imprecise, we should guarantee the extremes are right.
  86. if (x === 0) return 0;
  87. if (x === 1) return 1;
  88. return calcBezier(this._getTForX(x), mY1, mY2);
  89. },
  90. // Private part
  91. _precompute: function () {
  92. var mX1 = this._p[0],
  93. mY1 = this._p[1],
  94. mX2 = this._p[2],
  95. mY2 = this._p[3];
  96. this._precomputed = true;
  97. if (mX1 !== mY1 || mX2 !== mY2) { this._calcSampleValues(); }
  98. },
  99. _calcSampleValues: function () {
  100. var mX1 = this._p[0],
  101. mX2 = this._p[2];
  102. for (var i = 0; i < kSplineTableSize; ++i) {
  103. this._mSampleValues[i] = calcBezier(i * kSampleStepSize, mX1, mX2);
  104. }
  105. },
  106. /**
  107. * getTForX chose the fastest heuristic to determine the percentage value precisely from a given X projection.
  108. */
  109. _getTForX: function (aX) {
  110. var mX1 = this._p[0],
  111. mX2 = this._p[2],
  112. mSampleValues = this._mSampleValues;
  113. var intervalStart = 0.0;
  114. var currentSample = 1;
  115. var lastSample = kSplineTableSize - 1;
  116. for (; currentSample !== lastSample && mSampleValues[currentSample] <= aX; ++currentSample) {
  117. intervalStart += kSampleStepSize;
  118. }
  119. --currentSample;
  120. // Interpolate to provide an initial guess for t
  121. var dist = (aX - mSampleValues[currentSample]) / (mSampleValues[currentSample + 1] - mSampleValues[currentSample]);
  122. var guessForT = intervalStart + dist * kSampleStepSize;
  123. var initialSlope = getSlope(guessForT, mX1, mX2);
  124. if (initialSlope >= NEWTON_MIN_SLOPE) {
  125. return newtonRaphsonIterate(aX, guessForT, mX1, mX2);
  126. } if (initialSlope === 0.0) {
  127. return guessForT;
  128. }
  129. return binarySubdivide(aX, intervalStart, intervalStart + kSampleStepSize, mX1, mX2);
  130. },
  131. };
  132. return ob;
  133. }());
  134. export default BezierFactory;