Guest User

Untitled

a guest
Jun 25th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. /***
  2. * Generates the svg path data for a rounded rectangle
  3. * Rounded corners are approximated by Cubic bezier curve with control point at an end point and the other at the corner
  4. * @license MIT License
  5. * Copyright (c) 2018 Yusuf Ades
  6. * @param {Object} dimension - Dimensions of rectangle.
  7. * @param {number} width - Width.
  8. * @param {number} height - Height.
  9. * @param {number} [r=0] - Corner Radius. Keep this smaller than half of width or height.
  10. * @param {number} [x=0] - Offset from top left corner in x axis. default 0.
  11. * @param {number} [y=0] - Offset from top left corner in y axis. default 0.
  12. * @returns {string} - Rounded rectangle path data.
  13. */
  14. function roundRectPath({ width, height, r = 0, x = 0, y = 0 }) {
  15.  
  16. const v0 = { x: x, y: y };
  17. const v1 = { x: x, y: y + height };
  18. const v2 = { x: x + width, y: y + height };
  19. const v3 = { x: x + width, y: y };
  20.  
  21. return `M${v0.x} ${v0.y + r} V${v1.y - r} C${v1.x} ${v1.y} ${v1.x + r} ${v1.y} ${v1.x + r} ${v1.y} H${v2.x - r} \
  22. C${v2.x} ${v2.y} ${v2.x} ${v2.y - r} ${v2.x} ${v2.y - r} V${v3.y + r} C${v3.x} ${v3.y} ${v3.x - r} ${v3.y} ${v3.x - r} ${v3.y} \
  23. H${v0.x + r} C${v0.x} ${v0.y} ${v0.x} ${v0.y + r} ${v0.x} ${v0.y + r} Z`;
  24. }
Add Comment
Please, Sign In to add comment