SVG:带扇区和文本路径的圆

Jud*_*ahA 1 html svg svg-animate

我绝对是 SVG 的初学者,我必须创建类似下图的东西:

在此输入图像描述

规格:

  • 圆圈
  • 具有顶部扇形 (90°)
  • 顶部部分有一些文字

这是我的尝试:

<svg viewBox="0 0 100 100">

    <circle cx="50%" cy="50%" r="50%" style="fill:none;stroke:#00be00;stroke-width:5" />
    <path id="top-sector" style="fill:none;stroke:#be3000" d="M 15,37 A 50,50 0 0 1 80,50" />

    <text text-anchor="middle">
      <textPath xlink:href="#top-sector" startOffset="50%" style="font-size: 6px;">Hello World</textPath>
    </text>

</svg>
Run Code Online (Sandbox Code Playgroud)

JsFiddle: https: //jsfiddle.net/9hprLxat/2/

我不知道:

  • 如何将顶部扇区与圆圈对齐。
  • 如何使 textPath 透明。
  • 为什么圆圈溢出了 viewBox 感谢您的帮助!

Ale*_*_TT 5

也许是这样

<svg  xmlns="http://www.w3.org/2000/svg" 
    xmlns:xlink="http://www.w3.org/1999/xlink" width="50%" height="50%" viewBox="0 -10 120 120">

          <!-- Green circle    -->
   <circle cx="55" cy="55" r="50" style="fill:none;stroke:#92D050;stroke-width:10" />
    <!-- Red segment -->
     <circle  cx="55" cy="55" r="50" stroke="#C0504D" stroke-width="10" stroke-dasharray="78.5 235.5" stroke-dashoffset="117.75" fill="none" />
          <!-- Path for text -->
	 <path id="top-sector" style="fill:none;stroke:none" d="M 9,50 A 46,46.5 0 0 1 100.5,50" /> 
	<text text-anchor="middle">
      <textPath xlink:href="#top-sector" startOffset="50%" style="font-size: 10px; font-weight:700;">Hello World</textPath>
    </text>

</svg>
Run Code Online (Sandbox Code Playgroud)

更新


红色部分长度的计算

R ="50"该扇区所在的 整圆长度

C=2 * PI * R = 314

红色扇区所占整圆长度的四分之一是314/4 = 78.5

公式stroke-dasharray ="78.5 235.5"中 78.5 是破折号;235.5-间隙


如何将顶部扇区与圆圈对齐。

顶部扇区对齐是使用属性实现的stroke-dashoffset="117.75"

该属性指示圆起点的偏移量。我们将圆移动四分之一圈,再移动八分之一圈 78.5 + 39.25 = 117.75

如何使 textPath 透明。

style="fill:none;stroke:none"

为什么圆圈会溢出viewBox

由于宽线相对于圆的轮廓对称,因此其外部被切除。

在此输入图像描述

我必须扩大viewBox并将整个图像向下移动 10px viewBox="0 -10 120 120"

奖金

扇区内文本移动动画的示例

<svg id="svg1"  xmlns="http://www.w3.org/2000/svg" 
    xmlns:xlink="http://www.w3.org/1999/xlink" width="50%" height="50%" viewBox="0 -10 120 120">

          <!-- Green circle    -->
   <circle cx="55" cy="55" r="50" style="fill:none;stroke:#92D050;stroke-width:10" />
    <!-- Red segment -->
     <circle  cx="55" cy="55" r="50" stroke="#C0504D" stroke-width="10" stroke-dasharray="78.5 235.5" stroke-dashoffset="117.75" fill="none" />
          <!-- Path for text -->
	 <path id="top-sector" style="fill:none;stroke:none" d="M 9,50 A 46,46.5 0 0 1 100.5,50" /> 
	<text id="txt1" text-anchor="middle">
      <textPath xlink:href="#top-sector" startOffset="50%" style="font-size: 10px; font-weight:700;">Hello World 
	     <!-- Text movement animation starts after a click -->
       <animate
	     begin="svg1.click"
		 dur="4s"
		 repeatCount="indefinite"
		 attributeName="startOffset"
		 values="50%;42%;50%;50%;58%;50%;50%"/>
	  </textPath> 
         <!-- Text repainting animation starts after a click	   -->
	   <animate
	     attributeName="fill"
		 to="yellow"
		 begin="svg1.click"
		 dur="0.2s"
		 fill="freeze" /> 
     </text> 
	
	<text x="46%" y="50%" text-anchor="middle" font-size="14px" fill="dodgerblue"> Click me </text>

</svg>
Run Code Online (Sandbox Code Playgroud)