forked from Fuxy526/snowf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnowf.min.js
1 lines (1 loc) · 3.7 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(){var i={},c=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)},u=!1,n={dom:document.body,amount:50,size:5,speed:1.5,wind:0,color:"#fff",opacity:.8,swing:1,image:null,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 d(t,i){var n=i-t;return i===t?t:Math.random()*n+t}function f(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:"snowf-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.flakes=[],this.animationFrame=null,this.reset()},e.prototype.reset=function(){var e=this,o=e.options,s=(e.dom,e.canvas,e.context),a=e.flakes=[];function r(){var t;s.clearRect(0,0,e.width,e.height);for(var i=0;i<o.amount;i++){var n=a[i];o.image?(t||((t=new Image).src=o.image),s.drawImage(t,n.x-n.r,n.y-n.r,2*n.r,2*n.r)):(s.beginPath(),s.fillStyle="rgba("+f(o.color)+","+n.opacity+")",s.arc(n.x,n.y,n.r,2*Math.PI,!1),s.fill(),s.closePath()),n.velX=Math.abs(n.velX)<Math.abs(o.wind)?n.velX+o.wind/20:o.wind,n.y=n.y+.5*n.velY,n.x=n.x+(o.swing?.4*Math.cos(n.swing+=.03)*n.opacity*o.swing:0)+.5*n.velX,!u&&(n.x>e.width+n.r||n.x<-n.r||n.y>e.height+n.r||n.y<-n.r)&&h(n)}e.animationFrame=c(r)}function h(t){var i=t.r;t.r=d(o.size,2*o.size)/2,t.x>e.width+i?(t.x=-t.r,t.y=d(0,e.height)):t.x<-i?(t.x=e.width+t.r,t.y=d(0,e.height)):(t.x=d(0,e.width),t.y=-t.r),t.velX=0,t.velY=d(o.speed,2*o.speed),t.swing=d(0,2*Math.PI),t.opacity=d(0,o.opacity)}return t(e.animationFrame),function(){for(var t=0;t<o.amount;t++)a.push({x:d(0,e.width),y:d(0,e.height),r:d(o.size,2*o.size)/2,velX:0,velY:d(o.speed,2*o.speed),swing:d(0,2*Math.PI),opacity:d(0,o.opacity)});r()}(),this},e.prototype.pause=function(){u=!0},e.prototype.play=function(){u=!1},e.prototype.setOptions=function(t){return o(this.options,t),this.reset()},e.prototype.wind=function(t,i){var n=this.options,e=n.wind;return n.wind=t,i&&setTimeout(function(){n.wind=e},i),this},e.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},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.flakes[s];a.x=a.x/n*o,a.y=a.y/i*e}return this},{version:"0.0.3",init:function(t){return new e(t).init()},config:function(t){i=t}}});