Плавающие лучи и частицы на 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 |
#canvas { position: absolute; z-index: 0; top: 0; left: 0; height: 100vh; width: 100vw; background-image: -webkit-linear-gradient(#3f3f34, #1f1f12); background-image: linear-gradient(#3f3f34, #1f1f12); } #title { position: absolute; top: 60%; left: 0; z-index: 1; width: 100%; margin: 0; padding: 0; font-size: 3em; text-align: center; color: #ffffff; mix-blend-mode: overlay; } |
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 |
<canvas id="canvas"></canvas> <h1 id="title"></h1> <form> <label id="ray-count" for="ray-input">Rays: 50</label> <input type="range" id="ray-input" min="0" max="100"/> <label id="particle-count" for="particle-input">Particles: 50</label> <input type="range" id="particle-input" min="0" max="100"/> </form> <script> (function(window, document, undefined) { var canvas, ctx, rect, height, width, objects, numParticles, numRays, pInput, rInput, rCount, pCount, title, titleStr; var Ray = function() { this.ctx = ctx; this.angle = (105 * Math.PI) / 180; this.init = function() { this.velocity = 0.25 - Math.random() * 0.5; this.len = (canvas.height / 2) + Math.random() * (canvas.height / 2); this.start = { x: Math.random() * (canvas.width + 100) - 50, y: 0 }; this.end = { x: this.start.x + this.len * Math.cos(this.angle), y: this.start.y + this.len * Math.sin(this.angle) }; this.ttl = 100 + Math.random() * 200; this.life = 0; this.width = 0.5 + Math.random() * 4; this.hue = Math.round(45 + Math.random() * 15).toString(); this.saturation = Math.round(20 + Math.random() * 40).toString(); }; this.color = function() { var alpha = wave(this.life, this.ttl) * 0.005, color1 = 'hsla(' + this.hue + ',' + this.saturation + '%,' + '60%,' + alpha.toString() + ')', color2 = 'hsla(50,20%,20%,0)', gradient = ctx.createLinearGradient(this.start.x, this.start.y, this.end.x, this.end.y); gradient.addColorStop(0, color1); gradient.addColorStop(1, color2); return gradient; } this.draw = function(c) { this.ctx.beginPath(); this.ctx.strokeStyle = this.color(); this.ctx.lineWidth = this.width; this.ctx.moveTo(this.start.x, this.start.y); this.ctx.lineTo(this.end.x, this.end.y); this.ctx.stroke(); this.ctx.closePath(); }; this.update = function() { if (this.life > this.ttl) { this.init(); } else { this.life++; this.start.x += this.velocity; this.end.x += this.velocity; } }; this.init(); return this; }; var Particle = function() { this.ctx = ctx; this.init = function() { this.position = { x: Math.random() * width, y: height / 2 + Math.random() * height / 2 }; this.velocity = { x: 0.5 - Math.random() * 1, y: 0.5 - Math.random() * 1 }; this.ttl = 100 + Math.random() * 200; this.life = 0; this.size = 1 + Math.random() * 10; }; this.color = function() { var alpha = wave(this.life, this.ttl) * 0.005, color = 'hsla(50,50%,25%,' + alpha.toString() + ')'; return color; }; this.draw = function() { this.ctx.beginPath(); this.ctx.fillStyle = this.color(); this.ctx.moveTo(this.position.x, this.position.y); this.ctx.arc(this.position.x, this.position.y, this.size, 0, Math.PI * 2); this.ctx.fill(); this.ctx.closePath(); }; this.update = function() { if (this.life > this.ttl) { this.init(); } else { this.life++; this.position.x += this.velocity.x; this.position.y += this.velocity.y; } }; this.init(); return this; }; function onResize() { rect = canvas.getBoundingClientRect(); height = rect.height; width = rect.width; canvas.height = height; canvas.width = width; } function requestAnimationFrame() { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback) { window.setTimeout(callback, 1000 / 60); }; } function wave(t, a) { //function credit to http://stackoverflow.com/questions/26590800/how-can-we-increment-and-then-decrement-a-counter-without-conditionals return Math.abs(((t + a / 2) % a) - a / 2); } function setTitle() { if (numRays > 0 && numParticles > 0) titleStr = ''; else if (numRays > 0 && numParticles === 0) titleStr = 'Rays'; else if (numRays === 0 && numParticles > 0) titleStr = 'Particles'; else if (numRays === 0 && numParticles === 0) titleStr = '¯\\_(ツ)_/¯'; title.innerHTML = titleStr; } function createObjects() { numRays = parseInt(rInput.value); numParticles = parseInt(pInput.value); setTitle(); objects = []; for (var i = 0; i < numParticles; i++) { var particle = new Particle(); objects.push(particle); } for (var i = 0; i < numRays; i++) { var ray = new Ray(); objects.push(ray); } } function render(c) { c.clearRect(0, 0, width, height); c.shadowBlur = 30; c.shadowColor = 'white'; c.globalCompositeOperation = 'lighter'; for (var i = 0, len = objects.length; i < len; i++) { var object = objects[i]; object.update(); object.draw(); } } function loop() { render(ctx); window.requestAnimationFrame(loop); } function init() { canvas = document.getElementById('canvas'); ctx = canvas.getContext('2d'); title = document.getElementById('title'); rInput = document.getElementById('ray-input'); rCount = document.getElementById('ray-count'); rInput.oninput = function() { rCount.innerHTML = 'Rays: ' + this.value.toString(); createObjects(); }; pInput = document.getElementById('particle-input'); pCount = document.getElementById('particle-count'); pInput.oninput = function() { pCount.innerHTML = 'Particles: ' + this.value.toString(); createObjects(); }; onResize(); createObjects(); loop(); } window.onload = init; window.onresize = onResize; window.requestAnimationFrame = (requestAnimationFrame)(); })(this, document); </script> |
Это как раз то, что искала! Спасибо тебе, добрый человек, огромное человеческое спасибо :*
Пожалуйста 😃