current.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <template>
  2. <v-arrow
  3. :ref="(r: any) => arrow[0] = r"
  4. :config="{
  5. fill: currentColor,
  6. stroke: currentColor,
  7. strokeWidth: 1,
  8. pointerLength: 10,
  9. hitStrokeWidth: 10,
  10. pointerWidth: 10,
  11. ...currentMat,
  12. }"
  13. />
  14. <v-line
  15. v-if="!hideLine"
  16. :ref="(r: any) => arrow[1] = r"
  17. :config="{
  18. stroke: currentColor,
  19. strokeWidth: 2,
  20. hitStrokeWidth: 4,
  21. ...currentMat,
  22. }"
  23. />
  24. </template>
  25. <script setup lang="ts">
  26. import { computed, ref, watch, watchEffect } from "vue";
  27. import {
  28. useDrag,
  29. useGlobalResize,
  30. useGlobalVar,
  31. useViewer,
  32. useViewerInvertTransformConfig,
  33. useViewerTransform,
  34. } from "../drawing/hook";
  35. import { Transform } from "konva/lib/Util";
  36. import { Arrow } from "konva/lib/shapes/Arrow";
  37. import { DC } from "../drawing/dec";
  38. import { Line } from "konva/lib/shapes/Line";
  39. const props = withDefaults(
  40. defineProps<{
  41. currentTime: number;
  42. follow?: boolean;
  43. hideLine?: boolean;
  44. currentColor?: string;
  45. }>(),
  46. {
  47. follow: false,
  48. currentColor: "#fff",
  49. }
  50. );
  51. const currentColor = props.currentColor;
  52. const { misPixel } = useGlobalVar();
  53. const emit = defineEmits<{ (e: "update:currentTime", num: number): void }>();
  54. const arrow = ref<DC<Arrow>[]>([]);
  55. const { drag } = useDrag(arrow);
  56. watch(drag, (drag) => {
  57. if (!drag) return;
  58. const offsetX = drag.x;
  59. const currentX = props.currentTime * misPixel + offsetX;
  60. console.log(offsetX, currentX / misPixel);
  61. currentX >= 0 && emit("update:currentTime", currentX / misPixel);
  62. });
  63. const invConfig = useViewerInvertTransformConfig();
  64. const currentX = computed(() => props.currentTime * misPixel);
  65. const currentMat = computed(() => {
  66. return new Transform()
  67. .translate(currentX.value, 0)
  68. .scale(invConfig.value.scaleX, 1)
  69. .translate(-currentX.value, 0)
  70. .decompose();
  71. });
  72. const { viewer } = useViewer();
  73. const { size } = useGlobalResize();
  74. const viewerMat = useViewerTransform();
  75. watch(
  76. () => {
  77. if (!props.follow || !size.value) return;
  78. return currentX.value;
  79. },
  80. (x) => {
  81. if (typeof x !== "number") return;
  82. const currentPixel = viewerMat.value.point({ x: currentX.value, y: 0 }).x;
  83. const offsetX = size.value!.width / 2 - currentPixel;
  84. viewer.movePixel({ x: offsetX, y: 0 });
  85. }
  86. );
  87. watch(
  88. () => ({
  89. x: currentX.value,
  90. h: size.value!.height,
  91. shapes: arrow.value.map((item) => item.getNode()),
  92. }),
  93. ({ x, shapes }) => {
  94. if (shapes[0] && shapes[1]) {
  95. shapes[0].points([x, 0, x, 10]);
  96. shapes[1].points([x, size.value!.height, x, 10]);
  97. }
  98. }
  99. );
  100. </script>