Skip to content

Commit

Permalink
Added a getCanvas helper function on deck (#7919)
Browse files Browse the repository at this point in the history
Right now there isn't a way to access the canvas instance [as seems to be suggested](#4436 (comment)), as Typescript forbids it.

* add a getCanvas helper

* documentation and MapboxOverlay.getCanvas

---------

Co-authored-by: Chris Gervang <chrisgervang@users.noreply.github.com>
Co-authored-by: Xiaoji Chen <Pessimistress@users.noreply.github.com>
  • Loading branch information
3 people authored and felixpalmer committed Oct 11, 2023
1 parent 22af24e commit 6816db7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/api-reference/core/deck.md
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,19 @@ Frees all resources associated with this `Deck` instance.
`deck.finalize()`
##### `getCanvas` {#getcanvas}
Get the canvas element attached to this `Deck` instance.
`deck.getCanvas()`
Returns:
* Either an `HTMLCanvasElement` or `null` if one isn't assigned.
Notes:
* See the [canvas](#canvas) prop for more information.
##### `setProps` {#setprops}
Expand Down
3 changes: 3 additions & 0 deletions docs/api-reference/mapbox/mapbox-overlay.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ See [Deck.pickMultipleObjects](../core/deck.md#pickmultipleobjects).

Removes the control and deletes all resources.

##### getCanvas

See [Deck.getCanvas](../core/deck.md#getcanvas). When using `interleaved: true`, returns the base map's `canvas`.

## Remarks

Expand Down
5 changes: 5 additions & 0 deletions modules/core/src/lib/deck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,11 @@ export default class Deck {
return this.viewManager.getViewports(rect);
}

/** Get the current canvas element. */
getCanvas(): HTMLCanvasElement | null {
return this.canvas;
}

/** Query the object rendered on top at a given point */
pickObject(opts: {
/** x position in pixels */
Expand Down
5 changes: 5 additions & 0 deletions modules/mapbox/src/mapbox-overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ export default class MapboxOverlay implements IControl {
}
}

/** If interleaved: true, returns base map's canvas, otherwise forwards the Deck.getCanvas method. */
getCanvas(): HTMLCanvasElement | null {
return this._interleaved ? this._map.getCanvas() : this._deck.getCanvas();
}

private _handleStyleChange = () => {
resolveLayers(this._map, this._deck, this._props.layers, this._props.layers);
};
Expand Down

0 comments on commit 6816db7

Please sign in to comment.