Path with relative points in SVG - html

I have got svg with flexible height and width.
I'm trying to produce code equivalent to this:
<div id="svg-container">
<svg width='100%' height='100%' viewBox="0 0 100 100" preserveAspectRatio="none" style='background-color: yellow'>
<path
style="fill:none;stroke:blue;stroke-width:5;"
d="M0% 10% L50% 10% L50% 90% L100% 90%" <-here is the problem
/>
</svg>
</div>
So when I scale #svg-container i will get:
But since I can't create path with percent coordinates the best I can do is: (JSFiddle)
<div id="svg-container">
<svg width='100%' height='100%' viewBox="0 0 100 100" preserveAspectRatio="none" style='background-color: yellow'>
<path
style="fill:none;stroke:blue;stroke-width:5;"
d="M0 10 L50 10 L50 90 L100 90"
/>
</svg>
</div>
Which gives me this results:
On the second picture you can see problem with stretched stroke-width.
My question is: how to achieve behavior like on the first picture?
I'm not looking for JavaScript answers and breaking path.

Seems like you want a style of vector-effect: non-scaling-stroke
Note that not all UAs implement this but Chrome and Firefox certainly do.

I am sorry to say I think you will only be able to do this with JavaScript, even if this is not the answer that you're looking for. What is happening is that you are first generating the SVG path, then resizing it afterward so the image becomes stretched.
To use JavaScript you can use (browser.width/100)*10 to get 10%, for example, and this should work for all sizes of browser screen.
How are you re-scaling the image (i.e. is it a CSS #media query)? It might be possible to draw the path after the re-scale, but again I feel this will need JS as you will need to load the content after the browser load.
Sorry to answer in opposition to what you have asked, but unless there is an alternative I think this is the only way you can do this.

Related

Does SVG text have a minimal size?

It seems I'm hitting a lower limit on font-size or text in SVG. I can't seem to find anything about it in the docs, though.
I have a huge div, that contains a huge SVG part of which is filling the viewport completely. As a result my font-size can be less than 0.0105pt. At which point the text just disappears!
See this jsfiddle: https://jsfiddle.net/wc95t4dk/30/
<div style="position:absolute;top:-100000px; left: -100000px; width: 200000px; height:200000px">
<svg width="100%" height="100%" viewbox="0 0 100 100">
<text x="50.1" y="50.1" font-size="0.1pt" stroke="red" stroke-width="0.01" fill="red">My text</text>
<text x="50.15" y="50.15" font-size="0.011pt" stroke="green" stroke-width="0.001" fill="green">My text2</text>
<text x="50.25" y="50.15" font-size="0.01pt" stroke="blue" stroke-width="0.001" fill="blue">My text2</text>
</svg>
</div>
The red and green text displays fine but the blue one doesn't!
I am not sure about all browsers, but neither Chrome nor Firefox will render text whose font size is less than 0.5 screen pixels.
See: http://jsfiddle.net/vsxgy3fq/5/
However things get more complicated when there are SVG viewBox transforms (and other transforms) involved. I know that browsers have (or had) bugs related to this size check.
According to this answer https://stackoverflow.com/a/27165622/227507
it is caused by implementation detail where only 8 bits of precision (1/256) are reserved for decimal digits

css SVG background - strange transparent line around

I have tried to use svg image as background-image in css. And I have faced with strange behavior. Element that has svg background has strange transparent line around.
In the example below it is shown as line between the first and the second div. Size of the line changes depending on width of the element.
http://jsfiddle.net/mahnunchik/g1ux4e7o/
Screenshot from jsfiddle:
It reproduces at least in Chrome 37 and FF 32 on Windows and Linux
Svg image is quite simple:
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1920 76" enable-background="new 0 0 1920 76">
<path fill="#DE4943" d="M0 76L1921 0 0 0z"/>
</svg>
Any idea how to fix that? Or how to explain that?
Try adding preserveAspectRatio="none" to your <svg> element.
Also you don't need the enable-background attribute.
Try to add stroke-width="0" to the path

Resize svg image and areas

This is my first time using SVG so apologies if this is a stupid question but I am trying to create a clickable continent map on my site and have acquired an SVG image with the continents mapped out correctly from wikipedia.
http://commons.wikimedia.org/wiki/File:Continents.svg
However, this image is 585 x 299 pixels and I require an image that is 292 x 137 pixels. I've read online that these images are scalable and that all you need to do is modify the width and height value in the svg definition so I have done so here:
<svg width="292" height="137" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg" preserveAspectRatio="xMidYMid meet">
However, this only scales the canvas as such and does not scale the internal areas. How do I get the areas to scale to the modified dimensions of the image?
The SVG will be any size you tell it to in the CSS
JSfiddle Demo
CSS
svg {
width:292px;
height:137px;
border:1px solid grey
}
This works with or without the dimensions stated in the SVG. the important item is the 'viewbox' which sets the co-ordinate structure for the SVG.
The only way I have found so far is to wrap all of my areas in a tag and they apply a scale of
<g transform="scale(0.68)">
Use a viewBox like this :
<svg
xmlns="http://www.w3.org/2000/svg"
version="1.0"
width="292pt" height="137pt"
viewBox="0 0 468 239"
preserveAspectRatio="xMidYMid meet"
>
<g
transform="translate(0,239) scale(0.016963,-0.016963)"
fill="#000000"
stroke="none"
>
http://jsfiddle.net/Vac2Q/4147/
Add a viewBox attribute (viewBox="0 0 585 299" is probably what you want) to the svg element. We can simulate what that would look like by using a fragment identifier which will impose a viewBox on the original file e.g.
http://upload.wikimedia.org/wikipedia/commons/9/95/Continents.svg#svgView(viewBox(0,0,529,290))
That sure displays differently in Firefox.

element img with SVG Firefox

Trying to make compatible all svg's images in different browsers I have an issue with Firefox.
I am using a .SVG image in an element img. Something like that:
<img src="image.svg" />
If you can see the image below,seems that the SVG is repeated. If I will used the svg as background-image, problably I will can do background-repeat: no-repeat, but in this case I need use the svg at img element and can't specify background-repeat to an element img.
How I can fix this? All answers will be apreciate.
I'm going to explane this strange case..
This is the code of my SVG
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="Capa_1" x="0px" y="0px" width="21.391px" height="18.881px" viewBox="228.645 224.748 21.391 18.881" enable-background="new 228.645 224.748 21.391 18.881" xml:space="preserve" xmlns:xml="http://www.w3.org/XML/1998/namespace">
<g>
<path fill="#CEE4C4" d="M243.708,230.977c-3.494,0-6.328,2.83-6.328,6.323c0,3.494,2.834,6.328,6.328,6.328 c3.496,0,6.328-2.834,6.328-6.326C250.036,233.808,247.204,230.977,243.708,230.977z M244.437,242.177h-1.508v-1.805h1.508V242.177 z M244.308,239.066h-1.24l-0.129-5.723h1.498L244.308,239.066z"/>
<g>
<path fill="#9FAEB1" d="M237.384,237.376c0-0.024-0.004-0.051-0.004-0.076c0-0.627,0.096-1.229,0.266-1.803l-3,2.25v-3h-4v-8h12 v4.324c0.347-0.059,0.699-0.096,1.062-0.096c0.318,0,0.63,0.031,0.938,0.076v-6.305h-16v12h4v4L237.384,237.376z"/>
</g>
</g>
</svg>
The element SVG measures are 21x19 px.
BUT! the measure of the element SVG inside are 22x20 px.
So, if i put the measures of svg (21x19) that don't fix the initial problem, however if i put width and height of the element (22x20) solves the problem.
Ok i see what's your problem now. You should try to add for img tag the exact width and hight of the image you try to add, just to ensure it will display the expected dimension. Try it and see what happens, if not please make a jsfiddle to can help you

How do I scale a stubborn SVG embedded with the <object> tag?

I have some SVG files that specifies width and height as well as viewbox like this:
<svg width="576pt" height="432pt" viewBox="0 0 576 432" > ...
but how to display them in the browser at a size I decide? I want them smaller and have tried:
<object width="400" data="image.svg"></object>
but then I get visible scrollbars.
It works if I change the SVG files to set width and height to 100% instead, but I want to decide the size in the HTML regardless of what sizes are used in the SVG file. Is this possible ?
You can add "preserveAspectRatio" and "viewBox" attributes to the <svg> tag to accomplish this.
Open the .svg file in an editor and find the <svg> tag.
in that tag, add the following attributes:
preserveAspectRatio="xMinYMin meet"
viewBox="0 0 {width} {height}"
Replace {width} and {height} with some defaults for the viewBox. I used the values from the "width" and "height" attributes of the SVG tag and it seemed to work.
Save the SVG and it should now scale as expected.
I found this information here:
https://blueprints.launchpad.net/inkscape/+spec/allow-browser-resizing
None of the answers given here worked for me when I asked this back in 2009. As I now had the same issue again I noticed that using the <img> tag and width together with an svg works fine.
<img width="400" src="image.svg" />
<body>
<div>
<object type="image/svg+xml" data="img/logo.svg">
<img src="img/logo.svg" alt="Browser fail" />
</object>
</div>
img/logo.svg
...
<svg
width="100%"
height="100%"
viewBox="0 0 640 80"
xmlns="http://www.w3.org/2000/svg"
version="1.1" />
This setup worked for me.
You can reach into the embedded svg using JavaScript:
var svg = document.getElementsByTagName('object')[0].\
contentDocument.getElementsByTagName('svg')[0];
svg.removeAttribute('width');
svg.removeAttribute('height');
Since your svg already has a viewBox, Firefox should scale the 576 pixel width in the viewBox to the 400 pixel width in your document. Other svgs might benefit from a new viewBox derived from the advertised width and height (these are often the same numbers). Other browsers might benefit from different svg tweaks.
I encountered a problem where iOS on an iPad would not correctly resize SVG images in a <object> tag.
The CSS style would increase or decrease size of the <object> container, but the image inside of it would not be modified (on iPad, iOS 7).
The SVG images were exported from Adobe Illustrator, and the solution turned out to be replacing the width and height in this:
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="481.89px" height="294.843px" viewBox="0 0 481.89 294.843"
enable-background="new 0 0 481.89 294.843"
xml:space="preserve">
with:
width="100%" height="100%"
I needed to use the <object> tag because the <img> tag does not currently support embedding bitmapped images in SVG's.
Set the missing viewbox and fill in the height and width values of the set height and height attributes in the svg tag
Then scale the picture simply by setting the height and width to the desired percent values. Good luck.
You can set a fixed aspect ratio with preserveAspectRatio="x200Y200 meet, but it's not necessary
e.g.
<svg
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
width="10%"
height="10%"
preserveAspectRatio="x200Y200 meet"
viewBox="0 0 350 350"
id="svg2"
version="1.1"
inkscape:version="0.48.0 r9654"
sodipodi:docname="namesvg.svg">
Just use CSS to make the browser resize the SVG!
Like so:
<object style="width:30%">
See http://www.vlado-do.de/svg_test/ for more details. I just also tried it locally with an SVG that has its width and height given in "pt". It works well in Firefox.
Let see. I had to refresh my memory on SVG, I haven't used it much these years.
From what I found today, it seems that if you specify dimension of objects without units, they have a fixed size (in pixels, I think). Apparently, then, there is no way to resize them when you resize the SVG (it only change the viewport/canvas size).
Unless, as pointed out, you specify the size of the SVG in percentage OR specify a viewBox (eg. viewBox="0 0 600 500").
Now, if you have no way to change the exported SVG, you are out of luck, I fear. What library do you use?
Here is a PHP solution using QueryPath based on Jim Keller's answer.
Once QueryPath is loaded just pass your svg script to the function.
function scaleableSVG($svg){
$qp = qp($svg, 'svg');
$width = $qp->attr('width');
$height = $qp->attr('height');
$qp->removeAttr('width')->removeAttr('height');
$qp->attr('preserveAspectRatio', "xMinYMin meet");
$qp->attr('viewBox', "0 0 $width $height");
return $qp->html();
}