-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparty.min.js
1 lines (1 loc) · 13.1 KB
/
party.min.js
1
(function(t,e){"function"==typeof define&&define.amd?define("partyjs",[],e):"object"==typeof exports?module.exports=e():t.party=e()})(this,function(){function t(t,e){if(Array.isArray(e))A[t]=new k(e);else if("string"==typeof e){let r=new DOMParser,i=r.parseFromString(e,"application/xml"),o=i.getElementsByTagName("parsererror")[0];if(o)throw new Error("Invalid SVG shape.");var n;let s=i.getElementsByTagName("svg")[0];s&&s.hasAttribute("viewBox")&&(n=M.fromBounds(s.getAttribute("viewBox").split(" ").map(t=>parseFloat(t))));let a,h=i.getElementsByTagName("polygon")[0],l=i.getElementsByTagName("path")[0];if(h){let t=h.getAttribute("points"),e=/(-?\d*\.\d+|-?\d+)/g,n=t.match(e),r=[];for(let t=0;t<n.length;t+=2)r.push(new b(parseFloat(n[t]),parseFloat(n[t+1])));a=new k(r)}else if(l){let t=l.getAttribute("d"),e=/([A-Za-z]|-?\d*\.\d+|-?\d+)/g,n=t.match(e),r=[];for(let t=0;t<n.length;t++){let e=n[t],i=parseFloat(e);r.push(isNaN(i)?e:i)}a=new F(r)}if(!a)throw new Error("No shape was determined from the SVG.");a.normalize(n),A[t]=a}}function e(){return Math.random()}function n(t,n){return r(t,n,e())}function r(t,e,n){return(1-n)*t+n*e}function i(t,e){return t*n(1-e/2,1+e/2)}function o(t,e){return t+n(-e/2,e/2)}function s(t){for(let e in arguments)e>0&&(t=t.replace(new RegExp("\\{"+(e-1)+"\\}","g"),arguments[e]));return t}function a(t){if(!(t instanceof E))throw new TypeError("Invalid transform supplied to lighting calculation.");return Math.abs(Math.cos(t.rotation.x)*Math.cos(t.rotation.y))}function h(t,e,n){return t&&null!=t[e]?t[e]:n}function l(t,e,n){t&&null==t[e]&&(t[e]=n)}function c(t,e){for(var n in e)e.hasOwnProperty(n)&&l(t,n,e[n])}function u(t){if(["number","string","bigint","boolean","undefined"].includes(typeof t))return t;if("function"==typeof t)return t();if(Array.isArray(t))return t[Math.floor(e()*t.length)];throw new Error("Invalid randomized value")}function d(t,n,r){let s=u(h(n,"count",1)),l=u(h(n,"spread",0)),c=u(h(n,"angle",0));for(let d=0;d<s;d++){let s=o(c,l)*V,d=u(h(n,"velocity",0)),p=u(h(n,"angularVelocity",0)),m=u(h(n,"size",8));f({shape:u(h(n,"shape","rectangle")),acceleration:new E(new b(0,h(n,"gravity",!0)*g.gravityPixels)),velocity:new E(new b(Math.sin(s),Math.cos(s)).scale(d),b.generate(()=>u(p))),transform:new E(new b((t.left||0)+i((t.width||0)/2,2*h(n,"randomizePosition",!0))+(r?window.scrollX:0),(t.top||0)+i((t.height||0)/2,2*h(n,"randomizePosition",!0))+(r?window.scrollY:0)),b.generate(()=>Math.PI*h(n,"randomizeRotation",!0)*e()),b.one.scale(m)),color:u(h(n,"color",()=>C.fromHsl(360*e(),100,70).toString())),lighting:h(n,"lighting",!0),lifetime:0,draw:function(t){t.fillStyle=this.lighting?new C(0,0,0).mix(C.fromHex(this.color),.25+.75*a(this.transform)).toString():this.color;const e=.2;let n=this.lifetime>e?1:this.lifetime/e,r=A[this.shape];if(!r)throw Error(`Unknown shape '${this.shape}'.`);let i=new E(this.transform.position,this.transform.rotation,this.transform.scale.scale(n));r.withTransform(i).draw(t)},update:function(t){this.velocity=this.velocity.applyDelta(this.acceleration,t),this.transform=this.transform.applyDelta(this.velocity,t),this.lifetime+=t}})}}function f(t){for(;B.length>=g.maxParticles;)B.shift();B.push(t)}function p(t){B.forEach(e=>e.update(t));let e=Math.max(document.documentElement.offsetHeight,window.innerHeight);B=B.filter(t=>t.transform.position.y<=e)}function m(){P.canvas.width=window.innerWidth,P.canvas.height=window.innerHeight,P.clearRect(0,0,P.canvas.width,P.canvas.height),B.forEach(t=>t.draw(P))}function w(t){if(!P.canvas.parentElement)return;let e=(t-I)/1e3;B.length>0&&(p(e),m()),I=t,window.requestAnimationFrame(w)}function y(){if(document.getElementById("party-js-canvas"))return;const t=document.createElement("canvas");t.id="party-js-canvas",t.style="position: fixed; left: 0; top: 0; pointer-events: none; z-index: 99999;",P=t.getContext("2d"),document.body?document.body.appendChild(t):window.addEventListener("load",()=>document.body.appendChild(t)),window.requestAnimationFrame(w)}const g={maxParticles:1e3,gravityPixels:800},x={typeCheckFailed:"The supplied parameter must be of type '{0}'.",abstractMethodNotImplemented:"The type is required to implement the '{0}' method.",invalidPathNode:"Invalid node '{0}' detected in SVG path.",malformedPathNode:"Malformed node '{0}' detected in SVG path."};class v{constructor(t){this.index=0,this.items=t}first(){return this.reset(),this.next()}next(){return this.items[this.index++]}hasNext(){return this.index<this.items.length}reset(){this.index=0}each(t){for(let e=this.first();this.hasNext();e=this.next())t(e)}}class C{constructor(t,e,n){this.r=t,this.g=e,this.b=n}mix(t,e){if(!(t instanceof C))throw new TypeError(s(x.typeCheckFailed,"Color"));return null==e&&(e=.5),new C(r(this.r,t.r,e),r(this.g,t.g,e),r(this.b,t.b,e))}toString(){function t(t){return Math.round(255*t).toString(16).padStart(2,"0")}return`#${t(this.r)}${t(this.g)}${t(this.b)}`}static fromHex(t){return t.startsWith("#")&&(t=t.substring(1)),new C(parseInt(t.substring(0,2),16)/255,parseInt(t.substring(2,4),16)/255,parseInt(t.substring(4,6),16)/255)}static fromHsl(t,e,n){let r,i,o;if(t/=360,e/=100,n/=100,0===e)r=i=o=n;else{const s=(t,e,n)=>(n<0&&(n+=1),n>1&&(n-=1),n<1/6?t+6*(e-t)*n:n<.5?e:n<2/3?t+(e-t)*(2/3-n)*6:t),a=n<.5?n*(1+e):n+e-n*e,h=2*n-a;r=s(h,a,t+1/3),i=s(h,a,t),o=s(h,a,t-1/3)}return new C(r,i,o)}}class b{constructor(t,e,n){this.x=t||0,this.y=e||0,this.z=n||0}add(t){if(!(t instanceof b))throw new TypeError(s(x.typeCheckFailed,"Vector"));return new b(this.x+t.x,this.y+t.y,this.z+t.z)}scale(t){if("number"==typeof t)return new b(this.x*t,this.y*t,this.z*t);if(t instanceof b)return new b(this.x*t.x,this.y*t.y,this.z*t.z);throw new TypeError(s(x.typeCheckFailed,"Number/Vector"))}static get zero(){return new b}static get one(){return new b(1,1,1)}static generate(t){if("function"!=typeof t)throw new TypeError(s(x.typeCheckFailed,"Function"));return new b(t(),t(),t())}}class E{constructor(t,e,n){this.position=t||b.zero,this.rotation=e||b.zero,this.scale=n||b.zero}applyDelta(t,e){if(!(t instanceof E))throw new TypeError(s(x.typeCheckFailed,"Transform"));if("number"!=typeof e)throw new TypeError(s(x.typeCheckFailed,"Number"));return new E(this.position.add(t.position.scale(e)),this.rotation.add(t.rotation.scale(e)),this.scale.add(t.scale.scale(e)))}apply(t){if(!(t instanceof b))throw new TypeError(s(x.typeCheckFailed,"Vector"));let e=t.x*this.scale.x,n=t.y*this.scale.y;return new b(this.position.x+(e*Math.cos(this.rotation.z)-n*Math.sin(this.rotation.z))*Math.cos(this.rotation.y),this.position.y+(e*Math.sin(this.rotation.z)+n*Math.cos(this.rotation.z))*Math.cos(this.rotation.x))}}class z{constructor(t,e,n,r){this.xmin=t,this.ymin=e,this.xmax=n,this.ymax=r}static fromVertices(t){let e=new z(1/0,1/0,0,0);for(let n=0;n<t.length;n++){let r=t[n];e.xmin=Math.min(r.x,e.xmin),e.ymin=Math.min(r.y,e.ymin),e.xmax=Math.max(r.x,e.xmax),e.ymax=Math.max(r.y,e.ymax)}return e}}class M{constructor(t,e,n,r,i){this.xmin=t,this.ymin=e,this.width=i?n:Math.max(n,r),this.height=i?r:Math.max(n,r)}transformX(t){return(t-this.xmin-this.width/2)/this.width}transformY(t){return(t-this.ymin-this.height/2)/this.height}transformPoint(t){return new b(this.transformX(t.x),this.transformY(t.y))}static fromBounds(t){return new M(t.xmin,t.ymin,t.xmax-t.xmin,t.ymax-t.ymin)}}class T{withTransform(t){if(!(t instanceof E))throw new TypeError(s(x.typeCheckFailed,"Transform"));return this.transform=t,this}getBounds(){throw new Error(s(x.abstractMethodNotImplemented,"getBounds()"))}normalize(t){throw new Error(s(abstractMethodNotImplemented,"normalize(viewBox)"))}draw(t){throw new Error(s(abstractMethodNotImplemented,"draw(context)"))}}class k extends T{constructor(t){if(super(),!Array.isArray(t))throw new TypeError(s(x.typeCheckFailed,"Array"));this.points=t}getBounds(){return z.fromVertices(this.points)}normalize(t){if(t=t||M.fromBounds(this.getBounds()),!(t instanceof M))throw new TypeError(s(x.typeCheckFailed,"ViewBox"));this.points=this.points.map(e=>t.transformPoint(e))}draw(t){if(!(t instanceof CanvasRenderingContext2D))throw new TypeError(s(x.typeCheckFailed,"CanvasRenderingContext2D"));t.beginPath();for(let e=0;e<this.points.length;e++){let n=this.transform?this.transform.apply(this.points[e]):this.points[e];(0==e?t.moveTo:t.lineTo).apply(t,[n.x-window.scrollX,n.y-window.scrollY])}t.closePath(),t.fill()}}class F extends T{constructor(t){super(),this.nodes=[];let e=new b,n=new v(t);for(;n.hasNext();){let r=n.next();"string"!=typeof r&&(r=t.slice(0,n.index).reverse().find(t=>"string"==typeof t),"m"==r.toLowerCase()&&(r=r.toLowerCase()==r?"l":"L"),n.index--);let i,o,a=r.toLowerCase()==r,h=a?new b(e.x,e.y):new b;switch(r.toLowerCase()){case"m":i="move",o=[h.x+n.next(),h.y+n.next()],e.x=o[0],e.y=o[1];break;case"l":i="line",o=[h.x+n.next(),h.y+n.next()],e.x=o[0],e.y=o[1];break;case"h":i="line",o=[h.x+n.next(),e.y],e.x=o[0];break;case"v":i="line",o=[e.x,h.y+n.next()],e.y=o[1];break;case"z":i="line";let t=this.nodes.find(t=>"move"==t.type).getResultingCursor();o=[t.x,t.y],e.x=t.x,e.y=t.y;break;case"c":i="bezier",o=[];for(let t=0;t<3;t++)o.push(h.x+n.next()),o.push(h.y+n.next());e.x=o[o.length-2],e.y=o[o.length-1];break;case"s":i="bezier";let a=this.nodes[this.nodes.length-1];if("bezier"!=a.type)throw new Error(s(x.malformedPathNode,r));let l=e.x+(a.args[4]-a.args[2]),c=e.y+(a.args[5]-a.args[3]);o=[l,c];for(let t=0;t<2;t++)o.push(h.x+n.next()),o.push(h.y+n.next());e.x=o[o.length-2],e.y=o[o.length-1]}this.nodes.push(new N(i,o))}this.nodes.length>50&&console.warn("Complex shape registered, high usage may impact framerate.")}getBounds(){return z.fromVertices(this.nodes.map(t=>t.getResultingCursor()))}normalize(t){if(t=t||M.fromBounds(this.getBounds()),!(t instanceof M))throw new TypeError(s(x.typeCheckFailed,"ViewBox"));let e=new v(this.nodes);for(;e.hasNext();){let n=e.next();for(let e=0;e<n.args.length;e+=2)n.args[e]=t.transformX(n.args[e]),n.args[e+1]=t.transformY(n.args[e+1])}}draw(t){if(!(t instanceof CanvasRenderingContext2D))throw new TypeError(s(x.typeCheckFailed,"CanvasRenderingContext2D"));t.beginPath(),new v(this.nodes).each(e=>e.run(t,this.transform)),t.fill()}}class N{constructor(t,e){this.type=t,this.args=e}getResultingCursor(){return new b(this.args[this.args.length-2],this.args[this.args.length-1])}run(t,e){let n;switch(this.type){case"move":n=t.moveTo;break;case"line":n=t.lineTo;break;case"bezier":n=t.bezierCurveTo}let r=[];for(let t=0;t<this.args.length;t+=2){let n=new b(this.args[t],this.args[t+1]);e&&(n=e.apply(n)),r.push(n.x-window.scrollX),r.push(n.y-window.scrollY)}n.apply(t,r)}}var B=[],P=null;const A={square:new k([new b(-.5,.5),new b(.5,.5),new b(.5,-.5),new b(-.5,-.5)]),rectangle:new k([new b(-.3,1),new b(.3,1),new b(.3,-1),new b(-.3,-1)])};t("circle",'<path d="M0,1 C0.551915024494,1 1,0.551915024494 1,0 C1,-0.551915024494 0.551915024494,-1 0,-1 C-0.551915024494,-1 -1,-0.551915024494 -1,0 C-1,0.551915024494 -0.551915024494,1 0,1 Z"/>'),t("ellipse",'<path d="M0,0.5 C0.5,0.5 1,0.4 1,0 C1,-0.4 0.5,-0.5 0,-0.5 C-0.5,-0.5 -1,-0.4 -1,0 C-1,0.4 -0.5,0.5 0,0.5Z"/>'),t("rounded-square",'<path d="M-0.5,1 L0.5,1 C0.75,1 1,0.75 1,0.5 L1,-0.5 C1,-0.75 0.75,-1 0.5,-1 L-0.5,-1 C-0.75,-1 -1,-0.75 -1,-0.5 L-1,0.5 C-1,0.75 -0.75,1 -0.5,1 Z"/>'),t("rounded-rectangle",'<path d="M-0.6,0.5 L0.6,0.5 C0.8,0.5 1,0.3 1,0.1 L1,-0.1 C1,-0.3 0.8,-0.5 0.6,-0.5 L-0.6,-0.5 C-0.8,-0.5 -1,-0.3 -1,-0.1 L-1,0.1 C-1,0.3 -0.8,0.5 -0.6,0.5 Z"/>'),t("star",'<polygon points="512,197.816 325.961,185.585 255.898,9.569 185.835,185.585 0,197.816 142.534,318.842 95.762,502.431 255.898,401.21 416.035,502.431 369.263,318.842"/>');const V=Math.PI/180;var I=0;return y(),{init:y,area:function(t,e,n){d(t,e,null==n||n)},element:function(t,e){e=e||{},c(e,{shape:this.array(["square","rectangle"]),count:this.variation(40,.5),spread:this.constant(80),size:this.variation(10,.8),velocity:this.variation(-300,1),angularVelocity:this.minmax(1,6)}),this.area(t.getBoundingClientRect(),e)},position:function(t,e,n){n=n||{},c(n,{shape:this.array(["square","rectangle"]),count:this.variation(40,.5),spread:this.constant(80),size:this.variation(10,.8),velocity:this.variation(-300,1),angularVelocity:this.minmax(1,6)}),this.area({left:t,top:e},n)},cursor:function(t){let e=window.event;if(null==e.clientX||null==e.clientY)return console.error("Calling 'party.cursor()' with no current mouse event is not allowed.");this.position(e.clientX,e.clientY,t)},screen:function(t){t=t||{},c(t,{shape:this.array(["square","rectangle"]),count:this.variation(window.innerWidth/1980*500,.5),size:this.variation(10,.8),velocity:this.variation(-100,2),angularVelocity:this.minmax(1,6)}),this.area({width:window.innerWidth,height:-window.innerHeight},t)},registerShape:t,constant:function(t){return t},variation:function(t,e,n){if("number"!=typeof t||"number"!=typeof e)throw new TypeError(s(x.typeCheckFailed,"Number"));return()=>(n?o:i)(t,e)},minmax:function(t,e){if("number"!=typeof t||"number"!=typeof e)throw new TypeError(s(x.typeCheckFailed,"Number"));return()=>n(t,e)},array:function(t){if(!Array.isArray(t))throw new TypeError(s(x.typeCheckFailed,"Array"));return t},linearGradient:function(){if(!arguments||0==arguments.length)throw new Error;if(1==arguments.length)return arguments[0];var t=[...arguments].map(t=>C.fromHex(t));return()=>{let e=n(0,t.length-1),r=Math.floor(e),i=e%1;return t[r].mix(t[r+1],i).toString()}}}});