Realistic Raindrops Effect with Canvas and Rainyday.js

Realistic Raindrops Effect with Canvas and Rainyday.js
File Size: 16.1 KB
Views Total:
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   

rainyday.js is an awesome javascript library for creating a realistic raindrops effect on your webpage using Html5 Canvas element and javascript.

How to use it:

1. Include rainyday.js javascript library in the head section of your page

<script src="rainyday.js" type="text/javascript"></script>

2. The html

<body onload="demo();">
<div id="sound" style="z-index: -1;"></div>
<img id="background" src="" />
<div id="cholder">
<canvas id="canvas"></canvas>
</div>
</body>

3. The javascript. Opening demo.html?imgur=I2tZMAg&youtube=Ftu5ZpAk8dM will load this image with this sound in the background!

<script type="text/javascript">
function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null;
}

 function demo() {
var image = document.getElementById('background');
image.onload = function() {
var engine = new RainyDay('canvas','background', window.innerWidth, window.innerHeight, 1, getURLParameter("blur") || 20);

var preset = getURLParameter("preset") || 1;
if (preset == 1) {
engine.gravity = engine.GRAVITY_NON_LINEAR;
engine.trail = engine.TRAIL_DROPS;
engine.rain([ engine.preset(3, 3, 0.88), engine.preset(5, 5, 0.9), engine.preset(6, 2, 1) ], 100);
} else if (preset == 2) {
engine.gravity = engine.GRAVITY_NON_LINEAR;
 engine.trail = engine.TRAIL_DROPS;
 engine.VARIABLE_GRAVITY_ANGLE = Math.PI / 8;
engine.rain([ engine.preset(0, 2, 0.5), engine.preset(4, 4, 1) ], 50);
} else if (preset == 3) {
engine.gravity = engine.GRAVITY_NON_LINEAR;
 engine.trail = engine.TRAIL_SMUDGE;
engine.rain([ engine.preset(0, 2, 0.5), engine.preset(4, 4, 1) ], 50);
}
};
image.crossOrigin = "anonymous";
image.src="http://i.imgur.com/" + getURLParameter('imgur') + ".jpg";

var youtube = getURLParameter("youtube");
if (youtube) {
var div = document.getElementById("sound");
var player = document.createElement('iframe');
player.frameborder = "0";
if (!getURLParameter("novideo")) {
div.style.zIndex = 1000;
player.setAttribute("class", "video");
} else {
player.height = "1";
player.width = "1";
}
player.src = "https://youtube.com/embed/" + youtube + "?autoplay=1&controls=0&showinfo=0&autohide=1&loop=1";
div.appendChild(player);
}
 }
</script>

This awesome jQuery plugin is developed by maroslaw. For more Advanced Usages, please check the demo page or visit the official website.