当前位置:   article > 正文

HTML新年代码之新年快乐新年大吉_html 新年快乐

html 新年快乐

效果图预览:

cc4ad6eb6ee24088b9ff4bc6e1a09d71.pngf519ed659bc34bf78ff56c53e3ae5217.png8e5d976f141c432ca3a09c6ecb007b67.png

html界面代码如下:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  5. <style>
  6. body {
  7. margin: 0;
  8. padding: 0;
  9. overflow: hidden;
  10. }
  11. .city {
  12. width: 100%;
  13. position: fixed;
  14. bottom: 0px;
  15. z-index: 100;
  16. }
  17. .city img {
  18. width: 100%;
  19. }
  20. </style>
  21. <title>2024龙年快乐</title>
  22. </head>
  23. <body onselectstart="return false">
  24. <canvas id='cas' style="background-color:rgba(0,5,24,1)">浏览器不支持canvas</canvas>
  25. <div class="city"><img src="city.png" alt=""/></div>
  26. <img src="moon.png" alt="" id="moon" style="visibility: hidden;"/>
  27. <div style="display:none">
  28. <div class="shape">2024新年快乐</div>
  29. <div class="shape">龙年大吉</div>
  30. <div class="shape">ヾ(^∀^)ノ</div>
  31. </div>
  32. <audio src="boom.mp3" preload="auto"></audio>
  33. <audio src="boom.mp3" preload="auto"></audio>
  34. <audio src="boom.mp3" preload="auto"></audio>
  35. <audio src="boom.mp3" preload="auto"></audio>
  36. <audio src="boom.mp3" preload="auto"></audio>
  37. <audio src="boom.mp3" preload="auto"></audio>
  38. <audio src="shotfire.mp3" preload="auto"></audio>
  39. <audio src="shotfire.mp3" preload="auto"></audio>
  40. <audio src="shotfire.mp3" preload="auto"></audio>
  41. <script>
  42. var canvas = document.getElementById("cas");
  43. var ocas = document.createElement("canvas");
  44. var octx = ocas.getContext("2d");
  45. var ctx = canvas.getContext("2d");
  46. ocas.width = canvas.width = window.innerWidth;
  47. ocas.height = canvas.height = window.innerHeight;
  48. var bigbooms = [];
  49. window.onload = function() {
  50. initAnimate()
  51. }
  52. function initAnimate() {
  53. drawBg();
  54. lastTime = new Date();
  55. animate();
  56. }
  57. var lastTime;
  58. function animate() {
  59. ctx.save();
  60. ctx.globalCompositeOperation = 'destination-out';
  61. ctx.globalAlpha = 0.1;
  62. ctx.fillRect(0, 0, canvas.width, canvas.height);
  63. ctx.restore();
  64. var newTime = new Date();
  65. if (newTime - lastTime > 200 + (window.innerHeight - 767) / 2) {
  66. var random = Math.random() * 100 > 2 ? true : false;
  67. var x = getRandom(canvas.width / 5, canvas.width * 4 / 5);
  68. var y = getRandom(50, 200);
  69. if (random) {
  70. var bigboom = new Boom(getRandom(canvas.width / 3, canvas.width * 2 / 3), 2, "#FFF", {x: x, y: y});
  71. bigbooms.push(bigboom)
  72. }
  73. else {
  74. var bigboom = new Boom(getRandom(canvas.width / 3, canvas.width * 2 / 3), 2, "#FFF", {
  75. x: canvas.width / 2,
  76. y: 200
  77. }, document.querySelectorAll(".shape")[parseInt(getRandom(0, document.querySelectorAll(".shape").length))]);
  78. bigbooms.push(bigboom)
  79. }
  80. lastTime = newTime;
  81. }
  82. stars.foreach(function() {
  83. this.paint();
  84. })
  85. drawMoon();
  86. bigbooms.foreach(function(index) {
  87. var that = this;
  88. if (!this.dead) {
  89. this._move();
  90. this._drawLight();
  91. }
  92. else {
  93. this.booms.foreach(function(index) {
  94. if (!this.dead) {
  95. this.moveTo(index);
  96. }
  97. else if (index === that.booms.length - 1) {
  98. bigbooms.splice(bigbooms.indexOf(that), 1);
  99. }
  100. })
  101. }
  102. });
  103. raf(animate);
  104. }
  105. function drawMoon() {
  106. var moon = document.getElementById("moon");
  107. var centerX = canvas.width - 200, centerY = 100, width = 80;
  108. if (moon.complete) {
  109. ctx.drawImage(moon, centerX, centerY, width, width)
  110. }
  111. else {
  112. moon.onload = function() {
  113. ctx.drawImage(moon, centerX, centerY, width, width)
  114. }
  115. }
  116. var index = 0;
  117. for (var i = 0; i < 10; i++) {
  118. ctx.save();
  119. ctx.beginPath();
  120. ctx.arc(centerX + width / 2, centerY + width / 2, width / 2 + index, 0, 2 * Math.PI);
  121. ctx.fillStyle = "rgba(240,219,120,0.005)";
  122. index += 2;
  123. ctx.fill();
  124. ctx.restore();
  125. }
  126. }
  127. Array.prototype.foreach = function(callback) {
  128. for (var i = 0; i < this.length; i++) {
  129. if (this[i] !== null) callback.apply(this[i], [i])
  130. }
  131. }
  132. var raf = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback) {
  133. window.setTimeout(callback, 1000 / 10);
  134. };
  135. canvas.onclick = function() {
  136. var x = event.clientX;
  137. var y = event.clientY;
  138. var bigboom = new Boom(getRandom(canvas.width / 3, canvas.width * 2 / 3), 2, "#FFF", {x: x, y: y});
  139. bigbooms.push(bigboom)
  140. }
  141. var Boom = function(x, r, c, boomArea, shape) {
  142. this.booms = [];
  143. this.x = x;
  144. this.y = (canvas.height + r);
  145. this.r = r;
  146. this.c = c;
  147. this.shape = shape || false;
  148. this.boomArea = boomArea;
  149. this.theta = 0;
  150. this.dead = false;
  151. this.ba = parseInt(getRandom(80, 200));
  152. var audio = document.getElementsByTagName("audio");
  153. for (var i = 0; i < audio.length; i++) {
  154. if (audio[i].src.indexOf("shotfire") >= 0 && (audio[i].paused || audio[i].ended)) {
  155. audio[i].play();
  156. break;
  157. }
  158. }
  159. }
  160. Boom.prototype = {
  161. _paint: function() {
  162. ctx.save();
  163. ctx.beginPath();
  164. ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI);
  165. ctx.fillStyle = this.c;
  166. ctx.fill();
  167. ctx.restore();
  168. },
  169. _move: function() {
  170. var dx = this.boomArea.x - this.x, dy = this.boomArea.y - this.y;
  171. this.x = this.x + dx * 0.01;
  172. this.y = this.y + dy * 0.01;
  173. if (Math.abs(dx) <= this.ba && Math.abs(dy) <= this.ba) {
  174. if (this.shape) {
  175. this._shapBoom();
  176. }
  177. else this._boom();
  178. this.dead = true;
  179. }
  180. else {
  181. this._paint();
  182. }
  183. },
  184. _drawLight: function() {
  185. ctx.save();
  186. ctx.fillStyle = "rgba(255,228,150,0.3)";
  187. ctx.beginPath();
  188. ctx.arc(this.x, this.y, this.r + 3 * Math.random() + 1, 0, 2 * Math.PI);
  189. ctx.fill();
  190. ctx.restore();
  191. },
  192. _boom: function() {
  193. var fragNum = getRandom(100, 300);
  194. var style = getRandom(0, 10) >= 5 ? 1 : 2;
  195. var color;
  196. if (style === 1) {
  197. color = {
  198. a: parseInt(getRandom(128, 255)),
  199. b: parseInt(getRandom(128, 255)),
  200. c: parseInt(getRandom(128, 255))
  201. }
  202. }
  203. var fanwei = fragNum;
  204. var audio = document.getElementsByTagName("audio");
  205. for (var i = 0; i < audio.length; i++) {
  206. if (audio[i].src.indexOf("boom") >= 0 && (audio[i].paused || audio[i].ended)) {
  207. audio[i].play();
  208. break;
  209. }
  210. }
  211. for (var i = 0; i < fragNum; i++) {
  212. if (style === 2) {
  213. color = {
  214. a: parseInt(getRandom(128, 255)),
  215. b: parseInt(getRandom(128, 255)),
  216. c: parseInt(getRandom(128, 255))
  217. }
  218. }
  219. var a = getRandom(-Math.PI, Math.PI);
  220. var x = getRandom(0, fanwei) * Math.cos(a) + this.x;
  221. var y = getRandom(0, fanwei) * Math.sin(a) + this.y;
  222. var radius = getRandom(0, 2)
  223. var frag = new Frag(this.x, this.y, radius, color, x, y);
  224. this.booms.push(frag);
  225. }
  226. },
  227. _shapBoom: function() {
  228. var that = this;
  229. putValue(ocas, octx, this.shape, 5, function(dots) {
  230. var dx = canvas.width / 2 - that.x;
  231. var dy = canvas.height / 2 - that.y;
  232. for (var i = 0; i < dots.length; i++) {
  233. color = {a: dots[i].a, b: dots[i].b, c: dots[i].c}
  234. var x = dots[i].x;
  235. var y = dots[i].y;
  236. var radius = 1;
  237. var frag = new Frag(that.x, that.y, radius, color, x - dx, y - dy);
  238. that.booms.push(frag);
  239. }
  240. })
  241. }
  242. }
  243. function putValue(canvas, context, ele, dr, callback) {
  244. context.clearRect(0, 0, canvas.width, canvas.height);
  245. var img = new Image();
  246. if (ele.innerHTML.indexOf("img") >= 0) {
  247. img.src = ele.getElementsByTagName("img")[0].src;
  248. imgload(img, function() {
  249. context.drawImage(img, canvas.width / 2 - img.width / 2, canvas.height / 2 - img.width / 2);
  250. dots = getimgData(canvas, context, dr);
  251. callback(dots);
  252. })
  253. }
  254. else {
  255. var text = ele.innerHTML;
  256. context.save();
  257. var fontSize = 200;
  258. context.font = fontSize + "px 宋体 bold";
  259. context.textAlign = "center";
  260. context.textBaseline = "middle";
  261. context.fillStyle = "rgba(" + parseInt(getRandom(128, 255)) + "," + parseInt(getRandom(128, 255)) + "," + parseInt(getRandom(128, 255)) + " , 1)";
  262. context.fillText(text, canvas.width / 2, canvas.height / 2);
  263. context.restore();
  264. dots = getimgData(canvas, context, dr);
  265. callback(dots);
  266. }
  267. }
  268. function imgload(img, callback) {
  269. if (img.complete) {
  270. callback.call(img);
  271. }
  272. else {
  273. img.onload = function() {
  274. callback.call(this);
  275. }
  276. }
  277. }
  278. function getimgData(canvas, context, dr) {
  279. var imgData = context.getImageData(0, 0, canvas.width, canvas.height);
  280. context.clearRect(0, 0, canvas.width, canvas.height);
  281. var dots = [];
  282. for (var x = 0; x < imgData.width; x += dr) {
  283. for (var y = 0; y < imgData.height; y += dr) {
  284. var i = (y * imgData.width + x) * 4;
  285. if (imgData.data[i + 3] > 128) {
  286. var dot = {x: x, y: y, a: imgData.data[i], b: imgData.data[i + 1], c: imgData.data[i + 2]};
  287. dots.push(dot);
  288. }
  289. }
  290. }
  291. return dots;
  292. }
  293. function getRandom(a, b) {
  294. return Math.random() * (b - a) + a;
  295. }
  296. var maxRadius = 1, stars = [];
  297. function drawBg() {
  298. for (var i = 0; i < 100; i++) {
  299. var r = Math.random() * maxRadius;
  300. var x = Math.random() * canvas.width;
  301. var y = Math.random() * 2 * canvas.height - canvas.height;
  302. var star = new Star(x, y, r);
  303. stars.push(star);
  304. star.paint()
  305. }
  306. }
  307. var Star = function(x, y, r) {
  308. this.x = x;
  309. this.y = y;
  310. this.r = r;
  311. }
  312. Star.prototype = {
  313. paint: function() {
  314. ctx.save();
  315. ctx.beginPath();
  316. ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI);
  317. ctx.fillStyle = "rgba(255,255,255," + this.r + ")";
  318. ctx.fill();
  319. ctx.restore();
  320. }
  321. }
  322. var focallength = 250;
  323. var Frag = function(centerX, centerY, radius, color, tx, ty) {
  324. this.tx = tx;
  325. this.ty = ty;
  326. this.x = centerX;
  327. this.y = centerY;
  328. this.dead = false;
  329. this.centerX = centerX;
  330. this.centerY = centerY;
  331. this.radius = radius;
  332. this.color = color;
  333. }
  334. Frag.prototype = {
  335. paint: function() {
  336. // ctx.beginPath();
  337. // ctx.arc(this.x , this.y , this.radius , 0 , 2*Math.PI);
  338. ctx.fillStyle = "rgba(" + this.color.a + "," + this.color.b + "," + this.color.c + ",1)";
  339. ctx.fillRect(this.x - this.radius, this.y - this.radius, this.radius * 2, this.radius * 2)
  340. },
  341. moveTo: function(index) {
  342. this.ty = this.ty + 0.3;
  343. var dx = this.tx - this.x, dy = this.ty - this.y;
  344. this.x = Math.abs(dx) < 0.1 ? this.tx : (this.x + dx * 0.1);
  345. this.y = Math.abs(dy) < 0.1 ? this.ty : (this.y + dy * 0.1);
  346. if (dx === 0 && Math.abs(dy) <= 80) {
  347. this.dead = true;
  348. }
  349. this.paint();
  350. }
  351. }
  352. </script>
  353. </body>
  354. </html>

不能只有html

但是,如果只运行此处代码,界面会这样:

7eed3fa849c54e4ea577d0c7a0f0502d.png

此时需要其他资源素材影像等:

免费获取资源

b10b625316c0406a96b8c884e362da03.png

获取方式如下:免费获取新年代码

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/85174
推荐阅读
相关标签
  

闽ICP备14008679号