首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SVG -文本后的内联位置元素

SVG -文本后的内联位置元素
EN

Stack Overflow用户
提问于 2019-01-07 01:17:52
回答 1查看 311关注 0票数 0

我想将SVG中的几个元素内联到一个SVG中。

我有以下几点:

代码语言:javascript
复制
<svg>
    <g>
        <use width="28" height="28"class="cls-11"></use>
        <text id="policyRectText" transform="translate(50,20)" class="cls-54">Runs on a small number of endpoints</text>
        <circle r="15" stroke-width="4" transform="translate(250,15)" class="cls-8"></circle>
    </g>
</svg>  

因此,在动态文本之前定位一个元素是很容易的,但是如何定位在它之后呢?

EN

回答 1

Stack Overflow用户

发布于 2019-01-07 02:59:29

这就是我要做的:

首先,我将<text>放在一个<g>元素中,因为它被转换了,我需要获得边界框:

let bb = theTransformedText.getBBox();

有了文本的位置和大小(bb)之后,我将使用数据来设置圆的cxcy属性。

我注释掉了<use>元素,因为它没有xlink:href属性。

代码语言:javascript
复制
let bb = theTransformedText.getBBox();
let r = parseFloat(theCircle.getAttribute("r"));// the circle's radius.
let sw = parseFloat(theCircle.getAttribute("stroke-width"));// the stroke width
theCircle.setAttributeNS(null, "cx", bb.x + bb.width + r + sw/2);
// assuming that the font size is 16 you need to offset the circle half font size
theCircle.setAttributeNS(null, "cy", bb.y + 8);
代码语言:javascript
复制
<svg viewBox="0 -50 400 100">
    <g>
        <!--<use width="28" height="28"class="cls-11"></use>-->
      <g id="theTransformedText">
        <text id="policyRectText" transform="translate(50,20)" class="cls-54">Runs on a small number of endpoints</text>
      </g>
        <circle id="theCircle" r="15" stroke-width="4" class="cls-8"></circle>
    </g>
</svg>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54064026

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档