creepyface-firefly
Version:
A point provider for [Creepyface](https://github.com/4lejandrito/creepyface) to make your face follow a firefly.
2 lines (1 loc) • 3.21 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(require("creepyface")):"function"==typeof define&&define.amd?define(["creepyface"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).creepyface)}(this,function(e){"use strict";function t(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}e=t(e);const n=1/60*1e3,o="undefined"!=typeof performance?()=>performance.now():()=>Date.now(),i="undefined"!=typeof window?e=>window.requestAnimationFrame(e):e=>setTimeout(()=>e(o()),n);const r=40;let c=!0,d=!1,a=!1;const s={delta:0,timestamp:0},u=["read","update","preRender","render","postRender"],f=u.reduce((e,t)=>(e[t]=function(o){let i=[],r=[],c=0,d=!1,e=!1;const a=new WeakSet,s={schedule:(e,t=!1,n=!1)=>{n=n&&d;const o=n?i:r;return t&&a.add(e),-1===o.indexOf(e)&&(o.push(e),n&&d&&(c=i.length)),e},cancel:e=>{var t=r.indexOf(e);-1!==t&&r.splice(t,1),a.delete(e)},process:t=>{if(d)e=!0;else{if(d=!0,[i,r]=[r,i],r.length=0,c=i.length)for(let e=0;e<c;e++){const n=i[e];n(t),a.has(n)&&(s.schedule(n),o())}d=!1,e&&(e=!1,s.process(t))}}};return s}(()=>d=!0),e),{}),p=u.reduce((e,t)=>{const o=f[t];return e[t]=(e,t=!1,n=!1)=>(d||v(),o.schedule(e,t,n)),e},{}),h=u.reduce((e,t)=>(e[t]=f[t].cancel,e),{}),l=(u.reduce((e,t)=>(e[t]=()=>f[t].process(s),e),{}),e=>f[e].process(s)),m=e=>{d=!1,s.delta=c?n:Math.max(Math.min(e-s.timestamp,r),1),s.timestamp=e,a=!0,u.forEach(l),a=!1,d&&(c=!1,i(m))},v=()=>{d=!0,c=!0,a||i(m)};function w(e,n){return e.map(function(e,t){return e+n[t]})}function y(e){return e?e<0?-1:1:0}function M(e){return e*Math.PI/180}function g(e){return e=Math.atan2(e[1],e[0]),180*(((t=2*Math.PI)+e%t)%t)/Math.PI;var t}function x(e){return Math.floor(Math.random()*e)}function b(e,t){return e+t}function E(e){return e*e}e.default.registerPointProvider("firefly",function(e){function t(e){return d.destination=[e.clientX,e.clientY]}function n(e){for(var t=[0,0],n=0;n<e.touches.length;n++)var o=e.touches[n],t=w(t,[o.clientX,o.clientY]);d.destination=t}var d={destination:[window.innerWidth/2,window.innerHeight/2],position:[x(window.innerWidth),x(window.innerHeight)],vspeed:[.3,.3]},o=document.createElement("img"),i=(document.body.appendChild(o),o.src="https://creepyface.io/firefly.png",o.style.zIndex="1000",o.style.position="fixed",o.style.width="3em",document.addEventListener("mousemove",t,!0),document.addEventListener("touchmove",n,!0),p.update(function(e){var n,t,e=e.delta,o=d.destination,i=d.position,r=d.vspeed,o=(n=i,o.map(function(e,t){return e-n[t]})),c=g(o)-g(r),r=(180<Math.abs(c)&&(c-=360*y(c)),r=r,c=y(c)*Math.min(.27*e,Math.abs(c)),[r[0]*Math.cos(M(c))-r[1]*Math.sin(M(c)),r[0]*Math.sin(M(c))+r[1]*Math.cos(M(c))]);Math.sqrt(o.map(E).reduce(b,0))>20*Math.random()?(d.position=w(i,(t=e,r.map(function(e){return e*t}))),d.vspeed=r):d.destination=[x(window.innerWidth-200)+100,x(window.innerHeight-200)+100]},!0)),r=p.render(function(){o.style.left="".concat(d.position[0],"px"),o.style.top="".concat(d.position[1],"px"),o.style.transform="rotate(".concat(g(d.vspeed)+90,"deg)"),e(d.position)},!0);return function(){h.update(i),h.render(r),document.removeEventListener("mousemove",t,!0),document.removeEventListener("touchmove",n,!0),o.remove()}})});