You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
6.6 KiB

1 month ago
  1. import * as TWEEN from "./tween.min.js";
  2. const MAX_TOP = 300;
  3. const MAX_WIDTH = document.body.clientWidth;
  4. let defaultType = 0;
  5. let prizes;
  6. let lastDanMuList = [];
  7. let prizeElement = {};
  8. let lasetPrizeIndex = 0;
  9. const DEFAULT_MESS = [
  10. "我是该抽中一等奖还是一等奖呢,纠结ing...",
  11. "听说要提前一个月吃素才能中大奖喔!",
  12. "好想要一等奖啊!!!",
  13. "一等奖有没有人想要呢?",
  14. "五等奖也不错,只要自己能中奖就行",
  15. "祝大家新年快乐!",
  16. "中不中奖不重要,大家吃好喝好。",
  17. "新年,祝福大家事事顺遂。",
  18. "作为专业陪跑的我,我就看看你们有谁跟我一样",
  19. "新的一年祝福大家越来越好!",
  20. "来年再战!!!",
  21. ];
  22. class DanMu {
  23. constructor(option) {
  24. if (typeof option !== "object") {
  25. option = {
  26. text: option,
  27. };
  28. }
  29. this.position = {};
  30. this.text = option.text;
  31. this.onComplete = option.onComplete;
  32. this.init();
  33. }
  34. init() {
  35. this.element = document.createElement("div");
  36. this.element.className = "dan-mu";
  37. document.body.appendChild(this.element);
  38. this.start();
  39. }
  40. setText(text) {
  41. this.text = text || this.text;
  42. this.element.textContent = this.text;
  43. this.width = this.element.clientWidth + 100;
  44. }
  45. start(text) {
  46. let speed = ~~(Math.random() * 10000) + 6000;
  47. this.position = {
  48. x: MAX_WIDTH,
  49. };
  50. let delay = speed / 10;
  51. this.setText(text);
  52. this.element.style.transform = "translateX(" + this.position.x + "px)";
  53. this.element.style.top = ~~(Math.random() * MAX_TOP) + 10 + "px";
  54. this.element.classList.add("active");
  55. this.tween = new TWEEN.Tween(this.position)
  56. .to(
  57. {
  58. x: -this.width,
  59. },
  60. speed
  61. )
  62. .onUpdate(() => {
  63. this.render();
  64. })
  65. .onComplete(() => {
  66. this.onComplete && this.onComplete();
  67. })
  68. .start();
  69. }
  70. render() {
  71. this.element.style.transform = "translateX(" + this.position.x + "px)";
  72. }
  73. }
  74. class Qipao {
  75. constructor(option) {
  76. if (typeof option !== "object") {
  77. option = {
  78. text: option,
  79. };
  80. }
  81. this.text = option.text;
  82. this.onComplete = option.onComplete;
  83. this.$par = document.querySelector(".qipao-container");
  84. if (!this.$par) {
  85. this.$par = document.createElement("div");
  86. this.$par.className = "qipao-container";
  87. document.body.appendChild(this.$par);
  88. }
  89. this.init();
  90. }
  91. init() {
  92. this.element = document.createElement("div");
  93. this.element.className = "qipao animated";
  94. this.$par.appendChild(this.element);
  95. this.start();
  96. }
  97. setText(text) {
  98. this.text = text || this.text;
  99. this.element.textContent = this.text;
  100. }
  101. start(text) {
  102. this.setText(text);
  103. this.element.classList.remove("bounceOutRight");
  104. this.element.classList.add("bounceInRight");
  105. setTimeout(() => {
  106. this.element.classList.remove("bounceInRight");
  107. this.element.classList.add("bounceOutRight");
  108. this.onComplete && this.onComplete();
  109. }, 4000);
  110. }
  111. }
  112. function setPrizes(pri) {
  113. prizes = pri;
  114. defaultType = prizes[0]["type"];
  115. lasetPrizeIndex = pri.length - 1;
  116. }
  117. function showPrizeList(currentPrizeIndex) {
  118. let currentPrize = prizes[currentPrizeIndex];
  119. if (currentPrize.type === defaultType) {
  120. currentPrize.count = "不限制";
  121. }
  122. }
  123. function resetPrize(currentPrizeIndex) {
  124. prizeElement = {};
  125. lasetPrizeIndex = currentPrizeIndex;
  126. showPrizeList(currentPrizeIndex);
  127. }
  128. let setPrizeData = (function () {
  129. return function (currentPrizeIndex, count, isInit) {
  130. let currentPrize = prizes[currentPrizeIndex],
  131. type = currentPrize.type,
  132. elements = prizeElement[type],
  133. totalCount = currentPrize.count;
  134. if (!elements) {
  135. elements = {
  136. box: document.querySelector(`#prize-item-${type}`),
  137. bar: document.querySelector(`#prize-bar-${type}`),
  138. text: document.querySelector(`#prize-count-${type}`),
  139. };
  140. prizeElement[type] = elements;
  141. }
  142. if (!prizeElement.prizeType) {
  143. prizeElement.prizeType = document.querySelector("#prizeType");
  144. prizeElement.prizeLeft = document.querySelector("#prizeLeft");
  145. prizeElement.prizeText = document.querySelector("#prizeText");
  146. }
  147. if (isInit) {
  148. for (let i = prizes.length - 1; i > currentPrizeIndex; i--) {
  149. let type = prizes[i]["type"];
  150. document.querySelector(`#prize-item-${type}`).className =
  151. "prize-item done";
  152. document.querySelector(`#prize-bar-${type}`).style.width = "0";
  153. document.querySelector(`#prize-count-${type}`).textContent =
  154. "0" + "/" + prizes[i]["count"];
  155. }
  156. }
  157. if (lasetPrizeIndex !== currentPrizeIndex) {
  158. let lastPrize = prizes[lasetPrizeIndex],
  159. lastBox = document.querySelector(`#prize-item-${lastPrize.type}`);
  160. lastBox.classList.remove("shine");
  161. lastBox.classList.add("done");
  162. elements.box && elements.box.classList.add("shine");
  163. prizeElement.prizeType.textContent = currentPrize.text;
  164. prizeElement.prizeText.textContent = currentPrize.title;
  165. lasetPrizeIndex = currentPrizeIndex;
  166. }
  167. if (currentPrizeIndex === 0) {
  168. prizeElement.prizeType.textContent = "特别奖";
  169. prizeElement.prizeText.textContent = " ";
  170. prizeElement.prizeLeft.textContent = "不限制";
  171. return;
  172. }
  173. count = totalCount - count;
  174. count = count < 0 ? 0 : count;
  175. let percent = (count / totalCount).toFixed(2);
  176. elements.bar && (elements.bar.style.width = percent * 100 + "%");
  177. elements.text && (elements.text.textContent = count + "/" + totalCount);
  178. prizeElement.prizeLeft.textContent = count;
  179. };
  180. })();
  181. function startMaoPao() {
  182. let len = DEFAULT_MESS.length,
  183. count = 5,
  184. index = ~~(Math.random() * len),
  185. danmuList = [],
  186. total = 0;
  187. function restart() {
  188. total = 0;
  189. danmuList.forEach((item) => {
  190. let text =
  191. lastDanMuList.length > 0
  192. ? lastDanMuList.shift()
  193. : DEFAULT_MESS[index++];
  194. item.start(text);
  195. index = index > len ? 0 : index;
  196. });
  197. }
  198. for (let i = 0; i < count; i++) {
  199. setTimeout(() => {
  200. danmuList.push(
  201. new DanMu({
  202. text: DEFAULT_MESS[index++],
  203. onComplete: function () {
  204. setTimeout(() => {
  205. this.start(DEFAULT_MESS[index++]);
  206. index = index > len ? 0 : index;
  207. }, 1000);
  208. },
  209. })
  210. );
  211. index = index > len ? 0 : index;
  212. }, 1500 * i);
  213. }
  214. }
  215. function addDanMu(text) {
  216. lastDanMuList.push(text);
  217. }
  218. export {
  219. startMaoPao,
  220. showPrizeList,
  221. setPrizeData,
  222. addDanMu,
  223. setPrizes,
  224. resetPrize,
  225. };