deck.gl icon indicating copy to clipboard operation
deck.gl copied to clipboard

Accessing "this/layer" context - taken from subclassing example

Open cschlueter opened this issue 7 years ago • 3 comments

Hi,

I'm trying to subclass PathLayer in order to write my own "per segment" functions for e.g. width/color/pattern etc. I tried to use the example from the documentation as a starting point but I'm having trouble to understand how get the this context, and by that access to e.g. accessor functions, following a snippet with console log?

 class MultiColorPathLayer extends deck.PathLayer {
        initializeState() {
            super.initializeState();
            console.log(this.props);
            this.getAttributeManager().getAttributes().instanceColors.update = this.calculateColors;
        }
        
        calculateColors(attribute) {
            console.log(this);
            const {data, getPath, getColor} = this.props;

The first console.log(this) in initalizeState() reveals this as expected, but the second one in calculateColors() is basically inside the attributeManager I guess, and that makes const {data, getPath, getColor} = this.props; not work right now.

First console.log:

{pickable: true, widthScale: 20, widthMinPixels: 2, getPath: ƒ, getColor: ƒ, …}
data: (...)
getColor: d => randomColor
getPath: d => d.path
getWidth: 5
pickable: true
widthMinPixels: 2
widthScale: 20

Second console.log:

t {gl: WebGL2RenderingContext, id: "instanceColors", isIndexed: false, target: 34962, type: 5121, …}
buffer: null
constant: false
divisor: 1
externalBuffer: null
gl: WebGL2RenderingContext {_version: 2, luma: {…}, getQueryObject: ƒ, readBuffer: ƒ, getVertexAttrib: ƒ, …}
hasShaderAttributes: false
id: "instanceColors"
integer: false
isIndexed: false
normalized: false
offset: 0
shaderAttributes: {}

Any pointer is appreciated.

cschlueter avatar Sep 11 '19 09:09 cschlueter

Can you try to bind this before registering the attribute updater:

  this.getAttributeManager().getAttributes().instanceColors.update = this.calculateColors.bind(this);

ibgreen avatar Sep 11 '19 16:09 ibgreen

The example in the documentation is outdated. You end up overwriting the attribute.update method instead of changing its update config. So instead of

this.getAttributeManager().getAttributes().instanceColors.update = this.calculateColors

You need to do

this.getAttributeManager().addInstanced({
	instanceColors: {
	    size: 4,
	    type: GL.UNSIGNED_BYTE,
	    normalized: true,
	    update: this.calculateColors
	}
});

Pessimistress avatar Sep 11 '19 19:09 Pessimistress

Thanks, that did the trick!

cschlueter avatar Sep 11 '19 20:09 cschlueter