UNPKG

5.25 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5var _typeof = require("@babel/runtime/helpers/typeof");
6
7Object.defineProperty(exports, "__esModule", {
8 value: true
9});
10exports.default = void 0;
11
12var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
13
14var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
15
16var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
17
18var _interpolate = _interopRequireDefault(require("./interpolate"));
19
20var Easing = _interopRequireWildcard(require("./easing"));
21
22var _jsx = require("../../jsx");
23
24var _util = require("@antv/util");
25
26function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
27
28function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
29
30var Animator = /*#__PURE__*/function () {
31 function Animator(element, animation) {
32 (0, _classCallCheck2.default)(this, Animator);
33 // 是否是裁剪动画
34 this.isClip = false;
35 this.end = false;
36 this.element = element;
37 this.animation = animation;
38 var _animation$property = animation.property,
39 property = _animation$property === void 0 ? [] : _animation$property,
40 easing = animation.easing,
41 duration = animation.duration,
42 _animation$delay = animation.delay,
43 delay = _animation$delay === void 0 ? 0 : _animation$delay,
44 start = animation.start,
45 end = animation.end,
46 onFrame = animation.onFrame,
47 isClip = animation.isClip;
48 var interpolates = property.map(function (name) {
49 if ((0, _util.isString)(name)) {
50 return (0, _interpolate.default)(start[name], end[name]);
51 } // @ts-ignore
52
53
54 if (name.interpolate) {
55 // @ts-ignore
56 return name.interpolate(start, end);
57 }
58 });
59 this.easing = typeof easing === 'function' ? easing : Easing[easing] || Easing.linear;
60 this.property = property;
61 this.interpolates = interpolates;
62 this.duration = duration;
63 this.delay = delay;
64 this.onFrame = onFrame;
65 this.totalDuration = duration + delay;
66 this.isClip = isClip; // 更新到初始状态
67
68 this.update(0, 0);
69 }
70
71 (0, _createClass2.default)(Animator, [{
72 key: "to",
73 value: function to(time) {
74 var duration = this.duration,
75 delay = this.delay,
76 totalDuration = this.totalDuration,
77 easing = this.easing,
78 end = this.end; // 已结束
79
80 if (end) {
81 return;
82 } // 未开始
83
84
85 if (time <= delay || !duration) {
86 return;
87 } // 最大为1
88
89
90 var t = time >= totalDuration ? 1 : (time - delay) / duration;
91 this.update(easing(t), time); // 最后一帧
92
93 if (t === 1) {
94 this.onEnd();
95 }
96 }
97 }, {
98 key: "update",
99 value: function update(t, time) {
100 var element = this.element,
101 interpolates = this.interpolates,
102 property = this.property,
103 onFrame = this.onFrame;
104 var attrs = {};
105
106 for (var i = property.length - 1; i >= 0; i--) {
107 var name = property[i];
108
109 if ((0, _util.isString)(name)) {
110 attrs[name] = interpolates[i](t);
111 } else {
112 // @ts-ignore
113 attrs[name.name] = interpolates[i](t);
114 }
115 }
116
117 if (onFrame) {
118 attrs = (0, _objectSpread2.default)((0, _objectSpread2.default)({}, attrs), this.onFrame(t, time));
119 }
120
121 element.attr(attrs);
122 }
123 }, {
124 key: "onEnd",
125 value: function onEnd() {
126 var animation = this.animation,
127 isClip = this.isClip,
128 element = this.element;
129 var onEnd = animation.onEnd;
130 onEnd && onEnd.call(this);
131
132 if (isClip) {
133 // 如果是裁剪区动画,要移除裁剪区
134 element.remove(true);
135 } // 如果当前元素状态被标记为删除,等动画结束后直接删除
136
137
138 if (element._attrs.status === _jsx.ElementStatus.ELEMENT_DELETE) {
139 element.remove(true);
140 } // 清空 不需要重复执行
141
142
143 element.set('animation', null);
144 this.end = true;
145 }
146 }]);
147 return Animator;
148}();
149
150var _default = Animator;
151exports.default = _default;
\No newline at end of file