文本可以添加到SVG路径吗?

时间:2011-08-09 16:06:43

标签: javascript jquery svg

是否可以在svg路径中添加文本,我创建了一个svg三角形,并希望在此中心添加一个字母,但不确定这是否可行?

1 个答案:

答案 0 :(得分:14)

是。有关使用section 10.13 of the SVG1.1 specification元素的信息,请参阅textPath(标题为“路径上的文字”)。

总结:

  1. 为您的路径指定id属性。
  2. 创建<textPath xlink:href="#pathid">My text here</textPath>
  3. 这是一个直接来自规范的例子:

    SVG Image with text following the curve of the path with ID "MyPath"

    <?xml version="1.0" standalone="no"?>
    <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
      "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
    <svg width="12cm" height="3.6cm" viewBox="0 0 1000 300" version="1.1"
         xmlns="http://www.w3.org/2000/svg"
         xmlns:xlink="http://www.w3.org/1999/xlink">
      <defs>
        <path id="MyPath"
              d="M 100 200 
                 C 200 100 300   0 400 100
                 C 500 200 600 300 700 200
                 C 800 100 900 100 900 100" />
      </defs>
      <desc>Example toap01 - simple text on a path</desc>
    
      <use xlink:href="#MyPath" fill="none" stroke="red"  />
      <text font-family="Verdana" font-size="42.5" fill="blue" >
        <textPath xlink:href="#MyPath">
          We go up, then we go down, then up again
        </textPath>
      </text>
    
    </svg>