mirror of
https://github.com/Sped0n/bridget.git
synced 2026-04-17 19:49:30 -07:00
1 line
5.5 KiB
JavaScript
1 line
5.5 KiB
JavaScript
import{W as e,c as t,i as n,l as s,s as a,a as i,n as o,d as c,b as r,e as d}from"./main.js";const l=new e([]),g=new e(!1),u=new e(!1),h=new e(!1),m=document.createElement("div"),v=document.createElement("div");function p(e){const t=e.clientX,n=e.clientY;m.style.transform=`translate3d(${t}px, ${n}px, 0)`}function f(e){v.innerText=e}let y,w,E=[],L={x:0,y:0},I=!1;function x(){return l.get().map((e=>e.i))}function W(){const e=x().slice(-a.get().trailLength);return e.slice(0,e.length-1)}function k(){const e=x();return e[e.length-1]}function U(){const e=l.get(),t=a.get();return c(e[e.length-1].i,t.length)}function H(){const e=l.get(),t=a.get();return r(e[e.length-1].i,t.length)}function b(e){if(g.get()||n.get())return;if(!I)return void T();const t={x:e.clientX,y:e.clientY};if(Math.hypot(t.x-L.x,t.y-L.y)>a.get().threshold){L=t,i();const e={i:a.get().index,...t};l.set([...l.get(),e].slice(-a.get().length))}}function N(){if(n.get())return;g.set(!0),n.set(!0);const e=k(),t=S([e])[0];A(S([e,U(),H()])),M(t);const s=y.timeline(),a=S(W());s.to(a,{y:"+=20",ease:w.easeIn,stagger:.075,duration:.3,delay:.1,opacity:0}),s.to(t,{x:0,y:0,ease:w.easeInOut,duration:.7,delay:.3}),s.to(t,{delay:.1,scale:1,ease:w.easeInOut}),s.then((()=>{n.set(!1)})).catch((e=>{console.log(e)}))}function O(){if(n.get())return;g.set(!1),n.set(!0),o.set("none"),B(S([...W(),k()]));const e=y.timeline(),t=S([k()])[0],s=S(W());e.to(t,{scale:.6,duration:.6,ease:w.easeInOut}),e.to(t,{delay:.3,duration:.7,ease:w.easeInOut,x:l.get()[l.get().length-1].x-window.innerWidth/2,y:l.get()[l.get().length-1].y-window.innerHeight/2}),e.to(s,{y:"-=20",ease:w.easeOut,stagger:-.1,duration:.3,opacity:1}),e.then((()=>{n.set(!1)})).catch((e=>{console.log(e)}))}function S(e){return e.map((e=>E[e]))}function A(e){e.forEach((e=>{e.src!==e.dataset.hiUrl&&(e.src=e.dataset.hiUrl,e.height=parseInt(e.dataset.hiImgH),e.width=parseInt(e.dataset.hiImgW))}))}function B(e){e.forEach((e=>{e.src!==e.dataset.loUrl&&(e.src=e.dataset.loUrl,e.height=parseInt(e.dataset.loImgH),e.width=parseInt(e.dataset.loImgW))}))}function M(e){e.complete?(e.classList.remove("hide"),h.set(!1)):(h.set(!0),e.addEventListener("load",(()=>{h.set(!1),e.classList.remove("hide")}),{once:!0,passive:!0}),e.addEventListener("error",(()=>{h.set(!1)}),{once:!0,passive:!0}))}function T(){s().then((e=>{y=e[0],w=e[1],I=!0})).catch((e=>{console.log(e)}))}const X=[t.dataset.prev,t.dataset.close,t.dataset.next],Y=t.dataset.loading+"...";let $="";function j(e){e===X[0]?F():e===X[1]?O():C()}function z(e){if(!g.get()&&!n.get())switch(e.key){case"ArrowLeft":F();break;case"Escape":O();break;case"ArrowRight":C()}}function C(){n.get()||(o.set("next"),l.set(l.get().map((e=>({...e,i:r(e.i,a.get().length)})))),i())}function F(){n.get()||(o.set("prev"),l.set(l.get().map((e=>({...e,i:c(e.i,a.get().length)})))),d())}function R(e){j(e),h.set(!1)}function q(e){$=e,f(e)}function D(e){h.get()||j(e)}function G(e){$=e,h.get()?f(Y):f(e)}function J(e){m.className="cursor",v.className="cursorInner",m.append(v),t.append(m),window.addEventListener("mousemove",p,{passive:!0}),u.addWatcher((e=>{e?m.classList.add("active"):m.classList.remove("active")})),function(e){!function(e){const n=document.createElement("div");n.className="stage";for(const t of e){const e=document.createElement("img");e.height=t.loImgH,e.width=t.loImgW,e.dataset.hiUrl=t.hiUrl,e.dataset.hiImgH=t.hiImgH.toString(),e.dataset.hiImgW=t.hiImgW.toString(),e.dataset.loUrl=t.loUrl,e.dataset.loImgH=t.loImgH.toString(),e.dataset.loImgW=t.loImgW.toString(),e.alt=t.alt,n.append(e)}t.append(n)}(e);const s=document.getElementsByClassName("stage").item(0);E=Array.from(s.getElementsByTagName("img")),E.forEach(((e,t)=>{t<5&&(e.src=e.dataset.loUrl),function(e,t,n={attributes:!0}){new MutationObserver(((e,n)=>{t(e,n)})).observe(e,n)}(e,((s,a)=>{s.every((s=>!(!g.get()&&!n.get()&&"style"===s.attributeName&&1===parseFloat(e.style.opacity)&&(t+5<E.length&&(E[t+5].src=E[t+5].dataset.loUrl),a.disconnect(),1))))}))})),s.addEventListener("click",(()=>{N()}),{passive:!0}),s.addEventListener("keydown",(()=>{N()}),{passive:!0}),window.addEventListener("mousemove",b,{passive:!0}),g.addWatcher((e=>{u.set(e&&!n.get())})),n.addWatcher((e=>{u.set(g.get()&&!e)})),l.addWatcher((e=>{!function(){const e=x();if(0===e.length||!I)return;const t=S(e);if(y.set(t,{x:e=>l.get()[e].x-window.innerWidth/2,y:e=>l.get()[e].y-window.innerHeight/2,opacity:e=>e+1+a.get().trailLength<=l.get().length?0:1,zIndex:e=>e,scale:.6}),g.get()){const e=S([k()])[0];e.classList.add("hide");const t=[];switch(o.get()){case"prev":t.push(U());break;case"next":t.push(H())}A(S(t)),M(e),y.set(E,{opacity:0}),y.set(e,{opacity:1,x:0,y:0,scale:1})}else B(t)}()})),window.addEventListener("mousemove",(()=>{T()}),{once:!0,passive:!0})}(e),function(){h.addWatcher((e=>{f(e?Y:$)}));const e=document.createElement("div");e.className="navOverlay";for(const[t,n]of X.entries()){const s=document.createElement("div");s.className="overlay",1===t?(s.addEventListener("click",(()=>{R(n)}),{passive:!0}),s.addEventListener("keydown",(()=>{R(n)}),{passive:!0}),s.addEventListener("mouseover",(()=>{q(n)}),{passive:!0}),s.addEventListener("focus",(()=>{q(n)}),{passive:!0})):(s.addEventListener("click",(()=>{D(n)}),{passive:!0}),s.addEventListener("keydown",(()=>{D(n)}),{passive:!0}),s.addEventListener("mouseover",(()=>{G(n)}),{passive:!0}),s.addEventListener("focus",(()=>{G(n)}),{passive:!0})),e.append(s)}u.addWatcher((()=>{u.get()?e.classList.add("active"):e.classList.remove("active")})),t.append(e),window.addEventListener("keydown",z,{passive:!0})}()}export{J as initDesktop}; |