HTML之2021新年快乐牛年大吉<除夕快乐噢>
效果截图:

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

源码及文件在这里:
链接:https://pan.baidu.com/s/1C2Y59Pyys6Hlrz4v_Pm0FQ 提取码:8qbm
链接:https://pan.baidu.com/s/1tWauspaEOlqW75zcl5_krg
提取码:9q9t


浙公网安备 33010602011771号