Blame view

天文台pc/tianwentai-ui/node_modules/motion-utils/dist/es/easing/cubic-bezier.mjs 1.96 KB
bc518174   王天杨   提交两个项目文件
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
  import { noop } from '../noop.mjs';
  
  /*
    Bezier function generator
    This has been modified from Gaëtan Renaudeau's BezierEasing
    https://github.com/gre/bezier-easing/blob/master/src/index.js
    https://github.com/gre/bezier-easing/blob/master/LICENSE
    
    I've removed the newtonRaphsonIterate algo because in benchmarking it
    wasn't noticeably faster than binarySubdivision, indeed removing it
    usually improved times, depending on the curve.
    I also removed the lookup table, as for the added bundle size and loop we're
    only cutting ~4 or so subdivision iterations. I bumped the max iterations up
    to 12 to compensate and this still tended to be faster for no perceivable
    loss in accuracy.
    Usage
      const easeOut = cubicBezier(.17,.67,.83,.67);
      const x = easeOut(0.5); // returns 0.627...
  */
  // Returns x(t) given t, x1, and x2, or y(t) given t, y1, and y2.
  const calcBezier = (t, a1, a2) => (((1.0 - 3.0 * a2 + 3.0 * a1) * t + (3.0 * a2 - 6.0 * a1)) * t + 3.0 * a1) *
      t;
  const subdivisionPrecision = 0.0000001;
  const subdivisionMaxIterations = 12;
  function binarySubdivide(x, lowerBound, upperBound, mX1, mX2) {
      let currentX;
      let currentT;
      let i = 0;
      do {
          currentT = lowerBound + (upperBound - lowerBound) / 2.0;
          currentX = calcBezier(currentT, mX1, mX2) - x;
          if (currentX > 0.0) {
              upperBound = currentT;
          }
          else {
              lowerBound = currentT;
          }
      } while (Math.abs(currentX) > subdivisionPrecision &&
          ++i < subdivisionMaxIterations);
      return currentT;
  }
  function cubicBezier(mX1, mY1, mX2, mY2) {
      // If this is a linear gradient, return linear easing
      if (mX1 === mY1 && mX2 === mY2)
          return noop;
      const getTForX = (aX) => binarySubdivide(aX, 0, 1, mX1, mX2);
      // If animation is at start/end, return t without easing
      return (t) => t === 0 || t === 1 ? t : calcBezier(getTForX(t), mY1, mY2);
  }
  
  export { cubicBezier };
  //# sourceMappingURL=cubic-bezier.mjs.map