Advertisement
ArtSemkin

Untitled

Jun 30th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Preloader({
  2.   scope = window.$document,
  3.   target = $('#js-preloader'),
  4.   curtain = {
  5.     element: $('#js-page-transition-curtain'),
  6.     background: $('.section-masthead').attr('data-background-color')
  7.   },
  8.   cursor = {
  9.     element: $('#js-cursor'),
  10.     offset: {
  11.       top: 0.0,
  12.       left: 0.0
  13.     }
  14.   },
  15.   counter = {
  16.     easing: 'power4.out',
  17.     duration: 25,
  18.     start: 0,
  19.     target: 100,
  20.     prefix: '',
  21.     suffix: ''
  22.   }
  23. }) {
  24.  
  25.   const self = this;
  26.   this.$scope = scope;
  27.   this.$target = target;
  28.  
  29.   // Preloader
  30.   this.$header = this.$target.find('.preloader__header');
  31.   this.$content = this.$target.find('.preloader__content');
  32.   this.$wrapperCounter = this.$target.find('.preloader__counter');
  33.   this.$counter = this.$target.find('.preloader__counter-current');
  34.   this.$wrapperCircle = this.$target.find('.preloader__circle');
  35.  
  36.   // Cursor
  37.   this.cursor = cursor;
  38.   this.cursor.centerX = parseFloat(this.$wrapperCircle.innerWidth() / 2);
  39.   this.cursor.centerY = parseFloat(this.$wrapperCircle.innerHeight() / 2);
  40.   this.cursor.posX = 0;
  41.   this.cursor.posY = 0;
  42.   this.cursor.follower = {};
  43.   this.cursor.follower.element = this.cursor.element.find('.cursor__follower');
  44.   this.cursor.follower.inner = this.cursor.element.find('#inner');
  45.   this.cursor.follower.outer = this.cursor.element.find('#outer');
  46.   this.cursor.follower.size = {
  47.     element: {
  48.       width: this.cursor.follower.element.width(),
  49.       height: this.cursor.follower.element.height()
  50.     },
  51.     inner: {
  52.       cx: this.cursor.follower.inner.attr('cx'),
  53.       cy: this.cursor.follower.inner.attr('cy'),
  54.       r: this.cursor.follower.inner.attr('r')
  55.     },
  56.     outer: {
  57.       cx: this.cursor.follower.outer.attr('cx'),
  58.       cy: this.cursor.follower.outer.attr('cy'),
  59.       r: this.cursor.follower.outer.attr('r')
  60.     }
  61.   }; // original circles dimensions
  62.  
  63.   // Mouse Coordinates
  64.   this.mouseX = window.mouseX || window.innerWidth / 2;
  65.   this.mouseY = window.mouseY || window.innerHeight / 2;
  66.  
  67.   // Curtain
  68.   this.curtain = curtain;
  69.   this.curtain.svg = this.curtain.element.find('.curtain-svg');
  70.   this.curtain.rect = this.curtain.element.find('.curtain__rect');
  71.  
  72.   // Counter
  73.   this.counter = counter;
  74.   this.counter.val = 0;
  75.  
  76.   // Main Preloader Timeline
  77.   this.timeline = new gsap.timeline({});
  78.  
  79.   // Animation Tweens
  80.   this.tweens = {
  81.     drawCircle: gsap.fromTo(this.cursor.follower.outer, {
  82.       rotate: 90,
  83.       drawSVG: '100% 100%',
  84.       transformOrigin: 'center center',
  85.     }, {
  86.       drawSVG: '0% 100%',
  87.       rotate: 0,
  88.       transformOrigin: 'center center',
  89.       ease: this.counter.easing,
  90.       duration: this.counter.duration,
  91.       paused: true,
  92.     }),
  93.     count: gsap.to(this.counter, {
  94.       duration: this.counter.duration,
  95.       val: this.counter.target,
  96.       ease: this.counter.easing,
  97.       paused: true,
  98.       onUpdate: () => {
  99.         const value = parseFloat(this.counter.val).toFixed(0);
  100.         this.$counter.text(this.counter.prefix + value + this.counter.suffix);
  101.       },
  102.     }),
  103.     followMouse: gsap.to({}, {
  104.       paused: true,
  105.       duration: 0.01,
  106.       repeat: -1,
  107.       onRepeat: () => {
  108.         this.cursor.posX += (window.mouseX - this.cursor.posX);
  109.         this.cursor.posY += (window.mouseY - this.cursor.posY - this.cursor.offset.top);
  110.         gsap.to(this.cursor.element, {
  111.           duration: 0.3,
  112.           top: 0,
  113.           left: 0,
  114.           scale: (this.cursor.posX && this.cursor.posY) ? 1 : 0,
  115.           autoAlpha: (this.cursor.posX && this.cursor.posY) ? 1 : 0,
  116.           x: this.cursor.posX || window.innerWidth / 2,
  117.           y: this.cursor.posY + this.cursor.offset.top || window.innerHeight / 2,
  118.         });
  119.       },
  120.     })
  121.   };
  122.  
  123.   _bindEvents();
  124.  
  125.   this.start = () => {
  126.     window.dispatchEvent(new CustomEvent('arts/preloader/start'));
  127.  
  128.     if (!this.$target.length) {
  129.       return;
  130.     }
  131.  
  132.     window.$body.addClass('cursor-progress');
  133.  
  134.     if (this.cursor.element.length) {
  135.       gsap.set(this.cursor.element, {
  136.         display: 'block',
  137.         top: '50%',
  138.         left: '50%',
  139.       });
  140.  
  141.       gsap.set(this.cursor.follower.element, {
  142.         width: this.$wrapperCircle.innerWidth(),
  143.         height: this.$wrapperCircle.innerHeight(),
  144.       });
  145.  
  146.       gsap.set([this.cursor.follower.inner, this.cursor.follower.outer], {
  147.         attr: {
  148.           cx: this.cursor.centerX,
  149.           cy: this.cursor.centerY,
  150.           r: this.cursor.centerX - 1,
  151.         }
  152.       });
  153.     }
  154.  
  155.     if (this.curtain.element.length) {
  156.       gsap.set(this.curtain.svg, {
  157.         fill: this.curtain.background
  158.       });
  159.  
  160.       gsap.set(this.curtain.rect, {
  161.         background: this.curtain.background
  162.       });
  163.  
  164.       gsap.set(window.$pageContent, {
  165.         autoAlpha: 0
  166.       });
  167.     }
  168.  
  169.     this.timeline.add([
  170.       this.tweens.count.play(),
  171.       this.tweens.drawCircle.play()
  172.     ]);
  173.  
  174.   }
  175.  
  176.   this.finish = () => {
  177.     return new Promise((resolve, reject) => {
  178.       if (!this.$target.length) {
  179.         window.dispatchEvent(new CustomEvent('arts/preloader/end'));
  180.         resolve(true);
  181.         return;
  182.       }
  183.  
  184.       this.timeline
  185.         .clear()
  186.         .set(this.cursor.follower.outer, {
  187.           attr: {
  188.             transform: ''
  189.           }
  190.         })
  191.         .to(this.cursor.follower.outer, {
  192.           drawSVG: '0% 100%',
  193.           rotate: 0,
  194.           transformOrigin: 'center center',
  195.           ease: 'expo.inOut',
  196.           duration: 1.2
  197.         }, 'start')
  198.         .add([
  199.           gsap.to(this.counter, {
  200.             duration: 1.2,
  201.             val: this.counter.target,
  202.             ease: 'expo.inOut',
  203.             onUpdate: () => {
  204.               const value = parseFloat(this.counter.val).toFixed(0);
  205.               this.$counter.text(this.counter.prefix + value + this.counter.suffix);
  206.             }
  207.           }),
  208.         ], 'start')
  209.         .add([
  210.           this.tweens.followMouse.play(),
  211.           gsap.to(this.cursor.follower.element, {
  212.             width: this.cursor.follower.size.element.width,
  213.             height: this.cursor.follower.size.element.height,
  214.             ease: 'expo.out',
  215.             duration: 1.2
  216.           }),
  217.           gsap.to(this.cursor.follower.inner, {
  218.             attr: this.cursor.follower.size.inner,
  219.             ease: 'expo.out',
  220.             duration: 1.2,
  221.           }),
  222.           gsap.to(this.cursor.follower.outer, {
  223.             attr: this.cursor.follower.size.outer,
  224.             ease: 'expo.out',
  225.             autoAlpha: 0,
  226.             duration: 1.2,
  227.           }),
  228.         ])
  229.         .add([
  230.           gsap.effects.moveCurtain(this.curtain.element, {
  231.             duration: 1.2
  232.           }),
  233.           gsap.to(this.$content, {
  234.             y: -30,
  235.             delay: 0.1,
  236.             duration: 0.8,
  237.             ease: 'power3.inOut',
  238.           }),
  239.           gsap.to(this.$target, {
  240.             delay: 0.2,
  241.             display: 'none',
  242.             duration: 0.8,
  243.             ease: 'power3.inOut',
  244.           })
  245.         ], '-=1.2')
  246.         .set(window.$pageContent, {
  247.           autoAlpha: 1
  248.         })
  249.         .to(this.curtain.element, {
  250.           autoAlpha: 0,
  251.           delay: 0.4,
  252.           duration: 0.3
  253.         })
  254.         .set([this.$target, this.curtain.element], {
  255.           y: '-100%',
  256.           display: 'none',
  257.         })
  258.         .set(this.cursor.element, {
  259.           clearProps: 'top,left',
  260.           x: '-50%',
  261.           y: '-50%'
  262.         })
  263.         .add(() => {
  264.           window.dispatchEvent(new CustomEvent('arts/preloader/end'));
  265.           window.$body.removeClass('cursor-progress');
  266.           this.tweens.followMouse.kill();
  267.           resolve(true);
  268.         }, '-=0.6');
  269.  
  270.     });
  271.   }
  272.  
  273.   function _bindEvents() {
  274.     self.$scope.on('mousemove', (e) => {
  275.       window.mouseX = e.clientX;
  276.       window.mouseY = e.clientY;
  277.     });
  278.   }
  279.  
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement