effect-panorama.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. export default function Panorama({
  2. swiper,
  3. extendParams,
  4. on
  5. }) {
  6. extendParams({
  7. panorama: {
  8. depth: 200,
  9. rotate: 30,
  10. stretch: 1
  11. },
  12. });
  13. on('beforeInit', () => {
  14. if (swiper.params.effect !== 'panorama') return;
  15. swiper.classNames.push(`${swiper.params.containerModifierClass}panorama`);
  16. swiper.classNames.push(`${swiper.params.containerModifierClass}3d`);
  17. const overwriteParams = {
  18. watchSlidesProgress: true,
  19. };
  20. Object.assign(swiper.params, overwriteParams);
  21. Object.assign(swiper.originalParams, overwriteParams);
  22. });
  23. on('progress', () => {
  24. if (swiper.params.effect !== 'panorama') return;
  25. const sizesGrid = swiper.slidesSizesGrid;
  26. const {
  27. depth = 200, rotate = 30, stretch = 1
  28. } = swiper.params.panorama;
  29. const angleRad = (rotate * Math.PI) / 180;
  30. const halfAngleRad = angleRad / 2;
  31. const angleModifier = 1 / (180 / rotate);
  32. for (let i = 0; i < swiper.slides.length; i += 1) {
  33. const slideEl = swiper.slides[i];
  34. const slideProgress = slideEl.progress;
  35. const slideSize = sizesGrid[i];
  36. const progressModifier = swiper.params.centeredSlides ?
  37. 0 :
  38. (swiper.params.slidesPerView - 1) * 0.5;
  39. const modifiedProgress = slideProgress + progressModifier;
  40. const angleCos = 1 - Math.cos(modifiedProgress * angleModifier * Math.PI);
  41. const translateX = `${modifiedProgress * (stretch * slideSize / 3) * angleCos}px`;
  42. const rotateY = modifiedProgress * rotate;
  43. const radius = (slideSize * 0.5) / Math.sin(halfAngleRad);
  44. const translateZ = `${radius * angleCos - depth}px`;
  45. slideEl.transform(
  46. `translateX(${translateX}) translateZ(${translateZ}) rotateY(${rotateY}deg)`);
  47. if (swiper.params.willChange) {
  48. slideEl.willChange("transform");
  49. }
  50. slideEl.addClass('swiper-slide-panorama')
  51. }
  52. });
  53. on('setTransition', (s, duration) => {
  54. if (swiper.params.effect !== 'panorama') return;
  55. swiper.slides.forEach((slideEl) => {
  56. slideEl.transition(duration);
  57. });
  58. });
  59. }