deck.gl is a perfect match for React applications, since deck.gl layers fit naturally into React's component render flow.
To use deck.gl with React, simply import the DeckGL
React component and
render it as a child of another component, passing in your list of deck.gl
layers as a property.
import MapGL from 'react-map-gl';
import DeckGL from 'deck.gl/react';
import {ScatterplotLayer, Viewport} from 'deck.gl';
const data = [];
const viewport = new Viewport();
return (
<MapGL>
<DeckGL
viewport={viewport}
layers={[new ScatterplotLayer({data})]}
/>
</MapGL>
);
Remarks
-
The
DeckGL
component is typically rendered as a child of a map component like react-map-gl, but could be rendered as a child of any React component that you want to overlay your layers on top of. -
To achive the overlay effect, the
DeckGL
component creates a transparentcanvas
DOM element, into which the deck.gl layers passed in thelayers
prop will render (using WebGL). Since this canvas is transparent, any other component you have underneath (typically a map) will visible behind the layers. -
When the deck.gl layer list is drawn to screen, it matches the new Layer instances with the instances from the previous render call, and intelligently compares the new properties and only update WebGL state when needed (just like React does for DOM components).
-
Internally, the
DeckGL
component initializes a WebGL context attached to a canvas element, sets up the animation loop and calls provided callbacks on initial load and for each rendering frames. TheDeckGL
component also handles events propagation across layers, and prevents unnecessary calculations using React and deck.gl lifecycle functions.
DeckGL
is a React component that takes deck.gl layer instances and
viewport parameters, and renders those layers as a transparent overlay.
Canvas ID to allow style customization.
Width of the canvas.
Height of the canvas.
The array of deck.gl
layers to be rendered. This array is expected to be
an array of newly allocated instances of your deck.gl layers, created with
updated properties derived from the current application state.
Blending settings.
Css styles for the deckgl-canvas.
Will use device ratio by default.
gl context, will be autocreated if not supplied.
Flag to enable debug mode.
Note: debug mode is somewhat slower as it will use synchronous operations to keep track of GPU state.
Callback, called once the WebGL context has been initiated
Callback arguments:
- opts.gl - the WebGL context.
Callback - called when the mouse moves over the layers.
Arguments:
info
- theinfo
object of the topmostpickedInfos
- an array of info objects for all visible layers that matched the picked coordinate, in top to bottom order.
Callback - called when the mouse moves over the layers.
Arguments:
info
- theinfo
object of the topmostpickedInfos
- an array of info objects for all visible layers that matched the picked coordinate, in top to bottom order.
- Picking: The info objects have a number of fields, most interesting are
perhaps
layer
andindex
. If the data prop is anArray
,info.object
will contain the selected object in the array. - Layers can add additional fields to the picking
info
object, check the documentation of each layer. - Picking happens in top-to-bottom order (reverse of rendering), i.e. deck.gl traverses the layer list backwards during picking.