| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.default = exports.alpha = void 0;
|
|---|
| 7 | var _util = require("./util");
|
|---|
| 8 | 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; }
|
|---|
| 9 | 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; }
|
|---|
| 10 | function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|---|
| 11 | function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|---|
| 12 | function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|---|
| 13 | var alpha = (begin, end, k) => begin + (end - begin) * k;
|
|---|
| 14 | exports.alpha = alpha;
|
|---|
| 15 | var needContinue = _ref => {
|
|---|
| 16 | var {
|
|---|
| 17 | from,
|
|---|
| 18 | to
|
|---|
| 19 | } = _ref;
|
|---|
| 20 | return from !== to;
|
|---|
| 21 | };
|
|---|
| 22 | /*
|
|---|
| 23 | * @description: cal new from value and velocity in each stepper
|
|---|
| 24 | * @return: { [styleProperty]: { from, to, velocity } }
|
|---|
| 25 | */
|
|---|
| 26 | var calStepperVals = (easing, preVals, steps) => {
|
|---|
| 27 | var nextStepVals = (0, _util.mapObject)((key, val) => {
|
|---|
| 28 | if (needContinue(val)) {
|
|---|
| 29 | var [newX, newV] = easing(val.from, val.to, val.velocity);
|
|---|
| 30 | return _objectSpread(_objectSpread({}, val), {}, {
|
|---|
| 31 | from: newX,
|
|---|
| 32 | velocity: newV
|
|---|
| 33 | });
|
|---|
| 34 | }
|
|---|
| 35 | return val;
|
|---|
| 36 | }, preVals);
|
|---|
| 37 | if (steps < 1) {
|
|---|
| 38 | return (0, _util.mapObject)((key, val) => {
|
|---|
| 39 | if (needContinue(val) && nextStepVals[key] != null) {
|
|---|
| 40 | return _objectSpread(_objectSpread({}, val), {}, {
|
|---|
| 41 | velocity: alpha(val.velocity, nextStepVals[key].velocity, steps),
|
|---|
| 42 | from: alpha(val.from, nextStepVals[key].from, steps)
|
|---|
| 43 | });
|
|---|
| 44 | }
|
|---|
| 45 | return val;
|
|---|
| 46 | }, preVals);
|
|---|
| 47 | }
|
|---|
| 48 | return calStepperVals(easing, nextStepVals, steps - 1);
|
|---|
| 49 | };
|
|---|
| 50 | function createStepperUpdate(from, to, easing, interKeys, render, timeoutController) {
|
|---|
| 51 | var preTime;
|
|---|
| 52 | var stepperStyle = interKeys.reduce((res, key) => _objectSpread(_objectSpread({}, res), {}, {
|
|---|
| 53 | [key]: {
|
|---|
| 54 | from: from[key],
|
|---|
| 55 | velocity: 0,
|
|---|
| 56 | to: to[key]
|
|---|
| 57 | }
|
|---|
| 58 | }), {});
|
|---|
| 59 | var getCurrStyle = () => (0, _util.mapObject)((key, val) => val.from, stepperStyle);
|
|---|
| 60 | var shouldStopAnimation = () => !Object.values(stepperStyle).filter(needContinue).length;
|
|---|
| 61 | var stopAnimation = null;
|
|---|
| 62 | var stepperUpdate = now => {
|
|---|
| 63 | if (!preTime) {
|
|---|
| 64 | preTime = now;
|
|---|
| 65 | }
|
|---|
| 66 | var deltaTime = now - preTime;
|
|---|
| 67 | var steps = deltaTime / easing.dt;
|
|---|
| 68 | stepperStyle = calStepperVals(easing, stepperStyle, steps);
|
|---|
| 69 | // get union set and add compatible prefix
|
|---|
| 70 | render(_objectSpread(_objectSpread(_objectSpread({}, from), to), getCurrStyle()));
|
|---|
| 71 | preTime = now;
|
|---|
| 72 | if (!shouldStopAnimation()) {
|
|---|
| 73 | stopAnimation = timeoutController.setTimeout(stepperUpdate);
|
|---|
| 74 | }
|
|---|
| 75 | };
|
|---|
| 76 |
|
|---|
| 77 | // return start animation method
|
|---|
| 78 | return () => {
|
|---|
| 79 | stopAnimation = timeoutController.setTimeout(stepperUpdate);
|
|---|
| 80 |
|
|---|
| 81 | // return stop animation method
|
|---|
| 82 | return () => {
|
|---|
| 83 | var _stopAnimation;
|
|---|
| 84 | (_stopAnimation = stopAnimation) === null || _stopAnimation === void 0 || _stopAnimation();
|
|---|
| 85 | };
|
|---|
| 86 | };
|
|---|
| 87 | }
|
|---|
| 88 | function createTimingUpdate(from, to, easing, duration, interKeys, render, timeoutController) {
|
|---|
| 89 | var stopAnimation = null;
|
|---|
| 90 | var timingStyle = interKeys.reduce((res, key) => {
|
|---|
| 91 | var fromElement = from[key];
|
|---|
| 92 | var toElement = to[key];
|
|---|
| 93 | if (fromElement == null || toElement == null) {
|
|---|
| 94 | return res;
|
|---|
| 95 | }
|
|---|
| 96 | return _objectSpread(_objectSpread({}, res), {}, {
|
|---|
| 97 | [key]: [fromElement, toElement]
|
|---|
| 98 | });
|
|---|
| 99 | }, {});
|
|---|
| 100 | var beginTime;
|
|---|
| 101 | var timingUpdate = now => {
|
|---|
| 102 | if (!beginTime) {
|
|---|
| 103 | beginTime = now;
|
|---|
| 104 | }
|
|---|
| 105 | var t = (now - beginTime) / duration;
|
|---|
| 106 | var currStyle = (0, _util.mapObject)((key, val) => alpha(...val, easing(t)), timingStyle);
|
|---|
| 107 |
|
|---|
| 108 | // get union set and add compatible prefix
|
|---|
| 109 | render(_objectSpread(_objectSpread(_objectSpread({}, from), to), currStyle));
|
|---|
| 110 | if (t < 1) {
|
|---|
| 111 | stopAnimation = timeoutController.setTimeout(timingUpdate);
|
|---|
| 112 | } else {
|
|---|
| 113 | var finalStyle = (0, _util.mapObject)((key, val) => alpha(...val, easing(1)), timingStyle);
|
|---|
| 114 | render(_objectSpread(_objectSpread(_objectSpread({}, from), to), finalStyle));
|
|---|
| 115 | }
|
|---|
| 116 | };
|
|---|
| 117 |
|
|---|
| 118 | // return start animation method
|
|---|
| 119 | return () => {
|
|---|
| 120 | stopAnimation = timeoutController.setTimeout(timingUpdate);
|
|---|
| 121 |
|
|---|
| 122 | // return stop animation method
|
|---|
| 123 | return () => {
|
|---|
| 124 | var _stopAnimation2;
|
|---|
| 125 | (_stopAnimation2 = stopAnimation) === null || _stopAnimation2 === void 0 || _stopAnimation2();
|
|---|
| 126 | };
|
|---|
| 127 | };
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | // configure update function
|
|---|
| 131 | // eslint-disable-next-line import/no-default-export
|
|---|
| 132 | var _default = (from, to, easing, duration, render, timeoutController) => {
|
|---|
| 133 | var interKeys = (0, _util.getIntersectionKeys)(from, to);
|
|---|
| 134 | if (easing == null) {
|
|---|
| 135 | // no animation, just set to final state
|
|---|
| 136 | return () => {
|
|---|
| 137 | render(_objectSpread(_objectSpread({}, from), to));
|
|---|
| 138 | return () => {};
|
|---|
| 139 | };
|
|---|
| 140 | }
|
|---|
| 141 | return easing.isStepper === true ? createStepperUpdate(from, to, easing, interKeys, render, timeoutController) : createTimingUpdate(from, to, easing, duration, interKeys, render, timeoutController);
|
|---|
| 142 | };
|
|---|
| 143 | exports.default = _default; |
|---|