-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathaction.ts
160 lines (144 loc) · 4.82 KB
/
action.ts
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
import { transform } from '@antv/matrix-util';
import { ShapeDrawCFG } from 'interface';
import { Coordinate, IGroup, IShape } from '../../dependents';
import { getAngle, getCoordinateClipCfg, getSectorPath } from './util';
/**
* 垂直方向的缩放动画
* @param shape
* @param animateCfg
* @param shapeModel
*/
export function scaleInY(shape: IShape | IGroup, animateCfg, coordinate: Coordinate, shapeModel: ShapeDrawCFG) {
const box = shape.getBBox();
const x = (box.minX + box.maxX) / 2;
const { points } = shapeModel;
// 数值如果为负值,那么应该从上往下生长,通过 shape 的关键点进行判断
const y = points[0].y - points[1].y <= 0 ? box.maxY : box.minY;
const v = [x, y, 1];
shape.applyToMatrix(v);
const matrix = transform(shape.getMatrix(), [['t', -x, -y], ['s', 1, 0.01], ['t', x, y]]);
shape.setMatrix(matrix);
const endState = {
matrix: transform(shape.getMatrix(), [['t', -x, -y], ['s', 1, 100], ['t', x, y]]),
};
shape.animate(endState, animateCfg);
}
/**
* x 方向的缩放动画
* @param shape
* @param animateCfg
* @param shapeModel
*/
export function scaleInX(shape: IShape | IGroup, animateCfg, coordinate: Coordinate, shapeModel: ShapeDrawCFG) {
const box = shape.getBBox();
const { points } = shapeModel;
// x 数值如果为负值,那么应该从右往左生长
const x = points[0].y - points[1].y > 0 ? box.maxX : box.minX;
const y = (box.minY + box.maxY) / 2;
const v = [x, y, 1];
shape.applyToMatrix(v);
const matrix = transform(shape.getMatrix(), [['t', -x, -y], ['s', 0.01, 1], ['t', x, y]]);
shape.setMatrix(matrix);
const endState = {
matrix: transform(shape.getMatrix(), [['t', -x, -y], ['s', 100, 1], ['t', x, y]]),
};
shape.animate(endState, animateCfg);
}
/**
* 渐隐动画
* @param shape
* @param animateCfg
* @param shapeModel
*/
export function fadeOut(shape: IShape | IGroup, animateCfg, coordinate: Coordinate, shapeModel: ShapeDrawCFG) {
const endState = {
fillOpacity: 0,
strokeOpacity: 0,
};
const { easing, duration, delay } = animateCfg;
shape.animate(
endState,
duration,
easing,
() => {
shape.remove(true);
},
delay
);
}
// TODO: 待 G 4.0 修复,关联 issue https://github.com/antvis/g/issues/218
export function clipIn(shape: IShape | IGroup, animateCfg, coordinate: Coordinate, shapeModel: ShapeDrawCFG) {
const clipCfg = getCoordinateClipCfg(coordinate); // 根据坐标系类型获取整体的剪切区域配置信息
const endState = clipCfg.endState;
// 为 shape 设置剪切区域
const clipShape = shape.setClip(clipCfg);
// 对剪切图形做动画
const { easing, duration, delay } = animateCfg;
clipShape.animate(
endState,
duration,
easing,
() => {
if (shape && !shape.get('destroyed')) {
shape.set('clipShape', null);
}
},
delay
);
}
// TODO: 待 G 4.0 修复,关联 issue https://github.com/antvis/g/issues/218
export function pieChartEnter(shape: IShape | IGroup, animateCfg, coordinate: Coordinate, shapeModel: ShapeDrawCFG) {
const { endAngle } = getAngle(shapeModel, coordinate);
const coordinateStartAngle = coordinate.startAngle;
const center = coordinate.getCenter();
// @ts-ignore FIXME: coordinate 支持下
const radius = coordinate.getRadius();
const startPath = getSectorPath(center.x, center.y, radius, coordinateStartAngle, coordinateStartAngle);
const clipShape = shape.setClip({
type: 'path',
attrs: {
path: startPath,
},
});
const { easing, duration, delay } = animateCfg;
clipShape.animate(
(ratio) => {
const diff = (endAngle - coordinateStartAngle) * ratio + coordinateStartAngle;
const path = getSectorPath(center.x, center.y, radius, coordinateStartAngle, diff);
return {
path,
};
},
duration,
easing,
() => {
if (shape && !shape.get('destroyed')) {
shape.set('clipShape', null);
}
},
delay
);
}
export function pieChartUpdate(
shape: IShape | IGroup,
animateCfg,
coordinate: Coordinate,
shapeModel: ShapeDrawCFG,
toAttrs
) {
const { startAngle: currentStartAngle, endAngle: currentEndAngle } = getAngle(shapeModel, coordinate);
const { startAngle: preStartAngle, endAngle: preEndAngle } = getAngle(shape.get('origin'), coordinate);
const center = coordinate.getCenter();
// @ts-ignore FIXME: coordinate 支持下
const radius = coordinate.getRadius();
const diffStartAngle = currentStartAngle - preStartAngle;
const diffEndAngle = currentEndAngle - preEndAngle;
shape.animate((ratio) => {
const onFrameStartAngle = preStartAngle + ratio * diffStartAngle;
const onFrameEndAngle = preEndAngle + ratio * diffEndAngle;
return {
...toAttrs,
path: getSectorPath(center.x, center.y, radius, onFrameStartAngle, onFrameEndAngle),
};
}, animateCfg);
}