湘妃到家前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
2.3 KiB

1 month ago
  1. /**
  2. *@param cxt:canvas的上下文环境
  3. *@param x:左上角x轴坐标
  4. *@param y:左上角y轴坐标
  5. *@param width:矩形的宽度
  6. *@param height:矩形的高度
  7. *@param radius:圆的半径
  8. *@param fillColor:填充颜色
  9. **/
  10. export function fillRoundRect(cxt, x, y, width, height, radius, /*optional*/ fillColor) {
  11. //圆的直径必然要小于矩形的宽高
  12. if (2 * radius > width || 2 * radius > height) { return false; }
  13. cxt.save();
  14. cxt.translate(x, y);
  15. //绘制圆角矩形的各个边
  16. drawRoundRectPath(cxt, width, height, radius);
  17. cxt.fillStyle = fillColor || "#000"; //若是给定了值就用给定的值否则给予默认值
  18. cxt.fill();
  19. cxt.restore();
  20. }
  21. /**
  22. *@param cxt:canvas的上下文环境
  23. *@param x:左上角x轴坐标
  24. *@param y:左上角y轴坐标
  25. *@param width:矩形的宽度
  26. *@param height:矩形的高度
  27. *@param radius:圆的半径
  28. *@param lineWidth:线条粗细
  29. *@param strokeColor:线条颜色
  30. **/
  31. export function strokeRoundRect(cxt, x, y, width, height, radius, /*optional*/ lineWidth, /*optional*/ strokeColor) {
  32. //圆的直径必然要小于矩形的宽高
  33. if (2 * radius > width || 2 * radius > height) { return false; }
  34. cxt.save();
  35. cxt.translate(x, y);
  36. //绘制圆角矩形的各个边
  37. drawRoundRectPath(cxt, width, height, radius);
  38. cxt.lineWidth = lineWidth || 2; //若是给定了值就用给定的值否则给予默认值2
  39. cxt.strokeStyle = strokeColor || "#000";
  40. cxt.stroke();
  41. cxt.restore();
  42. }
  43. export function drawRoundRectPath(cxt, width, height, radius) {
  44. cxt.beginPath(0);
  45. //从右下角顺时针绘制,弧度从0到1/2PI
  46. cxt.arc(width - radius, height - radius, radius, 0, Math.PI / 2);
  47. //矩形下边线
  48. cxt.lineTo(radius, height);
  49. //左下角圆弧,弧度从1/2PI到PI
  50. cxt.arc(radius, height - radius, radius, Math.PI / 2, Math.PI);
  51. //矩形左边线
  52. cxt.lineTo(0, radius);
  53. //左上角圆弧,弧度从PI到3/2PI
  54. cxt.arc(radius, radius, radius, Math.PI, Math.PI * 3 / 2);
  55. //上边线
  56. cxt.lineTo(width - radius, 0);
  57. //右上角圆弧
  58. cxt.arc(width - radius, radius, radius, Math.PI * 3 / 2, Math.PI * 2);
  59. //右边线
  60. cxt.lineTo(width, height - radius);
  61. cxt.closePath();
  62. }
  63. export default {
  64. fillRoundRect,
  65. strokeRoundRect,
  66. drawRoundRectPath
  67. }