-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrainf.min.js
1 lines (1 loc) · 3.18 KB
/
rainf.min.js
1
!function(t,i){"object"==typeof module&&module.exports?module.exports=i():"function"==typeof define&&define.amd?define(i):t.rainf=i()}(this,function(){var i={},h=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.msRequestAnimationFrame||function(t){window.setTimeout(t,1e3/60)},t=window.cancelAnimationFrame||window.webkitCancelAnimationFrame||window.mozCancelAnimationFrame||window.msCancelAnimationFrame||function(t){window.clearTimeout(t)},c=!1,n={dom:document.body,amount:100,size:1,speed:45,color:"#fff",opacity:.5,zIndex:null};function e(t){this.options=o({},n,i,t)}function o(){for(var t=arguments,i=1,n=t.length;i<n;i++)for(var e in t[i])t[0][e]=t[i][e];return t[0]}function s(t,i){return o(t.style,i)}function u(t,i){var n=i-t;return i===t?t:Math.random()*n+t}function d(t){return 0===t.indexOf("#")?4===t.length?t.substr(1).split("").map(function(t){return parseInt(t.concat(t),16)}).join(","):7===t.length?[t.substr(1,2),t.substr(3,2),t.substr(5,2)].map(function(t){return parseInt(t,16)}).join(","):"255,255,255":0===t.indexOf("rgb(")?t.substring(4,t.length-1):"255,255,255"}return e.prototype.init=function(){if(this.canvas)return this;var t,i=this.options;return this.dom="string"===(t=i.dom,Object.prototype.toString.call(t).toLowerCase().match(/\[object (\S*)\]/)[1])?document.querySelector(i.dom):i.dom,this.canvas=function(t,i){var n=document.createElement(t);for(var e in i)n[e]=i[e];return n}("canvas",{className:"rainf-canvas"}),s(this.canvas,{position:"absolute",top:0,left:0,width:"100%",height:"100%",pointerEvents:"none"}),i.zIndex&&s(this.canvas,{zIndex:i.zIndex}),this.dom.appendChild(this.canvas),this.height=this.canvas.height=this.canvas.offsetHeight,this.width=this.canvas.width=this.canvas.offsetWidth,this.context=this.canvas.getContext("2d"),this.drops=[],this.animationFrame=null,this.reset()},e.prototype.reset=function(){var n=this,e=n.options,o=(n.dom,n.canvas,n.context),s=n.drops=[];function a(){o.clearRect(0,0,n.width,n.height);for(var t=0;t<e.amount;t++){var i=s[t];o.fillStyle="rgba("+d(e.color)+","+i.opacity+")",o.fillRect(i.x,i.y,i.width,i.height),i.velY+=.4,i.y+=Math.pow(1.05,i.velY),!c&&(i.y>n.height+i.height||i.y<-i.height)&&r(i)}n.animationFrame=h(a)}function r(t){var i=u(16*e.size,32*e.size);t.width=e.size,t.x=u(0,n.width),t.y=u(n.height/5*-1,-1*i),t.velY=e.speed,t.opacity=e.opacity,t.height=i}return t(n.animationFrame),function(){for(var t=0;t<e.amount;t++){var i=u(16*e.size,32*e.size);s.push({x:u(0,n.width),y:u(0,n.height),width:e.size,height:i,velY:e.speed,opacity:e.opacity})}a()}(),this},e.prototype.pause=function(){c=!0},e.prototype.play=function(){c=!1},e.prototype.setOptions=function(t){return o(this.options,t),this.reset()},e.prototype.speed=function(t){var i=this.options,n=i.speed;i.speed=t;for(var e=0;e<i.amount;e++)this.drops[e].velY*=t/n;return this},e.prototype.resize=function(){var t=this.options,i=this.canvas.height,n=this.canvas.width,e=this.dom.offsetHeight,o=this.dom.offsetWidth;this.canvas.height=this.height=e,this.canvas.width=this.width=o;for(var s=0;s<t.amount;s++){var a=this.drops[s];a.x=a.x/n*o,a.y=a.y/i*e}return this},{version:"0.0.1",init:function(t){return new e(t).init()},config:function(t){i=t}}});