Learn all about jCanvas

Retrieving layers

Retrieving a single layer

You can retrieve a specific layer using the getLayer() method. You can pass it the index of the layer, or the layer’s assigned name.

// Get the first layer
// Get the layer whose name is 'myBox'

You can also retrieve the first layer whose names matches the given regular expression.

// Get first layer whose name contains 'box'

Finally, a negative index is also acceptable, with -1 being the index of the last (topmost) layer.

// Get the last layer

Retrieving all layers

To retrieve all layers (as an array) for any canvas element, use the getLayers() method.

The getLayers() method returns an array containing each layer object. Therefore, you can act on the array after retrieving it.

var layers = $('canvas').getLayers();
// Reverse layer order

The getLayers() method accepts one optional argument: a callback function which filters the layers array to those layers which pass the callback function. That is, If the callback function returns true, the layer will be in the resulting array. If the function returns false, the layer will not be included in the resulting array.

// Returns an array containing all draggable layers
$('canvas').getLayers(function(layer) {
  return (layer.draggable === true);

Note that the getLayers() method always returns an array, even for non-canvases.

Retrieving layer groups

If you’ve defined the same group for a set of layers, you can get those layers using the getLayerGroup() method.


You can also retrieve all layers whose group name matches the given regular expression.


Retrieving layer indexes

You can retrieve a layer’s index in the layers array using the getLayerIndex() method.

Fish Ladybug Water