最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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)線條上的文字功能分享到腳本之家平臺供大家參考

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

 

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

<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),這樣就可以避免編寫繁瑣的SVG代碼了。

總結(jié)

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

相關(guān)文章

最新評論

通渭县| 抚顺市| 冷水江市| 常熟市| 金坛市| 星子县| 安乡县| 博兴县| 措勤县| 清涧县| 哈尔滨市| 大名县| 尉犁县| 木兰县| 房产| 融水| 且末县| 万安县| 望城县| 闽侯县| 荔波县| 佛冈县| 湖口县| 南宫市| 元氏县| 荣昌县| 南召县| 子长县| 波密县| 逊克县| 三穗县| 万州区| 阳信县| 府谷县| 江阴市| 海兴县| 沾益县| 慈利县| 谢通门县| 大化| 东城区|