Огненно-радужный шторм на HTML5 Canvas
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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
<canvas id="c"></canvas> <script> /* * The design was completely made by @jackrugile in his other pen: * http://codepen.io/jackrugile/pen/AokpF * This is just a recreation of that pen * */ //==//==============// // canvas setup // //==//==============// var ctx = c.getContext('2d'), width = c.width = window.innerWidth, height = c.height = window.innerHeight, //==//==================// // a few parameters // //==//==================// particleCount = (width / 2) | 0, obstacleCount = ((width + height) / 20) | 0, obstacleRadius = 100, gravity = .1, //==//========================// // other needed variables // //==//========================// particles = [], obstacles = [], frame = 0, tau = Math.PI * 2, //==//=========================// // initialization function // //==//=========================// init = function() { // initial fade to cancel repaint leftover lines, pointed out by the great lemon ctx.fillStyle = '#222'; ctx.fillRect(0, 0, width, height); while (--particleCount) particles.push(new Particle); while (--obstacleCount) obstacles.push(new Obstacle); anim(); }, //==//===================================// // animation loop and main functions // //==//===================================// anim = function() { window.requestAnimationFrame(anim); step(); draw(); }, step = function() { particles.map(function(particle) { particle.step(); }); obstacles.map(function(obstacle) { obstacle.step(); }); }, draw = function() { ctx.globalCompositeOperation = 'destination-out'; ctx.shadowBlur = 0; ctx.fillStyle = 'rgba(0, 0, 0, .1)'; ctx.fillRect(0, 0, width, height); ctx.globalCompositeOperation = 'lighter'; particles.map(function(particle) { particle.draw(); }); ctx.globalCompositeOperation = 'source-over'; ctx.fillStyle = 'rgba(25, 25, 25, .2)'; // purposely didn't change ctx.shadowColor obstacles.map(function(obstacle) { obstacle.draw(); }); }, spawnNew = function(x, y) { var part = new Particle; part.x = x; part.y = y; particles.push(part); }, //==//===================// // utility functions // //==//===================// checkDistance = function(par, obs) { var x = par.x - obs.x, y = par.y - obs.y, d = obs.radius; return d * d >= x * x + y * y; }, rand = function(min, max) { return Math.random() * (max - min) + min; }, validateObstacleResize = function(obs) { obs.validateResize(); }, //==//====================================// // Particle and Obstacle constructors // //==//====================================// Particle = function() { this.reset(); }, Obstacle = function() { this.reset(); }; Particle.prototype = { reset: function() { this.x = rand(0, width) | 0; this.y = rand(-obstacleRadius, 0) | 0; this.vx = this.vy = this.shine = 0; this.last = { x: this.x, y: this.y }; this.color = 'hsla(hue, 80%, 50%, alp)'.replace('hue', (frame + 360 * this.x / width) | 0); }, step: function() { if (this.shine)--this.shine; this.last.x = this.x; this.last.y = this.y; this.x += this.vx; this.y += this.vy += gravity; if ((this.x < 0 || this.x > width) && Math.random() < .4) this.vx *= -1; if (this.y < -obstacleRadius || this.y > height) { frame += .3; this.reset(); } var len = obstacles.length; while (--len + 1) { if (checkDistance(this, obstacles[len])) { this.hit(obstacles[len]); len = 0; } } }, hit: function(obs) { this.vx = (this.x - obs.x) * rand(.02, .03); this.vy = (this.y - obs.y) * rand(.02, .03); this.shine = 2; ++obs.toRemove; }, draw: function() { ctx.strokeStyle = this.color.replace('alp', .4); ctx.beginPath(); ctx.moveTo(this.last.x, this.last.y); ctx.lineTo(this.x, this.y); ctx.stroke(); if (this.shine) { ctx.shadowColor = ctx.strokeStyle; ctx.shadowBlur = this.shine * 3; ctx.fillStyle = this.color.replace('alp', .05); ctx.beginPath(); ctx.arc(this.x | 0, this.y | 0, this.shine * 8, 0, tau); ctx.fill(); } else ctx.shadowBlur = 0; } } Obstacle.prototype = { reset: function() { this.x = rand(0, width) | 0; this.y = rand(obstacleRadius * 2, height) | 0; this.maxRadius = rand(obstacleRadius / 2, obstacleRadius) | 0; this.radius = rand(this.maxRadius / 2, this.maxRadius); this.new = 30; this.toRemove = 0; }, step: function() { if (this.new) this.new -= 5; if (this.toRemove) { --this.radius; --this.toRemove; if (this.radius < 0) this.reset(); } else if (this.radius < this.maxRadius)++this.radius; }, draw: function() { ctx.shadowBlur = this.new; ctx.beginPath(); ctx.arc(this.x, this.y, this.radius, 0, tau); ctx.fill(); }, validateResize: function() { if (this.x > width || this.y > height) this.reset(); } } //==//=================// // and here we go! // //==//=================// init(); //==//================// // resize handler // //==//================// window.addEventListener('resize', function() { width = c.width = window.innerWidth; height = c.height = window.innerHeight; ctx.fillStyle = '#222'; ctx.fillRect(0, 0, width, height); obstacles.map(validateObstacleResize); }); //==//==============// // add on click // //==//==============// window.addEventListener('click', function() { for (var i = 0; i < 10; ++i) particles.push(new Particle); }); </script> |