-
Notifications
You must be signed in to change notification settings - Fork 60
/
index.js
261 lines (217 loc) · 6.56 KB
/
index.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
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
/*
* @Author: hijiangtao ([email protected])
* @Date: 2019-01-11 15:00:17
* @Description: A Layer based on ScatterplotLayer, which enable point fade-out animation on Scatterplot.
* @Color schema: [255,0,0] to [255,140,0]
* @Last Modified time: 2019-01-11 15:00:38
*/
import {ScatterplotLayer, CompositeLayer} from 'deck.gl';
// import AnimationLayer from '../AnimationLayer';
const MAX_RADIUSSCALE = 8; // Default Max Display radiusScale
const OVERFLOW_FLAG = 9999; // Animation End Flag
// class FadeScatterplotLayer extends AnimationLayer {
// initializeState() {
// // super.initializeState();
// this.state = {
// maxSpaces: 1,
// }
// }
// shouldUpdateState(props) {
// super.shouldUpdateState(props);
// }
// updateState({props, oldProps, changeFlags}) {
// super.updateState({props, oldProps, changeFlags});
// /**
// * Animaiton trigger judgement
// */
// if (props.data && props.data.length) {
// let maxSpaces = 1;
// props.data.map((d) => {
// if (d.SPACES > maxSpaces) {
// maxSpaces = d.SPACES;
// }
// });
// this.setState({maxSpaces});
// if (!props.showWaveAnimation) return ;
// }
// }
// renderLayers() {
// let {updateTriggers = {}, showWaveAnimation, data, ...otherProps} = this.props;
// const {radiusScale, MAX_RADIUSSCALE} = this.state;
// const {maxSpaces} = this.state;
// console.log('Scatterplot Layer State', this.state);
// if (showWaveAnimation) {
// otherProps.radiusScale = radiusScale;
// }
// if (radiusScale === MAX_RADIUSSCALE) return [];
// const alpha = showWaveAnimation ? 255 * (MAX_RADIUSSCALE - radiusScale) / MAX_RADIUSSCALE : 255;
// const layerProps = {
// LayerType: ScatterplotLayer,
// ...otherProps,
// data,
// getColor: d => [255, (maxSpaces - d.SPACES) / maxSpaces * 140, 0, alpha],
// updateTriggers: {
// ...updateTriggers,
// getColor: [radiusScale],
// },
// };
// return [
// new ScatterplotLayer(layerProps)
// ]
// }
// }
const quantizeScale = (domain, range, value) => {
const domainRange = domain[1] - domain[0];
if (domainRange <= 0) {
console.warn('quantizeScale: invalid domain, returning range[0]');
return range[0];
}
const step = domainRange / range.length;
const idx = Math.floor((value - domain[0]) / step);
const clampIdx = Math.max(Math.min(idx, range.length - 1), 0);
return range[clampIdx];
}
const getQuantizeScale = (domain, range) => {
return value => quantizeScale(domain, range, value.SPACES);
}
class FadeScatterplotLayer extends CompositeLayer {
initializeState() {
this.state = {
radiusScale: OVERFLOW_FLAG,
data: [],
MAX_RADIUSSCALE,
raf: null,
maxSpaces: 1,
minSpaces: 9999,
};
}
shouldUpdateState({changeFlags}) {
// console.log(this.state.radiusScale)
if (changeFlags.propsChanged || changeFlags.dataChanged || changeFlags.stateChanged && this.state.radiusScale !== OVERFLOW_FLAG) {
return true;
}
return false;
}
updateState({props, oldProps, changeFlags}) {
super.updateState({props, oldProps, changeFlags});
const {propsChanged, stateChanged, dataChanged} = changeFlags;
// Stop following operation if update is triggered by internal state update
if (!propsChanged && !dataChanged && stateChanged) return false;
const prevMaxRadiusScale = this.state.MAX_RADIUSSCALE;
const newMaxRadiusScale = props.radiusScale || prevMaxRadiusScale;
if (propsChanged) {
this.setState((prevState) => {
return {
...prevState,
data: props.data || prevState.data,
MAX_RADIUSSCALE: newMaxRadiusScale,
}
})
}
/**
* Animaiton trigger judgement
*/
if (props.data && props.data.length) {
// Get max spaces in datasets
let maxSpaces = 1;
let minSpaces = 9999;
props.data.map((d) => {
if (d.SPACES > maxSpaces) {
maxSpaces = d.SPACES;
}
if (d.SPACES < minSpaces) {
minSpaces = d.SPACES;
}
});
this.setState({maxSpaces, minSpaces});
if (!props.showWaveAnimation) {
this.setState({
radiusScale: props.radiusScale || MAX_RADIUSSCALE
});
return ;
}
let {raf} = this.state;
raf && cancelAnimationFrame(raf);
this.startAnimation();
}
}
/**
* Point Spread Animation Method
*/
startAnimation() {
// console.log(this.props)
const { speed } = this.props;
let {radiusScale, MAX_RADIUSSCALE, raf} = this.state;
if (radiusScale === OVERFLOW_FLAG) {
radiusScale = 0;
}
radiusScale += speed;
if (radiusScale >= MAX_RADIUSSCALE) {
radiusScale = OVERFLOW_FLAG;
window.cancelAnimationFrame(raf);
}
this.setState({radiusScale});
if (radiusScale < MAX_RADIUSSCALE) {
const raf = window.requestAnimationFrame(this.startAnimation.bind(this));
this.setState({
raf
});
}
}
renderLayers() {
let {
updateTriggers = {},
showWaveAnimation,
data,
colorRange,
...otherProps
} = this.props;
const {radiusScale, maxSpaces, minSpaces, MAX_RADIUSSCALE} = this.state;
if (showWaveAnimation) {
otherProps.radiusScale = radiusScale;
}
let alpha = showWaveAnimation ? 255 * (MAX_RADIUSSCALE - radiusScale) / MAX_RADIUSSCALE : 255;
const visible = radiusScale !== OVERFLOW_FLAG;
if (!visible) alpha = 0;
const newColorRange = colorRange.map(e => {
if (!e.length) {
console.error('Wrong Color Range Format');
return [255,255,255,255];
}
const newColorArray = e.slice(0, 3);
return newColorArray.concat(alpha);
});
const layerProps = {
...otherProps,
id: `${this.id}-sp-child`,
data,
getColor: getQuantizeScale([minSpaces, maxSpaces], newColorRange), // d => [255, (maxSpaces - d.SPACES) / maxSpaces * 140, 0, alpha],
updateTriggers: {
...updateTriggers,
getColor: [radiusScale],
},
visible,
};
return [
new ScatterplotLayer(layerProps)
]
}
}
FadeScatterplotLayer.layerName = 'FadeScatterplotLayer';
FadeScatterplotLayer.defaultProps = {
...ScatterplotLayer.defaultProps,
showWaveAnimation: false,
speed: 0.02,
colorRange: {
type: 'accessor',
value: [
[255,140,0],
[255,112,0],
[255,84,0],
[255,56,0],
[255,28,0],
[255,0,0],
],
}
};
export default FadeScatterplotLayer;