Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SVG - rect with left rounded corners

Below you can see the code which generates a path (rect with right rounded corners). What should I change to prepare the same or generic function to have possibility to round also left corners on demand ?

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

Link to founded CodePen example with visualization: https://codepen.io/ajv/pen/wKdrWb

like image 332
ELing Avatar asked Aug 25 '17 14:08

ELing


2 Answers

An equivalent function for doing the left hand side looks like this:

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

I'll leave you to merge them if you need to.

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>
like image 176
Paul LeBeau Avatar answered Oct 04 '22 16:10

Paul LeBeau


The original solution is described in http://blog.bripkens.de/2011/06/svg-path-arc-curve-command/

With my modifications you can assign a different angle to any corner and also offset by 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 at http://jsfiddle.net/wcvrp0mq/

like image 44
gaitat Avatar answered Oct 04 '22 15:10

gaitat