Adding layers
Adding layers
The addLayer()
method adds a layer to the canvas in the form of an object or a function. The layer is not drawn after calling this method.
If the input is an object, you must specify the type
property (i.e. the type of drawing associated with the properties you specify). Recognized types include rectangle
, arc
, line
, quadratic
, bezier
, text
, and many others.
You can also add a layer using the layer
property with any method.
Because we are creating a layer and calling the drawRect()
method simultaneously, this will also draw the layer.
Naming layers
To name a layer, use the name
property. This name can be used later to retrieve, remove, or animate the layer.
Note that the layer name
must be unique (that is, no two layers on the canvas can have the same name). The layer name can not be a number, either.
Grouping layers
To assign one or more groups to a layer, use the groups
property. Doing so will not change the order in which the layer is drawn. The property is merely for categorization purposes.
To dynamically add an existing layer to a group, use the addLayerToGroup()
method.
To dynamically remove an existing layer from a group, use the removeLayerFromGroup()
method. Note that this does not remove the layer from jCanvas.
Hiding layers
To temporarily hide a layer, set its visible
property to false
. This also will prevent any of the layer’s events from triggering.
To show the layer again, set its visible
property to true
.
Function layers
Instead of an object, the addLayer()
method accepts a function using the fn
property (this function will run when the layer is drawn). The function also accepts one parameter: the canvas context.
jCanvas refers to these types of layers as function layers.
This also works when using the draw()
method and the layer
property.
Setting a layer’s index
You can also set a layer’s index (in the current layers array) using the index
property.
Keep in mind that a layer’s index cannot be greater than the current number of layers. Therefore, you couldn’t assign the first layer an index, because there are no other existing layers at that moment.
You can also use a negative index; a value of -1
will insert the layer before
the last.
Storing Data
You can also store arbitrary data on any jCanvas layer using the data
property.