configUpdate.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
  2. function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
  3. function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  4. function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
  5. function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
  6. function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
  7. function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
  8. function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  9. function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
  10. function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
  11. function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
  12. function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
  13. function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
  14. function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
  15. function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
  16. function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
  17. import { getIntersectionKeys, mapObject } from './util';
  18. var alpha = function alpha(begin, end, k) {
  19. return begin + (end - begin) * k;
  20. };
  21. var needContinue = function needContinue(_ref) {
  22. var from = _ref.from,
  23. to = _ref.to;
  24. return from !== to;
  25. };
  26. /*
  27. * @description: cal new from value and velocity in each stepper
  28. * @return: { [styleProperty]: { from, to, velocity } }
  29. */
  30. var calStepperVals = function calStepperVals(easing, preVals, steps) {
  31. var nextStepVals = mapObject(function (key, val) {
  32. if (needContinue(val)) {
  33. var _easing = easing(val.from, val.to, val.velocity),
  34. _easing2 = _slicedToArray(_easing, 2),
  35. newX = _easing2[0],
  36. newV = _easing2[1];
  37. return _objectSpread(_objectSpread({}, val), {}, {
  38. from: newX,
  39. velocity: newV
  40. });
  41. }
  42. return val;
  43. }, preVals);
  44. if (steps < 1) {
  45. return mapObject(function (key, val) {
  46. if (needContinue(val)) {
  47. return _objectSpread(_objectSpread({}, val), {}, {
  48. velocity: alpha(val.velocity, nextStepVals[key].velocity, steps),
  49. from: alpha(val.from, nextStepVals[key].from, steps)
  50. });
  51. }
  52. return val;
  53. }, preVals);
  54. }
  55. return calStepperVals(easing, nextStepVals, steps - 1);
  56. };
  57. // configure update function
  58. export default (function (from, to, easing, duration, render) {
  59. var interKeys = getIntersectionKeys(from, to);
  60. var timingStyle = interKeys.reduce(function (res, key) {
  61. return _objectSpread(_objectSpread({}, res), {}, _defineProperty({}, key, [from[key], to[key]]));
  62. }, {});
  63. var stepperStyle = interKeys.reduce(function (res, key) {
  64. return _objectSpread(_objectSpread({}, res), {}, _defineProperty({}, key, {
  65. from: from[key],
  66. velocity: 0,
  67. to: to[key]
  68. }));
  69. }, {});
  70. var cafId = -1;
  71. var preTime;
  72. var beginTime;
  73. var update = function update() {
  74. return null;
  75. };
  76. var getCurrStyle = function getCurrStyle() {
  77. return mapObject(function (key, val) {
  78. return val.from;
  79. }, stepperStyle);
  80. };
  81. var shouldStopAnimation = function shouldStopAnimation() {
  82. return !Object.values(stepperStyle).filter(needContinue).length;
  83. };
  84. // stepper timing function like spring
  85. var stepperUpdate = function stepperUpdate(now) {
  86. if (!preTime) {
  87. preTime = now;
  88. }
  89. var deltaTime = now - preTime;
  90. var steps = deltaTime / easing.dt;
  91. stepperStyle = calStepperVals(easing, stepperStyle, steps);
  92. // get union set and add compatible prefix
  93. render(_objectSpread(_objectSpread(_objectSpread({}, from), to), getCurrStyle(stepperStyle)));
  94. preTime = now;
  95. if (!shouldStopAnimation()) {
  96. cafId = requestAnimationFrame(update);
  97. }
  98. };
  99. // t => val timing function like cubic-bezier
  100. var timingUpdate = function timingUpdate(now) {
  101. if (!beginTime) {
  102. beginTime = now;
  103. }
  104. var t = (now - beginTime) / duration;
  105. var currStyle = mapObject(function (key, val) {
  106. return alpha.apply(void 0, _toConsumableArray(val).concat([easing(t)]));
  107. }, timingStyle);
  108. // get union set and add compatible prefix
  109. render(_objectSpread(_objectSpread(_objectSpread({}, from), to), currStyle));
  110. if (t < 1) {
  111. cafId = requestAnimationFrame(update);
  112. } else {
  113. var finalStyle = mapObject(function (key, val) {
  114. return alpha.apply(void 0, _toConsumableArray(val).concat([easing(1)]));
  115. }, timingStyle);
  116. render(_objectSpread(_objectSpread(_objectSpread({}, from), to), finalStyle));
  117. }
  118. };
  119. update = easing.isStepper ? stepperUpdate : timingUpdate;
  120. // return start animation method
  121. return function () {
  122. requestAnimationFrame(update);
  123. // return stop animation method
  124. return function () {
  125. cancelAnimationFrame(cafId);
  126. };
  127. };
  128. });