use-copy.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import { DC, EntityShape } from "@/deconstruction";
  2. import { Ref } from "vue";
  3. import { useStage } from "./use-global-vars";
  4. import { useViewerInvertTransform } from "./use-viewer";
  5. import { Transform } from "konva/lib/Util";
  6. export const useGetShapeCopyTransform = (shape: Ref<DC<EntityShape> | undefined>, padding = 10) => {
  7. const stage = useStage()
  8. const invViewTransform = useViewerInvertTransform()
  9. return () => {
  10. const $shape = shape.value!.getNode()
  11. const $stage = stage.value!.getNode()
  12. const shapeRect = $shape.getClientRect({ skipTransform: false })
  13. const stageRect = {
  14. x: 0,
  15. y: 0,
  16. width: $stage.width(),
  17. height: $stage.height()
  18. }
  19. // 可用性,分别对应 右左上下
  20. const available = [
  21. {
  22. diff: stageRect.x + stageRect.width - (shapeRect.x + shapeRect.width * 2 + padding),
  23. translate: { x: shapeRect.width + padding, y: 0 },
  24. },
  25. {
  26. diff: shapeRect.x - shapeRect.width - padding - stageRect.x,
  27. translate: { x: -shapeRect.width - padding, y: 0 },
  28. },
  29. {
  30. diff: shapeRect.y - shapeRect.height - padding - stageRect.y,
  31. translate: { x: 0, y: -shapeRect.height - padding },
  32. },
  33. {
  34. diff: stageRect.y + stageRect.height - (shapeRect.y + shapeRect.height * 2 + padding),
  35. translate: { x: 0, y: shapeRect.height + padding },
  36. }
  37. ]
  38. let translate = available.find(({ diff }) => diff >= 0)?.translate
  39. if (!translate) {
  40. // 都不可用取最可用复制位置
  41. let maxDiff = available[0].diff
  42. let ndx = 0
  43. for (let i = 1; i < available.length; i++) {
  44. if (available[i].diff > maxDiff) {
  45. ndx = i
  46. }
  47. }
  48. translate = available[ndx].translate
  49. }
  50. const origin = invViewTransform.value.point({x: 0, y: 0})
  51. const target = invViewTransform.value.point(translate);
  52. // 转化为真实坐标
  53. console.log(target.x - origin.x, target.y - origin.y)
  54. return new Transform().translate(target.x - origin.x, target.y - origin.y)
  55. }
  56. }