Анимированный фон с падающими звёздами на HTML5 Canvas
1 2 3 4 5 6 7 8 9 10 |
canvas#stars{ background: -webkit-linear-gradient(#99daea 0%, #ffffff 100%); background: -moz-linear-gradient(#99daea 0%, #ffffff 100%); background: -o-linear-gradient(#99daea 0%, #ffffff 100%); background: linear-gradient(#99daea 0%, #ffffff 100%); position:fixed; top:0; left:0; z-index:-1; } |
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 |
<script> // Polyfills window.requestAnimationFrame = (function() { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function(callback) { window.setTimeout(callback, 1000 / 60); }; })(); window.particles = { body: document.body || document.querySelector("body"), canvas: document.createElement("canvas"), i: 0, max: 1200, particles: [], colors: ["#FFF"], particle: function(x, y, r, o) { this.x = x; this.y = y; this.r = r; this.a = Math.random() * 360; this.o = o; this._o = o; this.on = false; this.cached = document.createElement("canvas"); this.ctx = this.cached.getContext("2d"); this.create(); }, init: function() { var self = this; self.canvas.setAttribute('id', 'stars'); self.canvas.width = window.innerWidth; self.canvas.height = window.innerHeight; self.ctx = self.canvas.getContext("2d"); self.particle.prototype = { create: function() { this.cached.width = this.cached.height = this.r * 4; var x = this.r, y = this.r, r = this.r, p = 5, m = 0.5; this.ctx.save(); this.ctx.beginPath(); this.ctx.translate(x, y); this.ctx.moveTo(0, 0 - r); for (var i = 0; i < p; i++) { this.ctx.rotate(Math.PI / p); this.ctx.lineTo(0, 0 - (r * m)); this.ctx.rotate(Math.PI / p); this.ctx.lineTo(0, 0 - r); }; this.ctx.closePath(); this.ctx.fillStyle = this.fill(); this.ctx.fill(); this.ctx.restore(); }, fill: function() { var i = Math.floor(Math.random() * self.colors.length); return self.colors[i]; }, draw: function() { self.ctx.globalAlpha = this.o; self.ctx.save(); self.ctx.translate(this.x, this.y); self.ctx.rotate((this.a += 0.01)); self.ctx.drawImage(this.cached, 0, 0, this.r * 2, this.r * 2); self.ctx.restore(); }, update: function() { this.y = this.y + (this.r / 15); if (this.y < window.innerHeight) { this.on = true; } else { this.on = false; this.o = this._o; }; }, fade: function() { if (this.y < 1) { this.o = this._o; }; if (this.on) { this.o *= 0.9966; }; } }; self.body.appendChild(self.canvas); self.cache(); self.fall(); self.update(); self.render(); }, cache: function() { var self = this; for (var i = 0; i < self.max; i++) { var radius = Math.floor(Math.random() * 30) + 10; var opacity = (Math.floor(Math.random() * 50) + 50) / 100; self.particles.push(new self.particle(0, window.innerHeight, radius, opacity)); }; }, trigger: function() { var self = this; var particles = self.particles; var particle = particles[self.i]; particle.x = Math.floor(Math.random() * window.innerWidth) - particle.r; particle.y = -particle.r * 2; self.i = self.i === particles.length - 1 ? 0 : ++self.i; }, render: function() { var self = particles; requestAnimationFrame(self.render); self.ctx.clearRect(0, 0, window.innerWidth, window.innerHeight); for (var i = 0, c = self.particles.length; i < c; i++) { if (!self.particles[i].on) continue; self.particles[i].draw(); //self.particles[i].fade(); }; }, update: function() { var self = particles; for (var i = 0, c = self.particles.length; i < c; i++) { self.particles[i].update(); }; setTimeout(self.update, 1000 / 30); }, fall: function() { var self = particles; self.trigger(); setTimeout(self.fall, 5000 / 20); } }; particles.init(); </script> |