当前位置:   article > 正文

2023跨年代码(烟花+自定义文字+背景音乐+雪花+倒计时)_2024年跨年代码

2024年跨年代码

 2023年快要到来啦,很高兴这次我们又能一起度过~

b40cd513233542378ffc663941a495ad.jpeg

 

 


目录

一、前言

二、跨年烟花

三、效果展示

倒计时

 2023​编辑

 兔圆圆​编辑

四、编码实现

index.html

 烟花,雪花,背景音乐,页面样式

index.js 

 自定义文字

五、获取代码

需要源码,可以私信我(⊙o⊙)?


 

一、前言

时光荏苒,白驹过隙。

2022这一年又在忙碌中度过了,过去的一年,我们同努力,我们共欢笑,每一次成功都蕴藏着我们辛勤的劳动。

新的一年即将来到,我们不能停滞不前,一味只是骄傲。愿大家与时俱进,拼搏不懈,共创新的辉煌!

借着新年到来的喜庆,给大家分享一个新年烟花的前端代码,快拿着代码展示给你的朋友们看吧!!

转眼间已经到了2022的尾巴,这一年对于国家来说丰富充实,冬奥会在北京举行、中国共产党第二十次全国代表大会召开、社会共同抗击疫情的第三年、我国完成天宫空间站建造……对于我们每一个个体,2022也承载着我们的快乐与悲伤,它是无法替代的。

新的一年马上就要到了,让我们告别2022,迎接2023!

希望2023,全糖去冰。

二、跨年烟花

本文是一篇简单的前端代码,主要内容有HTML,JS,CSS等内容

1、HTML代码

2、JS代码

3、CSS样式

4、插件等

5、可根据需求自定义文字

6、背景音乐

7、倒计时

三、效果展示

文末有完整代码,只需要下载压缩包,解压运行即可。接下来我们一起来看看效果吧!!!

倒计时

7f19ec0c9b4748e3b24ce11ac529420d.png


 

 202360136e19e0ec42f6822620baab4bab8b.png


 

 兔圆圆a2caa860766e409bbe59312123e98a07.png

 包含背景音乐,自定义文字,倒计时等


 

四、编码实现

 

index.html

 

 烟花,雪花,背景音乐,页面样式

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8" />
  5. <title>时光荏苒,白驹过隙!</title>
  6. <style>
  7. html,
  8. body {
  9. margin: 0px;
  10. width: 100%;
  11. height: 100%;
  12. overflow: hidden;
  13. background: #000;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <canvas
  19. id="canvas"
  20. style="position: absolute; width: 100%; height: 100%; z-index: 8888"
  21. ></canvas>
  22. <canvas
  23. style="position: absolute; width: 100%; height: 100%; z-index: 9999"
  24. class="canvas"
  25. ></canvas>
  26. <div class="overlay">
  27. <div class="tabs">
  28. <div class="tabs-labels">
  29. <span class="tabs-label">Commands</span
  30. ><span class="tabs-label">Info</span
  31. ><span class="tabs-label">Share</span>
  32. </div>
  33. <div class="tabs-panels">
  34. <ul class="tabs-panel commands"></ul>
  35. </div>
  36. </div>
  37. </div>
  38. <script>
  39. function initVars() {
  40. pi = Math.PI;
  41. ctx = canvas.getContext("2d");
  42. canvas.width = canvas.clientWidth;
  43. canvas.height = canvas.clientHeight;
  44. cx = canvas.width / 2;
  45. cy = canvas.height / 2;
  46. playerZ = -25;
  47. playerX =
  48. playerY =
  49. playerVX =
  50. playerVY =
  51. playerVZ =
  52. pitch =
  53. yaw =
  54. pitchV =
  55. yawV =
  56. 0;
  57. scale = 600;
  58. seedTimer = 0;
  59. (seedInterval = 5), (seedLife = 100);
  60. gravity = 0.02;
  61. seeds = new Array();
  62. sparkPics = new Array();
  63. s = "https://cantelope.org/NYE/";
  64. for (i = 1; i <= 10; ++i) {
  65. sparkPic = new Image();
  66. sparkPic.src = s + "spark" + i + ".png";
  67. sparkPics.push(sparkPic);
  68. }
  69. sparks = new Array();
  70. pow1 = new Audio(s + "pow1.ogg");
  71. pow2 = new Audio(s + "pow2.ogg");
  72. pow3 = new Audio(s + "pow3.ogg");
  73. pow4 = new Audio(s + "pow4.ogg");
  74. frames = 0;
  75. }
  76. function rasterizePoint(x, y, z) {
  77. var p, d;
  78. x -= playerX;
  79. y -= playerY;
  80. z -= playerZ;
  81. p = Math.atan2(x, z);
  82. d = Math.sqrt(x * x + z * z);
  83. x = Math.sin(p - yaw) * d;
  84. z = Math.cos(p - yaw) * d;
  85. p = Math.atan2(y, z);
  86. d = Math.sqrt(y * y + z * z);
  87. y = Math.sin(p - pitch) * d;
  88. z = Math.cos(p - pitch) * d;
  89. var rx1 = -1000,
  90. ry1 = 1,
  91. rx2 = 1000,
  92. ry2 = 1,
  93. rx3 = 0,
  94. ry3 = 0,
  95. rx4 = x,
  96. ry4 = z,
  97. uc = (ry4 - ry3) * (rx2 - rx1) - (rx4 - rx3) * (ry2 - ry1);
  98. if (!uc) return { x: 0, y: 0, d: -1 };
  99. var ua = ((rx4 - rx3) * (ry1 - ry3) - (ry4 - ry3) * (rx1 - rx3)) / uc;
  100. var ub = ((rx2 - rx1) * (ry1 - ry3) - (ry2 - ry1) * (rx1 - rx3)) / uc;
  101. if (!z) z = 0.000000001;
  102. if (ua > 0 && ua < 1 && ub > 0 && ub < 1) {
  103. return {
  104. x: cx + (rx1 + ua * (rx2 - rx1)) * scale,
  105. y: cy + (y / z) * scale,
  106. d: Math.sqrt(x * x + y * y + z * z),
  107. };
  108. } else {
  109. return {
  110. x: cx + (rx1 + ua * (rx2 - rx1)) * scale,
  111. y: cy + (y / z) * scale,
  112. d: -1,
  113. };
  114. }
  115. }
  116. function spawnSeed() {
  117. seed = new Object();
  118. seed.x = -50 + Math.random() * 100;
  119. seed.y = 25;
  120. seed.z = -50 + Math.random() * 100;
  121. seed.vx = 0.1 - Math.random() * 0.2;
  122. seed.vy = -1.5; //*(1+Math.random()/2);
  123. seed.vz = 0.1 - Math.random() * 0.2;
  124. seed.born = frames;
  125. seeds.push(seed);
  126. }
  127. function splode(x, y, z) {
  128. t = 5 + parseInt(Math.random() * 150);
  129. sparkV = 1 + Math.random() * 2.5;
  130. type = parseInt(Math.random() * 3);
  131. switch (type) {
  132. case 0:
  133. pic1 = parseInt(Math.random() * 10);
  134. break;
  135. case 1:
  136. pic1 = parseInt(Math.random() * 10);
  137. do {
  138. pic2 = parseInt(Math.random() * 10);
  139. } while (pic2 == pic1);
  140. break;
  141. case 2:
  142. pic1 = parseInt(Math.random() * 10);
  143. do {
  144. pic2 = parseInt(Math.random() * 10);
  145. } while (pic2 == pic1);
  146. do {
  147. pic3 = parseInt(Math.random() * 10);
  148. } while (pic3 == pic1 || pic3 == pic2);
  149. break;
  150. }
  151. for (m = 1; m < t; ++m) {
  152. spark = new Object();
  153. spark.x = x;
  154. spark.y = y;
  155. spark.z = z;
  156. p1 = pi * 2 * Math.random();
  157. p2 = pi * Math.random();
  158. v = sparkV * (1 + Math.random() / 6);
  159. spark.vx = Math.sin(p1) * Math.sin(p2) * v;
  160. spark.vz = Math.cos(p1) * Math.sin(p2) * v;
  161. spark.vy = Math.cos(p2) * v;
  162. switch (type) {
  163. case 0:
  164. spark.img = sparkPics[pic1];
  165. break;
  166. case 1:
  167. spark.img = sparkPics[parseInt(Math.random() * 2) ? pic1 : pic2];
  168. break;
  169. case 2:
  170. switch (parseInt(Math.random() * 3)) {
  171. case 0:
  172. spark.img = sparkPics[pic1];
  173. break;
  174. case 1:
  175. spark.img = sparkPics[pic2];
  176. break;
  177. case 2:
  178. spark.img = sparkPics[pic3];
  179. break;
  180. }
  181. break;
  182. }
  183. spark.radius = 25 + Math.random() * 50;
  184. spark.alpha = 1;
  185. spark.trail = new Array();
  186. sparks.push(spark);
  187. }
  188. switch (parseInt(Math.random() * 4)) {
  189. case 0:
  190. pow = new Audio(s + "pow1.ogg");
  191. break;
  192. case 1:
  193. pow = new Audio(s + "pow2.ogg");
  194. break;
  195. case 2:
  196. pow = new Audio(s + "pow3.ogg");
  197. break;
  198. case 3:
  199. pow = new Audio(s + "pow4.ogg");
  200. break;
  201. }
  202. d = Math.sqrt(
  203. (x - playerX) * (x - playerX) +
  204. (y - playerY) * (y - playerY) +
  205. (z - playerZ) * (z - playerZ)
  206. );
  207. pow.volume = 1.5 / (1 + d / 10);
  208. pow.play();
  209. }
  210. function doLogic() {
  211. if (seedTimer < frames) {
  212. seedTimer = frames + seedInterval * Math.random() * 10;
  213. spawnSeed();
  214. }
  215. for (i = 0; i < seeds.length; ++i) {
  216. seeds[i].vy += gravity;
  217. seeds[i].x += seeds[i].vx;
  218. seeds[i].y += seeds[i].vy;
  219. seeds[i].z += seeds[i].vz;
  220. if (frames - seeds[i].born > seedLife) {
  221. splode(seeds[i].x, seeds[i].y, seeds[i].z);
  222. seeds.splice(i, 1);
  223. }
  224. }
  225. for (i = 0; i < sparks.length; ++i) {
  226. if (sparks[i].alpha > 0 && sparks[i].radius > 5) {
  227. sparks[i].alpha -= 0.01;
  228. sparks[i].radius /= 1.02;
  229. sparks[i].vy += gravity;
  230. point = new Object();
  231. point.x = sparks[i].x;
  232. point.y = sparks[i].y;
  233. point.z = sparks[i].z;
  234. if (sparks[i].trail.length) {
  235. x = sparks[i].trail[sparks[i].trail.length - 1].x;
  236. y = sparks[i].trail[sparks[i].trail.length - 1].y;
  237. z = sparks[i].trail[sparks[i].trail.length - 1].z;
  238. d =
  239. (point.x - x) * (point.x - x) +
  240. (point.y - y) * (point.y - y) +
  241. (point.z - z) * (point.z - z);
  242. if (d > 9) {
  243. sparks[i].trail.push(point);
  244. }
  245. } else {
  246. sparks[i].trail.push(point);
  247. }
  248. if (sparks[i].trail.length > 5) sparks[i].trail.splice(0, 1);
  249. sparks[i].x += sparks[i].vx;
  250. sparks[i].y += sparks[i].vy;
  251. sparks[i].z += sparks[i].vz;
  252. sparks[i].vx /= 1.075;
  253. sparks[i].vy /= 1.075;
  254. sparks[i].vz /= 1.075;
  255. } else {
  256. sparks.splice(i, 1);
  257. }
  258. }
  259. p = Math.atan2(playerX, playerZ);
  260. d = Math.sqrt(playerX * playerX + playerZ * playerZ);
  261. d += Math.sin(frames / 80) / 1.25;
  262. t = Math.sin(frames / 200) / 40;
  263. playerX = Math.sin(p + t) * d;
  264. playerZ = Math.cos(p + t) * d;
  265. yaw = pi + p + t;
  266. }
  267. function rgb(col) {
  268. var r = parseInt((0.5 + Math.sin(col) * 0.5) * 16);
  269. var g = parseInt((0.5 + Math.cos(col) * 0.5) * 16);
  270. var b = parseInt((0.5 - Math.sin(col) * 0.5) * 16);
  271. return "#" + r.toString(16) + g.toString(16) + b.toString(16);
  272. }
  273. function draw() {
  274. ctx.clearRect(0, 0, cx * 2, cy * 2);
  275. ctx.fillStyle = "#ff8";
  276. for (i = -100; i < 100; i += 3) {
  277. for (j = -100; j < 100; j += 4) {
  278. x = i;
  279. z = j;
  280. y = 25;
  281. point = rasterizePoint(x, y, z);
  282. if (point.d != -1) {
  283. size = 250 / (1 + point.d);
  284. d = Math.sqrt(x * x + z * z);
  285. a = 0.75 - Math.pow(d / 100, 6) * 0.75;
  286. if (a > 0) {
  287. ctx.globalAlpha = a;
  288. ctx.fillRect(
  289. point.x - size / 2,
  290. point.y - size / 2,
  291. size,
  292. size
  293. );
  294. }
  295. }
  296. }
  297. }
  298. ctx.globalAlpha = 1;
  299. for (i = 0; i < seeds.length; ++i) {
  300. point = rasterizePoint(seeds[i].x, seeds[i].y, seeds[i].z);
  301. if (point.d != -1) {
  302. size = 200 / (1 + point.d);
  303. ctx.fillRect(point.x - size / 2, point.y - size / 2, size, size);
  304. }
  305. }
  306. point1 = new Object();
  307. for (i = 0; i < sparks.length; ++i) {
  308. point = rasterizePoint(sparks[i].x, sparks[i].y, sparks[i].z);
  309. if (point.d != -1) {
  310. size = (sparks[i].radius * 200) / (1 + point.d);
  311. if (sparks[i].alpha < 0) sparks[i].alpha = 0;
  312. if (sparks[i].trail.length) {
  313. point1.x = point.x;
  314. point1.y = point.y;
  315. switch (sparks[i].img) {
  316. case sparkPics[0]:
  317. ctx.strokeStyle = "#f84";
  318. break;
  319. case sparkPics[1]:
  320. ctx.strokeStyle = "#84f";
  321. break;
  322. case sparkPics[2]:
  323. ctx.strokeStyle = "#8ff";
  324. break;
  325. case sparkPics[3]:
  326. ctx.strokeStyle = "#fff";
  327. break;
  328. case sparkPics[4]:
  329. ctx.strokeStyle = "#4f8";
  330. break;
  331. case sparkPics[5]:
  332. ctx.strokeStyle = "#f44";
  333. break;
  334. case sparkPics[6]:
  335. ctx.strokeStyle = "#f84";
  336. break;
  337. case sparkPics[7]:
  338. ctx.strokeStyle = "#84f";
  339. break;
  340. case sparkPics[8]:
  341. ctx.strokeStyle = "#fff";
  342. break;
  343. case sparkPics[9]:
  344. ctx.strokeStyle = "#44f";
  345. break;
  346. }
  347. for (j = sparks[i].trail.length - 1; j >= 0; --j) {
  348. point2 = rasterizePoint(
  349. sparks[i].trail[j].x,
  350. sparks[i].trail[j].y,
  351. sparks[i].trail[j].z
  352. );
  353. if (point2.d != -1) {
  354. ctx.globalAlpha =
  355. ((j / sparks[i].trail.length) * sparks[i].alpha) / 2;
  356. ctx.beginPath();
  357. ctx.moveTo(point1.x, point1.y);
  358. ctx.lineWidth =
  359. 1 +
  360. (sparks[i].radius * 10) /
  361. (sparks[i].trail.length - j) /
  362. (1 + point2.d);
  363. ctx.lineTo(point2.x, point2.y);
  364. ctx.stroke();
  365. point1.x = point2.x;
  366. point1.y = point2.y;
  367. }
  368. }
  369. }
  370. ctx.globalAlpha = sparks[i].alpha;
  371. ctx.drawImage(
  372. sparks[i].img,
  373. point.x - size / 2,
  374. point.y - size / 2,
  375. size,
  376. size
  377. );
  378. }
  379. }
  380. }
  381. function frame() {
  382. if (frames > 100000) {
  383. seedTimer = 0;
  384. frames = 0;
  385. }
  386. frames++;
  387. draw();
  388. doLogic();
  389. requestAnimationFrame(frame);
  390. }
  391. window.addEventListener("resize", () => {
  392. canvas.width = canvas.clientWidth;
  393. canvas.height = canvas.clientHeight;
  394. cx = canvas.width / 2;
  395. cy = canvas.height / 2;
  396. });
  397. initVars();
  398. frame();
  399. </script>
  400. <script src="js/index.js"></script>
  401. <div id="page_end_html">
  402. <!--放大图片-->
  403. <link
  404. rel="stylesheet"
  405. type="text/css"
  406. href="https://blog-static.cnblogs.com/files/zouwangblog/zoom.css"
  407. />
  408. <script src="https://cdn.bootcss.com/jquery/1.8.3/jquery.min.js"></script>
  409. <script src="https://cdn.bootcss.com/bootstrap/3.2.0/js/transition.js"></script>
  410. <script src="https://blog-static.cnblogs.com/files/zouwangblog/zoom.js"></script>
  411. <script type="text/javascript">
  412. $("#cnblogs_post_body img").attr("data-action", "zoom");
  413. </script>
  414. <!--放大图片end-->
  415. <!--鼠标特效-->
  416. <script src="https://blog-static.cnblogs.com/files/zouwangblog/mouse-click.js"></script>
  417. <canvas
  418. width="1777"
  419. height="841"
  420. style="
  421. position: fixed;
  422. left: 0px;
  423. top: 0px;
  424. z-index: 2147483647;
  425. pointer-events: none;
  426. "
  427. ></canvas>
  428. <!--鼠标特效 end-->
  429. <!-- require APlayer -->
  430. <link
  431. rel="stylesheet"
  432. href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css"
  433. />
  434. <script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script>
  435. <!-- require MetingJS -->
  436. <script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script>
  437. <!-- // 随机线条 -->
  438. <script>
  439. !(function () {
  440. function n(n, e, t) {
  441. return n.getAttribute(e) || t;
  442. }
  443. function e(n) {
  444. return document.getElementsByTagName(n);
  445. }
  446. function t() {
  447. var t = e("script"),
  448. o = t.length,
  449. i = t[o - 1];
  450. return {
  451. l: o,
  452. z: n(i, "zIndex", -1),
  453. o: n(i, "opacity", 0.6),
  454. c: n(i, "color", "148,0,211"),
  455. n: n(i, "count", 99),
  456. };
  457. }
  458. function o() {
  459. (a = m.width =
  460. window.innerWidth ||
  461. document.documentElement.clientWidth ||
  462. document.body.clientWidth),
  463. (c = m.height =
  464. window.innerHeight ||
  465. document.documentElement.clientHeight ||
  466. document.body.clientHeight);
  467. }
  468. function i() {
  469. r.clearRect(0, 0, a, c);
  470. var n, e, t, o, m, l;
  471. s.forEach(function (i, x) {
  472. for (
  473. i.x += i.xa,
  474. i.y += i.ya,
  475. i.xa *= i.x > a || i.x < 0 ? -1 : 1,
  476. i.ya *= i.y > c || i.y < 0 ? -1 : 1,
  477. r.fillRect(i.x - 0.5, i.y - 0.5, 1, 1),
  478. e = x + 1;
  479. e < u.length;
  480. e++
  481. )
  482. (n = u[e]),
  483. null !== n.x &&
  484. null !== n.y &&
  485. ((o = i.x - n.x),
  486. (m = i.y - n.y),
  487. (l = o * o + m * m),
  488. l < n.max &&
  489. (n === y &&
  490. l >= n.max / 2 &&
  491. ((i.x -= 0.03 * o), (i.y -= 0.03 * m)),
  492. (t = (n.max - l) / n.max),
  493. r.beginPath(),
  494. (r.lineWidth = t / 2),
  495. (r.strokeStyle = "rgba(" + d.c + "," + (t + 0.2) + ")"),
  496. r.moveTo(i.x, i.y),
  497. r.lineTo(n.x, n.y),
  498. r.stroke()));
  499. }),
  500. x(i);
  501. }
  502. var a,
  503. c,
  504. u,
  505. m = document.createElement("canvas"),
  506. d = t(),
  507. l = "c_n" + d.l,
  508. r = m.getContext("2d"),
  509. x =
  510. window.requestAnimationFrame ||
  511. window.webkitRequestAnimationFrame ||
  512. window.mozRequestAnimationFrame ||
  513. window.oRequestAnimationFrame ||
  514. window.msRequestAnimationFrame ||
  515. function (n) {
  516. window.setTimeout(n, 1e3 / 45);
  517. },
  518. w = Math.random,
  519. y = { x: null, y: null, max: 2e4 };
  520. (m.id = l),
  521. (m.style.cssText =
  522. "position:fixed;top:0;left:0;z-index:" + d.z + ";opacity:" + d.o),
  523. e("body")[0].appendChild(m),
  524. o(),
  525. (window.onresize = o),
  526. (window.onmousemove = function (n) {
  527. (n = n || window.event), (y.x = n.clientX), (y.y = n.clientY);
  528. }),
  529. (window.onmouseout = function () {
  530. (y.x = null), (y.y = null);
  531. });
  532. for (var s = [], f = 0; d.n > f; f++) {
  533. var h = w() * a,
  534. g = w() * c,
  535. v = 2 * w() - 1,
  536. p = 2 * w() - 1;
  537. s.push({ x: h, y: g, xa: v, ya: p, max: 6e3 });
  538. }
  539. (u = s.concat([y])),
  540. setTimeout(function () {
  541. i();
  542. }, 100);
  543. })();
  544. </script>
  545. <!-- 雪花特效 -->
  546. <script type="text/javascript">
  547. (function ($) {
  548. $.fn.snow = function (options) {
  549. var $flake = $('<div id="snowbox" />')
  550. .css({ position: "absolute", "z-index": "9999", top: "-50px" })
  551. .html("❄"),
  552. documentHeight = $(document).height(),
  553. documentWidth = $(document).width(),
  554. defaults = {
  555. minSize: 10,
  556. maxSize: 20,
  557. newOn: 1000,
  558. flakeColor:
  559. "#00CED1" /* 此处可以定义雪花颜色,若要白色可以改为#FFFFFF */,
  560. },
  561. options = $.extend({}, defaults, options);
  562. var interval = setInterval(function () {
  563. var startPositionLeft = Math.random() * documentWidth - 100,
  564. startOpacity = 0.5 + Math.random(),
  565. sizeFlake = options.minSize + Math.random() * options.maxSize,
  566. endPositionTop = documentHeight - 200,
  567. endPositionLeft = startPositionLeft - 500 + Math.random() * 500,
  568. durationFall = documentHeight * 10 + Math.random() * 5000;
  569. $flake
  570. .clone()
  571. .appendTo("body")
  572. .css({
  573. left: startPositionLeft,
  574. opacity: startOpacity,
  575. "font-size": sizeFlake,
  576. color: options.flakeColor,
  577. })
  578. .animate(
  579. {
  580. top: endPositionTop,
  581. left: endPositionLeft,
  582. opacity: 0.2,
  583. },
  584. durationFall,
  585. "linear",
  586. function () {
  587. $(this).remove();
  588. }
  589. );
  590. }, options.newOn);
  591. };
  592. })(jQuery);
  593. $(function () {
  594. $.fn.snow({
  595. minSize: 5 /* 定义雪花最小尺寸 */,
  596. maxSize: 80 /* 定义雪花最大尺寸 */,
  597. newOn: 200 /* 定义密集程度,数字越小越密集 */,
  598. });
  599. });
  600. </script>
  601. </body>
  602. </html>

 


index.js 

 

 自定义文字

  1. var S = {
  2. init: function () {
  3. var action = window.location.href,
  4. i = action.indexOf('?a=');
  5. S.Drawing.init('.canvas');
  6. document.body.classList.add('body--ready');
  7. if (i !== -1) {
  8. S.UI.simulate(decodeURI(action).substring(i + 3));
  9. } else {
  10. S.UI.simulate('|#countdown 3||2023|兔圆圆||#rectangle|');
  11. // S.UI.simulate('|写|尽|千|山|落|笔|是|你|#rectangle|');
  12. }
  13. S.Drawing.loop(function () {
  14. S.Shape.render();
  15. });
  16. }
  17. };
  18. S.Drawing = (function () {
  19. var canvas,
  20. context,
  21. renderFn
  22. requestFrame = window.requestAnimationFrame ||
  23. window.webkitRequestAnimationFrame ||
  24. window.mozRequestAnimationFrame ||
  25. window.oRequestAnimationFrame ||
  26. window.msRequestAnimationFrame ||
  27. function(callback) {
  28. window.setTimeout(callback, 1000 / 60);
  29. };
  30. return {
  31. init: function (el) {
  32. canvas = document.querySelector(el);
  33. context = canvas.getContext('2d');
  34. this.adjustCanvas();
  35. window.addEventListener('resize', function (e) {
  36. S.Drawing.adjustCanvas();
  37. });
  38. },
  39. loop: function (fn) {
  40. renderFn = !renderFn ? fn : renderFn;
  41. this.clearFrame();
  42. renderFn();
  43. requestFrame.call(window, this.loop.bind(this));
  44. },
  45. adjustCanvas: function () {
  46. canvas.width = window.innerWidth;
  47. canvas.height = window.innerHeight;
  48. },
  49. clearFrame: function () {
  50. context.clearRect(0, 0, canvas.width, canvas.height);
  51. },
  52. getArea: function () {
  53. return { w: canvas.width, h: canvas.height };
  54. },
  55. drawCircle: function (p, c) {
  56. context.fillStyle = c.render();
  57. context.beginPath();
  58. context.arc(p.x, p.y, p.z, 0, 2 * Math.PI, true);
  59. context.closePath();
  60. context.fill();
  61. }
  62. }
  63. }());
  64. S.UI = (function () {
  65. var canvas = document.querySelector('.canvas'),
  66. interval,
  67. isTouch = false, //('ontouchstart' in window || navigator.msMaxTouchPoints),
  68. currentAction,
  69. resizeTimer,
  70. time,
  71. maxShapeSize = 30,
  72. firstAction = true,
  73. sequence = [],
  74. cmd = '#';
  75. function formatTime(date) {
  76. var h = date.getHours(),
  77. m = date.getMinutes(),
  78. m = m < 10 ? '0' + m : m;
  79. return h + ':' + m;
  80. }
  81. function getValue(value) {
  82. return value && value.split(' ')[1];
  83. }
  84. function getAction(value) {
  85. value = value && value.split(' ')[0];
  86. return value && value[0] === cmd && value.substring(1);
  87. }
  88. function timedAction(fn, delay, max, reverse) {
  89. clearInterval(interval);
  90. currentAction = reverse ? max : 1;
  91. fn(currentAction);
  92. if (!max || (!reverse && currentAction < max) || (reverse && currentAction > 0)) {
  93. interval = setInterval(function () {
  94. currentAction = reverse ? currentAction - 1 : currentAction + 1;
  95. fn(currentAction);
  96. if ((!reverse && max && currentAction === max) || (reverse && currentAction === 0)) {
  97. clearInterval(interval);
  98. }
  99. }, delay);
  100. }
  101. }
  102. function reset(destroy) {
  103. clearInterval(interval);
  104. sequence = [];
  105. time = null;
  106. destroy && S.Shape.switchShape(S.ShapeBuilder.letter(''));
  107. }
  108. function performAction(value) {
  109. var action,
  110. value,
  111. current;
  112. // overlay.classList.remove('overlay--visible');
  113. sequence = typeof(value) === 'object' ? value : sequence.concat(value.split('|'));
  114. // input.value = '';
  115. // checkInputWidth();
  116. timedAction(function (index) {
  117. current = sequence.shift();
  118. action = getAction(current);
  119. value = getValue(current);
  120. switch (action) {
  121. case 'countdown':
  122. value = parseInt(value) || 10;
  123. value = value > 0 ? value : 10;
  124. timedAction(function (index) {
  125. if (index === 0) {
  126. if (sequence.length === 0) {
  127. S.Shape.switchShape(S.ShapeBuilder.letter(''));
  128. } else {
  129. performAction(sequence);
  130. }
  131. } else {
  132. S.Shape.switchShape(S.ShapeBuilder.letter(index), true);
  133. }
  134. }, 1000, value, true);
  135. break;
  136. case 'rectangle':
  137. value = value && value.split('x');
  138. value = (value && value.length === 2) ? value : [maxShapeSize, maxShapeSize / 2];
  139. S.Shape.switchShape(S.ShapeBuilder.rectangle(Math.min(maxShapeSize, parseInt(value[0])), Math.min(maxShapeSize, parseInt(value[1]))));
  140. break;
  141. case 'circle':
  142. value = parseInt(value) || maxShapeSize;
  143. value = Math.min(value, maxShapeSize);
  144. S.Shape.switchShape(S.ShapeBuilder.circle(value));
  145. break;
  146. case 'time':
  147. var t = formatTime(new Date());
  148. if (sequence.length > 0) {
  149. S.Shape.switchShape(S.ShapeBuilder.letter(t));
  150. } else {
  151. timedAction(function () {
  152. t = formatTime(new Date());
  153. if (t !== time) {
  154. time = t;
  155. S.Shape.switchShape(S.ShapeBuilder.letter(time));
  156. }
  157. }, 1000);
  158. }
  159. break;
  160. default:
  161. S.Shape.switchShape(S.ShapeBuilder.letter(current[0] === cmd ? 'What?' : current));
  162. }
  163. }, 2000, sequence.length);
  164. }
  165. function checkInputWidth(e) {
  166. if (input.value.length > 18) {
  167. ui.classList.add('ui--wide');
  168. } else {
  169. ui.classList.remove('ui--wide');
  170. }
  171. if (firstAction && input.value.length > 0) {
  172. ui.classList.add('ui--enter');
  173. } else {
  174. ui.classList.remove('ui--enter');
  175. }
  176. }
  177. function bindEvents() {
  178. document.body.addEventListener('keydown', function (e) {
  179. input.focus();
  180. if (e.keyCode === 13) {
  181. firstAction = false;
  182. reset();
  183. performAction(input.value);
  184. }
  185. });
  186. canvas.addEventListener('click', function (e) {
  187. overlay.classList.remove('overlay--visible');
  188. });
  189. }
  190. function init() {
  191. bindEvents();
  192. // input.focus();
  193. isTouch && document.body.classList.add('touch');
  194. }
  195. // Init
  196. init();
  197. return {
  198. simulate: function (action) {
  199. performAction(action);
  200. }
  201. }
  202. }());
  203. S.UI.Tabs = (function () {
  204. var tabs = document.querySelector('.tabs'),
  205. labels = document.querySelector('.tabs-labels'),
  206. triggers = document.querySelectorAll('.tabs-label'),
  207. panels = document.querySelectorAll('.tabs-panel');
  208. function activate(i) {
  209. triggers[i].classList.add('tabs-label--active');
  210. panels[i].classList.add('tabs-panel--active');
  211. }
  212. function bindEvents() {
  213. labels.addEventListener('click', function (e) {
  214. var el = e.target,
  215. index;
  216. if (el.classList.contains('tabs-label')) {
  217. for (var t = 0; t < triggers.length; t++) {
  218. triggers[t].classList.remove('tabs-label--active');
  219. panels[t].classList.remove('tabs-panel--active');
  220. if (el === triggers[t]) {
  221. index = t;
  222. }
  223. }
  224. activate(index);
  225. }
  226. });
  227. }
  228. function init() {
  229. activate(0);
  230. bindEvents();
  231. }
  232. // Init
  233. init();
  234. }());
  235. S.Point = function (args) {
  236. this.x = args.x;
  237. this.y = args.y;
  238. this.z = args.z;
  239. this.a = args.a;
  240. this.h = args.h;
  241. };
  242. S.Color = function (r, g, b, a) {
  243. this.r = r;
  244. this.g = g;
  245. this.b = b;
  246. this.a = a;
  247. };
  248. S.Color.prototype = {
  249. render: function () {
  250. return 'rgba(' + this.r + ',' + + this.g + ',' + this.b + ',' + this.a + ')';
  251. }
  252. };
  253. S.Dot = function (x, y) {
  254. this.p = new S.Point({
  255. x: x,
  256. y: y,
  257. z: 5,
  258. a: 1,
  259. h: 0
  260. });
  261. this.e = 0.07;
  262. this.s = true;
  263. this.c = new S.Color(255, 255, 255, this.p.a);
  264. this.t = this.clone();
  265. this.q = [];
  266. };
  267. S.Dot.prototype = {
  268. clone: function () {
  269. return new S.Point({
  270. x: this.x,
  271. y: this.y,
  272. z: this.z,
  273. a: this.a,
  274. h: this.h
  275. });
  276. },
  277. _draw: function () {
  278. this.c.a = this.p.a;
  279. S.Drawing.drawCircle(this.p, this.c);
  280. },
  281. _moveTowards: function (n) {
  282. var details = this.distanceTo(n, true),
  283. dx = details[0],
  284. dy = details[1],
  285. d = details[2],
  286. e = this.e * d;
  287. if (this.p.h === -1) {
  288. this.p.x = n.x;
  289. this.p.y = n.y;
  290. return true;
  291. }
  292. if (d > 1) {
  293. this.p.x -= ((dx / d) * e);
  294. this.p.y -= ((dy / d) * e);
  295. } else {
  296. if (this.p.h > 0) {
  297. this.p.h--;
  298. } else {
  299. return true;
  300. }
  301. }
  302. return false;
  303. },
  304. _update: function () {
  305. if (this._moveTowards(this.t)) {
  306. var p = this.q.shift();
  307. if (p) {
  308. this.t.x = p.x || this.p.x;
  309. this.t.y = p.y || this.p.y;
  310. this.t.z = p.z || this.p.z;
  311. this.t.a = p.a || this.p.a;
  312. this.p.h = p.h || 0;
  313. } else {
  314. if (this.s) {
  315. this.p.x -= Math.sin(Math.random() * 3.142);
  316. this.p.y -= Math.sin(Math.random() * 3.142);
  317. } else {
  318. this.move(new S.Point({
  319. x: this.p.x + (Math.random() * 50) - 25,
  320. y: this.p.y + (Math.random() * 50) - 25,
  321. }));
  322. }
  323. }
  324. }
  325. d = this.p.a - this.t.a;
  326. this.p.a = Math.max(0.1, this.p.a - (d * 0.05));
  327. d = this.p.z - this.t.z;
  328. this.p.z = Math.max(1, this.p.z - (d * 0.05));
  329. },
  330. distanceTo: function (n, details) {
  331. var dx = this.p.x - n.x,
  332. dy = this.p.y - n.y,
  333. d = Math.sqrt(dx * dx + dy * dy);
  334. return details ? [dx, dy, d] : d;
  335. },
  336. move: function (p, avoidStatic) {
  337. if (!avoidStatic || (avoidStatic && this.distanceTo(p) > 1)) {
  338. this.q.push(p);
  339. }
  340. },
  341. render: function () {
  342. this._update();
  343. this._draw();
  344. }
  345. }
  346. S.ShapeBuilder = (function () {
  347. var gap = 13,
  348. shapeCanvas = document.createElement('canvas'),
  349. shapeContext = shapeCanvas.getContext('2d'),
  350. fontSize = 500,
  351. fontFamily = 'Avenir, Helvetica Neue, Helvetica, Arial, sans-serif';
  352. function fit() {
  353. shapeCanvas.width = Math.floor(window.innerWidth / gap) * gap;
  354. shapeCanvas.height = Math.floor(window.innerHeight / gap) * gap;
  355. shapeContext.fillStyle = 'red';
  356. shapeContext.textBaseline = 'middle';
  357. shapeContext.textAlign = 'center';
  358. }
  359. function processCanvas() {
  360. var pixels = shapeContext.getImageData(0, 0, shapeCanvas.width, shapeCanvas.height).data;
  361. dots = [],
  362. pixels,
  363. x = 0,
  364. y = 0,
  365. fx = shapeCanvas.width,
  366. fy = shapeCanvas.height,
  367. w = 0,
  368. h = 0;
  369. for (var p = 0; p < pixels.length; p += (4 * gap)) {
  370. if (pixels[p + 3] > 0) {
  371. dots.push(new S.Point({
  372. x: x,
  373. y: y
  374. }));
  375. w = x > w ? x : w;
  376. h = y > h ? y : h;
  377. fx = x < fx ? x : fx;
  378. fy = y < fy ? y : fy;
  379. }
  380. x += gap;
  381. if (x >= shapeCanvas.width) {
  382. x = 0;
  383. y += gap;
  384. p += gap * 4 * shapeCanvas.width;
  385. }
  386. }
  387. return { dots: dots, w: w + fx, h: h + fy };
  388. }
  389. function setFontSize(s) {
  390. shapeContext.font = 'bold ' + s + 'px ' + fontFamily;
  391. }
  392. function isNumber(n) {
  393. return !isNaN(parseFloat(n)) && isFinite(n);
  394. }
  395. function init() {
  396. fit();
  397. window.addEventListener('resize', fit);
  398. }
  399. // Init
  400. init();
  401. return {
  402. imageFile: function (url, callback) {
  403. var image = new Image(),
  404. a = S.Drawing.getArea();
  405. image.onload = function () {
  406. shapeContext.clearRect(0, 0, shapeCanvas.width, shapeCanvas.height);
  407. shapeContext.drawImage(this, 0, 0, a.h * 0.6, a.h * 0.6);
  408. callback(processCanvas());
  409. };
  410. image.onerror = function () {
  411. callback(S.ShapeBuilder.letter('What?'));
  412. }
  413. image.src = url;
  414. },
  415. circle: function (d) {
  416. var r = Math.max(0, d) / 2;
  417. shapeContext.clearRect(0, 0, shapeCanvas.width, shapeCanvas.height);
  418. shapeContext.beginPath();
  419. shapeContext.arc(r * gap, r * gap, r * gap, 0, 2 * Math.PI, false);
  420. shapeContext.fill();
  421. shapeContext.closePath();
  422. return processCanvas();
  423. },
  424. letter: function (l) {
  425. var s = 0;
  426. setFontSize(fontSize);
  427. s = Math.min(fontSize,
  428. (shapeCanvas.width / shapeContext.measureText(l).width) * 0.8 * fontSize,
  429. (shapeCanvas.height / fontSize) * (isNumber(l) ? 1 : 0.45) * fontSize);
  430. setFontSize(s);
  431. shapeContext.clearRect(0, 0, shapeCanvas.width, shapeCanvas.height);
  432. shapeContext.fillText(l, shapeCanvas.width / 2, shapeCanvas.height / 2);
  433. return processCanvas();
  434. },
  435. rectangle: function (w, h) {
  436. var dots = [],
  437. width = gap * w,
  438. height = gap * h;
  439. for (var y = 0; y < height; y += gap) {
  440. for (var x = 0; x < width; x += gap) {
  441. dots.push(new S.Point({
  442. x: x,
  443. y: y,
  444. }));
  445. }
  446. }
  447. return { dots: dots, w: width, h: height };
  448. }
  449. };
  450. }());
  451. S.Shape = (function () {
  452. var dots = [],
  453. width = 0,
  454. height = 0,
  455. cx = 0,
  456. cy = 0;
  457. function compensate() {
  458. var a = S.Drawing.getArea();
  459. cx = a.w / 2 - width / 2;
  460. cy = a.h / 2 - height / 2;
  461. }
  462. return {
  463. shuffleIdle: function () {
  464. var a = S.Drawing.getArea();
  465. for (var d = 0; d < dots.length; d++) {
  466. if (!dots[d].s) {
  467. dots[d].move({
  468. x: Math.random() * a.w,
  469. y: Math.random() * a.h
  470. });
  471. }
  472. }
  473. },
  474. switchShape: function (n, fast) {
  475. var size,
  476. a = S.Drawing.getArea();
  477. width = n.w;
  478. height = n.h;
  479. compensate();
  480. if (n.dots.length > dots.length) {
  481. size = n.dots.length - dots.length;
  482. for (var d = 1; d <= size; d++) {
  483. dots.push(new S.Dot(a.w / 2, a.h / 2));
  484. }
  485. }
  486. var d = 0,
  487. i = 0;
  488. while (n.dots.length > 0) {
  489. i = Math.floor(Math.random() * n.dots.length);
  490. dots[d].e = fast ? 0.25 : (dots[d].s ? 0.14 : 0.11);
  491. if (dots[d].s) {
  492. dots[d].move(new S.Point({
  493. z: Math.random() * 20 + 10,
  494. a: Math.random(),
  495. h: 18
  496. }));
  497. } else {
  498. dots[d].move(new S.Point({
  499. z: Math.random() * 5 + 5,
  500. h: fast ? 18 : 30
  501. }));
  502. }
  503. dots[d].s = true;
  504. dots[d].move(new S.Point({
  505. x: n.dots[i].x + cx,
  506. y: n.dots[i].y + cy,
  507. a: 1,
  508. z: 5,
  509. h: 0
  510. }));
  511. n.dots = n.dots.slice(0, i).concat(n.dots.slice(i + 1));
  512. d++;
  513. }
  514. for (var i = d; i < dots.length; i++) {
  515. if (dots[i].s) {
  516. dots[i].move(new S.Point({
  517. z: Math.random() * 20 + 10,
  518. a: Math.random(),
  519. h: 20
  520. }));
  521. dots[i].s = false;
  522. dots[i].e = 0.04;
  523. dots[i].move(new S.Point({
  524. x: Math.random() * a.w,
  525. y: Math.random() * a.h,
  526. a: 0.3, //.4
  527. z: Math.random() * 4,
  528. h: 0
  529. }));
  530. }
  531. }
  532. },
  533. render: function () {
  534. for (var d = 0; d < dots.length; d++) {
  535. dots[d].render();
  536. }
  537. }
  538. }
  539. }());
  540. S.init();

 以上就是我们跨年烟花的全部代码了,能否看懂呢? 本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/156767

推荐阅读
相关标签