Дождевые капли, отскакивающие от границы окна на SVG и 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 |
.container { display: -webkit-box; display: -webkit-flex; display: -ms-flexbox; display: flex; -webkit-box-orient: vertical; -webkit-box-direction: normal; -webkit-flex-direction: column; -ms-flex-direction: column; flex-direction: column; -webkit-box-align: center; -webkit-align-items: center; -ms-flex-align: center; align-items: center; -webkit-box-pack: center; -webkit-justify-content: center; -ms-flex-pack: center; justify-content: center; margin: 0; padding: 0; height: 100%; width: 100%; } #card { background: #fff; box-shadow: 9px 7px 37px -6px rgba(0, 0, 0, 0.75); overflow: hidden; width: 320px; padding: 0 10px; height: 400px; margin: 10pc 20px 0 20px; } #rain { top: 0px; bottom: 0; left: 0; right: 0; } #splash { margin-top: -80px; } |
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 |
<div class="container"> <div> <div id="card"> <svg width="300" height="400" viewbox="0 0 300 400" id="rain"></svg> </div> <svg width="380" height="200" viewbox="0 0 380 200" id="splash"></svg> </div> </div> <script> var rainColor = '#0000ff'; var rainSVG = Snap("#rain"); var rainContainer = { width: 300, height: 400 }; var rain = []; var rainCount = 10; var rainLength = 50; var rainSpeed = 0.3; var splashColor = '#3333ff' var splashSVG = Snap('#splash'); var splashContainer = { width: 380, height: 200 }; var splashes = []; var splashBounce = 80; var splashSpeed = 0.6; for (var i = 0; i < rainCount; i++) { // var x = gaps + (gaps * i); var line = rainSVG.line(0, 0, 0, rainLength).attr({ fill: "none", stroke: rainColor, strokeWidth: Math.random() * 3, //strokeDasharray: [rainLength, rainContainer.height - rainLength], //strokeDashoffset: Math.random() * rainContainer.height + rainLength }); rain.push(line); var points = []; var randomX = ((Math.random() * 80) - 40); points.push('M' + 0 + ',' + splashBounce); points.push('Q' + randomX + ',' + (0 - (Math.random() * splashBounce))); points.push((randomX * 2) + ',' + ((Math.random() * (splashContainer.height - splashBounce)) + splashBounce)); var splash = splashSVG.path(points.join(',')).attr({ fill: "none", stroke: splashColor, strokeWidth: 1 }); splashes.push(splash); } start(); document.onclick = start; function animate(line, delay, index) { var x = Math.random() * rainContainer.width; var delay = delay || splashSpeed + Math.random() * rainSpeed; //var time = rainSpeed - ((line.node.style.strokeDashoffset / rainContainer.height) * rainSpeed); TweenMax.fromTo(line.node, rainSpeed, { y: -rainLength, x: x }, { y: rainContainer.height - rainLength, delay: delay, ease: Linear.easeIn, onComplete: startSplash, onCompleteParams: [line, index] }) //TweenMax.kil(splashes[index]) var pathLength = Snap.path.getTotalLength(splashes[index]); splashes[index].node.style.strokeDasharray = 50 + ' ' + (pathLength); // splash.node.style.strokeDashoffset = pathLength; TweenMax.fromTo(splashes[index].node, splashSpeed, { strokeWidth: line.node.style.strokeWidth, x: 40 + x, opacity: 1, strokeDashoffset: 50 }, { strokeWidth: 0, strokeDashoffset: -pathLength, opacity: 1, delay: delay + rainSpeed, ease: SlowMo.ease.config(0.4, 0.1, false) }) } function startSplash(line, index) { //line.node.style.strokeDashoffset = rainContainer.height; animate(line, null, index); } function start() { TweenMax.killAll(); for (var i = 0; i < rain.length; i++) { animate(rain[i], Math.random() * 30, i); } } </script> |