Btwonu

solution-extended

Oct 5th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // You can make the colorData arr a json file called data.json, add it to the
  2. // current directory and use the commented out function to get the colors
  3. // this makes the colorData arr unnecessary
  4.  
  5. // extract colors data from JSON
  6. // function readTextFile(file, callback) {
  7. //   var rawFile = new XMLHttpRequest();
  8. //   rawFile.overrideMimeType('application/json');
  9. //   rawFile.open('GET', file, true);
  10.  
  11. //   rawFile.onreadystatechange = function () {
  12. //     if (rawFile.readyState === 4 && rawFile.status == '200') {
  13. //       callback(rawFile.responseText);
  14. //     }
  15. //   };
  16.  
  17. //   rawFile.send(null);
  18. // }
  19.  
  20. // // get json data from local file
  21. // readTextFile('./data.json', (text) => {
  22. //   const dataArr = JSON.parse(text);
  23. //   generateColors(dataArr);
  24. // });
  25.  
  26. // REWRITE of the above with hard-coded color data, instead of fetching it
  27.  
  28. const colorData = [
  29.   'IndianRed',
  30.   'LightCoral',
  31.   'Salmon',
  32.   'DarkSalmon',
  33.   'LightSalmon',
  34.   'Crimson',
  35.   'Red',
  36.   'FireBrick',
  37.   'DarkRed',
  38.   'Pink',
  39.   'LightPink',
  40.   'HotPink',
  41.   'DeepPink',
  42.   'MediumVioletRed',
  43.   'PaleVioletRed',
  44.   'LightSalmon',
  45.   'Coral',
  46.   'Tomato',
  47.   'OrangeRed',
  48.   'DarkOrange',
  49.   'Orange',
  50.   'Gold',
  51.   'Yellow',
  52.   'LightYellow',
  53.   'LemonChiffon',
  54.   'LightGoldenrodYellow',
  55.   'PapayaWhip',
  56.   'Moccasin',
  57.   'PeachPuff',
  58.   'PaleGoldenrod',
  59.   'Khaki',
  60.   'DarkKhaki',
  61.   'Lavender',
  62.   'Thistle',
  63.   'Plum',
  64.   'Violet',
  65.   'Orchid',
  66.   'Fuchsia',
  67.   'Magenta',
  68.   'MediumOrchid',
  69.   'MediumPurple',
  70.   'RebeccaPurple',
  71.   'BlueViolet',
  72.   'DarkViolet',
  73.   'DarkOrchid',
  74.   'DarkMagenta',
  75.   'Purple',
  76.   'Indigo',
  77.   'SlateBlue',
  78.   'DarkSlateBlue',
  79.   'MediumSlateBlue',
  80.   'GreenYellow',
  81.   'Chartreuse',
  82.   'LawnGreen',
  83.   'Lime',
  84.   'LimeGreen',
  85.   'PaleGreen',
  86.   'LightGreen',
  87.   'MediumSpringGreen',
  88.   'SpringGreen',
  89.   'MediumSeaGreen',
  90.   'SeaGreen',
  91.   'ForestGreen',
  92.   'Green',
  93.   'DarkGreen',
  94.   'YellowGreen',
  95.   'OliveDrab',
  96.   'Olive',
  97.   'DarkOliveGreen',
  98.   'MediumAquamarine',
  99.   'DarkSeaGreen',
  100.   'LightSeaGreen',
  101.   'DarkCyan',
  102.   'Teal',
  103.   'Aqua',
  104.   'Cyan',
  105.   'LightCyan',
  106.   'PaleTurquoise',
  107.   'Aquamarine',
  108.   'Turquoise',
  109.   'MediumTurquoise',
  110.   'DarkTurquoise',
  111.   'CadetBlue',
  112.   'SteelBlue',
  113.   'LightSteelBlue',
  114.   'PowderBlue',
  115.   'LightBlue',
  116.   'SkyBlue',
  117.   'LightSkyBlue',
  118.   'DeepSkyBlue',
  119.   'DodgerBlue',
  120.   'CornflowerBlue',
  121.   'MediumSlateBlue',
  122.   'RoyalBlue',
  123.   'Blue',
  124.   'MediumBlue',
  125.   'DarkBlue',
  126.   'Navy',
  127.   'MidnightBlue',
  128.   'Cornsilk',
  129.   'BlanchedAlmond',
  130.   'Bisque',
  131.   'NavajoWhite',
  132.   'Wheat',
  133.   'BurlyWood',
  134.   'Tan',
  135.   'RosyBrown',
  136.   'SandyBrown',
  137.   'Goldenrod',
  138.   'DarkGoldenrod',
  139.   'Peru',
  140.   'Chocolate',
  141.   'SaddleBrown',
  142.   'Sienna',
  143.   'Brown',
  144.   'Maroon',
  145.   'White',
  146.   'Snow',
  147.   'HoneyDew',
  148.   'MintCream',
  149.   'Azure',
  150.   'AliceBlue',
  151.   'GhostWhite',
  152.   'WhiteSmoke',
  153.   'SeaShell',
  154.   'Beige',
  155.   'OldLace',
  156.   'FloralWhite',
  157.   'Ivory',
  158.   'AntiqueWhite',
  159.   'Linen',
  160.   'LavenderBlush',
  161.   'MistyRose',
  162.   'Gainsboro',
  163.   'LightGray',
  164.   'Silver',
  165.   'DarkGray',
  166.   'Gray',
  167.   'DimGray',
  168.   'LightSlateGray',
  169.   'SlateGray',
  170.   'DarkSlateGray',
  171.   'Black',
  172. ];
  173.  
  174. function main() {
  175.   // create color choice divs
  176.   function generateColors(colorsArr) {
  177.     const conditionsElement = document.querySelector('.conditions');
  178.  
  179.     colorsArr.forEach((color) => {
  180.       let div = document.createElement('div');
  181.       div.style.width = '35px';
  182.       div.style.height = '35px';
  183.       div.style.backgroundColor = color;
  184.       div.style.display = 'inline-block';
  185.       div.classList.add('color');
  186.  
  187.       // append
  188.       conditionsElement.appendChild(div);
  189.     });
  190.   }
  191.  
  192.   function addColorListeners() {
  193.     let colorDivElements = Array.from(document.querySelectorAll('.color'));
  194.  
  195.     colorDivElements.forEach((color) => {
  196.       color.addEventListener('click', addToChoices);
  197.     });
  198.  
  199.     let order = 0;
  200.  
  201.     function addToChoices() {
  202.       let colorPicked = this.style.backgroundColor;
  203.  
  204.       // iterate over color buttons
  205.       const colorsElement = document.getElementById('colors');
  206.  
  207.       let divColorElements = Array.from(colorsElement.children);
  208.  
  209.       // remove fixed sizing from div buttons
  210.       divColorElements.forEach((ele) => {
  211.         ele.style.width = 'auto';
  212.         ele.style.minWidth = '30%';
  213.         ele.style.padding = '13px 4px';
  214.       });
  215.  
  216.       // set colors one by one, after each choice
  217.       if (order === 0) {
  218.         divColorElements[order].style.backgroundColor = colorPicked;
  219.         divColorElements[order].innerHTML = colorPicked;
  220.         order++;
  221.       } else if (order === 1) {
  222.         divColorElements[order].style.backgroundColor = colorPicked;
  223.         divColorElements[order].innerHTML = colorPicked;
  224.         order++;
  225.       } else if (order === 2) {
  226.         divColorElements[order].style.backgroundColor = colorPicked;
  227.         divColorElements[order].innerHTML = colorPicked;
  228.         order++;
  229.       }
  230.     }
  231.   }
  232.  
  233.   generateColors(colorData);
  234.   addColorListeners();
  235. }
  236.  
  237. function growingWord() {
  238.   const exerciseElement = document.getElementById('exercise');
  239.   const growingWordElement = exerciseElement.querySelector('p');
  240.  
  241.   const colorsArr = initializeColors();
  242.  
  243.   // Initiate
  244.   increaseFont(growingWordElement);
  245.   nextColor(growingWordElement, colorsArr);
  246.  
  247.   // Declarations
  248.   function increaseFont(element) {
  249.     if (!element.style.fontSize) {
  250.       element.style.fontSize = '1px';
  251.     }
  252.  
  253.     let currentSize = element.style.fontSize;
  254.     element.style.fontSize = parseInt(currentSize) * 2 + 'px';
  255.   }
  256.  
  257.   function nextColor(element, arr) {
  258.     if (!element.style.color) {
  259.       element.setAttribute('data-color', 0);
  260.     }
  261.  
  262.     let currentColor = Number(element.getAttribute('data-color'));
  263.  
  264.     if (currentColor > 2) {
  265.       currentColor = 0;
  266.     }
  267.  
  268.     element.style.color = arr[currentColor];
  269.     element.setAttribute('data-color', ++currentColor);
  270.   }
  271. }
  272.  
  273. function initializeColors(pickedColors) {
  274.   // get and contain colors
  275.   const colorsArr = [];
  276.  
  277.   const colorsElement = document.getElementById('colors');
  278.   Array.from(colorsElement.children).forEach((color) => {
  279.     // change the button color
  280.     color.style.backgroundColor = color.innerHTML;
  281.  
  282.     // add the color to the collection
  283.     colorsArr.push(color.innerHTML.toLowerCase());
  284.   });
  285.  
  286.   return colorsArr;
  287. }
  288.  
  289. // On Load
  290. window.addEventListener('load', main);
  291.  
Add Comment
Please, Sign In to add comment