tdesign-mobile-vue
Version:
tdesign-mobile-vue
157 lines (149 loc) • 5.62 kB
JavaScript
/**
* tdesign v1.7.0
* (c) 2024 TDesign Group
* @license MIT
*/
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
var _slicedToArray = require('@babel/runtime/helpers/slicedToArray');
var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
var _createClass = require('@babel/runtime/helpers/createClass');
var _defineProperty = require('@babel/runtime/helpers/defineProperty');
var _common_js_utils_helper = require('../utils/helper.js');
require('@babel/runtime/helpers/toConsumableArray');
require('@babel/runtime/helpers/objectWithoutProperties');
require('lodash/isString');
require('lodash/isNull');
require('lodash/isUndefined');
require('lodash/isNumber');
require('lodash/isArray');
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
var _slicedToArray__default = /*#__PURE__*/_interopDefaultLegacy(_slicedToArray);
var _classCallCheck__default = /*#__PURE__*/_interopDefaultLegacy(_classCallCheck);
var _createClass__default = /*#__PURE__*/_interopDefaultLegacy(_createClass);
var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
var quartOut = function quartOut(t) {
return 1 - Math.abs(Math.pow(t - 1, 4));
};
var Tween = /*#__PURE__*/function () {
function Tween(_ref) {
var _this = this;
var from = _ref.from,
to = _ref.to,
_ref$duration = _ref.duration,
duration = _ref$duration === void 0 ? 200 : _ref$duration,
onStart = _ref.onStart,
_ref$onUpdate = _ref.onUpdate,
onUpdate = _ref$onUpdate === void 0 ? function () {} : _ref$onUpdate,
onFinish = _ref.onFinish;
_classCallCheck__default["default"](this, Tween);
_defineProperty__default["default"](this, "from", void 0);
_defineProperty__default["default"](this, "to", void 0);
_defineProperty__default["default"](this, "duration", void 0);
_defineProperty__default["default"](this, "onStart", void 0);
_defineProperty__default["default"](this, "onUpdate", void 0);
_defineProperty__default["default"](this, "onFinish", void 0);
_defineProperty__default["default"](this, "startTime", void 0);
_defineProperty__default["default"](this, "started", void 0);
_defineProperty__default["default"](this, "finished", void 0);
_defineProperty__default["default"](this, "timer", void 0);
_defineProperty__default["default"](this, "keys", void 0);
_defineProperty__default["default"](this, "time", 0);
_defineProperty__default["default"](this, "elapsed", 0);
this.from = from;
this.to = to;
this.duration = duration;
this.onStart = onStart;
this.onUpdate = onUpdate;
this.onFinish = onFinish;
this.startTime = Date.now();
this.started = false;
this.finished = false;
this.timer = null;
this.keys = {};
Object.entries(from).forEach(function (_ref2) {
var _ref3 = _slicedToArray__default["default"](_ref2, 2),
key = _ref3[0],
value = _ref3[1];
if (_this.to[key] === void 0) {
_this.to[key] = value;
}
});
Object.entries(to).forEach(function (_ref4) {
var _ref5 = _slicedToArray__default["default"](_ref4, 2),
key = _ref5[0],
value = _ref5[1];
if (_this.from[key] === void 0) {
_this.from[key] = value;
}
});
}
return _createClass__default["default"](Tween, [{
key: "update",
value: function update() {
var _this2 = this;
this.time = Date.now();
if (this.time < this.startTime || this.finished) return;
if (this.elapsed >= this.duration) {
var _this$onFinish;
this.finished = true;
(_this$onFinish = this.onFinish) === null || _this$onFinish === void 0 || _this$onFinish.call(this, this.keys);
return;
}
var elapsed = Math.min(this.time - this.startTime, this.duration);
this.elapsed = elapsed;
var progress = quartOut(elapsed / this.duration);
Object.keys(this.to).forEach(function (key) {
var delta = _this2.to[key] - _this2.from[key];
_this2.keys[key] = _this2.from[key] + delta * progress;
});
if (!this.started) {
var _this$onStart;
(_this$onStart = this.onStart) === null || _this$onStart === void 0 || _this$onStart.call(this, this.keys);
this.started = true;
}
this.onUpdate(this.keys);
}
}, {
key: "polyfillStart",
value: function polyfillStart() {
var _this3 = this;
var elapsed = Date.now() - this.startTime;
var interval = quartOut(elapsed / this.duration);
this.timer = setInterval(function () {
_this3.update();
if (_this3.finished) {
clearInterval(_this3.timer);
}
}, interval);
}
}, {
key: "normalStart",
value: function normalStart() {
var _this4 = this;
var _tick = function tick() {
_this4.update();
_this4.timer = requestAnimationFrame(_tick);
if (_this4.finished) {
cancelAnimationFrame(_this4.timer);
_this4.timer = null;
}
};
_tick();
}
}, {
key: "start",
value: function start() {
this.startTime = Date.now();
if (_common_js_utils_helper.getIEVersion() < 10) this.polyfillStart();else this.normalStart();
}
}, {
key: "stop",
value: function stop() {
if (_common_js_utils_helper.getIEVersion() < 10) clearInterval(this.timer);else cancelAnimationFrame(this.timer);
this.timer = null;
}
}]);
}();
exports["default"] = Tween;
//# sourceMappingURL=tween.js.map