代码之家  ›  专栏  ›  技术社区  ›  manymanymore

无法使用d3.js在svg中绘制圆角矩形

  •  0
  • manymanymore  · 技术社区  · 6 年前

    Here 是我的代码:

    var svg = d3.select("#drawRegion")
                    .append("svg");
    
    svg
    	.attr("width", "100%")
      .attr("height", "100%")
      .attr("viewBox", "0 0 400 100");
      
    
    var roundedPath = svg
    								.append("path");
    
    var path = "M0 100 L0 0 L300 0 A100 100 0 0 0 0 300 100 Z"
    
    roundedPath
    				.attr("d", path)
            .attr("fill", "none")
            .attr("stroke-width", "2px")
            .attr("stroke", "#000");
    #drawRegion {
        width: 400px;
        height: 100px;
        display: inline-block;
        position: fixed;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        margin: auto;
        position: relative;
    }
    <div id="drawRegion">
    
    </div>
    <script src="https://d3js.org/d3.v5.min.js"></script>

    我根据文档做了所有的工作,我希望得到右下角圆角的矩形。但我得到的是一些奇怪的形状。

    根据 this 解决办法可能是改变 large-arc-flag, sweep-flag 值,我尝试了所有4种可能的组合 0 1 ,但仍有一些奇怪的形状出现。

    我在这里做错什么了?

    3 回复  |  直到 6 年前
        1
  •  3
  •   Paul LeBeau    6 年前

    有几件事不对:

    1. arc(a)命令中的值太多。
    2. 你的弧尾坐标错了。你需要从300,0扫到200100(不是300100)。
    3. 你的扫描旗错了。必须是“1”(顺时针)。

    var svg = d3.select("#drawRegion")
                    .append("svg");
    
    svg
      .attr("width", "100%")
      .attr("height", "100%")
      .attr("viewBox", "0 0 400 100");
      
    
    var roundedPath = svg
                        .append("path");
    
    var path = "M0 100 L0 0 L300 0 A100 100 0 0 1 200 100 Z"
    
    roundedPath
            .attr("d", path)
            .attr("fill", "none")
            .attr("stroke-width", "2px")
            .attr("stroke", "#000");
    #drawRegion {
        width: 400px;
        height: 100px;
        display: inline-block;
        position: fixed;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        margin: auto;
        position: relative;
    }
    <div id="drawRegion">
    
    </div>
    <script src="https://d3js.org/d3.v5.min.js"></script>
        2
  •  2
  •   Mark    6 年前

    如果检查调试控制台,将看到以下错误:

    错误:属性d:应为数字,“0 0 0 300 100 Z”

    本质上这是告诉你 d 属性格式错误。a(弧)的参数太多(多了一个0)。修正你仍然会看到你的形状是有点古怪。应该是:

    var svg = d3.select("#drawRegion")
                    .append("svg");
    
    svg
    	.attr("width", "100%")
      .attr("height", "100%")
      .attr("viewBox", "0 0 400 100");
      
    
    var roundedPath = svg
    								.append("path");
    
    var path = "M0 100 L0 0 L300 0 A100 100 0 0 1 300 100 Z"
    
    roundedPath
    				.attr("d", path)
            .attr("fill", "none")
            .attr("stroke-width", "2px")
            .attr("stroke", "#000");
     
    #drawRegion {
        width: 400px;
        height: 100px;
        display: inline-block;
        position: fixed;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        margin: auto;
        position: relative;
    }
    <div id="drawRegion">
    
    </div>
    <script src="https://d3js.org/d3.v5.min.js"></script>
        3
  •  -2
  •   Vikas Kumar    6 年前

    使用此链接

    https://bl.ocks.org/mbostock/3468167

    <!DOCTYPE html>
    <meta charset="utf-8">
    <style>
    
    body {
      margin: auto;
      width: 960px;
    }
    
    path {
      fill: #ccc;
      stroke: #000;
      stroke-width: 1.5px;
    }
    
    </style>
    <body>
    <script src="//d3js.org/d3.v3.min.js"></script>
    <script>
    
    var svg = d3.select("body").append("svg")
        .attr("width", 960)
        .attr("height", 500)
      .append("g")
        .attr("transform", "translate(480,250)");
    
    var rect = svg.append("path")
        .attr("d", rightRoundedRect(-240, -120, 480, 240, 20));
    
    // Returns path data for a rectangle with rounded right corners.
    // Note: it’s probably easier to use a <rect> element with rx and ry attributes!
    // The top-left corner is ⟨x,y⟩.
    function rightRoundedRect(x, y, width, height, radius) {
      return "M" + x + "," + y
           + "h" + (width - radius)
           + "a" + radius + "," + radius + " 0 0 1 " + radius + "," + radius
           + "v" + (height - 2 * radius)
           + "a" + radius + "," + radius + " 0 0 1 " + -radius + "," + radius
           + "h" + (radius - width)
           + "z";
    }
    
    </script>