-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathanimate.js
137 lines (115 loc) · 3 KB
/
animate.js
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
/*
* @Author: hijiangtao (hijiangtao@gmail.com)
* @Date: 2019-01-11 15:06:15
* @Description: A Layer based on ArcLayer, which enable source-to-target animation on Arc Lines.
* @Last Modified time: 2019-01-11 15:06:15
*/
import {CompositeLayer} from 'deck.gl';
import ArcLayer from './index';
const MAX_COEF = 1; // Default Max Display coef
const OVERFLOW_FLAG = 9999; // Animation End Flag
class BrushArcLayer extends CompositeLayer {
initializeState() {
this.state = {
coef: OVERFLOW_FLAG,
data: [],
raf: null,
};
}
shouldUpdateState({changeFlags}) {
if (changeFlags.propsChanged || changeFlags.dataChanged || changeFlags.stateChanged && this.state.coef !== OVERFLOW_FLAG) {
return true;
}
return false;
}
updateState({props, oldProps, changeFlags}) {
super.updateState({props, oldProps, changeFlags});
const {propsChanged, stateChanged, dataChanged} = changeFlags;
if (!propsChanged && !dataChanged && stateChanged) return false;
if (propsChanged) {
this.setState((prevState) => {
return {
...prevState,
data: props.data || prevState.data,
}
})
}
/**
* Animaiton trigger judgement
*/
if (props.data && props.data.length) {
if (!props.showBrushAnimation) {
this.setState({
coef: props.coef || MAX_COEF
});
return ;
}
let {raf} = this.state;
raf && cancelAnimationFrame(raf);
/**
* Arc Brush Animation Method
*/
const startAnimation = () => {
const { speed = 0.005 } = props;
let {coef} = this.state;
if (coef === OVERFLOW_FLAG) {
coef = 0;
}
coef += speed;
if (coef >= MAX_COEF) {
coef = OVERFLOW_FLAG;
cancelAnimationFrame(raf);
}
this.setState({coef});
if (coef < MAX_COEF) {
const raf = window.requestAnimationFrame(startAnimation);
this.setState({
raf
});
}
}
startAnimation();
}
}
renderLayers() {
let {
updateTriggers = {},
showBrushAnimation,
data,
getSourcePosition,
getTargetPosition,
getSourceColor,
getTargetColor,
getStrokeWidth,
...otherProps} = this.props;
const {coef} = this.state;
if (showBrushAnimation) {
otherProps.coef = coef;
}
if (coef === OVERFLOW_FLAG) return [];
const layerProps = {
...otherProps,
id: `${this.id}-aa-child`,
data,
getSourcePosition,
getTargetPosition,
getSourceColor,
getTargetColor,
updateTriggers: {
...updateTriggers,
getColor: [coef],
},
getStrokeWidth,
showBrushAnimation,
};
return [
new ArcLayer(layerProps)
]
}
}
BrushArcLayer.layerName = 'BrushArcLayer';
BrushArcLayer.defaultProps = {
...ArcLayer.defaultProps,
showBrushAnimation: false,
};
export default BrushArcLayer;