Skip to content

Instantly share code, notes, and snippets.

@lorenzopub
Created October 23, 2018 08:19
Show Gist options
  • Save lorenzopub/b1a57d5ff302dc2343a9b7a9d1034563 to your computer and use it in GitHub Desktop.
Save lorenzopub/b1a57d5ff302dc2343a9b7a9d1034563 to your computer and use it in GitHub Desktop.
Rounded Rectangle
license: gpl-3.0
<!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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment