当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


d3.js path.lineTo()用法及代码示例


path.lineTo()函数用于从当前点集中绘制一条直线到给定点。

用法:

path.lineTo(x, y);

参数:它采用上述和以下所述的两个参数。

  • x:它是要画线的点的x-position。
  • y:它是要画线的点的y-position。

返回值:它不返回任何值。

范例1:从(0,0)到(100,100)划一条线。



HTML

<!DOCTYPE html> 
<html lang="en"> 
  
<head> 
    <meta charset="UTF-8"> 
    <meta name="viewport" path1tent= 
        "width=device-width,initial-scale=1.0"> 
  
    <script src= 
        "https://d3js.org/d3.v4.min.js"> 
    </script> 
      
    <style> 
        h1 { 
            color:green; 
        } 
  
        svg { 
            background-color:#f2f2f2; 
        } 
  
        .path2 { 
            stroke:#000; 
        } 
    </style> 
</head> 
  
<body> 
    <div> 
        <h1>GeeksforGeeks</h1> 
        <b>D3.js | Path.lineTo() Function</b> 
        <br><br> 
        Making a line from (0, 0) to (100, 100) <br> 
        <svg width="100" height="100"> 
            <path class="path2"> 
        </svg> 
    </div> 
      
    <script> 
  
        // Creating a path  
        var path = d3.path(); 
        path.moveTo(0, 0); 
          
        // Making line to x:100 and y:100  
        path.lineTo(100, 100); 
          
        // Closing the path  
        path.closePath(); 
        d3.select(".path2").attr("d", path);  
    </script> 
</body> 
  
</html>

输出:

范例2:使用lineTo()函数创建一个正方形。

HTML

<!DOCTYPE html> 
<html lang="en"> 
  
<head> 
    <meta charset="UTF-8"> 
    <meta name="viewport" path1tent= 
        "width=device-width,initial-scale=1.0"> 
  
    <script src= 
        "https://d3js.org/d3.v4.min.js"> 
    </script> 
  
    <style> 
        h1 { 
            color:green; 
        } 
  
        svg { 
            background-color:#f2f2f2; 
        } 
  
        .path2 { 
            stroke:#000; 
        } 
    </style> 
</head> 
  
<body> 
    <div> 
        <h1>GeeksforGeeks</h1> 
        <b>D3.js | Path.lineTo() Function</b> 
        <br><br> 
        Making a square using lineTo() function<br> 
        <svg width="100" height="100"> 
            <path class="path2"> 
        </svg> 
    </div> 
      
    <script> 
  
        // Creating a path  
        var path = d3.path(); 
        path.moveTo(10, 10); 
          
        // Making line to x:90 and y:10  
        path.lineTo(90, 10); 
          
        // Closing the path  
        path.closePath(); 
        path.moveTo(90, 10); 
          
        // Making line to x:90 and y:90  
        path.lineTo(90, 90); 
          
        // Closing the path  
        path.closePath(); 
        path.moveTo(90, 90); 
          
        // Making line to x:10 and y:90  
        path.lineTo(10, 90); 
          
        // Closing the path  
        path.closePath(); 
        path.moveTo(10, 90); 
          
        // Making line to x:10 and y:10  
        path.lineTo(10, 10); 
          
        // Closing the path  
        path.closePath(); 
        d3.select(".path2").attr("d", path);  
    </script> 
</body> 
  
</html>

输出:




相关用法


注:本文由纯净天空筛选整理自tarun007大神的英文原创作品 D3.js path.lineTo() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。