UNPKG

7.97 kBJavaScriptView Raw
1/*! Snowflakes | © 2019 Denis Seleznev | MIT License | https://github.com/hcodes/snowflakes/ */
2!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):e.Snowflakes=t()}(this,function(){"use strict";function f(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){for(var n=0;n<t.length;n++){var i=t[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,i.key,i)}}function e(e,t,n){return t&&i(e.prototype,t),n&&i(e,n),e}function r(e,t){return function(e){if(Array.isArray(e))return e}(e)||function(e,t){if(!(Symbol.iterator in Object(e)||"[object Arguments]"===Object.prototype.toString.call(e)))return;var n=[],i=!0,a=!1,o=void 0;try{for(var r,s=e[Symbol.iterator]();!(i=(r=s.next()).done)&&(n.push(r.value),!t||n.length!==t);i=!0);}catch(e){a=!0,o=e}finally{try{i||null==s.return||s.return()}finally{if(a)throw o}}return n}(e,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}()}var a="";function m(n,i){Object.keys(i).forEach(function(e){var t=e;a&&-1<e.search("animation")&&(t=a+e[0].toUpperCase()+e.substr(1)),n.style[t]=i[e]})}function o(){var e,t=document.body,n=document.documentElement;return window.innerHeight?e=window.innerHeight:n&&n.clientHeight?e=n.clientHeight:t&&(e=t.clientHeight),e}function s(e,t){return t||(t=document.createElement("style"),document.body.appendChild(t)),t.styleSheet?t.styleSheet.cssText=e:"textContent"in t?t.textContent=e:t.innerHTML=e,t}function t(e){e&&e.parentNode&&e.parentNode.removeChild(e)}function u(e,t){e.classList.add(t)}function c(e,t){return e+Math.floor(Math.random()*(t-e))}function d(e,t,n,i,a){return i+(a-i)*(e-t)/(n-t)}"undefined"!=typeof window&&(a=-1<Array.prototype.slice.call(window.getComputedStyle(document.documentElement,"")).join(",").search(/,animation/)?"":"Webkit");var n,l,h,_=function(){function l(e,t,n){f(this,l);var i=n.minSize===n.maxSize;this.innerSize=i?0:c(0,l.maxInnerSize),this.size=l.calcSize(this.innerSize,n);var a=document.createElement("div"),o=document.createElement("div"),r=this.getAnimationProps(t,n),s={animationDelay:r.animationDelay,animationDuration:r.animationDuration,left:99*Math.random()+"%",marginTop:-Math.sqrt(2)*this.size+"px",width:this.size+"px",height:this.size+"px"};i||(s.zIndex=n.zIndex+10*this.size,s.opacity=d(this.size,n.minSize,n.maxSize,n.minOpacity,n.maxOpacity)),m(a,s),m(o,{animationName:"snowflake_x_"+this.innerSize,animationDelay:Math.random()+"s"}),u(a,"snowflake"),u(o,"snowflake__inner"),n.types&&u(o,"snowflake__inner_type_"+c(0,n.types)),n.wind&&u(o,"snowflake__inner_wind"),n.rotation&&u(o,"snowflake__inner_rotation"+(.5<Math.random()?"":"_reverse")),a.appendChild(o),this._elem=a,e.appendChild(a)}return e(l,[{key:"getAnimationProps",value:function(e,t){var n=e/50/t.speed,i=n/3;return{animationDelay:Math.random()*n+"s",animationDuration:d(this.size,t.minSize,t.maxSize,n,i)+"s"}}},{key:"resize",value:function(e,t){var n=this.getAnimationProps(e,t);m(this._elem,n)}},{key:"destroy",value:function(){delete this._elem}}],[{key:"calcSize",value:function(e,t){return Math.floor(d(e,0,l.maxInnerSize,t.minSize,t.maxSize))}}]),l}();h=20,(l="maxInnerSize")in(n=_)?Object.defineProperty(n,l,{value:h,enumerable:!0,configurable:!0,writable:!0}):n[l]=h;var y=function(){function a(e){var n=this;f(this,a),this.params=this._setParams(e),this._flakes=[],this._isBody=this.params.container===document.body;var i=this._container=document.createElement("div");u(i,"snowflakes"),this._isBody&&u(i,"snowflakes_body"),m(i,{zIndex:this.params.zIndex}),this.params.container.appendChild(i),this.params.stop&&this.stop(),a._mainStyleNode||(a._mainStyleNode=s('.snowflake{position:absolute;-webkit-user-select:none;-moz-user-select:none;-ms-user-select:none;user-select:none;pointer-events:none;-webkit-animation:snowflake_y 10s linear infinite;animation:snowflake_y 10s linear infinite;will-change:transform}.snowflake__inner,.snowflake__inner:before{position:absolute;top:0;right:0;bottom:0;left:0}.snowflake__inner:before{content:"";background-size:100% 100%}.snowflake__inner_wind{-webkit-animation:snowflake_x_8 1s ease-in-out infinite alternate;animation:snowflake_x_8 1s ease-in-out infinite alternate}.snowflake__inner_rotation:before{-webkit-animation:snowflake_rotation 2s linear infinite;animation:snowflake_rotation 2s linear infinite}.snowflake__inner_rotation_reverse:before{-webkit-animation:snowflake_rotation_reverse 2s linear infinite;animation:snowflake_rotation_reverse 2s linear infinite}.snowflakes{pointer-events:none}.snowflakes_paused .snowflake,.snowflakes_paused .snowflake__inner,.snowflakes_paused .snowflake__inner:before{-webkit-animation-play-state:paused;animation-play-state:paused}.snowflakes_body{position:fixed;top:0;left:0;width:100%;height:1px}@-webkit-keyframes snowflake_rotation{0%{-webkit-transform:rotate(0deg);transform:rotate(0deg)}to{-webkit-transform:rotate(1turn);transform:rotate(1turn)}}@keyframes snowflake_rotation{0%{-webkit-transform:rotate(0deg);transform:rotate(0deg)}to{-webkit-transform:rotate(1turn);transform:rotate(1turn)}}@-webkit-keyframes snowflake_rotation_reverse{0%{-webkit-transform:rotate(0deg);transform:rotate(0deg)}to{-webkit-transform:rotate(-1turn);transform:rotate(-1turn)}}@keyframes snowflake_rotation_reverse{0%{-webkit-transform:rotate(0deg);transform:rotate(0deg)}to{-webkit-transform:rotate(-1turn);transform:rotate(-1turn)}}'),a._count=(a._count||0)+1),this._winHeight=o(),this._onResize=function(){n._winHeight=o();var t=n._height();m(i,{display:"none"}),n._flakes.forEach(function(e){return e.resize(t,n.params)}),n._updateAnimationStyle(),m(i,{display:"block"})},this._animationStyleNode=s(this._getAnimationStyle()),window.addEventListener("resize",this._onResize,!1);for(var t=0;t<this.params.count;t++)this._flakes.push(new _(i,this._height(),this.params))}return e(a,[{key:"destroy",value:function(){this._removeStyle(),t(this._container),delete this._container,window.removeEventListener("resize",this._onResize,!1),this._flakes.forEach(function(e){return e.destroy()}),delete this._flakes,delete this.params}},{key:"start",value:function(){var e,t;e=this._container,t="snowflakes_paused",e.classList.remove(t)}},{key:"stop",value:function(){u(this._container,"snowflakes_paused")}},{key:"_setParams",value:function(a){a=a||{};var o={};return[["color","#5ECDEF"],["container",document.body],["count",50],["speed",1],["stop",!1],["rotation",!0],["minOpacity",.6],["maxOpacity",1],["minSize",8],["maxSize",18],["types",6],["width"],["height"],["wind",!0],["zIndex",9999]].forEach(function(e){var t=r(e,2),n=t[0],i=t[1];o[n]="boolean"==typeof i?n in a?a[n]:i:a[n]||i}),o}},{key:"_getAnimationStyle",value:function(){for(var e=this._height()+this.params.maxSize+"px",t="@-webkit-keyframes snowflake_y{from{-webkit-transform:translateY(0px)}to{-webkit-transform:translateY(".concat(e,");}}\n@keyframes snowflake_y{from{transform:translateY(0px)}to{transform:translateY(").concat(e,")}}"),n=0;n<=_.maxInnerSize;n++){var i=4*(_.calcSize(n,this.params)-this.params.minSize)+"px";t+="@-webkit-keyframes snowflake_x_".concat(n,"{from{-webkit-transform:translateX(0px)}to{-webkit-transform:translateX(").concat(i,");}}\n@keyframes snowflake_x_").concat(n,"{from{transform:translateX(0px)}to{transform:translateX(").concat(i,")}}")}return t}},{key:"_updateAnimationStyle",value:function(){s(this._getAnimationStyle(),this._animationStyleNode)}},{key:"_removeStyle",value:function(){--a._count<=0&&(a._count=0,t(a._mainStyleNode),delete a._mainStyleNode),t(this._animationStyleNode),delete this._animationStyleNode,t(this._imagesStyleNode),delete this._imagesStyleNode}},{key:"_height",value:function(){return this.params.height||(this._isBody?this._winHeight:this.params.container.offsetHeight+this.params.maxSize)}}]),a}();return function(e){return new y(e)}});
\No newline at end of file