SVG - 左侧圆角矩形

6

下面是生成路径(带有右圆角的矩形)的代码。 如果要求也能够将左侧角落变为圆角,应该更改什么以准备相同或通用函数?

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


3
将其按比例缩放 (-1, 1) 是最省力的选择。 - Robert Longson
@Robert Longson,这会抛出一个错误,因为您将负数传递给了 scale() - Trash Can
4
在发表错误评论之前,你应该尝试并亲身体验一下。 - Robert Longson
1
它将围绕y轴翻转形状,您可能需要进行平移以将其放置在所需位置。 - Robert Longson
https://codepen.io/anon/pen/oePNeo - Robert Longson
显示剩余2条评论
2个回答

5

做左侧操作的等效函数如下:

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

如果需要,我会让您合并它们。

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


2

原始解决方案的说明可以在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(' ');
};

请访问http://jsfiddle.net/wcvrp0mq/查看JS代码演示。


非常棒的代码片段! - ELing

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接