Advertisement
Guest User

BBabbababalaka

a guest
Oct 18th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4.  
  5. // ——————————————————————————————————————————————————
  6. // TextScramble
  7. // ——————————————————————————————————————————————————
  8.  
  9. var TextScramble = function () {
  10.   function TextScramble(el) {
  11.     _classCallCheck(this, TextScramble);
  12.  
  13.     this.el = el;
  14.     this.chars = '!<>-_\\/[]{}—=+*^?#________';
  15.     this.update = this.update.bind(this);
  16.   }
  17.  
  18.   TextScramble.prototype.setText = function setText(newText) {
  19.     var _this = this;
  20.  
  21.     var oldText = this.el.innerText;
  22.     var length = Math.max(oldText.length, newText.length);
  23.     var promise = new Promise(function (resolve) {
  24.       return _this.resolve = resolve;
  25.     });
  26.     this.queue = [];
  27.     for (var i = 0; i < length; i++) {
  28.       var from = oldText[i] || '';
  29.       var to = newText[i] || '';
  30.       var start = Math.floor(Math.random() * 40);
  31.       var end = start + Math.floor(Math.random() * 40);
  32.       this.queue.push({ from: from, to: to, start: start, end: end });
  33.     }
  34.     cancelAnimationFrame(this.frameRequest);
  35.     this.frame = 0;
  36.     this.update();
  37.     return promise;
  38.   };
  39.  
  40.   TextScramble.prototype.update = function update() {
  41.     var output = '';
  42.     var complete = 0;
  43.     for (var i = 0, n = this.queue.length; i < n; i++) {
  44.       var _queue$i = this.queue[i];
  45.       var from = _queue$i.from;
  46.       var to = _queue$i.to;
  47.       var start = _queue$i.start;
  48.       var end = _queue$i.end;
  49.       var char = _queue$i.char;
  50.  
  51.       if (this.frame >= end) {
  52.         complete++;
  53.         output += to;
  54.       } else if (this.frame >= start) {
  55.         if (!char || Math.random() < 0.28) {
  56.           char = this.randomChar();
  57.           this.queue[i].char = char;
  58.         }
  59.         output += '<span class="dud">' + char + '</span>';
  60.       } else {
  61.         output += from;
  62.       }
  63.     }
  64.     this.el.innerHTML = output;
  65.     if (complete === this.queue.length) {
  66.       this.resolve();
  67.     } else {
  68.       this.frameRequest = requestAnimationFrame(this.update);
  69.       this.frame++;
  70.     }
  71.   };
  72.  
  73.   TextScramble.prototype.randomChar = function randomChar() {
  74.     return this.chars[Math.floor(Math.random() * this.chars.length)];
  75.   };
  76.  
  77.   return TextScramble;
  78. }();
  79.  
  80. // ——————————————————————————————————————————————————
  81. // Example
  82. // ——————————————————————————————————————————————————
  83.  
  84. var phrases = ['Neo,', 'sooner or later', 'you\'re going to realize', 'just as I did', 'that there\'s a difference', 'between knowing the path', 'and walking the path'];
  85.  
  86. var el = document.querySelector('.text');
  87. var fx = new TextScramble(el);
  88.  
  89. var counter = 0;
  90. var next = function next() {
  91.   fx.setText(phrases[counter]).then(function () {
  92.     setTimeout(next, 800);
  93.   });
  94.   counter = (counter + 1) % phrases.length;
  95. };
  96.  
  97. next();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement