Skip to content

Instantly share code, notes, and snippets.

@turboMaCk
Last active August 15, 2016 15:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turboMaCk/8d57adba769dbe964b5c07d1b96ec611 to your computer and use it in GitHub Desktop.
Save turboMaCk/8d57adba769dbe964b5c07d1b96ec611 to your computer and use it in GitHub Desktop.
d3 rounded rect function
<!doctype html>
<html>
<body>
<svg></svg>
</body>
</html>
function roundedRect(x, y, width, height, radius) {
return "M" + (x + radius) + "," + y +
"h" + (width - radius*2) +
"a" + radius + "," + radius + " 0 0 1 " + radius + "," + radius +
"v" + (height - 2 * radius) +
"a" + radius + "," + radius + " 0 0 1 " + -radius + "," + radius +
"h" + (radius*2 - width) +
"a" + radius + "," + radius + " 0 0 1 " + -radius + "," + -radius +
"v" + (2*radius - height) +
"a" + radius + "," + radius + " 0 0 1 " + radius + "," + -radius +
"z";
}
d3.select('svg')
.append('path')
.attr('d', function(d) {
return roundedRect(0,0,300,50,10);
})
.style('fill', 'red');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment