UNPKG

let-it-go

Version:

❄️ Let your website snow instantly

2 lines (1 loc) 10.3 kB
function t(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=Array(e);n<e;n++)r[n]=t[n];return r}function e(t,e,n){if("function"==typeof t?t===e:t.has(e))return arguments.length<3?e:n;throw new TypeError("Private element is not present on this object")}function n(t,e){if(e.has(t))throw new TypeError("Cannot initialize the same private elements twice on an object")}function r(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function i(t,n){return t.get(e(t,n))}function o(t,e,r){n(t,e),e.set(t,r)}function a(t,n,r){return t.set(e(t,n),r),r}function s(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,v(r.key),r)}}function h(t,e,n){return e&&s(t.prototype,e),n&&s(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t}function u(t,e,n){return(e=v(e))in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function c(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(t);e&&(r=r.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,r)}return n}function l(t,e){return function(t){if(Array.isArray(t))return t}(t)||function(t,e){var n=null==t?null:"undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(null!=n){var r,i,o,a,s=[],h=!0,u=!1;try{if(o=(n=n.call(t)).next,0===e){if(Object(n)!==n)return;h=!1}else for(;!(h=(r=o.call(n)).done)&&(s.push(r.value),s.length!==e);h=!0);}catch(t){u=!0,i=t}finally{try{if(!h&&null!=n.return&&(a=n.return(),Object(a)!==a))return}finally{if(u)throw i}}return s}}(t,e)||d(t,e)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function f(e){return function(e){if(Array.isArray(e))return t(e)}(e)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(e)||d(e)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function v(t){var e=function(t,e){if("object"!=typeof t||!t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,e||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:e+""}function d(e,n){if(e){if("string"==typeof e)return t(e,n);var r={}.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?t(e,n):void 0}}var y=function(){return h((function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:0,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:0;r(this,t),this.x=e,this.y=n}),[{key:"add",value:function(t){var e=t.x,n=void 0===e?0:e,r=t.y,i=void 0===r?0:r;return this.x+=n,this.y+=i,this}}])}(),p=function(){return h((function t(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},n=e.p,i=void 0===n?new y:n,o=e.v,a=void 0===o?new y:o,s=e.r,h=void 0===s?.5:s,c=e.color,l=void 0===c?"#ffffff":c,f=e.alpha,v=void 0===f?1:f;r(this,t),u(this,"p",void 0),u(this,"v",void 0),u(this,"r",void 0),u(this,"color",void 0),u(this,"alpha",void 0),this.p=i,this.v=a,this.r=h,this.color=l,this.alpha=v}),[{key:"update",value:function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},e=t.width,n=void 0===e?0:e,r=t.height,i=void 0===r?0:r,o=this.p,a=this.r,s=this.v;o.y-a>i&&(this.p.y=0-a),o.x-a>n&&(this.p.x=0-a),o.x+a<0&&(this.p.x=n+a),this.p.add(s)}},{key:"draw",value:function(t){var e=this.p,n=this.r,r=this.color,i=this.alpha;t.save(),t.beginPath(),t.arc(e.x,e.y,n,0,2*Math.PI),t.closePath(),t.fillStyle=r,t.globalAlpha=i,t.fill(),t.restore()}}])}();function b(t){if(!t)throw Error("[let-it-go] ".concat(arguments.length>1&&void 0!==arguments[1]?arguments[1]:"internal error."))}function g(t){b(Array.isArray(t),"Range must be an array."),b(2===t.length,"Range size must be 2."),b(t.every((function(t){return"number"==typeof t})),"Range value must be a number.")}function m(t){g(t),b(t.every((function(t){return t>=0})),"Radius range value must be positive.")}function w(t){g(t),b(t.every((function(t){return t>=0&&t<=1})),"Alpha range value must be from 0 to 1.")}function k(t,e){return Math.random()*(e-t)+t}function R(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{};Object.entries(e).forEach((function(e){var n=l(e,2),r=n[0],i=n[1];t.style[r]=i||""}))}var E,A={root:document.body,number:window.innerWidth,velocityXRange:[-3,3],velocityYRange:[1,5],radiusRange:[.5,1],color:"#ffffff",alphaRange:[.8,1],backgroundColor:"transparent",style:{zIndex:"-1",pointerEvents:"none"}},O=new WeakMap,j=new WeakMap,M=new WeakMap,I=new WeakMap,S=new WeakMap,P=new WeakMap,W=new WeakMap,x=new WeakMap,N=new WeakMap,T=new WeakMap,C=new WeakMap,_=new WeakMap,F=new WeakSet,L=new WeakMap,U=new WeakMap,V=function(){return h((function t(){var s,h,c=this,l=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},f=l.root,v=void 0===f?A.root:f,d=l.number,y=void 0===d?A.number:d,p=l.velocityXRange,b=void 0===p?A.velocityXRange:p,k=l.velocityYRange,R=void 0===k?A.velocityYRange:k,E=l.radiusRange,V=void 0===E?A.radiusRange:E,Y=l.color,q=void 0===Y?A.color:Y,G=l.alphaRange,H=void 0===G?A.alphaRange:G,$=l.backgroundColor,B=void 0===$?A.backgroundColor:$,J=l.style,K=void 0===J?A.style:J;r(this,t),n(s=this,h=F),h.add(s),u(this,"root",document.body),o(this,O,!1),o(this,j,0),o(this,M,void 0),o(this,I,void 0),o(this,S,void 0),o(this,P,void 0),o(this,W,void 0),u(this,"backgroundColor",void 0),u(this,"style",A.style),u(this,"canvas",document.createElement("canvas")),o(this,x,void 0),o(this,N,[]),o(this,T,null),o(this,C,null),o(this,_,null),o(this,L,(function(){i(N,c).forEach((function(t){return t.update(c.canvas)}))})),o(this,U,(function(){if(i(O,c)){var t=c.canvas,e=t.width,n=t.height;i(x,c).clearRect(0,0,e,n),i(x,c).fillStyle=c.backgroundColor,i(x,c).fillRect(0,0,e,n),i(N,c).forEach((function(t){return t.draw(i(x,c))})),a(C,c,requestAnimationFrame(i(U,c)))}})),g(b),g(R),m(V),w(H),this.root=v,a(j,this,y),a(M,this,b.sort()),a(I,this,R.sort()),a(S,this,V.sort()),a(P,this,q),a(W,this,H.sort()),this.backgroundColor=B,this.style=K;var Q=this.canvas.getContext("2d");if(!Q)throw new Error("[let-it-go] The 2d context canvas is not supported.");a(x,this,Q),e(F,this,z).call(this),e(F,this,D).call(this),e(F,this,X).call(this)}),[{key:"number",get:function(){return i(j,this)},set:function(t){b(t>=0,"Number must be positive"),a(j,this,t),e(F,this,D).call(this)}},{key:"velocityXRange",get:function(){return i(M,this)},set:function(t){g(t);var e=t.sort();a(M,this,e),i(N,this).forEach((function(t){t.v.x=k.apply(void 0,f(e))}))}},{key:"velocityYRange",get:function(){return i(I,this)},set:function(t){g(t);var e=t.sort();a(I,this,e),i(N,this).forEach((function(t){t.v.y=k.apply(void 0,f(e))}))}},{key:"radiusRange",get:function(){return i(S,this)},set:function(t){m(t);var e=t.sort();a(S,this,e),i(N,this).forEach((function(t){t.r=k.apply(void 0,f(e))}))}},{key:"color",get:function(){return i(P,this)},set:function(t){a(P,this,t),i(N,this).forEach((function(e){e.color=t}))}},{key:"alphaRange",get:function(){return i(W,this)},set:function(t){w(t);var e=t.sort();a(W,this,e),i(N,this).forEach((function(t){t.alpha=k.apply(void 0,f(e))}))}},{key:"letItStop",value:function(){a(O,this,!1),i(T,this)&&(clearInterval(i(T,this)),a(T,this,null)),i(C,this)&&(cancelAnimationFrame(i(C,this)),a(C,this,null))}},{key:"letItGoAgain",value:function(){e(F,this,X).call(this)}},{key:"clear",value:function(){this.letItStop(),a(N,this,[]),i(_,this)&&(i(_,this).disconnect(),a(_,this,null)),this.canvas.parentNode&&this.root.removeChild(this.canvas),i(x,this).clearRect(0,0,this.canvas.width,this.canvas.height),a(j,this,0)}}])}();function z(){var t=this;try{var e=new ResizeObserver((function(e){var n,r=function(t,e){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!n){if(Array.isArray(t)||(n=d(t))||e&&t&&"number"==typeof t.length){n&&(t=n);var r=0,i=function(){};return{s:i,n:function(){return r>=t.length?{done:!0}:{done:!1,value:t[r++]}},e:function(t){throw t},f:i}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,a=!0,s=!1;return{s:function(){n=n.call(t)},n:function(){var t=n.next();return a=t.done,t},e:function(t){s=!0,o=t},f:function(){try{a||null==n.return||n.return()}finally{if(s)throw o}}}}(e);try{for(r.s();!(n=r.n()).done;){var i=n.value;t.canvas.width=i.contentRect.width,t.canvas.height=i.contentRect.height}}catch(t){r.e(t)}finally{r.f()}}));e.observe(this.root),a(_,this,e)}catch(t){console.warn("[let-it-go] ResizeObserver is not supported.",t)}this.canvas.width=this.root.clientWidth,this.canvas.height=this.root.clientHeight,R(this.root,{position:"relative"}),R(this.canvas,function(t){for(var e=1;e<arguments.length;e++){var n=null!=arguments[e]?arguments[e]:{};e%2?c(Object(n),!0).forEach((function(e){u(t,e,n[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(n)):c(Object(n)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(n,e))}))}return t}({position:"absolute",top:"0",left:"0"},this.style)),this.root.appendChild(this.canvas)}function D(){var t=this,e=this.canvas;a(N,this,Array.from({length:i(j,this)},(function(){return new p({p:new y(k(0,e.width),k(0,-e.height)),v:new y(k.apply(void 0,f(i(M,t)))||Number.MIN_VALUE,k.apply(void 0,f(i(I,t)))||Number.MIN_VALUE),r:k.apply(void 0,f(i(S,t)))||Number.MIN_VALUE,color:i(P,t),alpha:k.apply(void 0,f(i(W,t)))||Number.MIN_VALUE})})))}function X(){i(O,this)||(a(T,this,setInterval(i(L,this),E.FRAME_INTERVAL)),a(C,this,requestAnimationFrame(i(U,this))),a(O,this,!0))}E=V,u(V,"DEFAULT_OPTIONS",A),u(V,"FRAME_RATE",30),u(V,"FRAME_INTERVAL",1e3/E.FRAME_RATE);export{A as DEFAULT_OPTIONS,V as LetItGo,V as default};