script.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. document.addEventListener('keydown', handleKeyPress);
  2. let x = 1;
  3. let y = 1;
  4. let medalNumber = 1;
  5. let medalId = `medal-${medalNumber}`;
  6. let selectedMedal = document.getElementById(medalId);
  7. let page = 1;
  8. let pages = 20;
  9. function handleKeyPress(event) {
  10. console.log(medalId);
  11. movement = false;
  12. switch (event.key) {
  13. case 'ArrowUp':
  14. movement = true;
  15. if (!((y - 1) < 1)) {
  16. y -= 1;
  17. }
  18. break;
  19. case 'ArrowDown':
  20. movement = true;
  21. if (!((y + 1) > 4)) {
  22. y += 1;
  23. }
  24. break;
  25. case 'ArrowLeft':
  26. movement = true;
  27. if (!((x - 1) < 1)) {
  28. x -= 1;
  29. } else {
  30. if (((page) > 1)) {
  31. page--;
  32. paginate(page, "left");
  33. x = 6;
  34. // I assume now given page number I must put the associated yo-kai
  35. }
  36. }
  37. break;
  38. case 'ArrowRight':
  39. movement = true;
  40. if (!((x + 1) > 6)) {
  41. x += 1;
  42. } else {
  43. console.log(page);
  44. console.log(pages);
  45. if (((page + 1) < pages)) {
  46. page++;
  47. paginate(page, "right");
  48. x = 1;
  49. // I assume now given page number I must put the associated yo-kai
  50. }
  51. }
  52. break;
  53. }
  54. function paginate(page, direction) {
  55. if (direction == "left") {
  56. move = -12;
  57. } else if (direction == "right") {
  58. move = 12;
  59. }
  60. // Apperently this is a new feature so it isn't supported in older browsers
  61. // https://developer.mozilla.org/en-US/docs/Web/API/Element/animate
  62. const binderMove = [
  63. { transform: `translateX(${move}px)` },
  64. { transform: "translateX(0px)" },
  65. ];
  66. const binderTiming = {
  67. duration: 75,
  68. iterations: 1,
  69. };
  70. const binder = document.querySelector("#binder");
  71. binder.animate(binderMove, binderTiming);
  72. document.getElementById("page-number-l").innerHTML = zfill(page);
  73. document.getElementById("page-number-r").innerHTML = zfill(page+1);
  74. }
  75. function zfill(number) {
  76. return number.toString().padStart(2, '0');
  77. }
  78. console.log(`(${x}, ${y})`);
  79. if (movement) {
  80. if (x > 3) {
  81. medalNumber = 12 + (y - 1) * 3 + (x - 3);
  82. } else if (x <= 3) {
  83. medalNumber = (y - 1) * 3 + x;
  84. }
  85. console.log(medalId);
  86. var outlinedElements = document.getElementsByClassName("outlined");
  87. while (outlinedElements.length > 0) {
  88. outlinedElements[0].style.cssText = ""; // Clear styles
  89. outlinedElements[0].classList.remove("outlined");
  90. }
  91. medalId = `medal-${medalNumber}`;
  92. selectedMedal = document.getElementById(medalId);
  93. console.log(selectedMedal.id);
  94. var outlineElement = selectedMedal.getElementsByClassName("outline")[0];
  95. if (outlineElement) {
  96. outlineElement.classList.add("outlined");
  97. selectedMedal = document.getElementsByClassName("outlined")[0];
  98. selectedMedal.style.cssText = `
  99. position: absolute;
  100. z-index: 7;
  101. top: -5%;
  102. left: -5%;
  103. width: 110%;
  104. height: 110%;
  105. border: 3px dashed yellow;
  106. box-sizing: border-box;
  107. border-radius: 50%;
  108. transform: scale(1.8);
  109. animation: spin 3s infinite linear;
  110. `;
  111. }
  112. }
  113. }
  114. function tableGen(width, height) {
  115. var verticals = "";
  116. var number = 0;
  117. for (let i = 0; i < height; i++) {
  118. var horizontals = "";
  119. for (let o = 0; o < width; o++) {
  120. number += 1;
  121. //var name = `${i+1}-${o+1}`
  122. var name = `medal-${number}`
  123. horizontals += `\n\t<td id="medal-${number}">
  124. <div class="medal-container">
  125. <div id="triangle"></div>
  126. <div id="text-${number}"></div>
  127. <div class="outline" data-selected="false"></div>
  128. <div id="circle"></div>
  129. </div>
  130. </td>`;
  131. var trformat = `\n<tr>${horizontals}\n</tr>\n`
  132. }
  133. verticals += trformat;
  134. }
  135. table = `<table>${verticals}</table>`
  136. //console.log(table);
  137. return table;
  138. }
  139. console.log(tableGen(3, 4));