We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi,
here's what I'm trying to do: I want to include a svg image in my p5 canvas and change the properties of certain shapes of that svg image using css. The image is basically a tree, featuring a trunk, big leaves and small leaves. In the svg file, I've grouped the corresponding shapes and assigned a class to each group like this:
<g class='tree'>
<g class='bigLeaf'>
<path style = 'fill:#000000;' d='this is the path of the first leaf ' />
</g>
<g class = 'trunk'>
<path ... />
</g>
</g>
The js. file looks like this:
var canvas;
var treeImg;
function preload(){
treeImg = loadImage("gfx/tree_simple_css.svg");
}
function setup(){
canvas = createCanvas(windowWidth/2,windowHeight/2);
canvas.position(windowWidth/4,windowHeight/4);
}
function draw(){
background('#C7F6F7');
image(treeImg, .5*width, .5*height);
}
With css I'd now like to be able to change e.g. the size of the tree with .tree{size: 50%} or hide the big leaves, you get the idea... Ultimately I'd like the sketch to behave responsive, that's why I'm trying to work with css here. This naive approach however doesn't work, instead what would be the best or the right way to do this?
I'd very much appreciate any help on this issue. Cheers!
Answers
Thank you for the links. I did look through the p5 reference beforehand and also checked out the tutorial, but I'm still not sure how to apply all of this since I'm relatively new to html and css and up til now only have been working with simple p5 only scripts. Since I'm including my p5 main.js into the index.html I don't want to work with svg containers in the index.html file, do I? With createImg I can access e.g. the size of the whole image, but I still don't know how to get down to the classes I set in the svg file itself. I'd be grateful if I could get some kind of instruction or a related example on how this works in principle. Thanks!
https://Forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text
http://ProcessingJS.org/reference/loadShape_/
https://Developer.Mozilla.org/en-US/docs/Web/API/HTMLCanvasElement
https://Developer.Mozilla.org/en-US/docs/Web/API/HTMLImageElement
According to the article from the 3rd link I've posted, there are 6 ways to include SVG in the HTML page:
http://EduTechWiki.Unige.ch/en/Using_SVG_with_HTML5_tutorial#How_can_I_include_SVG_in_HTML5
But not of all them allows later manipulation of the embedded ".svg" file though. #-o
Among them, the 1s which seem to have edit capability are: L-)
DISCLAIMER: Not tested. I've never dealt w/ ".svg" before!
The instructions below are just theories. Dunno whether they could actually work! =P~
<object>
to load your ".svg" file:http://EduTechWiki.Unige.ch/en/Using_SVG_with_HTML5_tutorial#Embeding_SVG_in_HTML_5_with_the_object_tag
const svg = createElement('object');
svg.attribute('type', 'image/svg+xml').attribute('data', 'gfx/tree_simple_css.svg');
.Thanks for your effort putting all this together. This seems to be more complicated than expected. I couldn't figure out a way to make it work with createElement and its attributes, so I guess I need to do it without p5...
Yeah, you can move to a more specialized SVG library. Or even try using 1 of them together w/ p5.js:
http://NoeticForce.com/Javascript-libraries-for-svg-animation
Alternatively, as I've already mentioned, Pjs library can deal w/ SVG via its loadShape():
http://ProcessingJS.org/reference/loadShape_/
Although you're most probably gonna need to use Java syntax rather than JS there. ~O)
BtW, here are 2 online examples I've found searching in OpenProcessing.org: :bz
And to get deep in JS' SVG, I've found out this collection of articles: L-) http://SVGDiscovery.com/
Well, good luck! B-)