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

1 line
2.3 KiB
JavaScript

$(function(){function t(t){function e(){w=document.createElement("canvas"),v=w.getContext("2d"),w.style.top="0px",w.style.left="0px",w.style.pointerEvents="none",r?(w.style.position="absolute",u.appendChild(w),w.width=u.clientWidth,w.height=u.clientHeight):(w.style.position="fixed",u.appendChild(w),w.width=f,w.height=x),v.font="21px serif",v.textBaseline="middle",v.textAlign="center",d.forEach(t=>{let e=v.measureText(B),i=document.createElement("canvas"),n=i.getContext("2d");i.width=e.width,i.height=e.actualBoundingBoxAscent+e.actualBoundingBoxDescent,n.fillStyle=t,n.textAlign="center",n.font="21px serif",n.textBaseline="middle",n.fillText(B,i.width/2,e.actualBoundingBoxAscent),m.push(i)}),i(),a()}function i(){u.addEventListener("mousemove",h),u.addEventListener("touchmove",o),u.addEventListener("touchstart",o),window.addEventListener("resize",n)}function n(t){f=window.innerWidth,x=window.innerHeight,r?(w.width=u.clientWidth,w.height=u.clientHeight):(w.width=f,w.height=x)}function o(t){if(t.touches.length>0)for(let e=0;e<t.touches.length;e++)s(t.touches[e].clientX,t.touches[e].clientY,m[Math.floor(Math.random()*m.length)])}function h(t){window.requestAnimationFrame(()=>{if(r){const e=u.getBoundingClientRect();p.x=t.clientX-e.left,p.y=t.clientY-e.top}else p.x=t.clientX,p.y=t.clientY;const e=Math.hypot(p.x-g.x,p.y-g.y);e>1.5&&(s(p.x,p.y,m[Math.floor(Math.random()*d.length)]),g.x=p.x,g.y=p.y)})}function s(t,e,i){y.push(new c(t,e,i))}function l(){v.clearRect(0,0,f,x);for(let t=0;t<y.length;t++)y[t].update(v);for(let t=y.length-1;t>=0;t--)y[t].lifeSpan<0&&y.splice(t,1)}function a(){l(),requestAnimationFrame(a)}function c(t,e,i){const n=Math.floor(30*Math.random()+60);this.initialLifeSpan=n,this.lifeSpan=n,this.velocity={x:(Math.random()<.5?-1:1)*(Math.random()/2),y:.7*Math.random()+.9},this.position={x:t,y:e},this.canv=i,this.update=function(t){this.position.x+=this.velocity.x,this.position.y+=this.velocity.y,this.lifeSpan--,this.velocity.y+=.02;const e=Math.max(this.lifeSpan/this.initialLifeSpan,0);t.drawImage(this.canv,this.position.x-this.canv.width/2*e,this.position.y-this.canv.height/2,this.canv.width*e,this.canv.height*e)}}let d=t&&t.colors||["#D61C59","#E7D84B","#1B8798"],r=t&&t.element,u=r||document.body,f=window.innerWidth,x=window.innerHeight;const p={x:f/2,y:f/2},g={x:f/2,y:f/2},y=[],m=[];let w,v;const B="*";e()}new t});