最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vertically align text inside of the svg circle - Stack Overflow

programmeradmin0浏览0评论

I am trying to centre the <text> inside of the <path> element. Path is a circle drawn using @opsb great answer. I can centre the text horizontally using text-anchor='middle'.

Is there a way to centre it Vertically?

<svg width="200px" height="200px" version="1.1" xmlns="">
  <path id='path' fill="transparent" stroke='red' stroke-width='6'/>
  <text x="100" y="100" font-family="Verdana" font-size="50" fill="blue" text-anchor='middle'>5</text>
</svg>

JSFiddle with the javascript.

I am trying to centre the <text> inside of the <path> element. Path is a circle drawn using @opsb great answer. I can centre the text horizontally using text-anchor='middle'.

Is there a way to centre it Vertically?

<svg width="200px" height="200px" version="1.1" xmlns="http://www.w3/2000/svg">
  <path id='path' fill="transparent" stroke='red' stroke-width='6'/>
  <text x="100" y="100" font-family="Verdana" font-size="50" fill="blue" text-anchor='middle'>5</text>
</svg>

JSFiddle with the javascript.

Share Improve this question edited Feb 18, 2016 at 18:22 Maxime Rouiller 13.7k9 gold badges60 silver badges108 bronze badges asked Feb 18, 2016 at 18:21 Kocur4dKocur4d 6,9418 gold badges39 silver badges54 bronze badges 2
  • 1 Possible duplicate of vertical alignment of text element in SVG – c-smile Commented Feb 18, 2016 at 18:25
  • Also: stackoverflow./questions/19253766/… – Paul LeBeau Commented Feb 18, 2016 at 18:53
Add a ment  | 

3 Answers 3

Reset to default 4

You'll want to use alignment-baseline to centre text vertically.

Something like this:

<text x="100" y="100" font-family="Verdana" font-size="50" fill="blue" text-anchor='middle' alignment-baseline="central">5</text>

Svg center text

Since svg is responsive I usually put in a hard value that is about the radius of the circle.

So simply by adding: transform="translate(0, 15)

function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
  var angleInRadians = (angleInDegrees - 90) * Math.PI / 180.0;

  return {
    x: centerX + (radius * Math.cos(angleInRadians)),
    y: centerY + (radius * Math.sin(angleInRadians))
  };
}

function describeArc(x, y, radius, startAngle, endAngle) {

  var start = polarToCartesian(x, y, radius, endAngle);
  var end = polarToCartesian(x, y, radius, startAngle);

  var arcSweep = endAngle - startAngle <= 180 ? "0" : "1";

  var d = [
    "M", start.x, start.y,
    "A", radius, radius, 0, arcSweep, 0, end.x, end.y
  ].join(" ");

  return d;
}

$('#path').attr("d", describeArc(100, 100, 50, 0, 180));
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<svg width="200px" height="200px" version="1.1" xmlns="http://www.w3/2000/svg">
  <path id='path' fill="transparent" stroke='red' stroke-width='6' />
  <text x="100" y="100" transform="translate(0, 15)" font-family="Verdana" font-size="50" fill="blue" text-anchor='middle'>5</text>
  <circle cx="100" cy="100" fill="black" r="2" />
  <circle cx="100" cy="50" fill="black" r="2" />
</svg>

You can now use dominant-baseline="central" to vertically align the text without javascript.

<svg width="150px" height="150px" version="1.1" xmlns="http://www.w3/2000/svg">
  <circle cx="75" cy="75" r="69" stroke="red" fill="none" stroke-width="6" />
  <text x="75" y="75" font-family="Verdana" font-size="50" fill="blue" text-anchor='middle' dominant-baseline="central">5</text>
</svg>

Source

发布评论

评论列表(0)

  1. 暂无评论