Всплывающие пузырьки на 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 |
<canvas id="bubbles"></canvas> <script> // Canvas Init var c = document.getElementById('bubbles'), ctx = c.getContext('2d'), width = window.innerWidth, height = window.innerHeight, particles = 60, minRadius = 5, maxRadius = 20, speed = 0.01, x = width / particles; // Bubbles var Bubbles = []; for (var i = 0; i < particles; i++) { Bubbles.push({ x: i * x, y: height * Math.random(), r: minRadius + Math.random() * (maxRadius - minRadius), speed: 10 * Math.random() }); } function bubble() { c.width = width; c.height = height; for (i = 0; i < Bubbles.length; i++) { var b = Bubbles[i]; console.log(i, b); ctx.beginPath(); ctx.arc(b.x, b.y, b.r, 0, 2 * Math.PI); b.alpha = .5 * (b.y / height); b.speed += speed; ctx.strokeStyle = "rgba(255, 255, 255, .5)"; ctx.stroke(); ctx.fillStyle = "hsla(203, 75%, 69%," + b.alpha + ")"; ctx.fill(); b.y -= b.speed; if (b.y < 0) { b.y = height; b.speed = Math.random() * 5; } } } // Draw function draw() { bubble(); window.requestAnimationFrame(draw); } // Resize Canvas function resizeCanvas() { width = window.innerWidth, height = window.innerHeight; c.width = width; c.height = height; draw(); } resizeCanvas(); window.addEventListener('resize', resizeCanvas, false); </script> |