temp-image.vue 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <template>
  2. <v-group :config="groupConfig" v-if="groupConfig" ref="shape">
  3. <v-image
  4. :config="{
  5. ...data,
  6. ...config,
  7. id: undefined,
  8. zIndex: undefined,
  9. }"
  10. v-if="image"
  11. />
  12. </v-group>
  13. </template>
  14. <script lang="ts" setup>
  15. import { defaultStyle, ImageData } from "./index.ts";
  16. import { computed, ref, watch } from "vue";
  17. import { getImage } from "@/utils/resource.ts";
  18. import { useResize } from "@/core/hook/use-event.ts";
  19. import { Transform } from "konva/lib/Util";
  20. import { Group } from "konva/lib/Group";
  21. import { DC } from "@/deconstruction.js";
  22. const props = defineProps<{ data: ImageData; addMode?: boolean }>();
  23. const data = computed(() => ({ ...defaultStyle, ...props.data }));
  24. const image = ref<HTMLImageElement | null>(null);
  25. const shape = ref<DC<Group>>();
  26. defineExpose({
  27. get shape() {
  28. return shape.value;
  29. },
  30. });
  31. watch(
  32. () => data.value.url,
  33. async (url) => {
  34. image.value = null;
  35. image.value = await getImage(url);
  36. },
  37. { immediate: true }
  38. );
  39. const size = useResize();
  40. const config = computed(() => {
  41. let w = data.value.width;
  42. let h = data.value.height;
  43. // 认为是百分比
  44. if (image.value && size.value && (w <= 1 || h <= 1)) {
  45. w = w <= 1 ? size.value.width * w : w;
  46. h = h <= 1 ? size.value.height * h : h;
  47. w = w || (image.value.width / image.value.height) * h;
  48. h = h || (image.value.height / image.value.width) * w;
  49. }
  50. w = w || image.value?.width || 0;
  51. h = h || image.value?.height || 0;
  52. return {
  53. image: image.value,
  54. opacity: props.addMode ? 0.3 : data.value.opacity,
  55. width: w,
  56. height: h,
  57. offset: {
  58. x: w / 2,
  59. y: h / 2,
  60. },
  61. };
  62. });
  63. const groupConfig = computed(() => {
  64. return {
  65. id: data.value.id,
  66. ...new Transform(data.value.mat).decompose(),
  67. };
  68. });
  69. </script>