index.ts 3.2 KB

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