UNPKG

let-it-go

Version:

❄️ Let your website snow instantly

2 lines (1 loc) 10.5 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).letItGo={})}(this,(function(t){"use strict";function e(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,i=Array(e);n<e;n++)i[n]=t[n];return i}function n(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 i(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 o(t,e){return t.get(n(t,e))}function a(t,e,n){i(t,e),e.set(t,n)}function s(t,e,i){return t.set(n(t,e),i),i}function u(t,e){for(var n=0;n<e.length;n++){var i=e[n];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(t,d(i.key),i)}}function l(t,e,n){return e&&u(t.prototype,e),n&&u(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t}function c(t,e,n){return(e=d(e))in t?Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}):t[e]=n,t}function h(t,e){var n=Object.keys(t);if(Object.getOwnPropertySymbols){var i=Object.getOwnPropertySymbols(t);e&&(i=i.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),n.push.apply(n,i)}return n}function f(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 i,r,o,a,s=[],u=!0,l=!1;try{if(o=(n=n.call(t)).next,0===e){if(Object(n)!==n)return;u=!1}else for(;!(u=(i=o.call(n)).done)&&(s.push(i.value),s.length!==e);u=!0);}catch(t){l=!0,r=t}finally{try{if(!u&&null!=n.return&&(a=n.return(),Object(a)!==a))return}finally{if(l)throw r}}return s}}(t,e)||p(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 v(t){return function(t){if(Array.isArray(t))return e(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||p(t)||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 d(t){var e=function(t,e){if("object"!=typeof t||!t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var i=n.call(t,e||"default");if("object"!=typeof i)return i;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===e?String:Number)(t)}(t,"string");return"symbol"==typeof e?e:e+""}function p(t,n){if(t){if("string"==typeof t)return e(t,n);var i={}.toString.call(t).slice(8,-1);return"Object"===i&&t.constructor&&(i=t.constructor.name),"Map"===i||"Set"===i?Array.from(t):"Arguments"===i||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(i)?e(t,n):void 0}}var y=function(){return l((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,i=t.y,r=void 0===i?0:i;return this.x+=n,this.y+=r,this}}])}(),b=function(){return l((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,u=void 0===s?.5:s,l=e.color,h=void 0===l?"#ffffff":l,f=e.alpha,v=void 0===f?1:f;r(this,t),c(this,"p",void 0),c(this,"v",void 0),c(this,"r",void 0),c(this,"color",void 0),c(this,"alpha",void 0),this.p=i,this.v=a,this.r=u,this.color=h,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,i=t.height,r=void 0===i?0:i,o=this.p,a=this.r,s=this.v;o.y-a>r&&(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,i=this.color,r=this.alpha;t.save(),t.beginPath(),t.arc(e.x,e.y,n,0,2*Math.PI),t.closePath(),t.fillStyle=i,t.globalAlpha=r,t.fill(),t.restore()}}])}();function g(t){if(!t)throw Error("[let-it-go] ".concat(arguments.length>1&&void 0!==arguments[1]?arguments[1]:"internal error."))}function m(t){g(Array.isArray(t),"Range must be an array."),g(2===t.length,"Range size must be 2."),g(t.every((function(t){return"number"==typeof t})),"Range value must be a number.")}function w(t){m(t),g(t.every((function(t){return t>=0})),"Radius range value must be positive.")}function k(t){m(t),g(t.every((function(t){return t>=0&&t<=1})),"Alpha range value must be from 0 to 1.")}function E(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=f(e,2),i=n[0],r=n[1];t.style[i]=r||""}))}var A,O={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"}},j=new WeakMap,M=new WeakMap,I=new WeakMap,S=new WeakMap,P=new WeakMap,T=new WeakMap,x=new WeakMap,W=new WeakMap,N=new WeakMap,C=new WeakMap,_=new WeakMap,F=new WeakMap,L=new WeakSet,U=new WeakMap,D=new WeakMap,V=function(){return l((function t(){var e,u,l=this,h=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},f=h.root,v=void 0===f?O.root:f,d=h.number,p=void 0===d?O.number:d,y=h.velocityXRange,b=void 0===y?O.velocityXRange:y,g=h.velocityYRange,E=void 0===g?O.velocityYRange:g,R=h.radiusRange,A=void 0===R?O.radiusRange:R,V=h.color,G=void 0===V?O.color:V,q=h.alphaRange,H=void 0===q?O.alphaRange:q,$=h.backgroundColor,B=void 0===$?O.backgroundColor:$,J=h.style,K=void 0===J?O.style:J;r(this,t),i(e=this,u=L),u.add(e),c(this,"root",document.body),a(this,j,!1),a(this,M,0),a(this,I,void 0),a(this,S,void 0),a(this,P,void 0),a(this,T,void 0),a(this,x,void 0),c(this,"backgroundColor",void 0),c(this,"style",O.style),c(this,"canvas",document.createElement("canvas")),a(this,W,void 0),a(this,N,[]),a(this,C,null),a(this,_,null),a(this,F,null),a(this,U,(function(){o(N,l).forEach((function(t){return t.update(l.canvas)}))})),a(this,D,(function(){if(o(j,l)){var t=l.canvas,e=t.width,n=t.height;o(W,l).clearRect(0,0,e,n),o(W,l).fillStyle=l.backgroundColor,o(W,l).fillRect(0,0,e,n),o(N,l).forEach((function(t){return t.draw(o(W,l))})),s(_,l,requestAnimationFrame(o(D,l)))}})),m(b),m(E),w(A),k(H),this.root=v,s(M,this,p),s(I,this,b.sort()),s(S,this,E.sort()),s(P,this,A.sort()),s(T,this,G),s(x,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.");s(W,this,Q),n(L,this,z).call(this),n(L,this,X).call(this),n(L,this,Y).call(this)}),[{key:"number",get:function(){return o(M,this)},set:function(t){g(t>=0,"Number must be positive"),s(M,this,t),n(L,this,X).call(this)}},{key:"velocityXRange",get:function(){return o(I,this)},set:function(t){m(t);var e=t.sort();s(I,this,e),o(N,this).forEach((function(t){t.v.x=E.apply(void 0,v(e))}))}},{key:"velocityYRange",get:function(){return o(S,this)},set:function(t){m(t);var e=t.sort();s(S,this,e),o(N,this).forEach((function(t){t.v.y=E.apply(void 0,v(e))}))}},{key:"radiusRange",get:function(){return o(P,this)},set:function(t){w(t);var e=t.sort();s(P,this,e),o(N,this).forEach((function(t){t.r=E.apply(void 0,v(e))}))}},{key:"color",get:function(){return o(T,this)},set:function(t){s(T,this,t),o(N,this).forEach((function(e){e.color=t}))}},{key:"alphaRange",get:function(){return o(x,this)},set:function(t){k(t);var e=t.sort();s(x,this,e),o(N,this).forEach((function(t){t.alpha=E.apply(void 0,v(e))}))}},{key:"letItStop",value:function(){s(j,this,!1),o(C,this)&&(clearInterval(o(C,this)),s(C,this,null)),o(_,this)&&(cancelAnimationFrame(o(_,this)),s(_,this,null))}},{key:"letItGoAgain",value:function(){n(L,this,Y).call(this)}},{key:"clear",value:function(){this.letItStop(),s(N,this,[]),o(F,this)&&(o(F,this).disconnect(),s(F,this,null)),this.canvas.parentNode&&this.root.removeChild(this.canvas),o(W,this).clearRect(0,0,this.canvas.width,this.canvas.height),s(M,this,0)}}])}();function z(){var t=this;try{var e=new ResizeObserver((function(e){var n,i=function(t,e){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(!n){if(Array.isArray(t)||(n=p(t))||e&&t&&"number"==typeof t.length){n&&(t=n);var i=0,r=function(){};return{s:r,n:function(){return i>=t.length?{done:!0}:{done:!1,value:t[i++]}},e:function(t){throw t},f:r}}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(i.s();!(n=i.n()).done;){var r=n.value;t.canvas.width=r.contentRect.width,t.canvas.height=r.contentRect.height}}catch(t){i.e(t)}finally{i.f()}}));e.observe(this.root),s(F,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?h(Object(n),!0).forEach((function(e){c(t,e,n[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(n)):h(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 X(){var t=this,e=this.canvas;s(N,this,Array.from({length:o(M,this)},(function(){return new b({p:new y(E(0,e.width),E(0,-e.height)),v:new y(E.apply(void 0,v(o(I,t)))||Number.MIN_VALUE,E.apply(void 0,v(o(S,t)))||Number.MIN_VALUE),r:E.apply(void 0,v(o(P,t)))||Number.MIN_VALUE,color:o(T,t),alpha:E.apply(void 0,v(o(x,t)))||Number.MIN_VALUE})})))}function Y(){o(j,this)||(s(C,this,setInterval(o(U,this),A.FRAME_INTERVAL)),s(_,this,requestAnimationFrame(o(D,this))),s(j,this,!0))}A=V,c(V,"DEFAULT_OPTIONS",O),c(V,"FRAME_RATE",30),c(V,"FRAME_INTERVAL",1e3/A.FRAME_RATE),t.DEFAULT_OPTIONS=O,t.LetItGo=V,t.default=V,Object.defineProperty(t,"__esModule",{value:!0})}));