SVG `use` is rendering blank image - svg
I am trying to use SVGs as CSS background images, and have everything working except for importing external files with use.
Here is what I have:
CSS and HTML:
.shape1 {
background: url("shapes/shape1.svg") no-repeat;
}
<div class="shape1"></div>
Then the shapes/shape1.svg file:
<svg viewBox="0 0 100 200" xmlns="http://www.w3.org/2000/svg">
<path d="
M 10 10
l 0 180
" fill="none" stroke="#000" stroke-width="1px"/>
<use href="parts/part1.svg#build"/>
</svg>
It references the parts/part1.svg file, which contains this:
<svg viewBox="0 0 100 200" xmlns="http://www.w3.org/2000/svg">
<defs>
<path id="build" d="
M 90 10
l 0 180
" fill="none" stroke="#000" stroke-width="1px"/>
</defs>
</svg>
However, when I render everything, it only displays one line, not two.
I've tried a few things. I've tried removing the defs and just loading directly, like this:
<svg viewBox="0 0 100 200" xmlns="http://www.w3.org/2000/svg">
<path d="
M 10 10
l 0 180
" fill="none" stroke="#000" stroke-width="1px"/>
<use href="parts/part1.svg"/>
</svg>
<svg viewBox="0 0 100 200" xmlns="http://www.w3.org/2000/svg">
<path d="
M 90 10
l 0 180
" fill="none" stroke="#000" stroke-width="1px"/>
</svg>
I've also tried messing with the file paths. My directory structure is this:
index.html
shapes/
shapes/shape1.svg
parts/
parts/part1.svg
I've tried ../parts/part1.svg, etc.. Also, I am running this directly from the filesystem, so the path is: file:///Users/me/Desktop/test/index.html. I am getting no errors in the web console. Any help would be appreciated!
I am on Chrome and only care about this working on Chrome for now.
When SVG is used as an image the SVG file must be self-contained for privacy reasons.
So shape1.svg cannot access anything outside itself. You'd have to embed part1.svg into shape1.svg.
Alternatively don't use SVG as a background image, embed it via an object or iframe tag or have it inline.
Related
Redundant <path> element inside an svg file
Thw following two SVG markups render identically: <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"> <path style={inputAdornmentIcon} d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/> <path d="M0 0h24v24H0z" fill="none"/> </svg> <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"> <path style={inputAdornmentIcon} d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/> </svg> Any idea what the second path element (<path d="M0 0h24v24H0z" fill="none"/>) in the first version actually does?
The extra path in your code is a a rectangle that covers all the svg canvas. As I've commented Google's icons are always using an extra rectangle like this for a background, but the rectangle is coming first. It's always a good idea to remove redundant elements and to simplify the markup.
SVG path not filling it's interior
I have a svg image which has four paths which draw the perimeters of cuboid figures with curved corners. I want to fill the interior of these figures. However, the fill="grey" property I am using on the path does not work. <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 40" x="0px" y="0px" fill="grey"> <path fill="grey" d="M5,14h6a3,3,0,0,0,3-3V5a3,3,0,0,0-3-3H5A3,3,0,0,0,2,5v6A3,3,0,0,0,5,14ZM4,5A1,1,0,0,1,5,4h6a1,1,0,0,1,1,1v6a1,1,0,0,1-1,1H5a1,1,0,0,1-1-1Z"/> <path d="M21,14h6a3,3,0,0,0,3-3V5a3,3,0,0,0-3-3H21a3,3,0,0,0-3,3v6A3,3,0,0,0,21,14ZM20,5a1,1,0,0,1,1-1h6a1,1,0,0,1,1,1v6a1,1,0,0,1-1,1H21a1,1,0,0,1-1-1Z"/> <path d="M2,27a3,3,0,0,0,3,3h6a3,3,0,0,0,3-3V21a3,3,0,0,0-3-3H5a3,3,0,0,0-3,3Zm2-6a1,1,0,0,1,1-1h6a1,1,0,0,1,1,1v6a1,1,0,0,1-1,1H5a1,1,0,0,1-1-1Z"/> <path d="M18,27a3,3,0,0,0,3,3h6a3,3,0,0,0,3-3V21a3,3,0,0,0-3-3H21a3,3,0,0,0-3,3Zm2-6a1,1,0,0,1,1-1h6a1,1,0,0,1,1,1v6a1,1,0,0,1-1,1H21a1,1,0,0,1-1-1Z"/> </svg> Any thoughts on what I need to do to fill in these cuboids? Thanks
Your paths are filed grey. The problem is that there is a "hole" in in every path. I've removed the "hole <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 40" fill="grey"> <path d="M5,14h6a3,3,0,0,0,3-3V5a3,3,0,0,0-3-3H5A3,3,0,0,0,2,5v6A3,3,0,0,0,5,14Z"/> <path d="M21,14h6a3,3,0,0,0,3-3V5a3,3,0,0,0-3-3H21a3,3,0,0,0-3,3v6A3,3,0,0,0,21,14Z"/> <path d="M2,27a3,3,0,0,0,3,3h6a3,3,0,0,0,3-3V21a3,3,0,0,0-3-3H5a3,3,0,0,0-3,3Z"/> <path d="M18,27a3,3,0,0,0,3,3h6a3,3,0,0,0,3-3V21a3,3,0,0,0-3-3H21a3,3,0,0,0-3,3Z"/> </svg>
SVG Path rendering not consistent across browsers
I'm experiencing two different renderings of the same SVG path on two webkit browsers : Chrome and Safari. Here is the markup : <svg class="drillPath0" viewBox="0 0 775 310"> <path fill="transparent" stroke="#000" d="M 100 100 V 0"></path> <path fill="transparent" stroke="#000" d="M 200 100 H 100"></path> <path fill="transparent" stroke="#000" d="M 200 100 V 200"></path> </svg> On Chrome, it properly starts from the top of the SVG element. On Safari, it has a 100px offset. There is a jsFiddle ready if needed.
This happened to be a Safari bug, that has been fixed with the 7.0.1 update and iOS 8.
Fill polygon with pattern doesn't work with leaflet
I try to render a SVG polygon filled with pattern. SVG.path filled with pattern doesn't work. As you can see in this jsfiddle, filled background shows up transparent in Firefox and black in Chrome. The example is based on leaflet GeoJSON Example and uses the diagonalHatch pattern described by carto.net. <defs> <pattern id="diagonalHatch" patternUnits="userSpaceOnUse" x="0" y="0" width="105" height="105"> <g style="fill:none; stroke:black; stroke-width:1"> <path d="M0 90 l15,15"/><path d="M0 75 l30,30"/> <path d="M0 60 l45,45"/><path d="M0 45 l60,60"/> <path d="M0 30 l75,75"/><path d="M0 15 l90,90"/> <path d="M0 0 l105,105"/><path d="M15 0 l90,90"/> <path d="M30 0 l75,75"/><path d="M45 0 l60,60"/> <path d="M60 0 l45,45"/><path d="M75 0 l30,30"/> <path d="M90 0 l15,15"/> </g> </pattern> </defs> As this jsfiddle shows, copying the SVG polygons below the map, makes it work on Chrome but not on Firefox. Apply a fill pattern adding this style to SVG.path: style="fill: url(#diagonalHatch)" I'm really not sure if this is a bug in leaflet or some conflict with SVG implementation on Firefox and Chrome.
I've wrote a leaflet plugin for this, you may try it, https://github.com/lnaweisu/leaflet-polygon-fillPattern
Maybe you can try setting the path via the shape's attribute as mentioned in this thread: Leaflet polygon with fuzzy outline // Set filter attribute on the polygon polygon._path.setAttribute('filter', 'url(#blur)');
Can strokes be used as part of clip-paths in SVGs?
I am in the middle of writing SVG output from MuPDF, and I've run up against what seems to be a limitation in the capabilities of SVG. I thought I'd ask here in case this was a known problem with a known workaround (or in case I'm doing something stupid!) I have the following SVG: <?xml version="1.0" standalone="no"?> <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="21.59cm" height="27.94cm" viewBox="0 0 600 600"> <path stroke-width="12" fill="none" stroke="#0000ff" d="M 150 300 L 80 300 L 80 370 L 150 370 " /> <clipPath id="cp0"> <path stroke-width="12" fill="none" stroke="#000000" d="M 150 300 L 80 300 L 80 370 L 150 370 " /> </clipPath> <g clip-path="url(#cp0)"> <rect fill="#ff0000" x="0" y="0" width="600" height="600"/> </g> </svg> This draws a stroked path (shaped like '[' in blue). Then it sets the same path to be a clipping path, and fills the clipping path in red. I was hoping that the clipping path would be set to the stroked version of the path, and hence the red shape would exactly overwrite the blue one. In my tests here however, the "fill or strokedness" of the path is ignored, and the path is "filled" - hence I get a red square drawn within the blue shape. Is there a way to get the behaviour I was hoping for? Or am I going to have to write code to manually flatten and stroke paths before outputting them to SVG? Thanks in advance for any replies!
Clip-paths in svg are meant to be just the shape, not the traits of the shape, so in other words you'll not get the stroke included. What you can do is use a mask instead, setting the stroke of the path in the mask to white. Here's an example: <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" viewBox="0 0 600 600"> <defs> <mask id="m0" maskUnits="userSpaceOnUse" x="0" y="0" width="600" height="600"> <path fill="none" stroke="white" stroke-width="5" d="M 150 300 L 80 300 L 80 370 L 150 370" /> </mask> </defs> <path stroke-width="12" fill="none" stroke="#0000ff" d="M 150 300 L 80 300 L 80 370 L 150 370 " /> <g mask="url(#m0)"> <rect fill="yellow" x="0" y="0" width="600" height="600" /> </g> </svg>