-
Notifications
You must be signed in to change notification settings - Fork 0
/
tail.min.js
1 lines (1 loc) · 7.53 KB
/
tail.min.js
1
!function(t,e){t.Tail=e()}(window,function(){"use strict";function t(t){this.m_Canvas=document.getElementById(t.canvas_id),this.m_nRootX=void 0!==t.root_pos_x?t.root_pos_x:0,this.m_nRootY=void 0!==t.root_pos_y?t.root_pos_y:this.m_Canvas.height/2,this.m_cSegments=Math.max(2,void 0!==t.segment_count?t.segment_count:70),this.m_flTailLengthPercentage=void 0!==t.tail_length?t.tail_length:.5,this.m_strFillColor=void 0!==t.fill_color?t.fill_color:"black",this.m_strStrokeColor=void 0!==t.stroke_color?t.stroke_color:null,this.m_flStrokeWidth=void 0!==t.stroke_width?t.stroke_width:0,this.m_bCollideWithFloor=void 0!==t.collide_with_floor?t.collide_with_floor:!0,this.m_fnGetBaseAngle=void 0!==t.get_base_angle_func?t.get_base_angle_func:this.GetBaseAngle_DefaultImpl_,this.m_fnGetMaxAngle=void 0!==t.get_max_angle_func?t.get_max_angle_func:this.GetMaxAngle_DefaultImpl_,this.m_fnGetPhase=void 0!==t.get_phase_func?t.get_phase_func:this.GetPhase_DefaultImpl_,this.m_fnGetCurlynessPoints=void 0!==t.get_curlyness_points_func?t.get_curlyness_points_func:this.GetCurlynessPoints_DefaultImpl_,this.m_fnGetCurlynessValues=void 0!==t.get_curlyness_values_func?t.get_curlyness_values_func:this.GetCurlynessValues_DefaultImpl_,this.m_fnGetTailWidthPoints=void 0!==t.get_tail_width_points_func?t.get_tail_width_points_func:this.GetTailWidthPoints_DefaultImpl_,this.m_fnGetTailWidthValues=void 0!==t.get_tail_width_values_func?t.get_tail_width_values_func:this.GetTailWidthValues_DefaultImpl_,this.m_fnFrantic=void 0!==t.frantic_func?t.frantic_func:this.GetFrantic_DefaultImpl_,this.m_bAnimate=void 0!==t.animate?t.animate:!0,this.m_flNoiseBase=1e4*Math.random(),this.m_bFirstFrame=!0,this.m_Noise=new SimplexNoise,this.m_Canvas||console.error("Canvas with ID '"+t.canvas_id+"' not found!")}return t.prototype.BIsVisible=function(){return CatUtils.BIsInViewport(this.m_Canvas)},t.prototype.Render=function(){if(this.m_Canvas&&this.BIsVisible()){var t=this.m_Canvas.getContext("2d");t.save();var e=this.m_bAnimate?CatUtils.GetTime():0,i=this.m_Canvas.width,s=this.m_Canvas.height,n=this.m_strStrokeColor&&this.m_strStrokeColor.length&&this.m_flStrokeWidth>1;t.clearRect(0,0,i,s);var o=i/this.m_cSegments*this.m_flTailLengthPercentage;t.lineCap="round",(this.m_fnGetCurlynessPoints(e).length!==this.m_fnGetCurlynessValues(e).length||this.m_fnGetTailWidthPoints(e).length!==this.m_fnGetTailWidthValues(e).length)&&console.error("Error: These need to be the same size, and both sorted in ascending order.");for(var r=n?2:1,a=0;r>a;++a){var h=r>1&&0==a,l=0,_=this.m_nRootX,m=this.m_nRootY;t.save(),t.translate(this.m_nRootX,this.m_nRootY);for(var u=0;u<this.m_cSegments;++u){var f=u/this.m_cSegments,p=this.GetTailWidth_(e,f),c=this.m_Noise.noise(this.m_flNoiseBase+1e4+e+.5*f,100),d=this.GetCurlyness_(e,f),g=this.m_fnFrantic(e,f),v=this.m_fnGetMaxAngle(e,f),C=CatUtils.DegreesToRadians(this.m_fnGetBaseAngle(e))+d*Math.min(CatUtils.DegreesToRadians(Math.sin(this.m_fnGetPhase(e,f)+e+2*Math.PI*f*c*g)*v),v),G=_+o*Math.cos(C),y=m+o*Math.sin(C),I=!1;this.m_bCollideWithFloor&&y>this.m_nRootY&&(y=this.m_nRootY,I=!0),I&&(C=0);var M=C-l;l=C,t.rotate(M);var T,D;h?(T=p*this.m_flStrokeWidth,D=this.m_strStrokeColor):(T=p,D=this.m_strFillColor),t.lineWidth=T,t.strokeStyle=D,t.beginPath(),t.moveTo(0,0),t.lineTo(o,0),t.stroke(),u<this.m_cSegments-1&&t.translate(o,0),_=G,m=y}t.restore()}t.restore(),this.m_bFirstFrame=!1}},t.prototype.GetCurlyness_=function(t,e){var i=this.m_fnGetCurlynessPoints(t),s=this.m_fnGetCurlynessValues(t),n=CatUtils.BinarySearch(e,i);return CatUtils.Interpolate(e,i[n],i[n+1],s[n],s[n+1])},t.prototype.GetTailWidth_=function(t,e){var i=this.m_fnGetTailWidthPoints(t),s=this.m_fnGetTailWidthValues(t),n=CatUtils.BinarySearch(e,i);return CatUtils.Interpolate(e,i[n],i[n+1],s[n],s[n+1])},t.prototype.LogIfFirstFrame=function(t){this.m_bFirstFrame&&console.log(t)},t.prototype.GetBaseAngle_DefaultImpl_=function(){return 0},t.prototype.GetMaxAngle_DefaultImpl_=function(){return 20},t.prototype.GetPhase_DefaultImpl_=function(){return this.m_flNoiseBase},t.prototype.GetCurlynessPoints_DefaultImpl_=function(){return[0,.5,.85,1]},t.prototype.GetCurlynessValues_DefaultImpl_=function(){return[0,2,8,9]},t.prototype.GetTailWidthPoints_DefaultImpl_=function(){return[0,.1,.8,1]},t.prototype.GetTailWidthValues_DefaultImpl_=function(){return[45,40,25,15]},t.prototype.GetFrantic_DefaultImpl_=function(t,e){return.03+e*(.5+.5*Math.sin(.3*t))*.15},t});var CatUtils={GetTime:function(){return(new Date).getTime()/1e3},BIsInViewport:function(t){var e=window.innerWidth||document.documentElement.clientWidth,i=window.innerHeight||document.documentElement.clientHeight,s=t.getBoundingClientRect();return s.bottom>=0&&s.top<=i&&s.right>=0&&s.left<=e},Clamp:function(t,e,i){return Math.max(e,Math.min(i,t))},Interpolate:function(t,e,i,s,n){if(e==i)return t>=i?n:s;var o=CatUtils.Clamp((t-e)/(i-e),0,1);return s+(n-s)*CatUtils.SCurve(o)},SCurve:function(t){var e=t*t;return 3*e-2*e*t},DegreesToRadians:function(t){return t*Math.PI/180},BinarySearch:function(t,e){for(var i=parseInt(e.length/2);i>=0&&i<e.length;)if(t>=e[i]){if(t<=e[i+1])break;++i}else--i;return i}},SimplexNoise=function(){var t=function(){this.grad3=[[1,1,0],[-1,1,0],[1,-1,0],[-1,-1,0],[1,0,1],[-1,0,1],[1,0,-1],[-1,0,-1],[0,1,1],[0,-1,1],[0,1,-1],[0,-1,-1]],this.p=[];for(var t=0;256>t;t++)this.p[t]=Math.floor(256*Math.random());this.perm=[];for(var t=0;512>t;t++)this.perm[t]=this.p[255&t];this.simplex=[[0,1,2,3],[0,1,3,2],[0,0,0,0],[0,2,3,1],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,2,3,0],[0,2,1,3],[0,0,0,0],[0,3,1,2],[0,3,2,1],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,3,2,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[1,2,0,3],[0,0,0,0],[1,3,0,2],[0,0,0,0],[0,0,0,0],[0,0,0,0],[2,3,0,1],[2,3,1,0],[1,0,2,3],[1,0,3,2],[0,0,0,0],[0,0,0,0],[0,0,0,0],[2,0,3,1],[0,0,0,0],[2,1,3,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0],[2,0,1,3],[0,0,0,0],[0,0,0,0],[0,0,0,0],[3,0,1,2],[3,0,2,1],[0,0,0,0],[3,1,2,0],[2,1,0,3],[0,0,0,0],[0,0,0,0],[0,0,0,0],[3,1,0,2],[0,0,0,0],[3,2,0,1],[3,2,1,0]]};return t.prototype.dot=function(t,e,i){return t[0]*e+t[1]*i},t.prototype.noise=function(t,e){var i,s,n,o,r,a=.5*(Math.sqrt(3)-1),h=(t+e)*a,l=Math.floor(t+h),_=Math.floor(e+h),m=(3-Math.sqrt(3))/6,u=(l+_)*m,f=l-u,p=_-u,c=t-f,d=e-p;c>d?(o=1,r=0):(o=0,r=1);var g=c-o+m,v=d-r+m,C=c-1+2*m,G=d-1+2*m,y=255&l,I=255&_,M=this.perm[y+this.perm[I]]%12,T=this.perm[y+o+this.perm[I+r]]%12,D=this.perm[y+1+this.perm[I+1]]%12,w=.5-c*c-d*d;0>w?i=0:(w*=w,i=w*w*this.dot(this.grad3[M],c,d));var S=.5-g*g-v*v;0>S?s=0:(S*=S,s=S*S*this.dot(this.grad3[T],g,v));var W=.5-C*C-G*G;return 0>W?n=0:(W*=W,n=W*W*this.dot(this.grad3[D],C,G)),70*(i+s+n)},t.prototype.noise3d=function(t,e,i){var s,n,o,r,a,h,l,_,m,u,f=1/3,p=(t+e+i)*f,c=Math.floor(t+p),d=Math.floor(e+p),g=Math.floor(i+p),v=1/6,C=(c+d+g)*v,G=c-C,y=d-C,I=g-C,M=t-G,T=e-y,D=i-I;M>=T?T>=D?(a=1,h=0,l=0,_=1,m=1,u=0):M>=D?(a=1,h=0,l=0,_=1,m=0,u=1):(a=0,h=0,l=1,_=1,m=0,u=1):D>T?(a=0,h=0,l=1,_=0,m=1,u=1):D>M?(a=0,h=1,l=0,_=0,m=1,u=1):(a=0,h=1,l=0,_=1,m=1,u=0);var w=M-a+v,S=T-h+v,W=D-l+v,P=M-_+2*v,b=T-m+2*v,B=D-u+2*v,F=M-1+3*v,k=T-1+3*v,x=D-1+3*v,R=255&c,V=255&d,U=255&g,A=this.perm[R+this.perm[V+this.perm[U]]]%12,N=this.perm[R+a+this.perm[V+h+this.perm[U+l]]]%12,Y=this.perm[R+_+this.perm[V+m+this.perm[U+u]]]%12,E=this.perm[R+1+this.perm[V+1+this.perm[U+1]]]%12,L=.6-M*M-T*T-D*D;0>L?s=0:(L*=L,s=L*L*this.dot(this.grad3[A],M,T,D));var X=.6-w*w-S*S-W*W;0>X?n=0:(X*=X,n=X*X*this.dot(this.grad3[N],w,S,W));var q=.6-P*P-b*b-B*B;0>q?o=0:(q*=q,o=q*q*this.dot(this.grad3[Y],P,b,B));var H=.6-F*F-k*k-x*x;return 0>H?r=0:(H*=H,r=H*H*this.dot(this.grad3[E],F,k,x)),32*(s+n+o+r)},t}();