Advertisement
yaung

js

Jul 20th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. class TextScramble {
  2. constructor(el) {
  3. this.el = el
  4. this.chars = '!@#$%^&*()_-=+{}:"|<>?,./;'
  5. this.update = this.update.bind(this)
  6. }
  7. setText(newText) {
  8. const oldText = this.el.innerText
  9. const length = Math.max(oldText.length, newText.length)
  10. const promise = new Promise((resolve) => this.resolve = resolve)
  11. this.queue = []
  12. for (let i = 0; i < length; i++) {
  13. const from = oldText[i] || ''
  14. const to = newText[i] || ''
  15. const start = Math.floor(Math.random() * 40)
  16. const end = start + Math.floor(Math.random() * 40)
  17. this.queue.push({ from, to, start, end })
  18. }
  19. cancelAnimationFrame(this.frameRequest)
  20. this.frame = 0
  21. this.update()
  22. return promise
  23. }
  24. update() {
  25. let output = ''
  26. let complete = 0
  27. for (let i = 0, n = this.queue.length; i < n; i++) {
  28. let { from, to, start, end, char } = this.queue[i]
  29. if (this.frame >= end) {
  30. complete++
  31. output += to
  32. } else if (this.frame >= start) {
  33. if (!char || Math.random() < 0.28) {
  34. char = this.randomChar()
  35. this.queue[i].char = char
  36. }
  37. output += `<span class="dud">${char}</span>`
  38. } else {
  39. output += from
  40. }
  41. }
  42. this.el.innerHTML = output
  43. if (complete === this.queue.length) {
  44. this.resolve()
  45. } else {
  46. this.frameRequest = requestAnimationFrame(this.update)
  47. this.frame++
  48. }
  49. }
  50. randomChar() {
  51. return this.chars[Math.floor(Math.random() * this.chars.length)]
  52. }
  53. }
  54.  
  55. const phrases = [
  56. 'Yes, I am a criminal.',
  57. 'But your security sucks.',
  58. 'I destroyed nothing.',
  59. 'Your database is 100% safe.',
  60. 'Just warn that you are vulnerable.',
  61. 'Sorry for index page.',
  62. 'But it is easy for you to repair.'
  63. ]
  64.  
  65. const el = document.querySelector('.text')
  66. const fx = new TextScramble(el)
  67.  
  68. let counter = 0
  69. const next = () => {
  70. fx.setText(phrases[counter]).then(() => {
  71. setTimeout(next, 1500)
  72. })
  73. counter = (counter + 1) % phrases.length
  74. }
  75.  
  76. next()
  77.  
  78. 'use strict';
  79.  
  80. var app = {
  81.  
  82. chars: ['Yaung...','Pwned','121.54.164.19','1337','YLH','N00B','LOVE YOU','HACKED!','Security','LOL','System'],
  83.  
  84. init: function () {
  85. app.container = document.createElement('div');
  86. app.container.className = 'animation-container';
  87. document.body.appendChild(app.container);
  88. window.setInterval(app.add, 100);
  89. },
  90.  
  91. add: function () {
  92. var element = document.createElement('span');
  93. app.container.appendChild(element);
  94. app.animate(element);
  95. },
  96.  
  97. animate: function (element) {
  98. var character = app.chars[Math.floor(Math.random() * app.chars.length)];
  99. var duration = Math.floor(Math.random() * 15) + 1;
  100. var offset = Math.floor(Math.random() * (50 - duration * 2)) + 3;
  101. var size = 10 + (15 - duration);
  102. element.style.cssText = 'right:'+offset+'vw; font-size:'+size+'px;animation-duration:'+duration+'s';
  103. element.innerHTML = character;
  104. window.setTimeout(app.remove, duration * 1000, element);
  105. },
  106.  
  107. remove: function (element) {
  108. element.parentNode.removeChild(element);
  109. },
  110.  
  111. };
  112.  
  113. document.addEventListener('DOMContentLoaded', app.init);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement