Skip to content

Commit e21fe07

Browse files
Document CompositeLayer's filterSubLayer method (visgl#6048)
1 parent 6668fe0 commit e21fe07

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

docs/api-reference/core/composite-layer.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,81 @@ The default implementation of `renderLayers` returns `null`.
116116
`renderLayers` can return a nested arrays with `null` values. deck.gl will automatically flatten and filter the array. See usage above.
117117

118118

119+
##### `filterSubLayer`
120+
121+
Allows a layer to dynamically show/hide sub layers based on the render context.
122+
123+
Receives arguments:
124+
125+
- `layer` (Layer) - the sub layer to be drawn
126+
- `viewport` (Viewport) - the current viewport
127+
- `isPicking` (Boolean) - whether this is a picking pass
128+
- `renderPass` (String) - the name of the current render pass. See [layerFilter](/docs/api-reference/core/deck.md#layerfilter) for possible values.
129+
130+
Returns:
131+
132+
`true` if the layer should be drawn.
133+
134+
This method achieves the same result as toggling sub layers' `visible` prop in `renderLayers`. The difference is that, `renderLayers` is only called when the layer is updated due to props or state change, and will recursively create new instances of all decendant layers, therefore is more expensive to invoke. `filterSubLayer` is evaluated before every redraw, and is intended to be a more performant solution to setting `visible` props dynamically during continuous viewport updates. Generally speaking, `filterSubLayer` is favorable if the visibilities of sub layers change frequently, and the logic to determine visibility is very cheap to compute.
135+
136+
An example of leveraging this method is to switch sub layers on viewport change:
137+
138+
```js
139+
/*
140+
* A layer that renders different dataset based on zoom level
141+
*/
142+
// Implementation A
143+
class LODLayer extends CompositeLayer {
144+
145+
// Force update layer and re-render sub layers when viewport changes
146+
shouldUpdateState({changeFlags}) {
147+
return changeFlags.somethingChanged;
148+
}
149+
150+
renderSubLayers() {
151+
const {lowResData, hiResData} = this.props;
152+
const {zoom} = this.context.viewport;
153+
return [
154+
new ScatterplotLayer({
155+
id: 'points-low-zoom',
156+
data: lowResData,
157+
visible: zoom < 8
158+
}),
159+
new ScatterplotLayer({
160+
id: 'points-high-zoom',
161+
data: highResData,
162+
visible: zoom >= 8
163+
})
164+
]
165+
}
166+
}
167+
168+
// Implementation B
169+
class LODLayer extends CompositeLayer {
170+
renderSubLayers() {
171+
const {lowResData, hiResData} = this.props;
172+
return [
173+
new ScatterplotLayer({
174+
id: 'points-low-zoom'
175+
data: lowResData,
176+
}),
177+
new ScatterplotLayer({
178+
id: 'points-high-zoom',
179+
data: highResData
180+
})
181+
]
182+
}
183+
184+
filterSubLayer({layer, viewport}) {
185+
if (viewport.zoom < 8) {
186+
return layer.id === 'points-low-zoom';
187+
} else {
188+
return layer.id === 'points-high-zoom';
189+
}
190+
}
191+
}
192+
```
193+
119194
##### `getPickingInfo`
120195

121196
Called when a sublayer is being hovered or clicked, after the `getPickingInfo`

0 commit comments

Comments
 (0)