简体   繁体   English

动态调整文本以适应 SVG 中的路径元素

[英]Dynamically adapt a text to a path element in an SVG

My SVG has shapes with different dimensions as below, and I would like my text to have a size adapted to the length of its associated shape (fill without overflow).我的 SVG 具有不同尺寸的形状,如下所示,我希望我的文本具有适合其相关形状长度的大小(填充不溢出)。

I tried font-size-adjust but no result and textLength lengthAdjust deforms my text, should i use textPath & href logic?我尝试了 font-size-adjust但没有结果并且textLength lengthAdjust使我的文本变形,我应该使用 textPath & href 逻辑吗?

Thank you for your help谢谢您的帮助

 <svg viewBox="0 0 220 220"> <g> <path fill="orange" d="M30 110 L 20 110 L 20 20 L 30 10 L 40 20 L 40 110 Z"/> <text font-size="1em" transform="rotate(-90, 30, 110)" x="30" y="110" dominant-baseline="middle">Object number 1</text> </g> <g> <path fill="red" d="M 50 110 L 40 110 L 40 50 L 50 40 L 60 50 L 60 110 Z"/> <text transform="rotate(-90, 50, 110)" x="50" y="110" dominant-baseline="middle">Object number 2</text> </g> <g> <path fill="yellow" d="M 80 110 L 60 110 L 60 50 L 80 40 L 100 50 L 100 110 Z"/> <text transform="rotate(-90, 80, 110)" x="80" y="110" dominant-baseline="middle">Object number 3</text> </g> </svg>

This is the rendering I would like to obtain:这是我想获得的渲染:这是我想获得的渲染:

The space between letters is why i don't want to use textLength:字母之间的空格是我不想使用 textLength 的原因:

这就是为什么我不想使用 textLength

My render here would be:我在这里的渲染是:

很好的渲染

First method: tou can set the textLength of the text to be equal to the height of the path.第一种方法:tou可以设置文本的textLength等于路径的高度。 Since your path has a tip I've opted for the 90% of the height.由于您的路径有小费,我选择了 90% 的高度。 However if your path is much smaller than the text the text will apear crammed.但是,如果您的路径比文本小得多,则文本将显得拥挤。 To understand what I mean change the font-size to 3em要理解我的意思,将字体大小更改为 3em

 let pthHeight = pth.getBBox().height; txt.setAttribute("textLength",pthHeight*.9 )
 svg{width:50vh}
 <svg viewBox="0 0 50 120"> <path fill="orange" id="pth" d="M30 110 L 20 110 L 20 20 L 30 10 L 40 20 L 40 110 Z"/> <text id="txt" font-size="1em" transform="rotate(-90, 30, 110)" x="30" y="110" dominant-baseline="middle" lengthAdjust="spacingAndGlyphs">Object number 1</text> </svg>

yet another solution would be changing the font size in a while loop till the text length is smaller than the height of the path.另一种解决方案是在 while 循环中更改字体大小,直到文本长度小于路径的高度。 This assumes that the initial length of the text is longet than the height.这假设文本的初始长度比高度更长。

 let pthHeight = pth.getBBox().height; function setFontSize(txt){ let fs = txt.getAttribute("font-size"); let textLength = txt.getComputedTextLength(); let fontSize = txt.getAttribute("font-size"); while(textLength > pthHeight){ fontSize -= 1; txt.setAttribute("font-size",fontSize); textLength = txt.getComputedTextLength(); } } setFontSize(txt)
 svg{width:50vh}
 <svg viewBox="0 0 50 120"> <path fill="orange" id="pth" d="M30 110 L 20 110 L 20 20 L 30 10 L 40 20 L 40 110 Z"/> <text id="txt" font-size="16" transform="rotate(-90, 30, 110)" x="30" y="110" dominant-baseline="middle" >Object number 1</text> </svg>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM