index.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { Transform } from "konva/lib/Util";
  2. import {
  3. BaseItem,
  4. generateSnapInfos,
  5. getBaseItem,
  6. getRectSnapPoints,
  7. } from "../util.ts";
  8. import { getMouseColors } from "@/utils/colors.ts";
  9. import { imageInfo } from "@/utils/resource.ts";
  10. import { InteractiveFix, InteractiveTo, MatResponseProps } from "../index.ts";
  11. import { Size } from "@/utils/math.ts";
  12. import { themeColor } from "@/constant";
  13. export { default as Component } from "./image.vue";
  14. export { default as TempComponent } from "./temp-image.vue";
  15. export const shapeName = "图片";
  16. export const defaultStyle = {
  17. strokeWidth: 0,
  18. stroke: themeColor,
  19. };
  20. export const addMode = "dot";
  21. export const getMouseStyle = (data: ImageData) => {
  22. const strokeStatus = getMouseColors(data.stroke || defaultStyle.stroke);
  23. return {
  24. default: { stroke: data.stroke || defaultStyle.stroke },
  25. hover: { stroke: strokeStatus.hover, strokeWidth: 1 },
  26. focus: { stroke: strokeStatus.hover, strokeWidth: 1 },
  27. select: { stroke: strokeStatus.select, strokeWidth: 1 },
  28. press: { stroke: strokeStatus.press, strokeWidth: 1 },
  29. };
  30. };
  31. export const getSnapInfos = (data: ImageData) => {
  32. return generateSnapInfos(getSnapPoints(data), true, false);
  33. };
  34. export const getSnapPoints = (data: ImageData) => {
  35. const tf = new Transform(data.mat);
  36. const useData = data.width && data.height;
  37. if (!useData && !(data.url in imageInfo)) {
  38. return [];
  39. }
  40. const w = useData ? data.width : imageInfo[data.url].width;
  41. const h = useData ? data.height : imageInfo[data.url].height;
  42. const points = getRectSnapPoints(w, h);
  43. return points.map((v) => tf.point(v));
  44. };
  45. export type ImageData = Partial<typeof defaultStyle> &
  46. BaseItem & Size & {
  47. fill?: string;
  48. stroke?: string;
  49. widthRaw?: number;
  50. proportion?: {
  51. scale: number;
  52. unit: string;
  53. };
  54. heightRaw?: number
  55. strokeWidth?: number;
  56. cornerRadius?: number;
  57. url: string;
  58. mat: number[];
  59. };
  60. export const interactiveToData: InteractiveTo<"image"> = ({
  61. info,
  62. preset = {},
  63. ...args
  64. }) => {
  65. if (info.cur) {
  66. return interactiveFixData({
  67. ...args,
  68. info,
  69. data: { ...getBaseItem(), ...preset } as unknown as ImageData,
  70. });
  71. }
  72. };
  73. export const interactiveFixData: InteractiveFix<"image"> = ({ data, info }) => {
  74. const mat = new Transform().translate(info.cur!.x, info.cur!.y);
  75. data.mat = mat.m;
  76. return data;
  77. };
  78. export const matResponse = ({data, mat, increment}: MatResponseProps<'image'>) => {
  79. data.mat = increment ? mat.copy().multiply(new Transform(data.mat)).m : mat.m;
  80. return data;
  81. }