round-rect.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /** 绘制圆角矩形原型方法 */
  2. export default {
  3. name: 'roundRect',
  4. handle: (canvas, ctx, x, y, w, h, r = 15, fill = false, stroke = false) => {
  5. if (r === 0) {
  6. if (stroke)
  7. ctx.strokeRect(x, y, w, h);
  8. if (fill)
  9. ctx.fillRect(x, y, w, h);
  10. return;
  11. }
  12. if (w < 2 * r) {
  13. r = w / 2;
  14. }
  15. if (h < 2 * r) {
  16. r = h / 2;
  17. }
  18. // 开始绘制
  19. ctx.beginPath();
  20. ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5);
  21. // 移动复制
  22. ctx.moveTo(x + r, y);
  23. ctx.lineTo(x + w - r, y);
  24. ctx.lineTo(x + w, y + r);
  25. // (x,y,z,j,f) x,y圆心z半径,j起始弧度f,终止弧度
  26. ctx.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2);
  27. ctx.lineTo(x + w, y + h - r);
  28. ctx.lineTo(x + w - r, y + h);
  29. ctx.arc(x + w - r, y + h - r, r, 0, Math.PI * 0.5);
  30. ctx.lineTo(x + r, y + h);
  31. ctx.lineTo(x, y + h - r);
  32. ctx.arc(x + r, y + h - r, r, Math.PI * 0.5, Math.PI);
  33. ctx.lineTo(x, y + r);
  34. ctx.lineTo(x + r, y);
  35. if (stroke)
  36. ctx.stroke();
  37. if (fill)
  38. ctx.fill();
  39. ctx.closePath();
  40. }
  41. };