/
Map.js
173 lines (153 loc) · 5.69 KB
/
Map.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
// Copyright (c) 2021 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import React, {Component} from 'react';
import DeckGL from '@deck.gl/react';
import {StaticMap} from 'react-map-gl';
import {MapboxLayer} from '@deck.gl/mapbox';
import {nearestEven} from '../../utils';
import isEqual from 'lodash.isequal';
export class Map extends Component {
constructor(props) {
super(props);
this.mapRef = React.createRef();
this.deckRef = React.createRef();
this.state = {
glContext: undefined,
memoDevicePixelRatio: window.devicePixelRatio // memoize
};
this._onMapLoad = this._onMapLoad.bind(this);
this._resizeVideo = this._resizeVideo.bind(this);
this._resizeVideo();
}
componentDidUpdate(prevProps) {
if (!isEqual(prevProps.dimension, this.props.dimension)) {
this._resizeVideo();
}
}
componentWillUnmount() {
const {memoDevicePixelRatio} = this.state;
this._setDevicePixelRatio(memoDevicePixelRatio);
}
_resizeVideo() {
const {width, dimension} = this.props;
this._setDevicePixelRatio(nearestEven(dimension.width / width));
if (this.mapRef.current) {
const map = this.mapRef.current.getMap();
map.resize();
}
}
_setDevicePixelRatio(devicePixelRatio) {
/**
* TODO: This is the only way to trick mapbox into scaling its render buffer up
* to match the desired resolution. It is built to always fit it's render buffer size
* to it's CSS container size, which makes it impossible to make a small "preview" box.
*
* deck.gl has the useDevicePixels prop, which would have been used if it also changed mapbox.
* https://github.com/visgl/luma.gl/pull/1155 for background.
*
* Compare implementations of luma.gl to mapbox for context:
* https://github.com/visgl/luma.gl/blob/f622105e30c4dcda434f80ebc4680356003b12fa/modules/gltools/src/utils/device-pixels.js#L31
* https://github.com/mapbox/mapbox-gl-js/blob/3136a53235cf17b732e84c9945c4e85ba3369a93/src/ui/map.js#L2324
*
* In luma the scaler can be overriden by useDevicePixels.
*
* The workaround is to change window.devicePixelRatio while the component is mounted to scale up the render buffers of deck and mapbox.
* This is hacky and can cause issues in certain applications. We should try to produce a better solution.
*/
// @ts-ignore
window.devicePixelRatio = devicePixelRatio;
}
_onMapLoad() {
const {
adapter,
deckProps: {layers},
updateTimeCursor
} = this.props;
// Adds mapbox layer to modal
const map = this.mapRef.current.getMap();
const deck = this.deckRef.current.deck;
// var mapboxLayers = map.getStyle().layers;
// console.log(mapboxLayers)
// If there aren't any layers, combine map and deck with a fake layer.
if (!layers.length) {
map.addLayer(new MapboxLayer({id: '%%blank-layer', deck}));
}
for (let i = 0; i < layers.length; i++) {
// TODO: layer mapbox and deck layers in order according to kepler config.
// map.addLayer(new MapboxLayer({id: layers[i].id, deck}), "tunnel-simple");
// Adds DeckGL layers to Mapbox so Mapbox can be the bottom layer. Removing this clips DeckGL layers
map.addLayer(new MapboxLayer({id: layers[i].id, deck}));
}
map.on('render', () =>
adapter.onAfterRender(timeMs => {
updateTimeCursor(timeMs);
})
);
}
render() {
const {
adapter,
viewState,
width,
height,
setViewState,
deckProps,
staticMapProps,
dimension
} = this.props;
const deck = this.deckRef.current && this.deckRef.current.deck;
const deckStyle = {
width: '100%',
height: '100%'
};
const containerStyle = {
width: `${width}px`,
height: `${height}px`,
position: 'relative'
};
return (
<div id="deck-canvas" style={containerStyle}>
<DeckGL
ref={this.deckRef}
viewState={{...viewState, maxPitch: 90}}
id="hubblegl-overlay"
style={deckStyle}
controller={true}
glOptions={{stencil: true}}
onWebGLInitialized={gl => this.setState({glContext: gl})}
onViewStateChange={({viewState: vs}) => setViewState(vs)}
width={dimension.width}
height={dimension.height}
{...adapter.getProps({deck, extraProps: deckProps})}
>
{this.state.glContext && (
<StaticMap
ref={this.mapRef}
preventStyleDiffing={true}
gl={this.state.glContext}
onLoad={this._onMapLoad}
{...staticMapProps}
/>
)}
</DeckGL>
</div>
);
}
}