slideToSuitableOffset.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import slideToPosition from './slideToPosition';
  2. import { getClosedHorizontal, getClosedVertical } from './getCloseEdge';
  3. import { CloseEdgeEnum } from '../types';
  4. /**
  5. * 适应到合适的图片偏移量
  6. */
  7. const slideToSuitableOffset = ({
  8. x,
  9. y,
  10. lastX,
  11. lastY,
  12. width,
  13. height,
  14. scale,
  15. touchedTime,
  16. }: {
  17. x: number;
  18. y: number;
  19. lastX: number;
  20. lastY: number;
  21. width: number;
  22. height: number;
  23. scale: number;
  24. touchedTime: number;
  25. }): {
  26. x: number;
  27. y: number;
  28. } => {
  29. // 滑动到结果的位置
  30. const { endX, endY } = slideToPosition({
  31. x,
  32. y,
  33. lastX,
  34. lastY,
  35. touchedTime,
  36. });
  37. let currentX = endX;
  38. let currentY = endY;
  39. const { innerWidth, innerHeight } = window;
  40. // 图片超出的长度
  41. const outOffsetX = (width * scale - innerWidth) / 2;
  42. const outOffsetY = (height * scale - innerHeight) / 2;
  43. const horizontalType = getClosedHorizontal(endX, scale, width);
  44. const verticalType = getClosedVertical(endY, scale, height);
  45. // x
  46. if (horizontalType === CloseEdgeEnum.Small) {
  47. currentX = 0;
  48. } else if (horizontalType === CloseEdgeEnum.Before) {
  49. currentX = outOffsetX;
  50. } else if (horizontalType === CloseEdgeEnum.After) {
  51. currentX = -outOffsetX;
  52. }
  53. // y
  54. if (verticalType === CloseEdgeEnum.Small) {
  55. currentY = 0;
  56. } else if (verticalType === CloseEdgeEnum.Before) {
  57. currentY = outOffsetY;
  58. } else if (verticalType === CloseEdgeEnum.After) {
  59. currentY = -outOffsetY;
  60. }
  61. return {
  62. x: currentX,
  63. y: currentY,
  64. };
  65. };
  66. export default slideToSuitableOffset;