Christmas.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. class Christmas {
  2. static init(root, hatSelectors, attributesForRng, exclusionSelectors,disableSnow) {
  3. this.root = root;
  4. this.hatSelectors = hatSelectors;
  5. this.attributesForRng = attributesForRng;
  6. this.exclusionSelectors = exclusionSelectors;
  7. this.disableSnow=disableSnow;
  8. document.addEventListener("DOMContentLoaded", () => {
  9. if (this.isChristmas() || window.location.hash == "#test-christmas25550") {
  10. this.loadNow();
  11. }
  12. });
  13. }
  14. static hashCode(v) {
  15. let hash = 0;
  16. for (var i = 0; i < v.length; i++) {
  17. var character = v.charCodeAt(i);
  18. hash = ((hash << 5) - hash) + character;
  19. hash = hash & hash; // Convert to 32bit integer
  20. }
  21. return hash;
  22. }
  23. static randomSeedParkMiller(seed) { // doesn't repeat b4 JS dies.
  24. // https://gist.github.com/blixt/f17b47c62508be59987b
  25. seed = seed % 2147483647
  26. seed = seed * 16807 % 2147483647
  27. return (seed - 1) / 2147483646
  28. }
  29. static loadNow() {
  30. if(!this.disableSnow)this.loadSnow();
  31. const hatLoadInt = () => {
  32. this.loadHats(this.hatSelectors, this.attributesForRng, this.exclusionSelectors);
  33. };
  34. setInterval(function () {
  35. hatLoadInt();
  36. }, 2000);
  37. hatLoadInt();
  38. }
  39. static isChristmas() {
  40. const date = new Date();
  41. if ((date.getMonth() + 1) != 12) return false;
  42. if (date.getDate() < 6) return false;
  43. return true;
  44. }
  45. static loadSnow() {
  46. console.log("Load snow...");
  47. const snowScript = document.createElement("script");
  48. snowScript.src = this.root + "snow.js"
  49. document.head.appendChild(snowScript);
  50. snowScript.onload = function () {
  51. console.log("Snow Loaded. Starting...");
  52. snow.start({
  53. "stickingRatio": 0.7,
  54. "wind": 3,
  55. "flakeCount": 32,
  56. "maxRadius": 1.9,
  57. "maxSpeed": 2,
  58. "minSpeed": 0.4
  59. });
  60. }
  61. }
  62. static getHats() {
  63. return [
  64. this.root + 'hats/hat1.png'
  65. ];
  66. }
  67. static loadHats(selectors, attributesForRng, exclusionSelectors) {
  68. const hats = this.getHats();
  69. selectors.forEach(selector => {
  70. document.querySelectorAll(selector).forEach(avatar => {
  71. if (avatar.hasAttribute("christmas25550-hasHat")) return;
  72. if (exclusionSelectors) {
  73. for (let i = 0; i < exclusionSelectors.length; i++) {
  74. const exclusionSelector = exclusionSelectors[i];
  75. if (avatar.matches(exclusionSelector)) {
  76. return;
  77. }
  78. }
  79. }
  80. let hueRotation = 0;
  81. if (attributesForRng && attributesForRng.length > 0) {
  82. let seed = "";
  83. for (let i = 0; i < attributesForRng.length; i++) {
  84. const r = attributesForRng[i];
  85. if (r.type == "attribute") {
  86. if (avatar.hasAttribute(r.value)) {
  87. seed = avatar.getAttribute(r.value);
  88. console.log("Use attribute", r.value, "as seed");
  89. if (r.transform) {
  90. seed = r.transform(seed);
  91. console.log("Transformed to ", seed);
  92. }
  93. break;
  94. }
  95. } else if (r.type == "text") {
  96. seed = avatar.innerText;
  97. console.log("Use innerText as seed");
  98. if (r.transform) {
  99. seed = r.transform(seed);
  100. console.log("Transformed to ", seed);
  101. }
  102. break;
  103. }
  104. }
  105. seed = seed.trim();
  106. const seedHashCode = this.hashCode(seed);
  107. if (seed != "") {
  108. hueRotation = this.randomSeedParkMiller(seedHashCode);
  109. }
  110. }
  111. const parent = avatar.parentNode;
  112. const avatarBound = avatar.getBoundingClientRect();
  113. if (avatarBound.width == 0 || avatarBound.height == 0) return;
  114. const parentBound = parent.getBoundingClientRect();
  115. const parentPosMode = window.getComputedStyle(parent).position;
  116. if (parentPosMode != "absolute" && parentPosMode != "fixed" && parentPosMode != "relative") parent.style.position = "relative";
  117. console.log("Add hat to", avatar);
  118. const hat = document.createElement("div");
  119. hat.classList.add("christmas25550-hat");
  120. const hatImg = hats[Math.floor(Math.random() * hats.length)];
  121. hat.style.pointerEvents = "none";
  122. hat.style.backgroundImage = "url('" + hatImg + "')";
  123. hat.style.backgroundPosition = "left bottom";
  124. hat.style.filter = "hue-rotate(" + hueRotation + "turn)";
  125. hat.style.backgroundRepeat = "no-repeat";
  126. hat.style.backgroundSize = "contain";
  127. hat.style.display = "block";
  128. hat.style.position = "absolute";
  129. const avatarWidth = Math.min(avatarBound.width, 124);
  130. const avatarHeight = Math.min(avatarBound.height, 124);
  131. hat.style.width = avatarWidth + "px";
  132. hat.style.height = avatarHeight + "px";
  133. hat.style.zIndex = "140";
  134. hat.style.left = (parentBound.left - avatarBound.left - Math.min(avatarHeight, avatarWidth) * 0.3) + "px";
  135. // hat.style.left = (avatarBound.left - parentBound.left - avatarBound.width * 0.3) + "px";
  136. hat.style.top = (avatarBound.top - parentBound.top - avatarHeight * 0.76) + "px";
  137. parent.appendChild(hat);
  138. avatar.setAttribute("christmas25550-hasHat", "true");
  139. });
  140. });
  141. }
  142. }