Search code examples
z-indexkineticjsmulti-layer

Check which Kinetic.Layer is on top


I have one stage and multiple layers which are superposed. I refer to each layer with a button. I must allow the user to draw on those multiple layers whenever he clicks on one button, and swap between them. How can I check which layer is on top of the others?

I tried layer.getAbsoluteZIndex() but it's not obvious. Is there a method that actually return true/false for example, maybe like .isOnTop()?

EDIT :

Well, I had to implement it by my own and it was okay I guess. The easiest solution was to add a boolean attribute isOnTop in the definition of each layer and make the appropriate tests and treatments.


Solution

  • First add a new method on the Kinetic.Stage:

      Kinetic.Stage.prototype.getTopLayer=function(){
          var layers=stage.find('Layer');
          var topLayerIndex=-100;
          var topLayer;
          for(var i=0;i<layers.length;i++){
              var index=layers[i].getAbsoluteZIndex();
              if(index>topLayerIndex){
                  topLayerIndex=index;
                  topLayer=layers[i];
              }        
          }
          return(topLayer);
      }
    

    Then you can get the layer with the highest z-index like this:

      var myTopLayer = stage.getTopLayer();