{"id":3072,"date":"2021-09-22T14:14:42","date_gmt":"2021-09-22T14:14:42","guid":{"rendered":"https:\/\/rengga.dev\/blog\/?p=3072"},"modified":"2023-02-28T00:33:14","modified_gmt":"2023-02-28T00:33:14","slug":"js-tutorial-math-random-text-scramble","status":"publish","type":"post","link":"https:\/\/rengga.dev\/blog\/js-tutorial-math-random-text-scramble\/","title":{"rendered":"JS Tutorial &#8211; Math.random() Text Scramble"},"content":{"rendered":"<p><span style=\"color: #ef3207;\"><a style=\"color: #ef3207;\" href=\"https:\/\/rengga.dev\/\" target=\"_blank\" rel=\"noopener\"><strong>Rengga Dev<\/strong><\/a> <\/span>&#8211; <code>Math.random()<\/code> is an API in JavaScript. It is a function that gives you a random number. The number returned will be between 0 (inclusive, as in, it\u2019s possible for an actual 0 to be returned) and 1 (exclusive, as in, it\u2019s not possible for an actual 1 to be returned).<\/p>\n<p><iframe style=\"width: 100%;\" title=\"Untitled\" src=\"https:\/\/codepen.io\/renggagumilar\/embed\/abYRENV?default-tab=result&amp;theme-id=dark\" height=\"300\" frameborder=\"no\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\"><br \/>\nSee the Pen <a href=\"https:\/\/codepen.io\/renggagumilar\/pen\/abYRENV\"><br \/>\nUntitled<\/a> by Rengga Gumilar (<a href=\"https:\/\/codepen.io\/renggagumilar\">@renggagumilar<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<br \/>\n<\/iframe><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\/\/ \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\r\n\/\/ TextScramble\r\n\/\/ \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\r\n\r\nclass TextScramble {\r\n  constructor(el) {\r\n    this.el = el\r\n    this.chars = '!&lt;&gt;-_\\\\\/[]{}\u2014=+*^?#________'\r\n    this.update = this.update.bind(this)\r\n  }\r\n  setText(newText) {\r\n    const oldText = this.el.innerText\r\n    const length = Math.max(oldText.length, newText.length)\r\n    const promise = new Promise((resolve) =&gt; this.resolve = resolve)\r\n    this.queue = []\r\n    for (let i = 0; i &lt; length; i++) {\r\n      const from = oldText[i] || ''\r\n      const to = newText[i] || ''\r\n      const start = Math.floor(Math.random() * 40)\r\n      const end = start + Math.floor(Math.random() * 40)\r\n      this.queue.push({ from, to, start, end })\r\n    }\r\n    cancelAnimationFrame(this.frameRequest)\r\n    this.frame = 0\r\n    this.update()\r\n    return promise\r\n  }\r\n  update() {\r\n    let output = ''\r\n    let complete = 0\r\n    for (let i = 0, n = this.queue.length; i &lt; n; i++) {\r\n      let { from, to, start, end, char } = this.queue[i]\r\n      if (this.frame &gt;= end) {\r\n        complete++\r\n        output += to\r\n      } else if (this.frame &gt;= start) {\r\n        if (!char || Math.random() &lt; 0.28) {\r\n          char = this.randomChar()\r\n          this.queue[i].char = char\r\n        }\r\n        output += `&lt;span class=\"dud\"&gt;${char}&lt;\/span&gt;`\r\n      } else {\r\n        output += from\r\n      }\r\n    }\r\n    this.el.innerHTML = output\r\n    if (complete === this.queue.length) {\r\n      this.resolve()\r\n    } else {\r\n      this.frameRequest = requestAnimationFrame(this.update)\r\n      this.frame++\r\n    }\r\n  }\r\n  randomChar() {\r\n    return this.chars[Math.floor(Math.random() * this.chars.length)]\r\n  }\r\n}\r\n\r\n\/\/ \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\r\n\/\/ Example\r\n\/\/ \u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\u2014\r\n\r\nconst phrases = [\r\n  'Neo,',\r\n  'sooner or later',\r\n  'you\\'re going to realize',\r\n  'just as I did',\r\n  'that there\\'s a difference',\r\n  'between knowing the path',\r\n  'and walking the path'\r\n]\r\n\r\nconst el = document.querySelector('.text')\r\nconst fx = new TextScramble(el)\r\n\r\nlet counter = 0\r\nconst next = () =&gt; {\r\n  fx.setText(phrases[counter]).then(() =&gt; {\r\n    setTimeout(next, 800)\r\n  })\r\n  counter = (counter + 1) % phrases.length\r\n}\r\n\r\nnext()<\/pre>\n<p>A few phrases are stored and displayed in sequence, separated by an animation that appears to scramble the letters with random characters between phrases that are selected by Math.random.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rengga Dev &#8211; Math.random() is an API in JavaScript. It is a <a class=\"read-more\" href=\"https:\/\/rengga.dev\/blog\/js-tutorial-math-random-text-scramble\/\" title=\"JS Tutorial &#8211; Math.random() Text Scramble\" itemprop=\"url\"><\/a><\/p>\n","protected":false},"author":1,"featured_media":3797,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[264,263,274,282,103,227],"newstopic":[],"class_list":{"0":"post-3072","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-javascript","8":"tag-javascript-tutorial","9":"tag-js-tutorial","10":"tag-math-random","11":"tag-text-scramble","12":"tag-web-design","13":"tag-web-designer"},"_links":{"self":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/3072","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/comments?post=3072"}],"version-history":[{"count":1,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/3072\/revisions"}],"predecessor-version":[{"id":3074,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/3072\/revisions\/3074"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media\/3797"}],"wp:attachment":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media?parent=3072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/categories?post=3072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/tags?post=3072"},{"taxonomy":"newstopic","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/newstopic?post=3072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}