index.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 } from "../index.ts";
  11. import { Size } from "@/utils/math.ts";
  12. export { default as Component } from "./image.vue";
  13. export { default as TempComponent } from "./temp-image.vue";
  14. export const shapeName = "图片";
  15. export const defaultStyle = {
  16. strokeWidth: 0,
  17. stroke: "#000000",
  18. };
  19. export const addMode = "dot";
  20. export const getMouseStyle = (data: ImageData) => {
  21. const strokeStatus = getMouseColors(data.stroke || defaultStyle.stroke);
  22. return {
  23. default: { stroke: strokeStatus.pub },
  24. hover: { stroke: strokeStatus.hover },
  25. press: { stroke: strokeStatus.press },
  26. };
  27. };
  28. export const getSnapInfos = (data: ImageData) => {
  29. return generateSnapInfos(getSnapPoints(data), true, false);
  30. };
  31. export const getSnapPoints = (data: ImageData) => {
  32. const tf = new Transform(data.mat);
  33. const useData = data.width && data.height;
  34. if (!useData && !(data.url in imageInfo)) {
  35. return [];
  36. }
  37. const w = useData ? data.width : imageInfo[data.url].width;
  38. const h = useData ? data.height : imageInfo[data.url].height;
  39. const points = getRectSnapPoints(w, h);
  40. return points.map((v) => tf.point(v));
  41. };
  42. export type ImageData = Partial<typeof defaultStyle> &
  43. BaseItem & Size & {
  44. fill?: string;
  45. stroke?: string;
  46. strokeWidth?: number;
  47. cornerRadius: number;
  48. url: string;
  49. mat: number[];
  50. };
  51. export const interactiveToData: InteractiveTo<"image"> = ({
  52. info,
  53. preset = {},
  54. ...args
  55. }) => {
  56. if (info.cur) {
  57. return interactiveFixData({
  58. ...args,
  59. info,
  60. data: { ...getBaseItem(), ...preset } as unknown as ImageData,
  61. });
  62. }
  63. };
  64. export const interactiveFixData: InteractiveFix<"image"> = ({ data, info }) => {
  65. const mat = new Transform().translate(info.cur!.x, info.cur!.y);
  66. data.mat = mat.m;
  67. return data;
  68. };