简体   繁体   English

路径内的SVG弯曲文字

[英]SVG curved text inside path

Hello guys I created a circle with 5 segments with SVG, im trying to add text inside the segments but I can't get it to work right. 大家好,我用SVG创建了一个包含5个细分的圈子,我试图在细分中添加文本,但我无法使其正常运行。

This is what Im trying to do Text curved and centered inside the path 这就是我正在尝试做的事情, 文本在路径内弯曲并居中

this is what im getting: enter image description here 这就是即时通讯: 在此输入图片描述

This is my code so far, any suggestions? 到目前为止,这是我的代码,有什么建议吗?

<svg  viewBox='0 0 110 110' style="margin-top: 10px;transform: rotate(18deg);">
   <a xlink:href="">
      <path  class="frag logoa" id="f1" data-link="1" d='M55,55 L14.54915028125263,25.61073738537635 A50,50 0 0,1 70.45084971874736,7.447174185242318z' >
      </path>
      <text font-family="Verdana" font-size="15" stroke="red";>
         <textPath xlink:href="#f1">
            We go up, then we go down, then up again
         </textPath>
      </text>
   </a>
   <a xlink:href="" >
      <path class="frag logoa" data-link="2" d='M55,55 L70.45084971874736,7.447174185242318 A50,50 0 0,1 105,54.999999999999986z' ></path>
   </a>
   <a xlink:href="" >
      <path class="frag logoa" data-link="3" d='M55,55 L105,55 A50,50 0 0,1 70.45084971874738,102.55282581475768z'></path>
   </a>
   <a xlink:href=""  >
      <path class="frag logoa" data-link="4" d='M55,55 L70.45084971874738,102.55282581475768 A50,50 0 0,1 14.549150281252636,84.38926261462366z' ></path>
   </a>
   <a xlink:href=""   >
      <path class="frag logoa" data-link="5" d='M55,55 L14.549150281252636,84.38926261462366 A50,50 0 0,1 14.54915028125263,25.61073738537635z' ></path>
   </a>
   <circle class="cente" cx='55' cy='55' r='35' ></circle>
   <circle class="minicirculo" cx='55' cy='55' r='15' ></circle>
</svg>

You are currently using one of the circle segment paths as the path for your text. 您当前正在使用圆弧段路径之一作为文本的路径。 See below. 见下文。

 path, circle { fill: transparent; stroke: black; } 
 <svg viewBox='0 0 110 110' style="margin-top: 10px;transform: rotate(18deg);"> <a xlink:href=""> <path class="frag logoa" id="f1" data-link="1" d='M55,55 L14.54915028125263,25.61073738537635 A50,50 0 0,1 70.45084971874736,7.447174185242318z' > </path> <text font-family="Verdana" font-size="4" fill="red";> <textPath xlink:href="#f1"> We go up, then we go down, then up again </textPath> </text> </a> <a xlink:href="" > <path class="frag logoa" data-link="2" d='M55,55 L70.45084971874736,7.447174185242318 A50,50 0 0,1 105,54.999999999999986z' ></path> </a> <a xlink:href="" > <path class="frag logoa" data-link="3" d='M55,55 L105,55 A50,50 0 0,1 70.45084971874738,102.55282581475768z'></path> </a> <a xlink:href="" > <path class="frag logoa" data-link="4" d='M55,55 L70.45084971874738,102.55282581475768 A50,50 0 0,1 14.549150281252636,84.38926261462366z' ></path> </a> <a xlink:href="" > <path class="frag logoa" data-link="5" d='M55,55 L14.549150281252636,84.38926261462366 A50,50 0 0,1 14.54915028125263,25.61073738537635z' ></path> </a> <circle class="cente" cx='55' cy='55' r='35' ></circle> <circle class="minicirculo" cx='55' cy='55' r='15' ></circle> </svg> 

Obviously you that path is not appropriate. 显然,您认为该路径不合适。 You need to add a new, simpler, path that goes from the left of a segment to the right of the segment. 您需要添加一个新的,更简单的路径,该路径从细分的左侧到该细分的右侧。 You'll need to do that for each segment. 您需要针对每个细分市场执行此操作。

Alternatively, you can use a path that forms an entire circle, then re-use that path for each segment. 或者,您可以使用形成整个圆的路径,然后对每个线段重复使用该路径。 But specifying a different startOffset attribute for each, corresponding to their positions around the circle. 但是,为每个对象指定不同的startOffset属性,这与它们在圆上的位置相对应。

In the following example, I create a circular path with a radius of 40. 在以下示例中,我创建一个半径为40的圆形路径。

<path id="clockwise" d="M55,15 A40,40 0 0 1 55,95 A40,40 0 0 1 55,15"/>

Then I use this same path for each segment. 然后,我对每个段使用相同的路径。 To make centering the text in each segment easy, I use the attribute text-anchor="middle" which will result in the text being centred on the startOffset we specify. 为了使文本易于在每个段中居中,我使用了属性text-anchor="middle" ,它将导致文本在我们指定的startOffset上居中。

We can use percentages to specify how far around our circular path we want the text to start. 我们可以使用百分比来指定我们希望文字开始在圆形路径周围的距离。 0% would be the start of the path (also the left side of the first segment), 20% would be the start of the second segment, etc. 0%将是路径的起点(也是第一段的左侧),20%将是第二段的起点,依此类推。

So, for the first segment we want the text to be centred at a point halfway in, so we use startOffset="10%" . 因此,对于第一段,我们希望文本居中于某个点的中间,因此我们使用startOffset="10%" For subsequent segments, we would use "30%", "50%" etc. 对于后续细分,我们将使用“ 30%”,“ 50%”等。

In the example below, I've done the first three segments. 在下面的示例中,我完成了前三个部分。 I'll leave the last two for you to finish. 我将把最后两个留给您完成。

To learn more about how <textPath> works, read the relevant section of the SVG specification . 要了解有关<textPath>如何工作的更多信息,请阅读SVG规范的相关部分

 path, circle { fill: transparent; stroke: black; } 
 <svg viewBox='0 0 110 110' style="margin-top: 10px;transform: rotate(18deg);"> <defs> <!-- Circular path with a radius of 40 --> <path id="clockwise" d='M55,15 A40,40 0 0 1 55,95 A40,40 0 0 1 55,15' transform="rotate(-54,55,55)"/> </defs> <a xlink:href=""> <path class="frag logoa" id="f1" data-link="1" d='M55,55 L14.54915028125263,25.61073738537635 A50,50 0 0,1 70.45084971874736,7.447174185242318z' > </path> <text font-family="Verdana" font-size="6" fill="red";> <textPath xlink:href="#clockwise" startOffset="10%" text-anchor="middle"> Cloud Marina </textPath> </text> </a> <a xlink:href="" > <path class="frag logoa" data-link="2" d='M55,55 L70.45084971874736,7.447174185242318 A50,50 0 0,1 105,54.999999999999986z' ></path> <text font-family="Verdana" font-size="6" fill="red";> <textPath xlink:href="#clockwise" startOffset="30%" text-anchor="middle"> Order This </textPath> </text> </a> <a xlink:href="" > <path class="frag logoa" data-link="3" d='M55,55 L105,55 A50,50 0 0,1 70.45084971874738,102.55282581475768z'></path> <text font-family="Verdana" font-size="6" fill="red";> <textPath xlink:href="#clockwise" startOffset="50%" text-anchor="middle"> Earn This </textPath> </text> </a> <a xlink:href="" > <path class="frag logoa" data-link="4" d='M55,55 L70.45084971874738,102.55282581475768 A50,50 0 0,1 14.549150281252636,84.38926261462366z' ></path> </a> <a xlink:href="" > <path class="frag logoa" data-link="5" d='M55,55 L14.549150281252636,84.38926261462366 A50,50 0 0,1 14.54915028125263,25.61073738537635z' ></path> </a> <circle class="cente" cx='55' cy='55' r='35' ></circle> <circle class="minicirculo" cx='55' cy='55' r='15' ></circle> </svg> 

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

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