index.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import { Pos } from "@/utils/math.ts";
  2. import { themeMouseColors } from "@/constant/help-style.ts";
  3. import { getMouseColors } from "@/utils/colors.ts";
  4. import { onlyId } from "@/utils/shared.ts";
  5. import { BaseItem, generateSnapInfos, getBaseItem } from "../util.ts";
  6. import { InteractiveFix, InteractiveTo, MatResponseProps } from "../index.ts";
  7. import { Transform } from "konva/lib/Util";
  8. export { default as Component } from "./rectangle.vue";
  9. export { default as TempComponent } from "./temp-rectangle.vue";
  10. export const shapeName = "矩形";
  11. export const defaultStyle = {
  12. dash: [30, 0],
  13. strokeWidth: 1,
  14. fill: '#ffffff',
  15. stroke: themeMouseColors.theme,
  16. fontSize: 16,
  17. align: "center",
  18. fontStyle: "normal",
  19. fontColor: themeMouseColors.theme,
  20. };
  21. export const getMouseStyle = (data: RectangleData) => {
  22. const fillStatus = data.fill && getMouseColors(data.fill);
  23. const strokeStatus = getMouseColors(data.stroke || defaultStyle.stroke);
  24. const strokeWidth = data.strokeWidth || defaultStyle.strokeWidth;
  25. return {
  26. default: { fill: fillStatus && fillStatus.pub, stroke: strokeStatus.pub, strokeWidth },
  27. hover: { fill: fillStatus && fillStatus.hover, stroke: strokeStatus.hover },
  28. press: { fill: fillStatus && fillStatus.press, stroke: strokeStatus.press },
  29. select: { fill: fillStatus && fillStatus.select, stroke: strokeStatus.select },
  30. };
  31. };
  32. export const addMode = "area";
  33. export type RectangleData = Partial<typeof defaultStyle> &
  34. BaseItem & {
  35. id: string;
  36. attitude: number[];
  37. points: Pos[];
  38. createTime: number;
  39. zIndex: number;
  40. dash?: number[];
  41. stroke?: string;
  42. fill?: string;
  43. strokeWidth?: number;
  44. content?: string
  45. };
  46. export const getSnapPoints = (data: RectangleData) => {
  47. return data.points
  48. }
  49. export const getSnapInfos = (data: RectangleData) => generateSnapInfos(getSnapPoints(data), true, false)
  50. export const interactiveToData: InteractiveTo<"rectangle"> = ({
  51. info,
  52. preset = {},
  53. ...args
  54. }) => {
  55. if (info.cur) {
  56. const item = {
  57. ...getBaseItem(),
  58. ...preset,
  59. id: onlyId(),
  60. createTime: Date.now(),
  61. zIndex: 0,
  62. } as unknown as RectangleData;
  63. return interactiveFixData({ ...args, info, data: item });
  64. }
  65. };
  66. export const interactiveFixData: InteractiveFix<"rectangle"> = ({
  67. data,
  68. info,
  69. }) => {
  70. if (info.cur) {
  71. const area = info.cur!;
  72. const width = area[1].x - area[0].x;
  73. const height = area[1].y - area[0].y;
  74. data.points = [
  75. info.cur[0],
  76. { x: info.cur[0].x + width, y: info.cur[0].y },
  77. { x: info.cur[0].x + width, y: info.cur[0].y + height },
  78. { x: info.cur[0].x, y: info.cur[0].y + height },
  79. ];
  80. data.attitude = [1, 0, 0, 1, 0, 0];
  81. }
  82. return data;
  83. };
  84. export const matResponse = ({data, mat, increment}: MatResponseProps<'rectangle'>) => {
  85. let transfrom: Transform
  86. const attitude = new Transform(data.attitude);
  87. if (!increment) {
  88. const inverMat = attitude.copy().invert();
  89. transfrom = mat.copy().multiply(inverMat);
  90. } else {
  91. transfrom = mat
  92. }
  93. data.points = data.points.map((v) => transfrom.point(v));
  94. data.attitude = transfrom.copy().multiply(attitude).m;
  95. return data;
  96. }