svg: text

text, tspan

<text>和<tspan>標籤是定義文本的基本標籤。瀏覽器

text 屬性

x: 文本繪製x軸位置
y: 文本繪製y軸位置
dx: 每一個字符相對前一個字符的偏移距離
dy: 每一個字符相對前一個字符的偏移距離svg

<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">
     <text x="100" y="100" dy="100" fill="black" style="font-size:40px;">
          <tspan fill="blue" dy="-20 10">我是</tspan><tspan fill="red">中國人</tspan>
     </text>
     <path d="M100,0 V200 M0,100 H200" stroke="red"/>
</svg>

文本水平垂直居中

水平排列

text-anchor屬性可設置文本水平排列,有三個取值 start | middle | endspa

<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">
     <text x="200" y="50" text-anchor="start" fill="black" style="font-size:40px;">我是中國人</text>
     <text x="200" y="100" text-anchor="middle"  fill="black" style="font-size:40px;">我是中國人</text>
     <text x="200" y="150" text-anchor="end" fill="black" style="font-size:40px;">我是中國人</text>
     <path d="M200,0 V200 M0,100 H400" stroke="red"/>
</svg>

<textpath> 標籤

<textpath>可讓文本在指定路徑上排列 。code

<svg width="100%" height="100%" version="1.1" xmlns="http://www.w3.org/2000/svg">
     <path id="p1" d="M100,200 Q200 100 300 200 T 500 200" stroke="green" fill="none"/>
     <text style="font-size:20px;">
          <textPath xlink:href="#p1">我是根據固定曲線來繪製的文字方向</textPath>
     </text>
</svg>

超出Path的部分,瀏覽器不進行渲染。xml

文本在路徑上的偏移能夠用定位屬性  x,  y,  dx,  dy,text-anchor  和  startOffset  屬性 。io

  1. x/dx/startOffset能夠設置字符在路徑上的渲染起點。
  2. y屬性設置是沒有任何效果的。
  3. dy可設置字符在法線上的偏移。
  4. text-anchor是設置文本水平排列效果。
相關文章
相關標籤/搜索