Jony/assets/cursor/cursor8.js
2021-03-04 12:53:13 +08:00

1 line
2.7 KiB
JavaScript

$(function(){function t(t){function e(){a=document.createElement("canvas"),v=a.getContext("2d"),a.style.top="0px",a.style.left="0px",a.style.pointerEvents="none",w?(a.style.position="absolute",f.appendChild(a),a.width=f.clientWidth,a.height=f.clientHeight):(a.style.position="fixed",document.body.appendChild(a),a.width=M,a.height=W),v.font="16px serif",v.textBaseline="middle",v.textAlign="center";let i=v.measureText(p),t=document.createElement("canvas"),e=t.getContext("2d");t.width=i.width,t.height=2*i.actualBoundingBoxAscent,e.textAlign="center",e.font="16px serif",e.textBaseline="middle",e.fillText(p,t.width/2,i.actualBoundingBoxAscent),u=t;let o=0;for(o=0;o<r;o++)q[o]=new x(u);n(),h()}function n(){f.addEventListener("mousemove",l),f.addEventListener("touchmove",c),f.addEventListener("touchstart",c),window.addEventListener("resize",o)}function o(i){M=window.innerWidth,W=window.innerHeight,w?(a.width=f.clientWidth,a.height=f.clientHeight):(a.width=M,a.height=W)}function c(i){if(i.touches.length>0)if(w){const t=f.getBoundingClientRect();L.x=i.touches[0].clientX-t.left,L.y=i.touches[0].clientY-t.top}else L.x=i.touches[0].clientX,L.y=i.touches[0].clientY}function l(i){if(w){const t=f.getBoundingClientRect();L.x=i.clientX-t.left,L.y=i.clientY-t.top}else L.x=i.clientX,L.y=i.clientY}function s(){for(a.width=a.width,q[0].position.x=L.x,q[0].position.y=L.y,i=1;i<r;i++){let t=new d(0,0);i>0&&y(i-1,i,t),i<r-1&&y(i+1,i,t);let e,n,o=new d(-q[i].velocity.x*b,-q[i].velocity.y*b),c=new d((t.X+o.X)/Y,(t.Y+o.Y)/Y+B);q[i].velocity.x+=g*c.X,q[i].velocity.y+=g*c.Y,Math.abs(q[i].velocity.x)<E&&Math.abs(q[i].velocity.y)<E&&Math.abs(c.X)<C&&Math.abs(c.Y)<C&&(q[i].velocity.x=0,q[i].velocity.y=0),q[i].position.x+=q[i].velocity.x,q[i].position.y+=q[i].velocity.y,e=a.clientHeight,n=a.clientWidth,q[i].position.y>=e-A-1&&(q[i].velocity.y>0&&(q[i].velocity.y=H*-q[i].velocity.y),q[i].position.y=e-A-1),q[i].position.x>=n-A&&(q[i].velocity.x>0&&(q[i].velocity.x=H*-q[i].velocity.x),q[i].position.x=n-A-1),q[i].position.x<0&&(q[i].velocity.x<0&&(q[i].velocity.x=H*-q[i].velocity.x),q[i].position.x=0),q[i].draw(v)}}function h(){s(),requestAnimationFrame(h)}function d(i,t){this.X=i,this.Y=t}function y(i,t,e){let n=q[i].position.x-q[t].position.x,o=q[i].position.y-q[t].position.y,c=Math.sqrt(n*n+o*o);if(c>m){let i=X*(c-m);e.X+=n/c*i,e.Y+=o/c*i}}function x(i){this.position={x:L.x,y:L.y},this.velocity={x:0,y:0},this.canv=i,this.draw=function(i){i.drawImage(this.canv,this.position.x-this.canv.width/2,this.position.y-this.canv.height/2,this.canv.width,this.canv.height)}}let a,v,u,p=t&&t.emoji||"🤪",w=t&&t.element,f=w||document.body,r=7,g=.01,m=10,X=10,Y=1,B=50,b=10,E=.1,C=.1,A=11,H=.7,M=window.innerWidth,W=window.innerHeight,L={x:M/2,y:M/2},q=[];e()}new t});