简体   繁体   English

如何使用javascript给SVG文本元素提供固定的高度和宽度?

[英]How to give a fixed height and width to SVG text element using javascript?

I have multiple rect of different height and width, I need to show a text on the top left corner of the rect and no matter how much the width of the text is ,it should get adjusted in 50% of rect width. 我有多个不同高度和宽度的rect,我需要在rect的左上角显示一个文本,无论文本的宽度是多少,都应将其调整为rect宽度的50%。

Here is the code I have tried. 这是我尝试过的代码。

 rectBbox = rectElem.getBBox();
 textBbox = textElem.getBBox();
 scale = rectBbox.width / textBbox.width;
 X = parseFloat(rectBbox.x) 
 Y = parseFloat(rectBbox.y) 
 textElem.setAttribute("transform", "translate(" + X + "," + Y + ") scale(" + scale + ")");

在此处输入图片说明

my svg looks like 我的svg看起来像

<svg>
<g id="maingroup">
<g id="firstchild">
<rect></rect>
<text></text>
</g>
<g id="nthchild">
<rect></rect>
<text></text>
</g>
<g>
</svg>

How can i scale it so that no matter what is the size of the rect or text is,text will get properly adjusted within 50% of rect's width 我如何缩放它,以便无论矩形或文本的大小是多少,文本都将在矩形宽度的50%内正确调整

In this example, we are positioning the <text> elements at the default 0,0 . 在此示例中,我们将<text>元素放置在默认的0,0 Then we calculate the difference between the top left of the rect and the top left of the text. 然后,我们计算rect的左上方和文本的左上方之间的差异。

The scale part is similar: 0.5 * the ratio of the text width and the rect width. 比例部分类似:0.5 *文本宽度与矩形宽度的比率。

 function adjustText(boxElem) { var rectElem = boxElem.querySelector("rect"); var textElem = boxElem.querySelector("text"); var rectBbox = rectElem.getBBox(); var textBbox = textElem.getBBox(); var scale = 0.5 * rectBbox.width / textBbox.width; var translateX = rectBbox.x - textBbox.x; var translateY = rectBbox.y - textBbox.y; textElem.setAttribute("transform", "translate(" + translateX + "," + translateY + ") scale(" + scale + ")"); } adjustText(document.getElementById("firstchild")); adjustText(document.getElementById("nthchild")); 
 <svg width="500" height="500"> <g id="maingroup"> <g id="firstchild"> <rect x="20" y="30" width="250" height="100" fill="lightgrey" stroke="black"/> <text>Very Very Very Very Very Long Text</text> </g> <g id="nthchild"> <rect x="200" y="200" width="200" height="100" fill="lightgrey" stroke="black"/> <text>Smaller Text</text> </g> </g> </svg> 

First I need to calculate the width of the text ( txtlength ) and the width of the box ( w ). 首先,我需要计算文本的宽度( txtlength )和框的宽度( w )。 I want to scale the text so I'm calculating the scale let thescale = w / (2*txtlength); 我想缩放文本,所以我要计算缩放比例, let thescale = w / (2*txtlength); . // this will scale the text at 50% of the rect width. //这会将文字缩放为矩形宽度的50%。 Next, using setAttributeNS I'm setting the value for the transform attribute. 接下来,使用setAttributeNS设置transform属性的值。 Please observe that the text has no x and no y attributes being centered around the origin of the SVG canvas. 请注意,文本没有以SVG画布的原点为中心的x和y属性。

 let txtlength = txt.getComputedTextLength() let w = theRect.getBBox().width; let c = {x:50,y:25}// the center of the rect let thescale = w / (2 * txtlength);// 50% of rect's width //scale the text and translate in the center txt.setAttributeNS(null, "transform", `scale(${thescale},${thescale}) translate(${cx/thescale},${cy/thescale})`) 
 svg{border:1px solid; width:120vh} text{font-size:10; dominant-baseline: middle; text-anchor: middle} 
 <svg viewBox="0 0 100 50"> <rect id="theRect" x="10" y="10" width="80" height ="30" stroke="black" fill="none" /> <text id="txt" dominant-baseline="middle" text-anchor="middle">A very long text, sooo long </text> </svg> 

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

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