码迷,mamicode.com
首页 > 其他好文 > 详细

SVG:textPath深入理解

时间:2016-10-27 12:21:53      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:tga   ada   元素   pcc   ovf   平移   ejs   pre   sde   

SVG的文本可以沿着一条自定义的Path来排布,比如曲线、圆形等等,使用方式如下所示(来源MDN):

<svg 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"> We go up, then we go down, then up again </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>

 

效果如下所示:

技术分享

 

使用很简单,在<defs>下定义一个path,在<text>元素下添加一个textPath引用,即可达到效果。

我们来对代码做一点儿修改,给text元素添加x和y:

 <text x=100 y=100  font-family="Verdana" font-size="42.5">
    <textPath xlink:href="#MyPath">
      We go up, then we go down, then up again
    </textPath>
  </text>

 

效果变成了下面这样:

技术分享

 

可以注意到,text并没有进行简单的平移操作(相对于原位置向x方向移动100px,y方向移动100px)。

这要如何理解呢?

原因很简单,text的坐标系被修改了,没有加入textPath之前,text处于一个直角坐标系下,x轴和y轴是两条相互垂直的直线。

加入textPath之后,text的坐标系有如下性质:

1、坐标系的x轴为path;

2、坐标系的y轴在x轴的任意点上,方向都不一致,但是必然是该点对于x轴切线的垂直线

 

SVG:textPath深入理解

标签:tga   ada   元素   pcc   ovf   平移   ejs   pre   sde   

原文地址:http://www.cnblogs.com/anrainie/p/6003197.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!