index.ts 2.9 KB

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