Jony/assets/cursor/cursor11.js

1 line
2.3 KiB
JavaScript
Raw Normal View History

2021-03-04 04:53:13 +00:00
$(function(){function t(t){function e(){d=document.createElement("canvas"),r=d.getContext("2d"),d.style.top="0px",d.style.left="0px",d.style.pointerEvents="none",f?(d.style.position="absolute",u.appendChild(d),d.width=u.clientWidth,d.height=u.clientHeight):(d.style.position="fixed",document.body.appendChild(d),d.width=x,d.height=g),r.font="12px serif",r.textBaseline="middle",r.textAlign="center",p.forEach(t=>{let e=r.measureText(t),i=document.createElement("canvas"),n=i.getContext("2d");i.width=e.width,i.height=2*e.actualBoundingBoxAscent,n.textAlign="center",n.font="12px serif",n.textBaseline="middle",n.fillText(t,i.width/2,e.actualBoundingBoxAscent),w.push(i)}),i(),l()}function i(){u.addEventListener("mousemove",h),u.addEventListener("touchmove",o),u.addEventListener("touchstart",o),window.addEventListener("resize",n)}function n(t){x=window.innerWidth,g=window.innerHeight,f?(d.width=u.clientWidth,d.height=u.clientHeight):(d.width=x,d.height=g)}function o(t){if(t.touches.length>0)for(let e=0;e<t.touches.length;e++)a(t.touches[e].clientX,t.touches[e].clientY,w[Math.floor(Math.random()*w.length)])}function h(t){if(f){const e=u.getBoundingClientRect();m.x=t.clientX-e.left,m.y=t.clientY-e.top}else m.x=t.clientX,m.y=t.clientY;a(m.x,m.y,w[Math.floor(Math.random()*p.length)])}function a(t,e,i){y.push(new c(t,e,i))}function s(){r.clearRect(0,0,x,g);for(let t=0;t<y.length;t++)y[t].update(r);for(let t=y.length-1;t>=0;t--)y[t].lifeSpan<0&&y.splice(t,1)}function l(){s(),requestAnimationFrame(l)}function c(t,e,i){const n=Math.floor(60*Math.random()+80);this.initialLifeSpan=n,this.lifeSpan=n,this.velocity={x:(Math.random()<.5?-1:1)*(Math.random()/2),y:1+Math.random()},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.x+=2*(Math.random()<.5?-1:1)/75,this.velocity.y-=Math.random()/300;const e=Math.max(this.lifeSpan/this.initialLifeSpan,0),i=2*this.lifeSpan,n=.0174533*i;t.translate(this.position.x,this.position.y),t.rotate(n),t.drawImage(this.canv,-this.canv.width/2*e,-this.canv.height/2,this.canv.width*e,this.canv.height*e),t.rotate(-n),t.translate(-this.position.x,-this.position.y)}}let d,r,f=t&&t.element,u=f||document.body,p=["❄️"],x=window.innerWidth,g=window.innerHeight,m={x:x/2,y:x/2},y=[],w=[];e()}new t});