textPath 文本路径

详细介绍SVG的textPath文本路径元素,学习如何让文本沿着路径排列。通过实例掌握文本路径的各种应用技巧。 textPath元素让文本沿着指定的路径排列,可以创建弧形文字、圆形文字等独特效果。

基本语法

<defs>
  <path id="路径ID" d="路径数据"/>
</defs>
<text>
  <textPath href="#路径ID">文本内容</textPath>
</text>

基本文本路径

文本沿着定义的曲线路径排列。

文本沿着曲线路径排列展示
<defs>
  <path id="curve1" d="M 30,100 Q 150,20 270,100" fill="none"/>
</defs>

<text font-size="18" fill="#3498db">
  <textPath href="#curve1">
    文本沿着曲线路径排列展示
  </textPath>
</text>

圆形文本

文本沿着圆形路径排列,常用于Logo设计。

SVG CIRCULAR TEXT 圆形路径文本示例 LOGO
<defs>
  <path id="circle-top" d="M 50,100 A 50,50 0 0 1 250,100" fill="none"/>
  <path id="circle-bottom" d="M 250,100 A 50,50 0 0 1 50,100" fill="none"/>
</defs>

<circle cx="150" cy="100" r="50" fill="none" stroke="#3498db" stroke-width="2"/>

<text font-size="14" fill="#e74c3c">
  <textPath href="#circle-top" startOffset="50%" text-anchor="middle">
    SVG CIRCULAR TEXT
  </textPath>
</text>

<text font-size="12" fill="#2ecc71">
  <textPath href="#circle-bottom" startOffset="50%" text-anchor="middle">
    圆形路径文本示例
  </textPath>
</text>

startOffset 起始偏移

startOffset属性控制文本在路径上的起始位置。

起点开始 中间开始 偏后开始 startOffset控制起始位置
<defs>
  <path id="line-path" d="M 30,50 L 270,50" fill="none"/>
</defs>

<text font-size="14" fill="#3498db">
  <textPath href="#line-path" startOffset="0%">
    起点开始
  </textPath>
</text>

<text font-size="14" fill="#e74c3c">
  <textPath href="#line-path" startOffset="50%">
    中间开始
  </textPath>
</text>

<text font-size="14" fill="#2ecc71">
  <textPath href="#line-path" startOffset="80%">
    偏后开始
  </textPath>
</text>

method 文本方法

method属性控制文本沿路径的排列方式。

align对齐方法 stretch拉伸方法 align保持字形 | stretch拉伸适应路径
<defs>
  <path id="curve-method" d="M 30,80 Q 150,20 270,80" fill="none"/>
</defs>

<text font-size="16" fill="#3498db">
  <textPath href="#curve-method" method="align">
    align对齐方法
  </textPath>
</text>

<text font-size="16" fill="#e74c3c">
  <textPath href="#curve-method" method="stretch">
    stretch拉伸方法
  </textPath>
</text>

spacing 间距控制

spacing属性控制字符间距的处理方式。

auto自动间距 exact精确间距 auto根据路径调整 | exact保持原始间距
<text font-size="14" fill="#3498db">
  <textPath href="#spacing-path" spacing="auto">
    auto自动间距
  </textPath>
</text>

<text font-size="14" fill="#e74c3c">
  <textPath href="#spacing-path" spacing="exact">
    exact精确间距
  </textPath>
</text>

实用案例

波浪文字

波浪形状的文字效果展示
<defs>
  <path id="wave" d="M 0,50 Q 37.5,30 75,50 T 150,50 T 225,50 T 300,50" fill="none"/>
</defs>

<text font-size="20" fill="#3498db" font-weight="bold">
  <textPath href="#wave">
    波浪形状的文字效果展示
  </textPath>
</text>

徽章文字

PREMIUM QUALITY SINCE 2024 BEST AWARD
<defs>
  <path id="badge-top" d="M 70,100 A 80,80 0 0 1 230,100" fill="none"/>
  <path id="badge-bottom" d="M 230,100 A 80,80 0 0 1 70,100" fill="none"/>
</defs>

<circle cx="150" cy="100" r="80" fill="#3498db"/>
<circle cx="150" cy="100" r="70" fill="#2980b9"/>

<text font-size="14" fill="white" font-weight="bold">
  <textPath href="#badge-top" startOffset="50%" text-anchor="middle">
    PREMIUM QUALITY
  </textPath>
</text>

<text font-size="12" fill="white">
  <textPath href="#badge-bottom" startOffset="50%" text-anchor="middle">
    SINCE 2024
  </textPath>
</text>

螺旋文字

螺旋形状的文字排列效果展示
<defs>
  <path id="spiral" d="M 150,100 
    A 10,10 0 0 1 160,100
    A 20,20 0 0 1 140,100
    A 30,30 0 0 1 170,100
    A 40,40 0 0 1 130,100
    A 50,50 0 0 1 180,100
    A 60,60 0 0 1 120,100" fill="none"/>
</defs>

<text font-size="12" fill="#3498db">
  <textPath href="#spiral">
    螺旋形状的文字排列效果展示
  </textPath>
</text>

textPath属性总结

属性说明
href引用路径#路径ID
startOffset起始偏移百分比或数值
method排列方法align / stretch
spacing间距方式auto / exact

注意事项

  1. 路径定义:路径必须在defs中定义并设置ID
  2. 路径方向:文本沿路径方向排列,路径起点决定文本起点
  3. 文本溢出:文本超出路径长度时会被截断
  4. 性能考虑:复杂路径的文本渲染可能影响性能