We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi, I found today that I can get the child count of SVGs. In trying to use it to load svg with different count of children so I can target each child with styling properties I encountered an issue where the vectors aren't displaying on screen. I tried displaying the svg without loading the children and they display fine.
I also can't find what I am doing wrong, I get no errors.
Anyone encountered this before or has any idea why is this happening?
I am on macOS using processing 3.3.6
String[] shapeNames = {"i-02.svg", "i-03.svg", "i-04.svg", "i-05.svg", "i-06.svg", "i-07.svg" };
PShape[] shapes;
PShape shapeToDraw, childToDraw;
int gridSize = 100;
void setup() {
size(1280, 720);
smooth();
setup_();
}
void setup_() {
shapes = new PShape[shapeNames.length];
for (int s = 0; s < shapes.length-1; s++) {
shapes[s] = loadShape(shapeNames[s]);
}
}
void draw() {
//Grid
//for(int x = 0; x < width-gridSize; x += gridSize){
// for(int y = 0; y < height-gridSize; y += gridSize){
// }
//}
shapeToDraw = shapes[int(random(5))];
shapeToDraw.disableStyle();
println(shapeToDraw.getChildCount());
for(int c = 0; c < shapeToDraw.getChildCount(); c++){
childToDraw = shapeToDraw.getChild(c);
println(childToDraw);
childToDraw.disableStyle();
fill(random(255),random(255),random(255));
shape(childToDraw, width/2,height/2, gridSize,gridSize);
ellipse(width/2, height/2, 10,10);
}
noLoop();
}
void mousePressed(){
redraw();
}
Answers
Child children?
I don't follow, sorry.
Just thought, maye the children are made of sub-children
Oh, Thanks for your answer Chrisir. I made sure my svg's don't have any sub-children.
I've managed to make it work in a hacky way. I can get the children by using getChild("ID of element")
So I named all the children from a to z and am using the child count index to reference an String array with the names of the layers {"a", "b"...etc}
I'll post it later
@brtdesign --
Here is a related example from a previous sketch that loops through the children of an SVG and assigns each one a random color:
https://forum.processing.org/two/discussion/comment/77199/#Comment_77199
The key thing in this approach is, as you say, to loop over the range for( int i=0; i<shape.getChildCount(); i++ ) and each time access shape.getChild(i).