Layer Retrieving layer 访问层 - caleb531/jcanvas-docs GitHub Wiki

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.
你可以使用getLayer()方法来访问一个指定的层,你可以传入层的索引号或分配的名字。

// Get the first layer获取第一个层
$('canvas').getLayer(0);
// Get the layer whose name is 'myBox' 获取名字为myBox的层
$('canvas').getLayer('myBox');

You can also retrieve the first layer whose names matches the given regular expression.
你也可以访问第一个名字符合指定正则表达式的层。

// Get first layer whose name contains 'box' 获取第一个name包含box的层
$('canvas').getLayer(/box/gi);

Finally, a negative index is also acceptable, with -1 being the index of the last (topmost) layer.
最后,负索引也可以接受,-1就是(最顶上的)最后一个层。

// Get the last layer获取最后一个层
$('canvas').getLayer(-1);

Retrieving all layers 访问所有的层

To retrieve all layers (as an array) for any canvas element, use the getLayers() method.
要访问任意画布元素上所有的层(作为数组),使用getLayers()方法。
The getLayers() method returns an array containing each layer object. Therefore, you can act on the array after retrieving it.
getLayers()方法返回一个包含每个层对象的数组,因此你可以在访问之后,在数组上操作。

$('canvas').getLayers();
var layers = $('canvas').getLayers();
// Reverse layer order 反转层的顺序
layers.reverse();

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.
getLayers()方法接收一个可选参数:一个回调函数,此函数会过滤(作为参数的)传入到回调函数中的层数组,也就是如果回调函数返回true,层就会出现在最终数组中,如果返回false,层就不会被包含在最终数组中。

// 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. 注意:getLayers方法总是返回数组,即使对于非画布来说。(应该是会返回空数组的意思)

Retrieving layer groups 访问层分组

If you've defined the same group for a set of layers, you can get those layers using the getLayerGroup() method.
如果你给一些层定义了相同的group属性的话,你可以使用getLayerGroup()方法来获取这些层。

$('canvas').getLayerGroup('myBoxes');

You can also retrieve all layers whose group name matches the given regular expression.
你也可以访问,组名符合指定正则表达式的所有的层。

$('canvas').getLayerGroup(/box/gi);

Retrieving layer indexes 访问层索引

You can retrieve a layer's index in the layers array using the getLayerIndex() method.
你可以使用getLayerIndex()方法在访问层在层数组中的索引。

$('canvas').getLayerIndex('box');