文本路径<textPath>
在SVG里,处理能沿直线方向写文字外,还能够使用<path>先定义一条路径,让文字沿着定义好的路径排列。textPath标记的作用就是放在<text>标记内部引用预定义的<path>,引用时,我们需要使用xlink:href属性指明需要引用的路径的ID。
上面的图形使用了下面的SVG代码:
<svg width="100%" height="100%" viewBox="0 0 1000 300"
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>
<use xlink:href="#MyPath" fill="none" stroke="red" />
<text font-family="Verdana" font-size="42.5">
<textPath xlink:href="#MyPath">
我先往上去,然后往下去,然后再往上去,漂亮吧!
</textPath>
</text>
<!-- Show outline of the viewport using 'rect' element -->
<rect x="1" y="1" width="998" height="298"
fill="none" stroke="black" stroke-width="2" />
</svg>