欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS中使用textPath實(shí)現(xiàn)線條上的文字

 更新時(shí)間:2017年12月25日 10:56:44   投稿:mrr  
最近項(xiàng)目經(jīng)理交給我一下新項(xiàng)目,要實(shí)現(xiàn)關(guān)系圖,需要在線條上繪制文字。下面小編把使用textPath實(shí)現(xiàn)線條上的文字功能分享到腳本之家平臺(tái)供大家參考

近期在項(xiàng)目中要實(shí)現(xiàn)關(guān)系圖,需要在線條上繪制文字。要實(shí)現(xiàn)這個(gè)功能,我們需要在SVG中連接的線條從標(biāo)簽line修改為path,這樣才可能實(shí)現(xiàn)類(lèi)似如下的效果:

 

1個(gè)簡(jiǎn)單的例子如下所示:

<svg viewBox="0 0 1000 300" 
   xmlns="http://www.w3.org/2000/svg"  
   xmlns:xlink="http://www.w3.org/1999/xlink"> 
  <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" fill="none" stroke="red"/> 
 <text font-family="Verdana" font-size="42.5"> 
  <textPath xlink:href="#MyPath" rel="external nofollow" > 
   We go up, then we go down, then up again 
  </textPath> 
 </text> 
</svg>

在這里我們需要實(shí)現(xiàn)1個(gè)path,然后設(shè)置其ID屬性,之后我們創(chuàng)建textPath標(biāo)簽,并鏈接到上述的ID屬性,這樣就可以實(shí)現(xiàn)在路徑上關(guān)聯(lián)文字了。

而在D3中我們可以這樣操作:

var link = svg.append("g").selectAll(".edgepath") 
       .data(graph.links) 
       .enter() 
       .append("path") 
       .style("stroke-width",0.5) 
       .style("fill","none") 
       .attr("marker-end",function(d){ 
        return "url(#"+d.source+")"; 
       }) 
       .style("stroke","black") 
       .attr("id", function(d,i){ 
        return "edgepath"+i; 
       }); 
var edges_text = svg.append("g").selectAll(".edgelabel") 
        .data(graph.nodes) 
          .enter() 
          .append("text") 
          .attr("class","edgelabel") 
          .attr("id", function(d,i){ 
           return "edgepath"+i; 
          }) 
          .attr("dx",80) 
          .attr("dy",0); 
edges_text.append("textPath") 
      .attr("xlink:href", function(d,i){ 
        return "#edgepath"+i; 
      }).text(function(d){ 
       return d.id; 
      })

實(shí)際上這段代碼就是上述例子的實(shí)現(xiàn),這樣就可以避免編寫(xiě)繁瑣的SVG代碼了。

總結(jié)

以上所述是小編給大家介紹的使用textPath實(shí)現(xiàn)線條上的文字,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論