-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsnowf.min.js
1 lines (1 loc) · 3.67 KB
/
snowf.min.js
1
!function(t,i){"object"==typeof module&&module.exports?module.exports=i():"function"==typeof define&&define.amd?define(i):t.snowf=i()}(this,function(){function t(t){this.options=i({},f,c,t)}function i(){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 n(t){return Object.prototype.toString.call(t).toLowerCase().match(/\[object (\S*)\]/)[1]}function e(t,i){var n=document.createElement(t);for(var e in i)n[e]=i[e];return n}function o(t,n){return i(t.style,n)}function s(t,i){var n=i-t;return i===t?t:Math.random()*n+t}function a(t){var i="";return i=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"}var r="snowf",h="0.0.3",c={},u=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.msRequestAnimationFrame||function(t){window.setTimeout(t,1e3/60)},d=window.cancelAnimationFrame||window.webkitCancelAnimationFrame||window.mozCancelAnimationFrame||window.msCancelAnimationFrame||function(t){window.clearTimeout(t)},f={dom:document.body,amount:50,size:5,speed:1.5,wind:0,color:"#fff",opacity:.8,swing:1,image:null,zIndex:null};t.prototype.init=function(){if(this.canvas)return this;var t=this.options;return this.dom="string"===n(t.dom)?document.querySelector(t.dom):t.dom,this.canvas=e("canvas",{className:r+"-canvas"}),o(this.canvas,{position:"absolute",top:0,left:0,width:"100%",height:"100%",pointerEvents:"none"}),t.zIndex&&o(this.canvas,{zIndex:t.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.flakes=[],this.animationFrame=null,this.reset()},t.prototype.reset=function(){function t(){for(var t=0;t<o.amount;t++)h.push({x:s(0,e.width),y:s(0,e.height),r:s(o.size,2*o.size)/2,velX:0,velY:s(o.speed,2*o.speed),swing:s(0,2*Math.PI),opacity:s(0,o.opacity)});i()}function i(){var t;r.clearRect(0,0,e.width,e.height);for(var s=0;s<o.amount;s++){var c=h[s];o.image?(t||(t=new Image,t.src=o.image),r.drawImage(t,c.x-c.r,c.y-c.r,2*c.r,2*c.r)):(r.beginPath(),r.fillStyle="rgba("+a(o.color)+","+c.opacity+")",r.arc(c.x,c.y,c.r,2*Math.PI,!1),r.fill(),r.closePath()),c.velX=Math.abs(c.velX)<Math.abs(o.wind)?c.velX+o.wind/20:o.wind,c.y=c.y+.5*c.velY,c.x=c.x+(o.swing?.4*Math.cos(c.swing+=.03)*c.opacity*o.swing:0)+.5*c.velX,(c.x>e.width+c.r||c.x<-c.r||c.y>e.height+c.r||c.y<-c.r)&&n(c)}e.animationFrame=u(i)}function n(t){var i=t.r;t.r=s(o.size,2*o.size)/2,t.x>e.width+i?(t.x=-t.r,t.y=s(0,e.height)):t.x<-i?(t.x=e.width+t.r,t.y=s(0,e.height)):(t.x=s(0,e.width),t.y=-t.r),t.velX=0,t.velY=s(o.speed,2*o.speed),t.swing=s(0,2*Math.PI),t.opacity=s(0,o.opacity)}var e=this,o=e.options,r=(e.dom,e.canvas,e.context),h=e.flakes=[];return d(e.animationFrame),t(),this},t.prototype.setOptions=function(t){return i(this.options,t),this.reset()},t.prototype.wind=function(t,i){var n=this.options,e=n.wind;return n.wind=t,i&&setTimeout(function(){n.wind=e},i),this},t.prototype.speed=function(t){var i=this.options,n=i.speed;i.speed=t;for(var e=0;e<i.amount;e++)this.flakes[e].velY*=t/n;return this},t.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.flakes[s];a.x=a.x/n*o,a.y=a.y/i*e}return this};var m={version:h,init:function(i){return new t(i).init()},config:function(t){c=t}};return m});