-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathapp.jsx
More file actions
186 lines (167 loc) · 4.45 KB
/
app.jsx
File metadata and controls
186 lines (167 loc) · 4.45 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
/**
* This is based on the deckgl building example that uses the TripsLayer and PolygonLayer
* You can find it here https://deck.gl/examples/trips-layer/
* Source code: https://github.com/visgl/deck.gl/tree/master/examples/website/trips
*/
import React, {useState, useRef, useEffect, forwardRef} from 'react';
import {createRoot} from 'react-dom/client';
import {BasicControls, useHubbleGl, useDeckAnimation} from '@hubble.gl/react';
import {MapboxOverlay} from '@deck.gl/mapbox';
import Map, {useControl} from 'react-map-gl';
import {PolygonLayer} from '@deck.gl/layers';
import {easeInOut} from 'popmotion';
import {setRef} from './set-ref';
// Source data CSV
const BUILDINGS =
'https://raw.githubusercontent.com/visgl/deck.gl-data/master/examples/trips/buildings.json';
const material = {
ambient: 0.8,
diffuse: 0.6,
shininess: 32,
specularColor: [60, 64, 70]
};
const START = {
longitude: -74,
latitude: 40.72,
zoom: 13,
pitch: 45,
bearing: 0,
maxPitch: 90
};
const END = {
latitude: 40.711793177246946,
longitude: -74.01008557686262,
zoom: 16.88,
bearing: -26,
pitch: 65,
maxPitch: 90
};
const resolution = {
width: 1280,
height: 720
};
/** @type {import('@hubble.gl/core/src/types').FormatConfigs} */
const formatConfigs = {
webm: {
quality: 0.8
},
png: {},
jpeg: {
quality: 0.8
},
gif: {
sampleInterval: 1000,
width: resolution.width,
height: resolution.height
}
};
const timecode = {
start: 0,
end: 5000,
framerate: 30
};
const DeckGLOverlay = forwardRef((props, ref) => {
// MapboxOverlay handles a variety of props differently than the Deck class.
// https://deck.gl/docs/api-reference/mapbox/mapbox-overlay#constructor
const deck = useControl(() => new MapboxOverlay({...props, interleaved: true}));
deck.setProps(props);
// @ts-expect-error private property
setRef(ref, deck._deck);
return null;
});
const Container = ({children}) => (
<div
style={{
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
width: '100%',
height: '100%',
position: 'relative',
backgroundColor: '#11183c',
overflow: 'hidden'
}}
>
{children}
</div>
);
const randomColor = () => [
Math.floor(Math.random() * 255),
Math.floor(Math.random() * 255),
Math.floor(Math.random() * 255)
];
export default function App({mapStyle = 'mapbox://styles/mapbox/streets-v11'}) {
const deckRef = useRef(null);
const mapRef = useRef(null);
const [busy, setBusy] = useState(false);
const deckAnimation = useDeckAnimation({
cameraKeyframe: {
timings: [0, timecode.end],
keyframes: [START, END],
easings: [easeInOut],
interpolators: 'flyTo',
width: resolution.width,
height: resolution.height
},
getLayers: a =>
a.applyLayerKeyframes([
new PolygonLayer({
id: 'buildings',
data: BUILDINGS,
extruded: true,
wireframe: false,
opacity: 0.5,
getPolygon: f => f.polygon,
getElevation: f => f.height,
getFillColor: randomColor,
material,
elevationScale: 1
})
]),
layerKeyframes: [
{
id: 'buildings',
timings: [0, timecode.end],
keyframes: [{elevationScale: 0.1}, {elevationScale: 1}],
easings: [easeInOut]
}
]
});
const {deckProps, mapProps, adapter, cameraFrame, setCameraFrame} = useHubbleGl({
deckRef,
mapRef,
deckAnimation,
initialViewState: START
});
const onViewStateChange = ({viewState}) => setCameraFrame(viewState);
useEffect(() => onViewStateChange({viewState: cameraFrame}), []);
return (
<Container>
<div style={{position: 'relative'}}>
<Map
ref={mapRef}
mapStyle={mapStyle}
{...mapProps}
{...cameraFrame}
style={{width: resolution.width, height: resolution.height}}
onMove={onViewStateChange}
// Note: 'reuseMap' prop with gatsby and mapbox extension causes stale reference error.
>
<DeckGLOverlay ref={deckRef} {...deckProps} />
</Map>
</div>
<BasicControls
adapter={adapter}
busy={busy}
setBusy={setBusy}
formatConfigs={formatConfigs}
timecode={timecode}
filename="basic-basemap-mapbox-legacy"
/>
</Container>
);
}
export async function renderToDOM(container) {
const root = createRoot(container);
root.render(<App />);
}