Эффект дождя на HTML5 Canvas
1 |
<canvas id="sky"></canvas> |
1 2 3 4 5 6 |
#sky { width: 100%; height: 100%; overflow: hidden; background: none; } |
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 |
<script> // Setup /////////////////////////////////////////////////////////// var canvas = document.getElementById('sky'); var sky = canvas.getContext('2d'); var window_width = window.innerWidth * 1.5; var window_height = window.innerHeight * 1.5; var fall_speed = 0.5; var wind_speed = 5; var rain_weight = 0.04; var rain_color = '255,255,255'; var drop_count; var drops = []; // Helpers ///////////////////////////////////////////////////////// window.requestAnimFrame = (function() { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.msRequestAnimationFrame || window.oRequestAnimationFrame || function(callback) { window.setTimeout(callback, 1000 / 60); }; })(); function randomFrom(min, max) { return (Math.random() * (max - min) + min); } function resizer() { window_width = window.innerWidth * 1.5; window_height = window.innerHeight * 1.5; drop_count = window_width * rain_weight; canvas.setAttribute('width', window_width); canvas.setAttribute('height', window_height); } window.addEventListener('resize', resizer, false); // Drawing ///////////////////////////////////////////////////////// function paintSky() { for (var i = 0; i < drop_count; i++) { drops[i] = new drop(); drops[i].reset(); } rain(); } function rain() { sky.clearRect(0, 0, window_width, window_height); var drops_length = drops.length; for (var i = 0; i < drops_length; i++) { var drop = drops[i]; drop.fall(); drop.draw(); } window.requestAnimFrame(rain); } function drop() { this.reset = function() { this.r = randomFrom(0.8, 1.6); this.l = (this.r * 250); this.x = randomFrom((window_width * -0.25), (window_width * 1.125)); this.y = randomFrom((window_height * -0.25), (window_height * - 1)); this.dx = randomFrom((wind_speed - 3), (wind_speed + 3)); this.dy = (this.r * (100 * fall_speed)); this.offset = (this.l * (this.dx / this.dy)); this.opacity = (this.r * randomFrom(0.2, 0.6)); this.drip = this.render(); }; this.render = function() { var canv = document.createElement('canvas'); var ctx = canv.getContext('2d'); canv.setAttribute('width', Math.abs(this.offset) + this.r); canv.setAttribute('height', this.l); ctx.beginPath(); var drip = ctx.createLinearGradient(0, 0, 0, this.l); drip.addColorStop(0, 'rgba(' + rain_color + ', 0)'); drip.addColorStop(1, 'rgba(' + rain_color + ', ' + this.opacity + ')'); ctx.fillStyle = drip; //sky.rect(this.x, this.y, this.r, this.l); var startX = (this.offset >= 0) ? 0 : Math.abs(this.offset); ctx.moveTo(startX, 0); ctx.lineTo(startX + this.r, 0); ctx.lineTo(startX + this.r + this.offset, this.l); ctx.lineTo(startX + this.offset, this.l); ctx.closePath(); ctx.fill(); return canv; }; this.draw = function() { sky.drawImage(this.drip, this.x, this.y); }; this.fall = function() { this.x += this.dx; this.y += this.dy; if (this.y > (window_height * 1.25)) { this.reset(); } }; } resizer(); paintSky(); </script> |
Замечательный эффект! Часто применял его в осенней теме