Constructor
new Drawing(svgElem, optIdPrefixopt)
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
svgElem |
SVGSVGElement | The SVG DOM Element that this JS object encapsulates. If the svgElem has a se:nonce attribute on it, then IDs will use the nonce as they are generated. |
||
optIdPrefix |
string |
<optional> |
svg_ | The ID prefix to use. |
- Source:
Throws:
-
If not initialized with an SVG element
- Type
- Error
Members
all_layers :Array.<Layer>
The z-ordered array of Layer objects. Each layer has a name and group element. The first layer is the one at the bottom of the rendering.
Type:
- Array.<Layer>
- Source:
current_layer :Layer
The current layer being used.
Type:
- Layer
- Source:
idPrefix :string
The prefix to prepend to each element id in the drawing.
Type:
- string
- Source:
layer_map :PlainObject.<string, Layer>
Map of all_layers by name.
Note: Layers are ordered, but referenced externally by name; so, we need both container types depending on which function is called (i.e. all_layers and layer_map).
Type:
- PlainObject.<string, Layer>
- Source:
(non-null) nonce_ :string
The nonce to use to uniquely identify elements across drawings.
Type:
- string
- Source:
obj_num :Integer
The latest object number used in this drawing.
Type:
- Source:
releasedNums :Array.<Integer>
An array of released element ids to immediately reuse.
Type:
- Array.<Integer>
- Source:
svgElem_ :SVGSVGElement
The SVG DOM Element that represents this drawing.
Type:
- SVGSVGElement
- Source:
Methods
clearNonce() → {void}
Clears any previously set nonce.
- Source:
Returns:
- Type
- void
cloneLayer(name, hrService) → {SVGGElement}
Creates a copy of the current layer with the given name and makes it the current layer.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | The given name. If the layer name exists, a new name will be generated. |
hrService |
module:history.HistoryRecordingService | History recording service |
- Source:
Returns:
The SVGGElement of the new layer, which is also the current layer of this drawing.
- Type
- SVGGElement
copyElem(el) → {Element}
Create a clone of an element, updating its ID and its children's IDs when needed.
Parameters:
Name | Type | Description |
---|---|---|
el |
Element | DOM element to clone |
- Source:
Returns:
- Type
- Element
createLayer(name, hrService) → {SVGGElement}
Creates a new top-level layer in the drawing with the given name and makes it the current layer.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | The given name. If the layer name exists, a new name will be generated. |
hrService |
module:history.HistoryRecordingService | History recording service |
- Source:
Returns:
The SVGGElement of the new layer, which is also the current layer of this drawing.
- Type
- SVGGElement
deleteCurrentLayer() → {SVGGElement}
Deletes the current layer from the drawing and then clears the selection. This function then calls the 'changed' handler. This is an undoable action.
- Source:
- To Do:
-
- Does this actually call the 'changed' handler?
Returns:
The SVGGElement of the layer removed or null.
- Type
- SVGGElement
getCurrentLayer() → {SVGGElement|null}
- Source:
Returns:
The SVGGElement representing the current layer.
- Type
- SVGGElement | null
getCurrentLayerName() → {string}
Returns the name of the currently selected layer. If an error occurs, an empty string is returned.
- Source:
Returns:
The name of the currently active layer (or the empty string if none found).
- Type
- string
getElem_(id) → {Element}
Parameters:
Name | Type | Description |
---|---|---|
id |
string | Element ID to retrieve |
- Source:
Returns:
SVG element within the root SVGSVGElement
- Type
- Element
getId() → {string}
Returns the latest object id as a string.
- Source:
Returns:
The latest object Id.
- Type
- string
getLayerByName(name) → {SVGGElement}
Get a layer by name.
Parameters:
Name | Type | Description |
---|---|---|
name |
string |
- Source:
Returns:
The SVGGElement representing the named layer or null.
- Type
- SVGGElement
getLayerName(i) → {string}
Returns the name of the ith layer. If the index is out of range, an empty string is returned.
Parameters:
Name | Type | Description |
---|---|---|
i |
Integer | The zero-based index of the layer you are querying. |
- Source:
Returns:
The name of the ith layer (or the empty string if none found)
- Type
- string
getLayerOpacity(layerName) → (nullable) {Float}
Returns the opacity of the given layer. If the input name is not a layer, null
is returned.
Parameters:
Name | Type | Description |
---|---|---|
layerName |
string | name of the layer on which to get the opacity |
- Source:
Returns:
The opacity value of the given layer. This will be a value between 0.0 and 1.0, or null
if layerName
is not a valid layer
- Type
- Float
getLayerVisibility(layerName) → {boolean}
Returns whether the layer is visible. If the layer name is not valid,
then this function returns false
.
Parameters:
Name | Type | Description |
---|---|---|
layerName |
string | The name of the layer which you want to query. |
- Source:
Returns:
The visibility state of the layer, or false
if the layer name was invalid.
- Type
- boolean
getNextId() → {string}
Returns the next object Id as a string.
- Source:
Returns:
The next object Id to use.
- Type
- string
getNonce() → (non-null) {string|Integer}
- Source:
Returns:
The previously set nonce
- Type
- string | Integer
getNumLayers() → {Integer}
Returns the number of layers in the current drawing.
- Source:
Returns:
The number of layers in the current drawing.
- Type
- Integer
getSvgElem() → {SVGSVGElement}
- Source:
Returns:
- Type
- SVGSVGElement
hasLayer(name) → {boolean}
Check if layer with given name already exists.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | The layer name to check |
- Source:
Returns:
- Type
- boolean
identifyLayers() → {void}
Updates layer system and sets the current layer to the
top-most layer (last <g>
child of this drawing).
- Source:
Returns:
- Type
- void
indexCurrentLayer(name) → {boolean}
Sets the current layer. If the name is not a valid layer name, then this
function returns false
. Otherwise it returns true
. This is not an
undo-able action.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | The name of the layer you want to switch to. |
- Source:
Returns:
true
if the current layer was switched, otherwise false
- Type
- boolean
mergeAllLayers(hrService) → {void}
Parameters:
Name | Type | Description |
---|---|---|
hrService |
module:history.HistoryRecordingService |
- Source:
Returns:
- Type
- void
mergeLayer(hrService) → {void}
Parameters:
Name | Type | Description |
---|---|---|
hrService |
module:history.HistoryRecordingService |
- Source:
Returns:
- Type
- void
releaseId(id) → {boolean}
Releases the object Id, letting it be used as the next id in getNextId(). This method DOES NOT remove any elements from the DOM, it is expected that client code will do this.
Parameters:
Name | Type | Description |
---|---|---|
id |
string | The id to release. |
- Source:
Returns:
True if the id was valid to be released, false otherwise.
- Type
- boolean
setCurrentLayer(name) → {boolean}
Sets the current layer. If the name is not a valid layer name, then this
function returns false
. Otherwise it returns true
. This is not an
undo-able action.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | The name of the layer you want to switch to. |
- Source:
Returns:
true
if the current layer was switched, otherwise false
- Type
- boolean
setCurrentLayerName(name, hrService) → {string|null}
Set the current layer's name.
Parameters:
Name | Type | Description |
---|---|---|
name |
string | The new name. |
hrService |
module:history.HistoryRecordingService | History recording service |
- Source:
Returns:
The new name if changed; otherwise, null.
- Type
- string | null
setCurrentLayerPosition(newpos) → {Object|null}
Set the current layer's position.
Parameters:
Name | Type | Description |
---|---|---|
newpos |
Integer | The zero-based index of the new position of the layer. Range should be 0 to layers-1 |
- Source:
Returns:
If the name was changed, returns {title:SVGGElement, previousName:string}; otherwise null.
- Type
- Object | null
setLayerOpacity(layerName, opacity) → {void}
Sets the opacity of the given layer. If the input name is not a layer, nothing happens. If opacity is not a value between 0.0 and 1.0, then nothing happens. NOTE: this function exists solely to apply a highlighting/de-emphasis effect to a layer. When it is possible for a user to affect the opacity of a layer, we will need to allow this function to produce an undo-able action.
Parameters:
Name | Type | Description |
---|---|---|
layerName |
string | Name of the layer on which to set the opacity |
opacity |
Float | A float value in the range 0.0-1.0 |
- Source:
Returns:
- Type
- void
setLayerVisibility(layerName, bVisible) → (nullable) {SVGGElement}
Sets the visibility of the layer. If the layer name is not valid, this
function returns null
, otherwise it returns the SVGElement
representing
the layer. This is an undo-able action.
Parameters:
Name | Type | Description |
---|---|---|
layerName |
string | The name of the layer to change the visibility |
bVisible |
boolean | Whether the layer should be visible |
- Source:
Returns:
The SVGGElement representing the layer if the
layerName
was valid, otherwise null
.
- Type
- SVGGElement
setNonce(nnon-null) → {void}
Parameters:
Name | Type | Description |
---|---|---|
n |
string | Integer | The nonce to set |
- Source:
Returns:
- Type
- void