在SVG中将弯曲文本居中在一条线上

huangapple go评论46阅读模式
英文:

Centering curved text on a line in an SVG

问题

I have created a half-circle path as an SVG in illustrator, I want to center the text along this path and the text will be dynamic so I basically need the text to always start in the center (bottom most point of the curve). I have this most of the way there but the text is still off-kilter a bit. The properties I have tried are x="50%" y="50%", dominant-baseline="middle", and text-anchor="middle"

<svg xmlns="http://www.w3.org/2000/svg" data-name="Layer 2" viewBox="0 0 500 500">
  <g data-name="Layer 1">
    <path d="M0 0h500v500H0z" style="fill:none"/>
    <path id="curved-text" d="M63 250c0 103.55 83.95 187.5 187.5 187.5S438 353.55 438 250" style="stroke:#000;fill:none"/>
    <text font-size="30" fill="#000000" letter-spacing="2" font-family="sans-serif" font-weight="bold" x="50%" y="50%" dominant-baseline="middle" text-anchor="middle">
      <textPath xlink:href="#curved-text">This is my centered text</textPath>
    </text>
  </g>
</svg>

The text ends up being more to the left of the bottom anchor than it should be.

英文:

I have created a half-circle path as an SVG in illustrator, I want to center the text along this path and the text will be dynamic so I basically need the text to always start in the center (bottom most point of the curve). I have this most of the way there but the text is still off-kilter a bit. The properties I have tried are x=&quot;50%&quot; y=&quot;50%&quot;, dominant-baseline=&quot;middle&quot;, and text-anchor=&quot;middle&quot;

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; data-name=&quot;Layer 2&quot; viewBox=&quot;0 0 500 500&quot;&gt;
  &lt;g data-name=&quot;Layer 1&quot;&gt;
    &lt;path d=&quot;M0 0h500v500H0z&quot; style=&quot;fill:none&quot;/&gt;
    &lt;path id=&quot;curved-text&quot; d=&quot;M63 250c0 103.55 83.95 187.5 187.5 187.5S438 353.55 438 250&quot; style=&quot;stroke:#000;fill:none&quot;/&gt;
    &lt;text font-size=&quot;30&quot; fill=&quot;#000000&quot; letter-spacing=&quot;2&quot; font-family=&quot;sans-serif&quot; font-weight=&quot;bold&quot; x=&quot;50%&quot; y=&quot;50%&quot; dominant-baseline=&quot;middle&quot; text-anchor=&quot;middle&quot;&gt;
      &lt;textPath xlink:href=&quot;#curved-text&quot;&gt;This is my centered text&lt;/textPath&gt;
    &lt;/text&gt;
  &lt;/g&gt;
&lt;/svg&gt;

<!-- end snippet -->

The text ends up being more to the left of the bottom anchor than it should be.

答案1

得分: 2

你可以在textPath上使用startOffset属性,并将其值设置为50%。

<svg xmlns="http://www.w3.org/2000/svg" data-name="Layer 2" viewBox="0 0 500 500" style="height:220px">
  <g data-name="Layer 1">
    <path d="M0 0h500v500H0z" style="fill:none"/>
    <path id="curved-text" d="M63 250c0 103.55 83.95 187.5 187.5 187.5S438 353.55 438 250" style="stroke:#000;fill:none" />
    <text font-size="30" fill="#000000" letter-spacing="2" font-family="sans-serif" font-weight="bold" dominant-baseline="middle" text-anchor="middle">
      <textPath startOffset="50%" href="#curved-text">This is my centered text</textPath>
    </text>
  </g>
</svg>
英文:

You can use the startOffset attribute on the textPath and give it the value 50%.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; data-name=&quot;Layer 2&quot; viewBox=&quot;0 0 500 500&quot; style=&quot;height:220px&quot;&gt;
  &lt;g data-name=&quot;Layer 1&quot;&gt;
    &lt;path d=&quot;M0 0h500v500H0z&quot; style=&quot;fill:none&quot;/&gt;
    &lt;path id=&quot;curved-text&quot; d=&quot;M63 250c0 103.55 83.95 187.5 187.5 187.5S438 353.55 438 250&quot; style=&quot;stroke:#000;fill:none&quot; /&gt;
    &lt;text font-size=&quot;30&quot; fill=&quot;#000000&quot; letter-spacing=&quot;2&quot; font-family=&quot;sans-serif&quot; font-weight=&quot;bold&quot; dominant-baseline=&quot;middle&quot; text-anchor=&quot;middle&quot;&gt;
      &lt;textPath startOffset=&quot;50%&quot; href=&quot;#curved-text&quot;&gt;This is my centered text&lt;/textPath&gt;
    &lt;/text&gt;
  &lt;/g&gt;
&lt;/svg&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月12日 01:16:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451636.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定