Guest User

mery2

a guest
Feb 8th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  2.  
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4.  
  5. var rid = null;
  6. var frames = 0;
  7. var index = 0;
  8. var fontSize = 50;
  9.  
  10. var lines = [];
  11. var points = [];
  12.  
  13. var speed = 11;
  14.  
  15. var ctx = canvas.getContext("2d");
  16. var btx = buffer.getContext("2d");
  17.  
  18. var bw = buffer.width = fontSize;
  19. var bh = buffer.height = fontSize;
  20. var cw = canvas.width = window.innerWidth;
  21. var ch = canvas.height = 300;
  22. btx.fillStyle = ctx.fillStyle = "#fff";
  23. btx.font = ctx.font = fontSize + "px Courier New";
  24.  
  25. var text = ["Kita hanya orang asing yang tak mengenal satu sama lain,",
  26. "Lalu dipertemukan",
  27. "Oleh rutinitas kehidupan",
  28. "Kita sering berpapasan",
  29. "Namun terlewatkan",
  30. "Sebab kita tak mengenal satu sama lain",
  31. "Sebenarnya timbul rasa penasaran",
  32. "Untuk ingin berkenalan",
  33. "Bukan aku tak memiliki keberanian",
  34. "Namun aku pernah dihiraukan",
  35. "Dan diabaikan",
  36. "Dan aku menerima itu sebagai sebuah kenyataan",
  37. "Dan aku juga menerima kekalahan",
  38. "Karena sesungguhnya dalam kehidupan",
  39. "Adalah bagaimana menerima keadaan dan kenyataan",
  40. "Sebagai sebuah kedewasaan",
  41. "Sekian :)",];
  42.  
  43. text.map(function (t, i) {
  44.   text[i] = t.toUpperCase();
  45. });
  46.  
  47. var Line = function () {
  48.   function Line(text) {
  49.     _classCallCheck(this, Line);
  50.  
  51.     this.text = text;
  52.     this.width = ctx.measureText(this.text).width;
  53.     this.startingPoint = -this.width / 2;
  54.     this.letters = [];
  55.  
  56.     this.lettersRy();
  57.   }
  58.  
  59.   _createClass(Line, [{
  60.     key: "lettersRy",
  61.     value: function lettersRy() {
  62.       for (var i = 0; i < this.text.length; i++) {
  63.         var letter = this.text[i];
  64.         var _start = this.startingPoint + ctx.measureText(this.text.substring(0, i)).width;
  65.         var pos = { x: _start, y: (ch - fontSize) / 2 };
  66.         this.letters.push(new Letter(i, pos, letter));
  67.       }
  68.     }
  69.   }]);
  70.  
  71.   return Line;
  72. }();
  73.  
  74. var Letter = function () {
  75.   function Letter(i, pos, letter) {
  76.     _classCallCheck(this, Letter);
  77.  
  78.     this.index = index;
  79.     this.letter = letter;
  80.     this.pos = pos;
  81.     this.points = [];
  82.     this.addToPointsRy();
  83.   }
  84.  
  85.   _createClass(Letter, [{
  86.     key: "addToPointsRy",
  87.     value: function addToPointsRy() {
  88.       btx.clearRect(0, 0, bw, bh);
  89.       btx.fillText(this.letter, 2, bh - 2);
  90.       var imgData = btx.getImageData(0, 0, bw, bh);
  91.       var pixels = imgData.data;
  92.  
  93.       for (var i = 0; i < pixels.length; i += 4) {
  94.         if (pixels[i] == 255) {
  95.           var x = 0.25 * i % bw;
  96.           var y = ~~(0.25 * i / bw);
  97.           this.points.push(new Particle(x + this.pos.x, y + this.pos.y));
  98.         }
  99.       }
  100.     }
  101.   }]);
  102.  
  103.   return Letter;
  104. }();
  105.  
  106. var Particle = function () {
  107.   function Particle(x, y) {
  108.     _classCallCheck(this, Particle);
  109.  
  110.     this.x = x;
  111.     this.y = y;
  112.     this.pos = { x: x, y: y };
  113.     this.pn = {
  114.       // positive or negative
  115.       x: Math.random() > 0.2 ? 1 : -1,
  116.       y: Math.random() > 0.2 ? -1 : 1
  117.     };
  118.     this.vel = {
  119.       x: this.pn.x * (this.pn.x + Math.random() * 10) / 90,
  120.       y: this.pn.y * (this.pn.y + Math.random() * 10) / 90,
  121.       alp: (0.1 + Math.random()) / 60
  122.     };
  123.     this.alp = 1;
  124.   }
  125.  
  126.   _createClass(Particle, [{
  127.     key: "draw",
  128.     value: function draw() {
  129.       ctx.fillStyle = "rgba(255,255,255," + this.alp + ")";
  130.       ctx.beginPath();
  131.       ctx.fillRect(this.pos.x, this.pos.y, 1, 1);
  132.     }
  133.   }, {
  134.     key: "update",
  135.     value: function update() {
  136.       this.pos.x += this.vel.x;
  137.       this.pos.y += this.vel.y;
  138.       this.alp -= this.vel.alp;
  139.     }
  140.   }]);
  141.  
  142.   return Particle;
  143. }();
  144.  
  145. for (var i = 0; i < text.length; i++) {
  146.   lines.push(new Line(text[i]));
  147. }
  148.  
  149. var numLine = 0;
  150. var line = lines[numLine];
  151.  
  152. ctx.translate(cw / 2, 0);
  153. function Frame() {
  154.   rid = window.requestAnimationFrame(Frame);
  155.  
  156.   ctx.clearRect(-cw, -ch, 2 * cw, 2 * ch);
  157.   points.map(function (p, i) {
  158.     p.update();
  159.     p.draw();
  160.     if (p.alp <= 0) {
  161.       points.splice(i, 1);
  162.     }
  163.   });
  164.  
  165.   if (frames % speed == 0) {
  166.     line.letters[index].points.map(function (p) {
  167.       p.pos = { x: p.x, y: p.y };
  168.       p.alp = 1;
  169.     });
  170.     points = points.concat(line.letters[index].points);
  171.     index++;
  172.   }
  173.  
  174.   if (index == line.letters.length) {
  175.     numLine++;
  176.     line = lines[numLine % text.length];
  177.     index = 0;
  178.     frames = 0;
  179.   }
  180.   frames++;
  181. }
  182. //Frame();
  183.  
  184. function Init() {
  185.   cw = canvas.width = window.innerWidth;
  186.   ctx.translate(cw / 2, 0);
  187.   //let imgHeight = window.getComputedStyle(LC, null).getPropertyValue("height");
  188.   //canvas.style.bottom = imgHeight + "px";
  189.   if (rid) {
  190.     window.cancelAnimationFrame(rid);
  191.     rid = null;
  192.   }
  193.   Frame();
  194. }
  195.  
  196. setTimeout(function () {
  197.   Init();
  198.   window.addEventListener("resize", Init, false);
  199. }, 15);
Advertisement
Add Comment
Please, Sign In to add comment