-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathanimation.ts
More file actions
372 lines (310 loc) · 9.11 KB
/
Copy pathanimation.ts
File metadata and controls
372 lines (310 loc) · 9.11 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import Children from '../../children';
import { VNode } from '../vnode';
import { DisplayObject, IChildNode, convertToPath } from '@antv/g-lite';
import { createShape } from './createShape';
import { Shape } from '../workTags';
import findClosestShapeNode from './findClosestShapeNode';
import Animator from './animator';
import applyStyle from './applyStyle';
import { isEqual } from '@antv/util';
function findAllShapeNode(vNode: VNode | VNode[] | null) {
const shapeNodes = [];
Children.map(vNode, (node: VNode) => {
if (!node) return;
const { tag, type, children } = node;
if (tag === Shape && type !== 'group') {
shapeNodes.push(node);
}
if (children) {
shapeNodes.push(...findAllShapeNode(children));
}
});
return shapeNodes;
}
function morphShape(lastNode: VNode, nextNode: VNode, animator?: Animator) {
const { props: nextProps, shape: nextShape, style: nextStyle, context } = nextNode;
const { shape: lastShape, style: lastStyle, animator: lastAnimation } = lastNode;
// 形变动画之前先把原 shape 销毁
lastShape.destroy();
const { animate, animation } = nextProps;
const animationEffect = animation ? animation.update : null;
if (animate === false || !animationEffect) {
return animator;
}
animator = animator || new Animator(context.timeline);
// shape 形变
const { start, end, property = [] } = animationEffect;
const { parsedStyle: nextParsedStyle } = nextShape;
const { parsedStyle: lastParsedStyle } = lastShape;
const lastPath = convertToPath(lastShape);
const nextPath = convertToPath(nextShape);
const startStyle = {
...lastStyle,
...start,
d: lastPath,
};
const endStyle = {
...nextStyle,
...end,
d: nextPath,
};
const pathShape = createShape('path', { style: { ...startStyle, d: '' } });
// 形变双方都有的属性才能动画
const animateProperty = property
.filter((key) => {
return nextParsedStyle.hasOwnProperty(key) && lastParsedStyle.hasOwnProperty(key);
})
.concat(['d']);
animator.animate(pathShape, startStyle, endStyle, {
...animationEffect,
property: animateProperty,
});
const { timeline } = nextNode?.context;
timeline && timeline.delete(lastAnimation);
animator.once('end', () => {
if (nextShape.destroyed) {
return;
}
applyStyle(nextShape, endStyle);
pathShape.replaceWith(nextShape);
});
return animator;
}
function appearAnimation(vNode: VNode | VNode[] | null) {
return Children.map(vNode, (node) => {
if (!node) return;
const { tag, shape, style, children, animate, props, animator } = node;
animator.reset(shape);
// 有叶子节点,先执行叶子节点
animator.children = children ? createAnimation(node, children, null) : null;
// 不需要执行动画
if (animate === false || tag !== Shape) {
applyStyle(shape, style);
return animator;
}
const { animation } = props;
const animationEffect = animation ? animation.appear : null;
if (!animationEffect) {
// 没有动画直接应用样式
applyStyle(shape, style);
return animator;
}
const { start = {}, end } = animationEffect;
const endStyle = {
...style,
...end,
};
animator.animate(shape, start, endStyle, animationEffect);
return animator;
});
}
function updateAnimation(nextNode, lastNode) {
const {
tag: nextTag,
type: nextType,
style: nextStyle,
children: nextChildren,
props: nextProps,
shape: nextShape,
animator,
animate,
} = nextNode;
const {
tag: lastTag,
type: lastType,
style: lastStyle,
children: lastChildren,
shape: lastShape,
} = lastNode;
animator.reset(nextShape);
// 先处理叶子节点
animator.children = createAnimation(nextNode, nextChildren, lastChildren);
const { animation } = nextProps;
const animationEffect = animation ? animation.update : null;
// 类型相同
if (nextType === lastType) {
// 清除之前的样式
const resetStyle = lastStyle
? Object.keys(lastStyle).reduce((prev, cur) => {
prev[cur] = '';
return prev;
}, {})
: null;
// 需要更新的样式
const style = {
...resetStyle,
...nextStyle,
};
// 组件,直接更新
if (nextTag !== Shape) {
applyStyle(nextShape, style);
return animator;
}
// 样式无改变,无更新
if (isEqual(nextStyle, lastStyle)) {
return animator;
}
// 没有动画直接应用样式
if (animate === false || !animationEffect) {
applyStyle(nextShape, style);
return animator;
}
const { start, end } = animationEffect;
const startStyle = {
...lastStyle,
...start,
};
const endStyle = {
...style,
...end,
};
animator.animate(nextShape, startStyle, endStyle, animationEffect);
return animator;
}
// 无法处理形变
if (nextTag !== Shape || lastTag !== Shape) {
lastShape.destroy();
return animator;
}
// 从 shape 到 group
if (nextType === 'group') {
const shapeNodes = findAllShapeNode(nextNode.children);
return shapeNodes.map((node) => {
return morphShape(lastNode, node);
});
}
// 从 group 到 shape
if (lastType === 'group') {
const shapeNodes = findAllShapeNode(lastNode.children);
return shapeNodes.map((node) => {
return morphShape(node, nextNode);
});
}
// 没有动画直接应用样式
if (animate === false || !animationEffect) {
applyStyle(nextShape, nextStyle);
return animator;
}
return morphShape(lastNode, nextNode, animator);
}
function destroyAnimation(node: VNode) {
return Children.map(node, (vNode) => {
if (!vNode) return null;
const { tag, shape, children, animate, style, props, animator, context } = vNode;
const { timeline } = context;
if (shape.destroyed) {
return null;
}
// 重置
animator.reset(shape);
// 先处理叶子节点
const childrenAnimation = children
? Children.toArray(children)
.map((child) => {
return destroyAnimation(child);
})
.filter(Boolean)
: null;
// 不需要动画直接删除
if (animate === false) {
shape.destroy();
return animator;
}
const { animation } = props;
const animationEffect = animation ? animation.leave : null;
// 没有叶子节点的动画, 直接删除
if (!(childrenAnimation && childrenAnimation.length) && !animationEffect) {
shape.destroy();
return animator;
}
animator.children = childrenAnimation;
// 图形有动画
if (animationEffect && tag === Shape) {
const { start, end = {} } = animationEffect;
const startStyle = {
...style,
...start,
};
const endStyle = end;
animator.animate(shape, startStyle, endStyle, animationEffect);
timeline && timeline.delete(animator.animations);
}
// 动画结束后,删除图形(包括子元素动画)
animator.once('end', () => {
shape.destroy();
});
return animator;
});
}
function createAnimator(nextNode, lastNode) {
if (!nextNode && !lastNode) {
return null;
}
// delete 动画
if (!nextNode && lastNode) {
return destroyAnimation(lastNode);
}
// 如果有 transform 则从 transform 比
const { transform } = nextNode;
if (transform) {
const closestShapeNode = findClosestShapeNode(nextNode);
nextNode.transform = null;
closestShapeNode.transform = transform;
}
if (nextNode.transform) {
if (!lastNode) {
return updateAnimation(nextNode, nextNode.transform);
}
return [updateAnimation(nextNode, nextNode.transform), destroyAnimation(lastNode)];
}
// appear 动画
if (nextNode && !lastNode) {
return appearAnimation(nextNode);
}
// update 动画
return updateAnimation(nextNode, lastNode);
}
function insertShape(parent: DisplayObject, shape: DisplayObject, nextSibling: IChildNode) {
if (nextSibling) {
parent.insertBefore(shape, nextSibling);
} else {
parent.appendChild(shape);
}
}
// 处理 children 的动画
function createAnimation(
parent: VNode,
nextChildren: VNode | VNode[],
lastChildren: VNode | VNode[],
) {
if (!nextChildren && !lastChildren) {
return [];
}
const { shape: parentShape } = parent;
// 上一个处理的元素
let prevSibling: IChildNode;
const childrenAnimator: Animator[] = [];
Children.compare(nextChildren, lastChildren, (nextNode, lastNode) => {
// shape 层才执行动画
const animator = createAnimator(nextNode, lastNode);
Children.map(animator, (item: Animator) => {
if (!item) return;
childrenAnimator.push(item);
const { shape } = item;
if (!shape || shape.destroyed) return;
let nextSibling: IChildNode;
// 更新文档流
if (!prevSibling) {
nextSibling = parentShape.firstChild;
} else {
nextSibling = prevSibling.nextSibling;
}
if (nextSibling !== shape) {
insertShape(parentShape, shape, nextSibling);
}
prevSibling = shape;
});
});
return childrenAnimator;
}
export { createAnimation };