Новогодний фейерверк на HTML5 и jQuery
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
<canvas id="canvas"></canvas> <script> $(function() { var canvas = $('#canvas')[0]; canvas.width = $(window).width(); canvas.height = $(window).height(); var ctx = canvas.getContext('2d'); // init ctx.fillStyle = '#000'; ctx.fillRect(0, 0, canvas.width, canvas.height); // objects var listFire = []; var listFirework = []; var fireNumber = 10; var center = { x: canvas.width / 2, y: canvas.height / 2 }; var range = 100; for (var i = 0; i < fireNumber; i++) { var fire = { x: Math.random() * range / 2 - range / 4 + center.x, y: Math.random() * range * 2 + canvas.height, size: Math.random() + 0.5, fill: '#fd1', vx: Math.random() - 0.5, vy: -(Math.random() + 4), ax: Math.random() * 0.02 - 0.01, far: Math.random() * range + (center.y - range) }; fire.base = { x: fire.x, y: fire.y, vx: fire.vx }; // listFire.push(fire); } function randColor() { var r = Math.floor(Math.random() * 256); var g = Math.floor(Math.random() * 256); var b = Math.floor(Math.random() * 256); var color = 'rgb($r, $g, $b)'; color = color.replace('$r', r); color = color.replace('$g', g); color = color.replace('$b', b); return color; } (function loop() { requestAnimationFrame(loop); update(); draw(); })(); function update() { for (var i = 0; i < listFire.length; i++) { var fire = listFire[i]; // if (fire.y <= fire.far) { // case add firework var color = randColor(); for (var i = 0; i < fireNumber * 5; i++) { var firework = { x: fire.x, y: fire.y, size: Math.random() + 1.5, fill: color, vx: Math.random() * 5 - 2.5, vy: Math.random() * -5 + 1.5, ay: 0.05, alpha: 1, life: Math.round(Math.random() * range / 2) + range / 2 }; firework.base = { life: firework.life, size: firework.size }; listFirework.push(firework); } // reset fire.y = fire.base.y; fire.x = fire.base.x; fire.vx = fire.base.vx; fire.ax = Math.random() * 0.02 - 0.01; } // fire.x += fire.vx; fire.y += fire.vy; fire.vx += fire.ax; } for (var i = listFirework.length - 1; i >= 0; i--) { var firework = listFirework[i]; if (firework) { firework.x += firework.vx; firework.y += firework.vy; firework.vy += firework.ay; firework.alpha = firework.life / firework.base.life; firework.size = firework.alpha * firework.base.size; firework.alpha = firework.alpha > 0.6 ? 1 : firework.alpha; // firework.life--; if (firework.life <= 0) { listFirework.splice(i, 1); } } } } function draw() { // clear ctx.globalCompositeOperation = 'source-over'; ctx.globalAlpha = 0.18; ctx.fillStyle = '#000'; ctx.fillRect(0, 0, canvas.width, canvas.height); // re-draw ctx.globalCompositeOperation = 'screen'; ctx.globalAlpha = 1; for (var i = 0; i < listFire.length; i++) { var fire = listFire[i]; ctx.beginPath(); ctx.arc(fire.x, fire.y, fire.size, 0, Math.PI * 2); ctx.closePath(); ctx.fillStyle = fire.fill; ctx.fill(); } for (var i = 0; i < listFirework.length; i++) { var firework = listFirework[i]; ctx.globalAlpha = firework.alpha; ctx.beginPath(); ctx.arc(firework.x, firework.y, firework.size, 0, Math.PI * 2); ctx.closePath(); ctx.fillStyle = firework.fill; ctx.fill(); } } }); </script> |