SVG node background tutorial
See original GitHub issueI have the demand for individual top/right/bottom/left padding when styling a node. At the moment only padding
is supported which affects all 4 sides at once.
My application needs to decorate each node with an icon (additionally to the node label). To make room for the icon I need extra padding just on one side.
Issue Analytics
- State:
- Created 6 years ago
- Comments:20 (9 by maintainers)
Top Results From Across the Web
How to add SVGs with CSS (background-image)
Let's look at how to add SVGs into the CSS property background-image and how the related background properties can transform the results.
Read more >How to Use SVG Patterns as Backgrounds
This tutorial is available in both video and textual form–here's a breakdown of what you'll learn: We'll begin by looking at the more...
Read more >How to set the SVG background color ? - GeeksforGeeks
The SVG stands for Scalable Vector Graphics. The SVG background is used to draw any kind of shape, set any color you want...
Read more >06: Using SVG - SVG as background-image - CSS-Tricks
SVG images can be used as background-image in CSS as well, just like PNG, JPG, or GIF. ... All the same awesomeness of...
Read more >background-image style of node not working with SVG images
This is a runnable code to demonstrate the use of background-image with SVG graphic. The SVG code must be appropriately encoded before use....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@jesuspc Eventually I got SVG node background images working, in all major browsers, including european
<text>
chars, and FontAwesome icons.Some gotchas:
Don’t use Base64 encoding in SVG data URLs. Use URL-encoding instead. See https://github.com/cytoscape/cytoscape.js/issues/1873
The node render function has to return an object containing the SVG (e.g. as data URL) and its dimensions. See https://github.com/cytoscape/cytoscape.js/issues/1802#issuecomment-310386514
You can’t SVG draw beyond a node’s bounding box. See https://github.com/cytoscape/cytoscape.js/issues/1865
Webfonts (e.g. FontAwesome) do not work in SVG
<text>
elements. As fas as I understand this is due to browser oddities (Canvas/SVG/Webfonts), and is not a Cytoscape problem. A workaround is to render the plain SVG glyphs as<path>
elements instead. See https://github.com/cytoscape/cytoscape.js/issues/1867The XML namespace (
<svg>
element’sxmlns
attribute) is required. A XML declaration (<?xml version="1.0" encoding="..."?>
) is not required. See https://github.com/cytoscape/cytoscape.js/issues/1873As an example see the Cytoscape code in my project, in particular the
renderNode
function: https://github.com/jri/dm5-topicmap-panel/blob/master/src/cytoscape-renderer.js#L350Leaving this here incase anyone else runs into the same issue I did. SVG’s fill the node by default, which isn’t the most beautiful thing, so lets add some padding* (not actual padding, just scaling down the icon within the node).
Small snippet to calculate the width/height/scale/position of the
<path/>
within the<svg/>
based on a few params. Still requires you to know the width/height of the<path d=""/>
value.If you’re lucky enough to find an svg icon lib that has all the same size svg icons, even better. My case I have the icon set to
32x32
, but you could calculate the size if you had varying<path d=""/>
sizes.