Advertisement
Guest User

randomizing

a guest
Mar 28th, 2021
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. <div id="yomis" hidden>
  2. <p>
  3. <div class="randomJapanese shuffle colorize">{{Expression}}</div>
  4. {{#Traditional}}
  5. <div class="randomTraditionalChinese shuffle colorize">{{Traditional}}</div>
  6. {{/Traditional}}
  7.  
  8. {{#Simplified}}
  9. <div class="randomSimplifiedChinese shuffle colorize">{{Simplified}}</div>
  10. {{/Simplified}}
  11.  
  12. <div id="container"></div>
  13. </p>
  14. </div>
  15.  
  16. <script>
  17. function shuffleArray(array) {
  18. for (let i = array.length - 1; i > 0; i--) {
  19. const j = Math.floor(Math.random() * (i + 1));
  20. [array[i], array[j]] = [array[j], array[i]];
  21. }
  22. }
  23.  
  24. function setRandomBackgroundColor() {
  25. const nightMode = document.body.classList.contains("night_mode");
  26. document.body.style.background = randomHsl(!nightMode);
  27. }
  28.  
  29. function randomHsl(lightColor) {
  30. const hue = (Math.random() * 361) | 0;
  31. const saturation = (50 + (Math.random() * 50)) | 0;
  32. let lightness = (Math.random() * 35) | 0;
  33. if (lightColor) {
  34. lightness += 65;
  35. }
  36. return `hsl(${hue}, ${saturation}%, ${lightness}%)`;
  37. }
  38.  
  39. function colorizeEachChar(element) {
  40. const text = element.textContent;
  41. const nightMode = document.body.classList.contains("night_mode");
  42. const resultArray = [];
  43. for (const char of text) {
  44. if (/\S/.test(char)) {
  45. resultArray.push(
  46. `<span style="color: ${randomHsl(nightMode)};">${char}</span>`
  47. );
  48. } else {
  49. resultArray.push(" ");
  50. }
  51. }
  52. element.innerHTML = resultArray.join("");
  53. }
  54.  
  55. setTimeout(() => {
  56. // Randomize the alignment and size of the text
  57.  
  58. const alignments = ["justify","left","center","right","top","bottom","vertical horizontal"];
  59. const alignmentindex = Math.floor(Math.random()*alignments.length);
  60.  
  61. const size = Math.floor(Math.random()*20)+Math.floor(Math.random()*20)+Math.floor(Math.random()*20)+10;
  62.  
  63. const yomis = document.getElementById("yomis");
  64. yomis.style.fontSize = size+"px";
  65. yomis.style.textAlign = alignments[alignmentindex];
  66.  
  67. // Randomize the color of each character
  68.  
  69. document.querySelectorAll(".colorize").forEach(colorizeEachChar);
  70.  
  71. // Randomize the order of the elements with the "shuffle" class
  72.  
  73. const fields = [...document.querySelectorAll(".shuffle")];
  74. shuffleArray(fields);
  75. const container = document.getElementById("container");
  76. fields.forEach((fld, index) => {
  77. if (index !== 0) {
  78. container.appendChild(document.createElement("br"));
  79. }
  80. container.appendChild(fld);
  81. });
  82.  
  83. // Randomize the background color
  84.  
  85. setRandomBackgroundColor();
  86.  
  87. // Un-hide the main container
  88.  
  89. yomis.hidden = false;
  90. }, 0);
  91. </script>
  92.  
  93.  
  94. <script>
  95. var writingModes = ["horizontal-tb", "vertical-lr"];
  96. var alignments = ["justify","left","center","right"];
  97. var randomWritingMode;
  98. var randomAlignment;
  99.  
  100. if(globalThis.onUpdateHook) {
  101. randomWritingMode = writingModes[Math.floor(Math.random() * writingModes.length)];
  102. randomAlignment = alignments[Math.floor(Math.random() * alignments.length)];
  103.  
  104. document.documentElement.style.setProperty('--writing-mode', randomWritingMode);
  105. document.documentElement.style.setProperty('--alignment', randomAlignment);
  106. }
  107. </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement