JSFiddle - React, Tailwind, and code Playground
JavaScript
// generate a path's arc data parameter
// http://www.w3.org/TR/SVG/paths.html#PathDataEllipticalArcCommands
var arcParameter = function(rx, ry, xAxisRotation, largeArcFlag, sweepFlag,
x, y) {
return [rx,
',',
ry,
' ',
xAxisRotation,
' ',
largeArcFlag,
',',
sweepFlag,
' ',
x,
',',
y].join('');
};
/*
* Generate a path's data attribute
*
* @param {Number} width Width of the rectangular shape
* @param {Number} height Height of the rectangular shape
* @param {Number} tr Top border radius of the rectangular shape
* @param {Number} br Bottom border radius of the rectangular shape
* @return {String} a path's data attribute value
*/
var generatePathData = function(width, height, tr, br) {
var data = [];
// start point in top-middle of the rectangle
data.push('M' + width / 2 + ',' + 0);
// next we go to the right
data.push('H' + (width - tr));
if (tr > 0) {
// now we draw the arc in the top-right corner
data.push('A' + arcParameter(tr, tr, 0, 0, 1, width, tr));
}
// next we go down
data.push('V' + (height - br));
if (br > 0) {
// now we draw the arc in the lower-right corner
data.push('A' + arcParameter(br, br, 0, 0, 1, width - br,
height));
}
// now we go to the left
data.push('H' + br);
if (br > 0) {
// now we draw the arc in the lower-left corner
data.push('A' + arcParameter(br, br, 0, 0, 1, 0, height - br));
}
// next we go up
data.push('V' + tr);
if (tr > 0) {
// now we draw the arc in the top-left corner
...