AS3SVGRenderer icon indicating copy to clipboard operation
AS3SVGRenderer copied to clipboard

how to get a list of elements of SVG

Open dezomer opened this issue 11 years ago • 4 comments

Hello,

how can i get a list of elements of an SVG when selecting it.

i can see in com.lorenz.SVG.display.SVG the "numElements" but i dont know how to access them!

Thanks in advance & regards

François

dezomer avatar Oct 19 '13 21:10 dezomer

Every svg container has the methods: getElementAt, and numElements. Using that you can iterate through the children of an element.

If you want to do that on a SVGDocument, it would be:

for(var i = 0; i < document.numElements; i++){
     var element = document.getElementAt(i);
     //do something with element
}

It is like a tree, not a flat list. To visit all elements, you will need to do a recursive function.

lucaslorentz avatar Oct 19 '13 21:10 lucaslorentz

thank you very much- works perfect !

is there also a way to get al list of filters and styles ?

Thanks in advance & regards

François

dezomer avatar Oct 21 '13 12:10 dezomer

SVGDocument has a list of definitions. All elements with id goes to definitions, so, your filters will be there, inside filter collections. You can use the following functions to retrieve them

listDefinitions():Vector.<String> //Returns a list with the ids of all definitions
hasDefinition(id:String):Boolean //Returns if a definition with that id exists
getDefinition(id:String):Object //Returns the definition, it may be a SVGFilterCollection, SVGElement, SVGGradient

Styles are also on the SVGDocument. Take a look on those functions:

listStyleDeclarations():Vector.<String> //Returns a list of selectors of all style declarations
getStyleDeclaration(selector:String):StyleDeclaration  //Get the style declaration

Each style declaration is composed by the selector and the properties.

rect {
     fill: #000;
}

Take a look on: https://github.com/LucasLorentz/AS3SVGRenderer/blob/master/SVGRenderer/src/com/lorentz/SVG/data/style/StyleDeclaration.as

Btw, just realized a bug on the styles behavior, the library index styleDeclarations by it's selector, so, if you have a SVG with 2 styles block with the same selector, only the last one will work.

lucaslorentz avatar Oct 21 '13 18:10 lucaslorentz

Yesss thank you very much - this works perfect !

now i need a way to add to add filters and patterns - i found document.addDefinition function - is this the way to do is - and if how can i convert the xml-pattern-format into the rigrt object-format ?

Thanks in advance & regards

François

dezomer avatar Oct 30 '13 16:10 dezomer