monitorNoRoom.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { Pen } from "@meta2d/core";
  2. export function monitorNoRoom(ctx: CanvasRenderingContext2D, pen: Pen): void {
  3. const radius = pen.calculative.borderRadius;
  4. const {
  5. x: rectX,
  6. y: rectY,
  7. width: rectWidth,
  8. height: rectHeight,
  9. } = pen.calculative.worldRect;
  10. ctx.save();
  11. // 条纹参数
  12. const stripeSpacing = 12;
  13. const stripeColor = "#d4d8d7";
  14. // 1. 绘制灰色背景
  15. ctx.beginPath();
  16. ctx.roundRect(rectX, rectY, rectWidth, rectHeight, radius);
  17. ctx.fillStyle = "#f1f1f1";
  18. ctx.fill();
  19. // 2. 创建裁剪区域
  20. ctx.beginPath();
  21. ctx.roundRect(rectX, rectY, rectWidth, rectHeight, radius);
  22. ctx.clip();
  23. // 3. 计算斜线参数(修正方向算法)
  24. const diagonal = Math.sqrt(rectWidth ** 2 + rectHeight ** 2);
  25. const bStep = stripeSpacing * Math.sqrt(2); // 修正步长算法
  26. // 4. 动态计算绘制范围(解决窄矩形问题)
  27. const startB = rectY - rectX - diagonal * 2; // 扩展起始位置
  28. const endB = rectY + rectHeight + rectX + diagonal * 2; // 扩展结束位置
  29. // 5. 绘制斜线(修正坐标计算)
  30. ctx.strokeStyle = stripeColor;
  31. ctx.lineWidth = 6;
  32. for (let b = startB; b <= endB; b += bStep) {
  33. ctx.beginPath();
  34. ctx.moveTo(rectX - diagonal, -(rectX - diagonal) + b);
  35. ctx.lineTo(
  36. rectX + rectWidth + diagonal,
  37. -(rectX + rectWidth + diagonal) + b
  38. );
  39. ctx.stroke();
  40. }
  41. ctx.restore();
  42. }
  43. export const monitorNoRoomData = {
  44. name: "monitorNoRoom",
  45. width: 214,
  46. height: 140,
  47. borderRadius: 12,
  48. };