SVG - 带左圆角的矩形

时间:2017-08-25 14:38:03

标签: javascript d3.js svg html5-canvas

下面您可以看到生成路径的代码(带有右圆角的矩形)。我应该改变什么来准备相同或通用的功能,以便有可能根据需要舍入左角?

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";
}

使用可视化链接到已创建的CodePen示例: https://codepen.io/ajv/pen/wKdrWb

2 个答案:

答案 0 :(得分:2)

执行左侧的等效功能如下所示:

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));
    .attr("d", leftRoundedRect(-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";
}

function leftRoundedRect(x, y, width, height, radius) {
  return "M" + (x + radius) + "," + y
       + "h" + (width - radius)
       + "v" + height
       + "h" + (radius - width)
       + "a" + radius + "," + radius + " 0 0 1 " + (-radius) + "," + (-radius)
       + "v" + (2 * radius - height)
       + "a" + radius + "," + radius + " 0 0 1 " + radius + "," + (-radius)
       + "z";
}

如果需要,我会让你合并。

&#13;
&#13;
body {
  margin: auto;
  width: 960px;
  background-color: tomato;
}
path {
  fill: #222;
  stroke: #fef;
  stroke-width: 1.5px;
}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
&#13;
{{1}}
&#13;
&#13;
&#13;

答案 1 :(得分:1)

http://blog.bripkens.de/2011/06/svg-path-arc-curve-command/

中描述了原始解决方案

通过我的修改,你可以为任何一个角分配一个不同的角度,也可以用x,y偏移。

function( x, y, width, height, tr, br, bl, tl ) {
    var data = [];

    // start point in top-middle of the rectangle
    data.push('M' + (x + width / 2) + ',' + y);

    // next we go to the right
    data.push('H' + (x + width - tr));

    if (tr > 0) {
        // now we draw the arc in the top-right corner
        data.push('A' + arcParameter(tr, tr, 0, 0, 1, (x + width), (y + tr)));
    }

    // next we go down
    data.push('V' + (y + height - br));

    if (br > 0) {
        // now we draw the arc in the lower-right corner
        data.push('A' + arcParameter(br, br, 0, 0, 1, (x + width - br), (y + height)));
    }

    // now we go to the left
    data.push('H' + (x + bl));

    if (bl > 0) {
        // now we draw the arc in the lower-left corner
        data.push('A' + arcParameter(bl, bl, 0, 0, 1, (x + 0), (y + height - bl)));
    }

    // next we go up
    data.push('V' + (y + tl));

    if (tl > 0) {
        // now we draw the arc in the top-left corner
        data.push('A' + arcParameter(tl, tl, 0, 0, 1, (x + tl), (y + 0)));
    }

    // and we close the path
    data.push('Z');

    return data.join(' ');
};

jsfiddle http://jsfiddle.net/wcvrp0mq/