SVG viewBox, rectangle, and polyline - svg

The SVG attribute viewBox appears to be inconsistent. It seems it doesn't scale all SVG graphics primitives the same way. Here's a sample SVG file that has a rectangle, a circle, a polyline, and a polygon. The rectangle has been properly scaled and almost filled the viewPort (which has a width of 500 and a height of 500).
Please see the SVG code and image it produced below. As you will notice the polyline, polygon, and circle did not scale to fill the view port. They do (consistently) occupy the top-left quarter of the view port though (moved but retaining the original size). Can anyone please throw some light on what's going on with this? I will greatly appreciate your feedback.
<?xml version='1.0' encoding='utf-8'?>
<svg version='1.1' xmlns='http://www.w3.org/2000/svg'
xmlns:xlink='http://www.w3.org/1999/xlink'
height='499' width='501' viewBox='100 100 200 200'>
<g stroke='BLACK' stroke-width='5' fill='none'>
<rect x='105' y='105' width='193' height='193'/>
<polygon points="150,100 200,200 100,200" style="stroke:purple" />
<polyline points='115,180 155,127 180,180' stroke='red'/>
<circle cx='150' cy='150' r='50' stroke='green'/>
</g>
</svg>

Short answer:
The SVG attribute viewBox on the sample code does scale all SVG graphics the same way; so the smaller object representations are actually smaller objects.
Explanation:
It's useful to look at he viewBox documentation to better understand the calculations. Let's try to go through you sample code step-by-step:
the SVG viewport dimensions are set to 501 by 499 (width by height)
the viewBox attributes are set as
100 for min-x and min-y, which will act like shifting the position of the viewport before its container top and left positions (which in the image it seems like irrelevant, since you also shifted all the coordinates by 100; see my note below)
200 for width and height, which will represent 100% of the viewport size (in this case ~500px); in other words, the 200 value in any children will be mapped (scaled) into ~500px
the rect has 193 as width and height, which is nearly 200, this makes it occupy almost all of the ~500px by 500px viewport area
the other items are scaled properly, but they seem smaller because, in fact, they are smaller
e.g. the circle has r='50' which would fit an imaginary outer square of 100 by 100; 100 is 50% of 200, so it is scaled to ~250px by ~250px (250 = 50% of 500); that is why the circle seems to use 1/4 of the area
the same idea is applied to the other graphic elements.
NOTE:
I found it was easier to understand the final results if there was no shift on the viewport and on the positioning coordinates. So, removing 100 from viewBox > min-x and min-y (step 2.1 above) and from all the positioning attributes would make this code easier to understand:
<?xml version='1.0' encoding='utf-8'?>
<svg version='1.1' xmlns='http://www.w3.org/2000/svg'
xmlns:xlink='http://www.w3.org/1999/xlink'
height='499' width='501' viewBox='0 0 200 200'>
<g stroke='BLACK' stroke-width='5' fill='none'>
<rect x='5' y='5' width='193' height='193'/>
<polygon points="50,0 100,100 0,100" style="stroke:purple" />
<polyline points='15,80 55,27 80,80' stroke='red'/>
<circle cx='50' cy='50' r='50' stroke='green'/>
</g>
</svg>

Related

How avoid SVG from scaling as its container shrinks?

I want an SVG to not scale as its container shrinks. On scroll, my container shrinks its height (its width stays the same). That container contains an SVG that I want to not become scaled, but instead have its lower part become invisible/cut off.
I can do it if I (by CSS) use my SVG as a background, but I'd prefer to have the SVG inline in the HTML.
I have tried with various values for the SVG attribute preserveAspectRatio. I thought that the value xMidYMin slice would slice off the bottom part of my SVG (like I want), but it squashes its height instead.
My container is 245x80 px and on scroll is shrinked to 245x40 px.
My svg element has attribute viewBox set to 0 0 245 80 , and has no width or height explicitly defined.
You can use preserveAspectRatio="xMidYMin slice" for the svg element.
Please observe that the svg element has a viewBox and also a width and a height. While the aspect ratio of the viewBox is 1:1 the aspect ratio from width and height is 2:1
xMidYMin - Force uniform scaling.
Align the midpoint X value of the element's viewBox with the midpoint X value of the viewport.
Align the of the element's viewBox with the smallest Y value of the viewport.
slice - Scale the graphic such that:
the aspect ratio is preserved and
the entire viewport is covered by the viewBox
Please read more about preserveAspectRatio
In the next demo use the slider to change the height of the svg element
itr.addEventListener("input",()=>{svg.setAttribute("height",itr.value)})
svg{border:solid}
<p><input id="itr" type="range" min="10" max="200" value="100"/></p>
<svg preserveAspectRatio="xMidYMin slice" viewBox="0 0 100 100" width="200" height="100" id="svg">
<defs>
<path style="fill:gold; stroke:black; stroke-width: 8px;
stroke-linecap: round; stroke-linejoin: round;" id="smiley" d="M50,10 A40,40,1,1,1,50,90 A40,40,1,1,1,50,10 M30,40 Q36,35,42,40 M58,40 Q64,35,70,40 M30,60 Q50,75,70,60 Q50,75,30,60" />
</defs>
<use href="#smiley" />
</svg>

svg viewBox strange behavior?

Im trying to figure out how svg viewBox work but the code below breaks all logic.
<svg width="100" height="100" viewBox="0 0 1 20">
<circle cx="10" cy="10" r="10" fill="red"/>
</svg>
In my reality viewBox is for defining the area of svg canvas to show in the viewport. The result of this code should be the left most little piece of circle is centered horizontaly with white space on both sides but chrome nor do firefox follow this logic.Can some one explain how this work? And sorry for my english, i hope you undestand what im trying to say.
The problems here are that most of the circle is outside the viewBox (it's only 1 unit wide and the circle is 20 units wide) and the viewBox aspect ratio is not the same as the aspect ratio of its container.
The default for preserveAspectRatio is to maintain the aspect ratio rather than distorting the shape so we still need to draw a circle rather than an ellipse. Since the drawing area is square the circle will therefore draw into that square area and as the height of the viewBox is 20 we'll have a 20 x 20 area to draw into to draw a circle.
What do we do with the 1 unit viewBox width then? How do we map 0-1 to 20 units? We need to keep the middle value of the viewBox so that's 0.5.
We need to solve these equations then for x and y: (x + y) / 2 = 0.5 (keep the centre the same) and y - x = 20 (keep the width the same as the height to preserve the aspect ratio)
Doing the maths here: x + y = 1 -> y - (1 - y) = 20 -> 2y - 1 = 20 -> y = 10.5 and x = -9.5.
So we're going to display 0 - 20 for height and -9.5 - 10.5 in width which means you'll see roughly (but not quite) half a circle. You actually see slightly more than half the circle.
The browsers are rendering your SVG correctly.
The viewBox just defines the area that should be scaled to fit the viewport. The content is not clipped to the viewBox. If there are elements outside of the viewBox, but still within the viewport, then those will be visible.
In your case, you have asked the browser to center the very left edge of the circle in the viewport. But there is enough room for much of the rest of the circle to be visible.
svg {
background-color: linen;
}
<svg width="100" height="100" viewBox="0 0 1 20">
<circle cx="10" cy="10" r="10" fill="red"/>
<rect width="1" height="20" fill="none" stroke="black" stroke-width="0.3" stroke-dasharray="0.5 0.5"/>
</svg>

SVG rotation around center is moving object

I have the following .svg file:
<?xml version="1.0" encoding="utf-8" ?>
<svg baseProfile="full" height="100cm" version="1.1" width="200cm" xmlns="http://www.w3.org/2000/svg">
<rect fill="rgb(61, 136, 199)" height="1.0cm" opacity="1.0" transform="rotate(45.0,181.45,181.45)" width="3.0cm" x="0.3144999999999999cm" y="1.3145cm" />
<circle cx="1.8145cm" cy="1.8145cm" fill="rgb(255, 0, 0)" opacity="1.0" r="0.025cm" />
</svg>
which displays like follows:
However, I want the rectangle to rotate around the red circle like this:
The Mozilla svg docs state:
The rotate(<a> [<x> <y>]) transform function specifies a rotation by a degrees about a given point. [...] If optional parameters x and y are supplied, the rotate is about the point (x, y).
Given that the circle's coordinates are cx="1.8145cm" cy="1.8145cm" and the rotation point of the rectangle is 181.45,181.45, why doesn't the rectangle rotate around the circle?
Note: Changing the rotation point to 1.8145, 1.8145 doesn't change anything.
Because rotating around 1.8145, 1.8145 (or in other words 1.8145px, 1.8145px) is not the same as rotating about 1.8145cm, 1.8145cm. px and cm are different units.
The transform attribute does not allow coordinates with units, so you will need to convert your centimetre values to pixels.
There are 2.54 cm per inch and 96 px per inch. So to convert between them, you will need to multiply your cm values by (96/2.54)
1.8145 * 96 / 2.54 ~= 68.58
So the SVG should be:
<svg baseProfile="full" height="100cm" version="1.1" width="200cm" xmlns="http://www.w3.org/2000/svg">
<rect fill="rgb(61, 136, 199)" height="1.0cm" opacity="1.0" transform="rotate(45.0,68.58,68.58)" width="3.0cm" x="0.3144999999999999cm" y="1.3145cm" />
<circle cx="1.8145cm" cy="1.8145cm" fill="rgb(255, 0, 0)" opacity="1.0" r="0.025cm" />
</svg>

SVG Width of a rotated (matrix) rectangle. Looks like width and height numbers are scaled

I am trying to analyse data in a SVG file I exported from coreldraw.
It is actually a file with a regular black colored 40x40 rectangle AND also the same 40x40 rectangle rotated 10 degrees and colored in red.
If I look at the SVG contents , then I can nicely see my 40x40 black rectangle and the line under it is the red rotated one.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN" "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<!-- Creator: CorelDRAW Home & Student X7 -->
<svg xmlns="http://www.w3.org/2000/svg" xml:space="preserve" width="100px" height="100px" version="1.0" style="shape-rendering:geometricPrecision; text-rendering:geometricPrecision; image-rendering:optimizeQuality; fill-rule:evenodd; clip-rule:evenodd"
viewBox="0 0 100 100"
xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<style type="text/css">
<![CDATA[
.str0 {stroke:black;stroke-width:0.2;stroke-linecap:round;stroke-linejoin:round}
.str1 {stroke:red;stroke-width:0.2;stroke-linecap:round;stroke-linejoin:round}
.fil0 {fill:none}
]]>
</style>
</defs>
<g id="Layer_x0020_1">
<metadata id="CorelCorpID_0Corel-Layer"/>
<rect class="fil0 str0" x="15" y="19" width="40" height="40"/>
<rect class="fil0 str1" transform="matrix(0.881289 -0.155395 0.169522 0.961407 11.5017 22.5159)" width="44.6985" height="40.9736"/>
</g>
</svg>
But.....
The red rotated one has no X or Y parameter specified. I read through some docs and think that this means that they are by default 0
Both the black original rectangle and the red rotated version have exactly the same size , but the red one is rotated by 10degrees.
If I look at the width and height of the red rectangle, then I see a width of 44.6985 and height of 40.9736 and that makes no sense to me at all.
It is a rectangle, so its height and width should be exactly the same. Even if you rotate it. I do also know that the rectangle will use more horizontal and vertical pixels of rotated by 10 degrees, but even then it should be the same number.
This makes me think that these values got somehow scaled in a non uniform way, but I can't imagine how I should calculate the correct width and height based on this info.
Grafical editors have sometimes quite obscure algorithms for what they consider optimizing. What CorelDraw did here was to exchange the square for a rectangle in a different place and with unequal side lenghts 44.6985 × 40.9736, and then apply a matrix that not only rotates and translates the rectangle, but also scales it non-uniformly back to a 40 × 40 square. Why? Ask Corel.
The matrix used by them is
matrix(0.881289 -0.155395 0.169522 0.961407 11.5017 22.5159)
and is equivalent to (computed with and rounded a bit from this online tool)
translate(11.5017 22.5159) rotate(-10) scale(0.894884 0.976238)
A (counter-clockwise!) rotation of the square around the same center would be written as
rotate(-10 33.3443 40.7508) //or
translate(-6.56971 6.40931) rotate(-10)
which is equivalent to
matrix(0.984807 -0.173648 0.173648 0.984807 -6.56971 6.40931)
You might contrast this with how Inkscape writes the same rotation. It writes out the rotation legibly, but moves the origin to somewhere else:
<rect x="7.41712" y="24.1711" width="40" height="40" transform="rotate(-10)" />

How does setting viewBox attribute, affects the user coordinates

I have the following code: http://jsfiddle.net/fCWJ5/1/, and following doubts regarding the viewbox.
body{margin:0;}
#test{width:200px;height:200px;border:solid red 1px;}
<body>
<div id="test">
<!-- preserveAspectRatio="xMinYMin meet" -->
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 150"
xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" baseProfile="full">
<g>
<rect class="drag resize" x="150" y="50" width="50" height="50" fill="#c66" />
</g>
</svg>
</div>
</body>
From the fiddle one user coordinate = .2, this I got by dividing
200/1000 (test div width / viewBox width attribute). According to
this, the rectangle should be at (30px, 10px), with a width and
height of 10px, 10px respectively. But the rectangle is at
(30px,97px), with a width and height of 10px,10px (some how height
and width is correct as per calculation.). Please point out why the
y coordinate is wrong.
Then I gave preserveAspectRatio="xMinYMin meet" as said in a svg
tutorial pdf. It was working fine for this value. But for other
value the display goes for toss. Please explain what is this. I
already asked a question regarding this
could not able to put viewbox,viewport,userspace together and get the picture.
I'm unable to understand the answer and the concept.
what will be the value of the ratio, if I didn't specified any width
and height for the svg dom element container.
I'm seeing that the ratio 1.3, (height of the test div/height
attribute of the viewBox), is not used. Should that be used for
calculating things like height,y coordinates.
The problem is the aspect ratio of your DIV does not match the aspect ratio of your viewBox. So HTML puts your SVG in the center of the DIV with the empty space above and below. Add the following to your SVG code to illustrate:
<rect x="0" y="0" width="100%" height="100%" fill="none" stroke="black"/>
This will show you the boundaries of your SVG element, while the red border you put on your DIV will show you its boundary. They don't match.
If you don't put a width or height on the SVG element then it will fill its container. In your example you set the DIV to 200px X 200px, the viewBox will then be applied effectively dividing the 200px by 1000 user units for X and 30px by 150 for the Y (because of the aspect ratio of the SVG only 15% of the DIV height is used by the SVG, 15% of 200px is 30px). Remove the width and height from the DIV and it will use the full width of the screen.
If you add my rect element you will see that your box is 1/3 (50/150 = 1/3) from the top extending 1/3 down, while also being 3/20 (150/1000 = 3/20) in from the left and extending 1/20 (50/1000 = 1/20) across.

Resources