Skip to content

Commit

Permalink
add pickObject API in layer
Browse files Browse the repository at this point in the history
  • Loading branch information
jianhuang01 committed Mar 14, 2019
1 parent eae5ed0 commit ceee77b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
12 changes: 12 additions & 0 deletions docs/api-reference/layer.md
Expand Up @@ -577,6 +577,18 @@ Returns:

* a "null" picking color which is equal the the color of pixels not covered by the layer. This color is guaranteed not to match any index value greater than or equal to zero.

##### `pickObject`

Get the closest pickable and visible object at the given screen coordinate.

* `x` (Number) - x position in pixels
* `y` (Number) - y position in pixels
* `radius` (Number, optional) - radius of tolerance in pixels. Default `0`.

Returns:

* a single [`info`](/docs/get-started/interactivity.md#the-picking-info-object) object, or `null` if nothing is found.

## Source

[modules/core/src/core/lib/layer.js](https://github.com/uber/deck.gl/blob/master/modules/core/src/lib/layer.js)
1 change: 1 addition & 0 deletions modules/core/src/lib/deck.js
Expand Up @@ -528,6 +528,7 @@ export default class Deck {
const viewport = this.viewManager.getViewports()[0];
// Note: avoid React setState due GL animation loop / setState timing issue
this.layerManager = new LayerManager(gl, {
deck: this,
stats: this.stats,
viewport
});
Expand Down
5 changes: 3 additions & 2 deletions modules/core/src/lib/layer-manager.js
Expand Up @@ -43,6 +43,7 @@ const LOG_PRIORITY_LIFECYCLE_MINOR = 4;
// CONTEXT IS EXPOSED TO LAYERS
const INITIAL_CONTEXT = Object.seal({
layerManager: null,
deck: null,
gl: null,

// Settings
Expand All @@ -64,7 +65,7 @@ const layerName = layer => (layer instanceof Layer ? `${layer}` : !layer ? 'null

export default class LayerManager {
// eslint-disable-next-line
constructor(gl, {stats, viewport = null} = {}) {
constructor(gl, {deck, stats, viewport = null} = {}) {
// Currently deck.gl expects the DeckGL.layers array to be different
// whenever React rerenders. If the same layers array is used, the
// LayerManager's diffing algorithm will generate a fatal error and
Expand All @@ -79,7 +80,7 @@ export default class LayerManager {

this.context = Object.assign({}, INITIAL_CONTEXT, {
layerManager: this,

deck,
gl,
// Enabling luma.gl Program caching using private API (_cachePrograms)
shaderCache: gl && new ShaderCache({gl, _cachePrograms: true}),
Expand Down
8 changes: 8 additions & 0 deletions modules/core/src/lib/layer.js
Expand Up @@ -288,6 +288,14 @@ export default class Layer extends Component {
return index;
}

// Return the closest pickable and visible object at the given screen coordinate.
pickObject({x, y, radius = 0}) {
if (this.context.deck) {
return this.context.deck.pickObject({x, y, radius, layerIds: [this.props.id]});
}
return null;
}

// //////////////////////////////////////////////////
// LIFECYCLE METHODS, overridden by the layer subclasses

Expand Down

0 comments on commit ceee77b

Please sign in to comment.