-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathindex.js
99 lines (86 loc) · 2.61 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
/*
* @Author: hijiangtao ([email protected])
* @Date: 2019-01-14 20:30:03
* @Desc: Trip Layer
* @Last Modified time: 2019-01-14 20:30:03
*/
import { TripsLayer } from '@deck.gl/experimental-layers';
import { CompositeLayer, PolygonLayer } from 'deck.gl';
// Source data CSV
const DATA_URL = {
BUILDINGS:
'https://raw.githubusercontent.com/uber-common/deck.gl-data/master/examples/trips/buildings.json', // eslint-disable-line
TRIPS:
'https://raw.githubusercontent.com/uber-common/deck.gl-data/master/examples/trips/trips.json' // eslint-disable-line
};
const LIGHT_SETTINGS = {
lightsPosition: [-74.05, 40.7, 8000, -73.5, 41, 5000],
ambientRatio: 0.05,
diffuseRatio: 0.6,
specularRatio: 0.8,
lightsStrength: [2.0, 0.0, 0.0, 0.0],
numberOfLights: 2,
};
export default class TripMap extends CompositeLayer {
initializeState() {
this.state = {
time: 0,
raf: null,
};
}
shouldUpdateState({changeFlags}) {
if (changeFlags.propsChanged || changeFlags.dataChanged || changeFlags.stateChanged) {
return true;
}
return false;
}
updateState({props, oldProps, changeFlags}) {
super.updateState({props, oldProps, changeFlags});
const _animate = () => {
const {
loopLength = 1800, // unit corresponds to the timestamp in source data
animationSpeed = 30, // unit time per second
} = props;
const timestamp = Date.now() / 1000;
const loopTime = loopLength / animationSpeed;
this.setState({
time: ((timestamp % loopTime) / loopTime) * loopLength,
raf: window.requestAnimationFrame(_animate),
});
}
if (changeFlags.propsChanged) {
const {raf} = this.state;
raf && cancelAnimationFrame(raf);
_animate();
}
}
renderLayers() {
const { buildings = DATA_URL.BUILDINGS, trips = DATA_URL.TRIPS, trailLength = 200 } = this.props;
return [
new TripsLayer({
id: 'trips',
data: trips,
getPath: d => d.segments,
getColor: d => (d.vendor === 0 ? [253, 128, 93] : [23, 184, 190]),
opacity: 0.6,
strokeWidth: 3,
trailLength,
currentTime: this.state.time,
onHover: info => console.log('Hovered:', info),
onClick: info => console.log('Clicked:', info),
}),
new PolygonLayer({
id: 'buildings',
data: buildings,
extruded: true,
wireframe: false,
fp64: true,
opacity: 0.5,
getPolygon: f => f.polygon,
getElevation: f => f.height,
getFillColor: [74, 80, 87],
lightSettings: LIGHT_SETTINGS,
}),
];
}
}