index.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import { Pos } from "@/utils/math.ts";
  2. import { flatPositions, inRevise, onlyId } from "@/utils/shared.ts";
  3. import { ArrowConfig } from "konva/lib/shapes/Arrow";
  4. import { BaseItem, generateSnapInfos, getBaseItem } from "../util.ts";
  5. import { getMouseColors } from "@/utils/colors.ts";
  6. import { InteractiveFix, InteractiveTo, MatResponseProps } from "../index.ts";
  7. import { Transform } from "konva/lib/Util";
  8. export { default as Component } from "./arrow.vue";
  9. export { default as TempComponent } from "./temp-arrow.vue";
  10. export enum PointerPosition {
  11. start = "start",
  12. end = "end",
  13. all = "all",
  14. }
  15. export const shapeName = "箭头";
  16. export const defaultStyle = {
  17. fill: '#000000',
  18. pointerPosition: PointerPosition.end,
  19. strokeWidth: 2,
  20. pointerLength: 10,
  21. };
  22. // export const fill
  23. export const addMode = "area";
  24. export const getMouseStyle = (data: ArrowData) => {
  25. const strokeStatus = getMouseColors(data.fill || defaultStyle.fill);
  26. const strokeWidth = data.strokeWidth || defaultStyle.strokeWidth;
  27. return {
  28. default: {
  29. fill: data.fill || defaultStyle.fill,
  30. strokeWidth,
  31. },
  32. hover: { fill: strokeStatus.hover },
  33. focus: { fill: strokeStatus.hover },
  34. select: { fill: strokeStatus.select },
  35. press: { fill: strokeStatus.press },
  36. };
  37. };
  38. export type ArrowData = Partial<typeof defaultStyle> &
  39. BaseItem & {
  40. points: Pos[];
  41. attitude: number[];
  42. };
  43. export const dataToConfig = (data: ArrowData): ArrowConfig => ({
  44. ...defaultStyle,
  45. ...data,
  46. points: flatPositions(data.points),
  47. hitStrokeWidth: 20,
  48. });
  49. export const getSnapInfos = (data: ArrowData) =>
  50. generateSnapInfos(getSnapPoints(data), true, true, true);
  51. export const getSnapPoints = (data: ArrowData) => data.points;
  52. export const interactiveToData: InteractiveTo<"arrow"> = ({
  53. info,
  54. preset,
  55. ...args
  56. }) => {
  57. if (info.cur) {
  58. return interactiveFixData({
  59. ...args,
  60. info,
  61. data: {
  62. ...getBaseItem(),
  63. ...preset,
  64. ...defaultStyle,
  65. id: onlyId(),
  66. points: [],
  67. attitude: [1, 0, 0, 1, 0, 0],
  68. },
  69. });
  70. }
  71. };
  72. export const interactiveFixData: InteractiveFix<"arrow"> = ({ data, info }) => {
  73. // const nv = [...info.consumed, info.cur!];
  74. // data.points.length = nv.length
  75. // for (let i = 0; i < nv.length; i++) {
  76. // if (inRevise(data.points[i], nv[i])) {
  77. // data.points[i] = nv[i]
  78. // }
  79. // }
  80. // return data;
  81. if (info.cur) {
  82. const area = info.cur!;
  83. data.points = area
  84. data.attitude = [1, 0, 0, 1, 0, 0];
  85. }
  86. return data
  87. };
  88. export const matResponse = ({data, mat, increment}: MatResponseProps<'arrow'>) => {
  89. let transfrom: Transform
  90. const attitude = new Transform(data.attitude);
  91. if (!increment) {
  92. const inverMat = attitude.copy().invert();
  93. transfrom = mat.copy().multiply(inverMat);
  94. } else {
  95. transfrom = mat
  96. }
  97. data.points = data.points.map((v) => transfrom.point(v));
  98. data.attitude = transfrom.copy().multiply(attitude).m;
  99. return data;
  100. }
  101. export const getPredefine = (key: keyof ArrowData) => {
  102. if (key === 'strokeWidth') {
  103. return { proportion: true }
  104. }
  105. }