temp-image.vue 2.0 KB

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