Class LayerHash
This is the most important feature of this library. Basically just a glorified Hash it offers some handy features for managing the layers.
Internally the layers are stored in an array and rendered from bottom to top. This means that 0 is the lowest layer and the layer with the highest key is at the top of the list and will be rendered last.
You should not work with LayerHash's internal storage directly but use the provided methods to manipulate the order of the layers. However, if you feel that you need to manipulate the array just use the rebuildTables method after you're done.
Implements
LayerHash Method: constructor
This usually is never required to be called by a user since a new LayerHash is created when the CANVAS init function is called.
Syntax
new LayerHash( options );
Arguments
- options - (object) An object with options for the LayerHash.
Options (only events)
- add - (function) The function to execute when a layer is added.
- remove - (function) The function to execute when a layer is removed.
- swap - (function) The function to execute when two layers are swapped.
- promote - (function) The function to execute when a layer is promoted.
- demote - (function) The function to execute when a layer is demoted.
- draw - (function) The function to execute when a layer has finished drawing all it's subitems.
Returns
- (object) this (LayerHash instance)
LayerHash: public properties
Syntax
CANVAS.layers.<PROPERTY>
Properties
- length - (number) current amount of layers in the Hash. Updated every time the stack is manipulated.
LayerHash Method: add
Adds a new layer at the top of the list.
Syntax
CANVAS.layers.add( layer );
Arguments
- layer - (object) new Layer.
Returns
- (object) Layer instance
LayerHash Method: addAt
Adds a new layer at the specified position. This method is used by all other add-methods.
Syntax
CANVAS.layers.addAt( layer, position );
Arguments
- layer - (object) new Layer.
- position - (number) where to add the layer.
Returns
- (object) Layer instance
LayerHash Method: addBefore
Adds a new layer before the specified layer.
Syntax
CANVAS.layers.addBefore( layer, layerId );
Arguments
- layer - (object) new Layer.
- layerId - (string) Id of the layer the new layer should be added before.
Returns
- (object) Layer instance
LayerHash Method: addAfter
Adds a new layer after the specified layer.
Syntax
CANVAS.layers.addAfter( layer, layerId );
Arguments
- layer - (object) new Layer.
- layerId - (string) Id of the layer the new layer should be added after.
Returns
- (object) Layer instance
LayerHash Method: replace
Replaces the specified layer with a new layer.
Syntax
CANVAS.layers.replace( layer, layerId );
Arguments
- layer - (object) new Layer.
- layerId - (string) Id of the layer thats gets replaced.
Returns
- (object) Layer instance
LayerHash Method: remove
Removes the specified layer.
Syntax
CANVAS.layers.remove( layerId );
Arguments
- layerId - (string) Id of the layer to remove.
Returns
- (object) this (Layer instance)
LayerHash Method: removeAt
Removes the specified layer by its position in the list.
Syntax
CANVAS.layers.removeAt( layerPosition );
Arguments
- layerPosition - (number) Position of the layer to remove.
Returns
- (object) this (Layer instance)
LayerHash Method: promote
Promotes the specified layer. If the layer is already at the highest position promote will do nothing.
Syntax
CANVAS.layers.promote( layerId );
Arguments
- layerId - (string) Id of the layer thats get promoted.
Returns
- (object) the promoted Layer instance
LayerHash Method: demote
Demotes the specified layer. If the layer is already at position 0 demote will do nothing.
Syntax
CANVAS.layers.demote( layerId );
Arguments
- layerId - (string) Id of the layer thats get demoted.
Returns
- (object) the demoted Layer instance
LayerHash Method: swap
Swaps the positions of 2 layers.
Syntax
CANVAS.layers.swap( from, to );
Arguments
- from - (string) Id of the first layer.
- to - (string) Id of the second layer.
Returns
- (object) Layer instance that was selected by argument 'from'
LayerHash Method: swapByPos
Swaps the positions of 2 layers selected by their position in the list.
Syntax
CANVAS.layers.swapByPos( from, to );
Arguments
- from - (number) Position of the first layer.
- to - (number) Position of the second layer.
Returns
- (object) Layer instance that was selected by argument 'from'
LayerHash Method: get
Selects a layer from the list by its id.
Syntax
CANVAS.layers.get( layerId );
Arguments
- layerId - (string) Id of the layer to select.
Returns
- (object) Layer instance
LayerHash Method: getAt
Selects a layer from the list by its position.
Syntax
CANVAS.layers.getAt( layerPosition );
Arguments
- layerPosition - (number) Position of the layer to select.
Returns
- (object) Layer instance
LayerHash Method: draw
Draws all or a single Layer of the list.
Syntax
CANVAS.layers.draw( [ layerId ] );
Arguments
- layerId - (string, optional) Id of the layer to draw. If empty, all layers will be drawn.
Returns
- (object) this (LayerHash instance)
LayerHash Method: getOrder
Returns an array containing the current order of the layer stack.
Syntax
CANVAS.layers.getOrder( );
Returns
- (array) the current order of the layers
LayerHash Method: setOrder
Applies the modified order on the layer stack and rebuilds all internal tables.
Syntax
CANVAS.layers.setOrder( newOrder );
Arguments
- newOrder - (array, required) Array containing the ids of the layers. Make sure this is consistent with the layers that actually exist.
Returns
- (object) this (LayerHash instance)