SVG text getting cut off - css

When I have a simple svg like this:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<text style="font-size: 24px;">Sample Title</text>
<text style="font-size: 12px;">Sample Subtitle</text>
</svg>
The words are cut off as if it is extending past the top of the viewport like this: https://i.stack.imgur.com/fAN4Z.png
If I add y="24" to the title text tag and y="36" to the subtitle text tag (where y has to be >= the font size), then it is not cut off: https://i.stack.imgur.com/beoee.png
Why is this?

If you don't include the y attribute then it defaults to 0. If you draw the text with a y position of 0, then its baseline is at the top of the image and you'll only see the letter that hang below the baseline.
SVG is primarily a language for describing graphics, so all elements are position with (x, y) coordinates, as opposed to HTML, which is markup language so text flows more naturally.

Related

Adding alpha mask to SVG

I'm attempting to add an alpha mask to my SVG file but am having some problems. I couldn't find a way to do it with Illustrator, so instead I used Photoshop to add the alpha layer and resaved it as SVG. Looking at the SVG file itself, I don't see anything that would indicate there's an alpha layer, and I haven't been able to get it to work.
The effect I want is for you to only see the grey layer through the magnifying glass lens, instead it looks like this.
This is my Codepen:
https://codepen.io/TheNomadicAspie/pen/xxdaWOa
And here is the actual raw text of the SVG:
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="836" height="1032" viewBox="0 0 836 1032">
<image x="153" y="216" width="530" height="599" xlink:href="data:img/png;base64,iVBORw0KGgoAAAANSUhEUgAAAhIAAAJXCAYAAADGqL1gAAAgAElEQVR4nO3dMZfjxrUg4NI4cWaGykxlyszNlJnKlJnOnLk33Kz9C7b3F9D+Be2XvRe1JnTEUeYXdSvTRj2TeaMeZc56DzRFm2KzuwsgUACqvu+ce2YkjWaGAAFcVN269VmAYa1CCIsYq/gn/erg5+Hov53j3dH/exdC+PHg5x9j3DnnAP2QSHCuZYx1/H1+G39cz+DI7hOP7w4SjPcxAEggkSDVYcLwm/jzPkYRpupdTCg+xJ/vRzQAOCCR4JRFTBhWcYRhPz1Ru/cxofhecgHwiUSCEBOFfdKwjqMNpLm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5ErkJggg=="/>
</svg>
What am I doing wrong?
SVGs don't have a background by default. So you shouldn't need to do anything to make the background areas transparent.
Given that, your statement "I'm attempting to add an alpha mask to my SVG file" doesn't really make much sense. It does not need a mask.
The example SVG you link to consists solely of a bitmap PNG image. But that image has a transparent background so it should work also. By that I mean that whatever you place the SVG in front of, should show through.
Note however that an SVG that consists solely of a bitmap image is pretty much pointless. You would be better just to use that bitmap directly. You are not getting any benefit from putting it inside an SVG file.
Update
Okay, so if you want to create an effect where an image is visible through the lens of your magnifying glass, then your mask image needs to be a circle that matches just the lens size and position, not the whole magnifying glass. In a mask image, white areas represent the part of the image you want to keep, and black areas are the parts you want to be transparent.
Like so.
This is the image that you ought to have had in your SVG.
However as I said, you're wasting the benefits of using an SVG if you just put a single large bitmap in it. You could use a small SVG like the following instead.
<svg xmlns="http://www.w3.org/2000/svg" width="530" height="599">
<!-- areas masked with black become transparent -->
<rect width="100%" height="100%" fill="black"/>
<!-- areas masked with white remain visible -->
<circle cx="170" cy="170" r="165" fill="white"/>
</svg>
Your main magnifying glass isn't suitable as a mask, but it can be used as an image to be superimposed over the masked background. But again, that image could be a proper SVG (using vector shapes instead of a bitmap) and it would be a lot smaller.
But if you are going to use SVGs it would be simpler to use an SVG for the whole thing. Especially if you are planning to move the lens about.
The mask-image property has some restrictions, and can be tricky to use.
Here's an example of doing it entirely within an SVG.
svg {
width: 530px;
height: 599px;
}
<svg viewBox="0 0 530 599">
<defs>
<!-- Our circular lens mask -->
<mask id="lens-mask">
<!-- areas masked with black become transparent -->
<rect width="530" height="599" fill="black"/>
<!-- areas masked with white remain visible -->
<circle cx="170" cy="170" r="165" fill="white"/>
</mask>
</defs>
<!-- Background image that we want to show through the lens -->
<image xlink:href="https://placekitten.com/530/599" width="530" height="599"
mask="url(#lens-mask)"/>
<!-- Magnifying glass image that we will lay directly over the top -->
<image xlink:href="https://i.stack.imgur.com/dvbYR.png" id="mag-glass"/>
</svg>
Update 2
I didn't add any movement to my last example, because you hadn't mentioned it in your question. I didn't want to complicate the example by adding unnecessary extras.
Here's an updated version which includes movement. Hopefully this helps.
var svg = document.getElementById("my-svg");
var magGlass = document.getElementById("mag-glass");
var lensCircle = document.querySelector("#my-svg #lens-mask circle");
svg.addEventListener("mousemove", moveLens);
function moveLens(evt) {
var mouseTransform = "translate(" + (evt.offsetX - 170) + "," + (evt.offsetY - 170) + ")";
lensCircle.setAttribute("transform", mouseTransform);
magGlass.setAttribute("transform", mouseTransform);
}
svg {
width: 530px;
height: 599px;
overflow: visible;
}
<svg viewBox="0 0 530 599" id="my-svg">
<defs>
<!-- Our circular lens mask -->
<mask id="lens-mask">
<!-- areas masked with black become transparent -->
<rect width="530" height="599" fill="black"/>
<!-- areas masked with white remain visible -->
<circle cx="170" cy="170" r="165" fill="white"/>
</mask>
</defs>
<!-- Background image that we want to show through the lens -->
<image xlink:href="https://placekitten.com/530/599" width="530" height="599"
mask="url(#lens-mask)"/>
<!-- Magnifying glass image that we will lay directly over the top -->
<image xlink:href="https://i.stack.imgur.com/dvbYR.png" id="mag-glass"/>
</svg>

One path/shape SVG with mysterious left padding/white space. Not artboard.

I designed a shape in Illustrator and trimmed artboard to shape so no outside white space. I've done this twice to be sure.
When I open it in Chrome and inspect. If I hover over the whole SVG tag it's tight to the shape. If I hover over the path, there's a white space/padding left (and top, actually) - see screenshot.
It means when I place or animate the shape it's not right. I don't know how I can get rid of it - any ideas?
Code:
<div style="margin:100px">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 157.144 210.706">
<path fill="#444" d="M78.394 210.706c-11.247-13.16-22.162-25.304-32.368-38.017-16.016-19.95-30.158-41.092-39.98-64.948-19.25-46.76 9.943-94.135 52.535-105.196 45.651-11.856 91.876 19.016 97.966 65.671 2.028 15.534-1.288 29.968-7.385 44.017-11.15 25.692-27.51 48.023-45.258 69.398-7.965 9.593-16.436 18.766-25.51 29.075zm52.722-131.714c.046-29.268-23.232-52.857-52.283-52.982-28.984-.125-52.775 23.526-52.815 52.506-.041 29.231 23.339 52.858 52.341 52.895 29.291.038 52.712-23.233 52.757-52.419z"/>
</svg>
</div>
It seems Chrome is taking into account the control points when painting the bounding box:
You can avoid this by reordering your control points, and this creates a better shape for your pin:
Here's the Path, but I did it quickly, you might want to redraw it yourself with more care.
<path d="m78.394,211.00938c-11.247,-13.16 -22.162,-25.304 -32.368,-38.017c-16.016,-19.95 -44.72017,-56.5643 -45.74419,-94.98246c-1.02402,-38.41816 32.67117,-77.86771 78.32217,-77.89195c45.651,-0.02424 78.27532,41.16539 78.54978,79.92979c0.27446,38.7644 -35.50176,80.51162 -53.24976,101.88662c-7.965,9.593 -16.436,18.766 -25.51,29.075zm52.722,-131.714c0.046,-29.268 -23.232,-52.857 -52.283,-52.982c-28.984,-0.125 -52.775,23.526 -52.815,52.506c-0.041,29.231 23.339,52.858 52.341,52.895c29.291,0.038 52.712,-23.233 52.757,-52.419z" fill="#444"/>

How to prevent inconsistent thickness and gaps in SVG lines?

My use case is probably a bit unique, but I am creating a dynamically-sized square grid using DOM elements encased in a flexbox. Each DOM element has an SVG image background and a height/width equal to calc(100vmin / <gridSize>). The SVG elements are simple:
<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100">
<path stroke-width="2" stroke="#000" stroke-linecap="square" d="M0,50 h100 M50,0 v50"/>
</svg>
However, I noticed that some lines appear darker than others, and there are gaps between some SVG lines. (See picture below) I'm assuming this is due to pixel rounding because when I resize the browser, the gaps and thicknesses jump around.
I tried to round the pixels from the calc, but I am using styled-components so I am unable to use SASS / LESS functions like floor / ceil. I also noticed making the stroke width thicker alleviates the problem, but I would prefer to keep my lines thin.
Are there other ways I can make the lines consistent?
It's antialiasing. Setting shape-rendering="crispEdges" will turn it off on most UAs.
vector-effect='non-scaling-stroke'
non-scaling-stroke
This value modifies the way an object is stroked. Normally stroking involves calculating stroke outline of the shapeʼs path in current user coordinate system and filling that outline with the stroke paint (color or gradient). The resulting visual effect of this value is that the stroke width is not dependent on the transformations of the element (including non-uniform scaling and shear transformations) and zoom level.
MDN web docs

Align text inside SVG

I have adobe illustrator. When I export graphic to svg, the text part looks like:
<style type="text/css">
.st6{fill:#FFFFFF;}
</style>
<text id="text2" transform="matrix(1 0 0 1 153.873 305.2743)" class="st6">Default text</text>
This text is aligned to center.
Now I change text inside browser with javascript - so, replace "Default text" with "New text". This text is not aligned to center any more.
If I change text, how can I achieve that it is always aligned to center?
I have try with adding "text-align:center" to st6 class or adding this property to text element:
text-anchor="middle"
but doesn't work.
Any idea?
One interesting other example which I don't understand. Here is part of svg template I have:
<g id="text_4">
<g>
<defs>
<rect id="SVGID_10_" x="9.96" y="273.53" width="170" height="15"/>
</defs>
<clipPath id="SVGID_11_">
<use xlink:href="#SVGID_10_" style="overflow:visible;"/>
</clipPath>
<g style="clip-path:url(#SVGID_11_);">
<text data-label="Text 4" text-anchor="middle" x="95" y="282.7" style="fill:#748B9E; font-family:'Merriweather'; font-size:8px;">2013 Riesling </text>
</g>
</g>
I can change text for example instead of "2013 Riesling" I can add "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" and text is still in the center.
Then I have removed all elements from this svg except text(so remove all g elements and clip path) only this left:
<text data-label="Text 4" text-anchor="middle" x="95" y="282.7" style="fill:#748B9E; font-family:'Merriweather'; font-size:8px;">2013 Riesling</text>
But template is still the same, nothing has changed if I view it inside browser. How is that possible? So, I can shorten size of SVG for 70%.
And here text is always centered while in my template if I do the same it is not. Interesting.
Adobe Illustrator has positioned your text so that it is centered according to your design. But obviously that positioning is specific to that particular piece of text.
text-align: center will not work with SVGs. That is an HTML property.
text-anchor: middle is the correct property to use. It will cause the text to be centred on the coordinates you specify. So in order for the new text to be centered properly, you will have to adjust the x coordinate of your text element.
In your case the text is being positioned using a transform attribute. You could either adjust the transform, or replace it with x and y attributes.
So, for example, if the centre position for your text was calculated to be (200, 305.2743), you would need to change your text element to:
<style type="text/css">
.st6 {fill:#FFFFFF; text-anchor:middle;}
</style>
<text id="text2" transform="matrix(1 0 0 1 200 305.2743)" class="st6">New text</text>
or
<style type="text/css">
.st6 {fill:#FFFFFF; text-anchor:middle;}
</style>
<text id="text2" x="200" y="305.2743" class="st6">New text</text>
How you determine the centre position at run-time (in browser) is up to you.
One option is to use getComputedTextLength() or getBBox() on the text element. Then add half the width to your x coord.
Or alternatively you could alter your Illustrator file so that your placeholder text was something tiny like ".". Then you would probably be close enough to centre that you wouldn't need to adjust the coordinates.
Elaborate comment containing workaround:
As has been pointed out in the accepted answer, Illustrator (2017.1.0 release) doesn't export the text-anchor=middle tag in it's SVG's. Which is a pity if you want to use the SVG with dynamically generated and centered text.
I found the following work-around.
Step 1: create your design in AI with the text centered and the text-frame in the exact right location
Step 2: when you're done drawing and before exporting it to SVG: RIGHT-align the text:
Step 3:
Manually (or dynamically) add the text-anchor=middle attribute to the text-frame.
<text text-anchor="middle">9650</text>
Step 4:
Admire the result

How do I scale a group of text tags relative to the size of a page?

I've been messing with this for weeks, so I'm just gonna flat out ask how to do it.
I've been working on a little SVG countdown clock just for the hell of it. But when I try and position things relative to how the page might be sized, I get stuck. I can't seem to size text tags relative to the page, and I can't find a way to put them into a group for sizing.
I've tried putting them into symbol tags, and using viewBox, but I can't seem to get that to work either.
My intended goal is to have this working on any sized screen from monitors to smartphones. So I wanted to do something with a min, and max width/height, or something along those lines, and apply it to a combination of the number and it's associated label. So I would have one group for days with both the number of days(being changed by the script), and then the word "days" underneath it.
Here is what I'm working with if it helps:
http://jsfiddle.net/2P9qV/
Does anyone have any ideas? I've been stumped for weeks.
Ok, so I finally figured it out. Luckily, I was able to use multiple SVGs within an SVG to create content, and then display it using use tags.
So I had each set as part of it's own symbol in defs. From there, I used the viewBox on the symbol tag to limit the draw space to the exact width and height of the content created, which I found by inspecting the element, and using the height and width it had been made as(without the 'px'). This way, I could center the whole set in the SVG that had the use tag, then set the width of the SVG to 100%, and the height to the percentage I wanted to scale it to. Since these are set as percentages, the page will limit the SVG's size to whichever is most limiting, while still maintaining scale, so it won't skew.
So one of the symbols would be:
<symbol id="daysSymbol" viewBox="0 0 110 156">
<g>
<text id="days" y="0" x="50%" font-size="100" class="time big">00</text>
<text id="daysT" y="100" x="50%" font-size="40" class="text bigText">DAYS</text>
</g>
</symbol>
and the SVG for this would be
<svg id="daysSVG" height="50%" width="100%" y="30%">
<use xlink:href="#daysSymbol" height="100%"/>
</svg>
It also let me get creative with how I managed the contents on the page. As something happened, I was able to dynamically remove content, and scale other elements to take up the new space.
For those curious, here is the final product
https://hostr.co/file/PLCY7lodb7Lk/xboxCountdown.svg
It won't last for that long, since it was a countdown clock. But I won't take it down(as long as it doesn't get automatically deleted), and if you wanted to see how it worked, you cold just download it and change the date it counted down to.

Resources