index.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { InteractiveMessage } from "../../hook/use-interactive.ts";
  2. import { CircleConfig } from "konva/lib/shapes/Circle";
  3. import { themeMouseColors } from "@/constant/help-style.ts";
  4. import {
  5. BaseItem,
  6. generateSnapInfos,
  7. getBaseItem,
  8. getRectSnapPoints,
  9. } from "../util.ts";
  10. import { getMouseColors } from "@/utils/colors.ts";
  11. export { default as Component } from "./circle.vue";
  12. export { default as TempComponent } from "./temp-circle.vue";
  13. export const shapeName = "圆形";
  14. export const defaultStyle = {
  15. dash: [1, 0],
  16. stroke: themeMouseColors.theme,
  17. strokeWidth: 1,
  18. };
  19. export const addMode = "area";
  20. export const getMouseStyle = (data: CircleData) => {
  21. const fillStatus = data.fill && getMouseColors(data.fill);
  22. const strokeStatus = getMouseColors(data.stroke || defaultStyle.stroke);
  23. const strokeWidth = data.strokeWidth || defaultStyle.strokeWidth;
  24. return {
  25. default: { fill: fillStatus && fillStatus.pub, stroke: strokeStatus.pub, strokeWidth },
  26. hover: { fill: fillStatus && fillStatus.hover, stroke: strokeStatus.hover },
  27. press: { fill: fillStatus && fillStatus.press, stroke: strokeStatus.press },
  28. };
  29. };
  30. export const getSnapInfos = (data: CircleData) => {
  31. const size = data.radius * 2;
  32. const points = getRectSnapPoints(size, size).map((v) => ({
  33. x: v.x + data.x,
  34. y: v.y + data.y,
  35. }));
  36. return generateSnapInfos(points, true, false);
  37. };
  38. export type CircleData = Partial<typeof defaultStyle> &
  39. BaseItem & {
  40. opacity?: number,
  41. fill?: string
  42. x: number;
  43. y: number;
  44. radius: number;
  45. };
  46. export const dataToConfig = (data: CircleData): CircleConfig => ({
  47. ...defaultStyle,
  48. ...data,
  49. });
  50. export const interactiveToData = (
  51. info: InteractiveMessage,
  52. preset: Partial<CircleData> = {}
  53. ): CircleData | undefined => {
  54. if (info.area) {
  55. const item = {
  56. ...getBaseItem(),
  57. ...preset,
  58. ...info.area[0],
  59. } as unknown as CircleData;
  60. return interactiveFixData(item, info);
  61. }
  62. };
  63. export const interactiveFixData = (
  64. data: CircleData,
  65. info: InteractiveMessage
  66. ) => {
  67. const area = info.area!;
  68. const xr = Math.abs(area[1].x - area[0].x);
  69. const yr = Math.abs(area[1].y - area[0].y);
  70. data.radius = Math.max(xr, yr, 0.01);
  71. return data;
  72. };