util.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { lineVector, Pos, vectorAngle, verticalVector } from "@/utils/math";
  2. import { onlyId, rangMod } from "@/utils/shared";
  3. import { MathUtils } from "three";
  4. import { ComponentSnapInfo } from ".";
  5. import { defaultLayer } from "@/constant";
  6. export type BaseItem = {
  7. id: string;
  8. createTime: number;
  9. zIndex: number;
  10. disableTransformer?: boolean
  11. disableEditText?: boolean
  12. disableDelete?: boolean
  13. lock: boolean,
  14. opacity: number
  15. key?: string
  16. layer: string
  17. ref: boolean
  18. hide?: boolean
  19. listening?: boolean
  20. };
  21. export const getBaseItem = (): BaseItem => ({
  22. id: onlyId(),
  23. createTime: Date.now(),
  24. lock: false,
  25. zIndex: 0,
  26. layer: defaultLayer,
  27. opacity: 1,
  28. ref: false,
  29. hide: false
  30. });
  31. export const getRectSnapPoints = (
  32. w: number,
  33. h: number,
  34. x = -w / 2,
  35. y = -h / 2
  36. ) => {
  37. const r = w + x;
  38. const b = h + y;
  39. return [
  40. { x: x, y: y },
  41. { x: r, y: y },
  42. { x: r, y: b },
  43. { x: x, y: b },
  44. { x: x + w / 2, y: y + h / 2 },
  45. ];
  46. };
  47. export const generateSnapInfos = (
  48. geo: (Pos & { view?: boolean })[],
  49. hvAxis = true,
  50. link = true,
  51. vertical = false,
  52. ): ComponentSnapInfo[] => {
  53. const len = geo.length;
  54. return geo.map((point, ndx) => {
  55. const links: Pos[] = [];
  56. const linkDirections: Pos[] = [];
  57. const linkAngle: number[] = [];
  58. const pushLink = (p: Pos) => {
  59. const prevVector = lineVector([point, p]);
  60. links.push(p);
  61. linkDirections.push(prevVector);
  62. linkAngle.push(
  63. rangMod(MathUtils.radToDeg(vectorAngle(prevVector)), 180),
  64. );
  65. if (vertical) {
  66. const vLine = verticalVector(prevVector)
  67. linkDirections.push(vLine);
  68. linkAngle.push(
  69. rangMod(MathUtils.radToDeg(vectorAngle(vLine)), 180),
  70. );
  71. }
  72. }
  73. if (link && geo.length > 1) {
  74. if (ndx > 0) {
  75. const prev = geo[rangMod(ndx - 1, len)];
  76. pushLink(prev)
  77. }
  78. if (ndx < len - 1) {
  79. const next = geo[rangMod(ndx + 1, len)];
  80. pushLink(next)
  81. }
  82. }
  83. if (hvAxis) {
  84. linkDirections.push({ x: 1, y: 0 }, { y: 1, x: 0 });
  85. linkAngle.push(0, 90);
  86. }
  87. return {
  88. point,
  89. links,
  90. linkDirections,
  91. linkAngle,
  92. };
  93. });
  94. };