We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi all, I'm trying to randomly colour the children of an SVG, similarly to the getChild example found here.
My issue is that the call to fill() changes the colour of all child elements instead of the one intended. I have included the xml from the svg file. This was created in illustrator with these settings:
SVG OUTPUT SETTINGS:
SVG Profiles: SVG 1.1
Type: Convert to Outline
Image Location: Embed
Advanced Options:
CSS Properties: Presentation Attributes
Decimal Places: 1
Encoding Unicode(UTF-8)
Responsive
I have tried svg 1.0, I've tried having the pieces(children) on indiviual layers or all on one layer and nothing seems to make a difference. My best guess is that it is an svg output issue.
Any help is appreciated. Thankyou.
SVG xml: http://pastebin.com/exjabwAE
Code:
PShape img, meatus, head, dGlans, mGlans, fGlans, tBones, tMeat, tongue,
lips, teethDirt, teeth, gums, uvula, dThroat, mThroat, fThroat, mouthInner;
PVector pos;
void setup() {
size(800, 800);
pos = new PVector(0, 0);
img = loadShape("WholeImageIII.svg");
meatus = img.getChild("dickSlit");
head = img.getChild("dickhead");
dGlans = img.getChild("deepGlans");
mGlans = img.getChild("midGlans");
fGlans = img.getChild("foreGlans");
tBones = img.getChild("toungueBones");
tMeat = img.getChild("tongueMeat");
tongue = img.getChild("tongue");
lips = img.getChild("lips");
teethDirt = img.getChild("teethDirt");
teeth = img.getChild("teeth");
gums = img.getChild("gums");
uvula = img.getChild("uvula");
dThroat = img.getChild("deepThroat");
mThroat = img.getChild("midThroat");
fThroat = img.getChild("foreThroat");
mouthInner = img.getChild("mouthInner");
}
void draw() {
background(255);
shape(img, pos.x, pos.y); //draw image
if (mousePressed) { //randomises the colour of the children
lips.disableStyle();
fill(random(255), random(255), random(255));
noStroke();
shape(lips, pos.x, pos.y);
head.disableStyle();
fill(random(255), random(255), random(255));
noStroke();
shape(head, pos.x, pos.y);
teeth.disableStyle();
fill(random(255), random(255), random(255));
noStroke();
shape(teeth, pos.x, pos.y);
tongue.disableStyle();
fill(random(255), random(255), random(255));
noStroke();
shape(tongue, pos.x, pos.y);
}
shape(mouthInner, pos.x, pos.y);
shape(fThroat, pos.x, pos.y);
shape(mThroat, pos.x, pos.y);
shape(dThroat, pos.x, pos.y);
shape(uvula, pos.x, pos.y);
shape(gums, pos.x, pos.y);
shape(teeth, pos.x, pos.y);
shape(teethDirt, pos.x, pos.y);
shape(lips, pos.x, pos.y);
shape(tongue, pos.x, pos.y);
shape(tMeat, pos.x, pos.y);
shape(tBones, pos.x, pos.y);
shape(fGlans, pos.x, pos.y);
shape(mGlans, pos.x, pos.y);
shape(dGlans, pos.x, pos.y);
shape(head, pos.x, pos.y);
shape(meatus, pos.x, pos.y); //draws the child
}
void keyPressed() {
meatus.enableStyle(); //reenables the css stylesheet
head.enableStyle();
dGlans.enableStyle();
mGlans.enableStyle();
fGlans .enableStyle();
tBones.enableStyle();
tMeat .enableStyle();
tongue.enableStyle();
lips .enableStyle();
teethDirt.enableStyle();
teeth.enableStyle();
gums.enableStyle();
uvula.enableStyle();
dThroat.enableStyle();
mThroat.enableStyle();
fThroat.enableStyle();
mouthInner.enableStyle();
}
Answers
@SnailPropulsionLabs --
Here is an example of a previous sketch that loops through the children of an SVG and assigns each one a random color:
The key thing in this approach is loop over the range
for( int i=0; i<shape.getChildCount(); i++ )
and each time accessshape.getChild(i)
before rendering each shape with a given fill.Thanks for the suggestion, I tried to implement that but it still appears to be applying each colour to all children of the svg.
I looked at the xml of the bot.svg image and then re-output my image to try and match the path structure as closely as possible hoping that was the difference, no luck. New xml here.
It looks like there is multicoloured flashes so all the colours are being applied, just to every child on every loop instead of selectively.
Current sketch version:
I've found that it seems to be the last colour called before the mouse is released is then used to colour the entire image.
In the new version below i've tried to separate concerns by having individual color variables for each child and changing the fill using them but i'm still getting the colour overlap issue as before. So I now know this isn't an svg issue, it seems to be a loop or order issue which still has me perplexed as to what i've done wrong.
Solution: Make it object oriented, then the objects hold their own colour information. Credit goes to Wootai on reddit for the oop suggestion.
Main Tab:
Child Tab: