Skip to content

Instantly share code, notes, and snippets.

@skokenes
Created September 26, 2019 14:25
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save skokenes/6fa266f4f50c86f77ceabcd6dfca9e42 to your computer and use it in GitHub Desktop.
Save skokenes/6fa266f4f50c86f77ceabcd6dfca9e42 to your computer and use it in GitHub Desktop.
SVG Path Generator for Rounded Rectangles
function createRoundedRectPath(x, y, width, height, radius) {
return (
// Move to position, offset by radius in x direction
"M" +(x + radius) + "," + y
// Draw a horizontal line to the top right curve start
+ "h" + (width - 2 * radius)
// Draw the top right corner curve
+ "a" + radius + "," + radius + " 0 0 1 " + radius + "," + radius
// Draw a vertical line to the bottom right corner
+ "v" + (height - 2 * radius)
// Draw the bottom right corner curve
+ "a" + radius + "," + radius + " 0 0 1 " + -radius + "," + radius
// Draw a horizontal line to the bottom left corner
+ "h" + (2 * radius - width)
// Draw the bottom left corner
+ "a" + -radius + "," + -radius + " 0 0 1 " + -radius + "," + -radius
// Draw a vertical line to the top left corner
+ "v" + (2 * radius - height)
// Draw the top left corner
+ "a" + radius + "," + -radius + " 0 0 1 " + radius + "," + -radius
// Close the shape
+ "z"
);
}
@LorenzoBloedow
Copy link

Works like a charm, great stuff!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment