useClipImage.ts 978 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import { computed, Ref } from 'vue'
  2. import { CLIPPATHS, ClipPathTypes } from '@/configs/imageClip'
  3. import { ImageElementClip } from '@/types/slides'
  4. export default (clip: Ref<ImageElementClip | undefined>) => {
  5. const clipShape = computed(() => {
  6. if (!clip.value) return CLIPPATHS.rect
  7. const shape = clip.value.shape || ClipPathTypes.RECT
  8. return CLIPPATHS[shape]
  9. })
  10. const imgPosition = computed(() => {
  11. if (!clip.value) {
  12. return {
  13. top: '0',
  14. left: '0',
  15. width: '100%',
  16. height: '100%',
  17. }
  18. }
  19. const [start, end] = clip.value.range
  20. const widthScale = (end[0] - start[0]) / 100
  21. const heightScale = (end[1] - start[1]) / 100
  22. const left = start[0] / widthScale
  23. const top = start[1] / heightScale
  24. return {
  25. left: -left + '%',
  26. top: -top + '%',
  27. width: 100 / widthScale + '%',
  28. height: 100 / heightScale + '%',
  29. }
  30. })
  31. return {
  32. clipShape,
  33. imgPosition,
  34. }
  35. }