circle.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <TempCircle :data="tData" :ref="(v: any) => shape = v?.shape" :id="data.id" />
  3. <PropertyUpdate
  4. :describes="describes"
  5. :data="data"
  6. :target="shape"
  7. @change="emit('updateShape', { ...data })"
  8. />
  9. <Operate :target="shape" :menus="operateMenus" />
  10. </template>
  11. <script lang="ts" setup>
  12. import { CircleData, getMouseStyle, defaultStyle } from "./index.ts";
  13. import { PropertyUpdate, Operate } from "../../propertys";
  14. import TempCircle from "./temp-circle.vue";
  15. import { useComponentStatus } from "@/core/hook/use-component.ts";
  16. import { cloneRepShape, useCustomTransformer } from "@/core/hook/use-transformer.ts";
  17. import { Transform } from "konva/lib/Util";
  18. import { Circle } from "konva/lib/shapes/Circle";
  19. const props = defineProps<{ data: CircleData }>();
  20. const emit = defineEmits<{
  21. (e: "updateShape", value: CircleData): void;
  22. (e: "addShape", value: CircleData): void;
  23. (e: "delShape"): void;
  24. }>();
  25. const { shape, tData, data, operateMenus, describes } = useComponentStatus<
  26. Circle,
  27. CircleData
  28. >({
  29. emit,
  30. props,
  31. getMouseStyle,
  32. defaultStyle,
  33. transformType: "custom",
  34. customTransform(callback, shape, data) {
  35. const update = (mat: Transform, data: CircleData) => {
  36. const { x, y, scaleX } = mat.decompose();
  37. if (scaleX !== 1) {
  38. return { radius: data.radius * scaleX, x, y };
  39. } else {
  40. return { x, y };
  41. }
  42. };
  43. useCustomTransformer(shape, data, {
  44. openSnap: true,
  45. getRepShape($shape) {
  46. const repShape = cloneRepShape($shape).shape;
  47. return {
  48. shape: repShape,
  49. update(data) {
  50. repShape.x(data.x).y(data.y).radius(data.radius);
  51. },
  52. };
  53. },
  54. transformerConfig: {
  55. rotateEnabled: false,
  56. keepRatio: true,
  57. enabledAnchors: ["top-left", "top-right", "bottom-left", "bottom-right"],
  58. },
  59. beforeHandler(data, mat) {
  60. return { ...data, ...update(mat, data) };
  61. },
  62. handler(data, mat) {
  63. const setAttrib = update(mat, data);
  64. Object.assign(data, setAttrib);
  65. if (setAttrib.radius) {
  66. return true;
  67. }
  68. },
  69. callback,
  70. });
  71. },
  72. copyHandler(tf, data) {
  73. const decTf = tf.decompose();
  74. console.log(data, decTf);
  75. return {
  76. ...data,
  77. x: data.x + decTf.x,
  78. y: data.y + decTf.y,
  79. };
  80. },
  81. propertys: ["fill", "stroke", "strokeWidth", "dash", "ref", "opacity", "zIndex"],
  82. });
  83. </script>