svg-scroll-draw 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/angular/index.cjs +3 -3
- package/dist/angular/index.d.mts +1 -1
- package/dist/angular/index.d.ts +1 -1
- package/dist/angular/index.mjs +3 -3
- package/dist/astro/index.cjs +3 -3
- package/dist/astro/index.d.mts +1 -1
- package/dist/astro/index.d.ts +1 -1
- package/dist/astro/index.mjs +3 -3
- package/dist/cdn/svg-scroll-draw.global.js +3 -3
- package/dist/group/index.cjs +2 -2
- package/dist/group/index.d.mts +1 -1
- package/dist/group/index.d.ts +1 -1
- package/dist/group/index.mjs +2 -2
- package/dist/index.cjs +3 -3
- package/dist/index.d.mts +31 -2
- package/dist/index.d.ts +31 -2
- package/dist/index.mjs +3 -3
- package/dist/nuxt/index.cjs +3 -3
- package/dist/nuxt/index.d.mts +1 -1
- package/dist/nuxt/index.d.ts +1 -1
- package/dist/nuxt/index.mjs +3 -3
- package/dist/react/index.cjs +3 -3
- package/dist/react/index.d.mts +1 -1
- package/dist/react/index.d.ts +1 -1
- package/dist/react/index.mjs +3 -3
- package/dist/solid/index.cjs +3 -3
- package/dist/solid/index.d.mts +1 -1
- package/dist/solid/index.d.ts +1 -1
- package/dist/solid/index.mjs +3 -3
- package/dist/svelte/index.cjs +3 -3
- package/dist/svelte/index.d.mts +1 -1
- package/dist/svelte/index.d.ts +1 -1
- package/dist/svelte/index.mjs +3 -3
- package/dist/timeline/index.cjs +1 -1
- package/dist/timeline/index.d.mts +1 -1
- package/dist/timeline/index.d.ts +1 -1
- package/dist/timeline/index.mjs +1 -1
- package/dist/vue/index.cjs +3 -3
- package/dist/vue/index.d.mts +1 -1
- package/dist/vue/index.d.ts +1 -1
- package/dist/vue/index.mjs +3 -3
- package/dist/web-component/index.cjs +3 -3
- package/dist/web-component/index.mjs +3 -3
- package/package.json +6 -2
package/dist/nuxt/index.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {defineComponent,ref,onMounted,onUnmounted,h}from'vue';var
|
|
2
|
-
<div style="position:absolute;${
|
|
3
|
-
<div style="position:absolute;${U?`left:${M}px;top:0;bottom:0;border-left:2px dashed #ef4444;`:`top:${M}px;left:0;right:0;border-top:2px dashed #ef4444;`}padding:2px 6px;color:#ef4444;background:rgba(0,0,0,.6)">\u25A0 end</div>`;}return document.body.appendChild(n),window.addEventListener("scroll",a,{passive:true}),a(),n}function Ve(e,r,o){let n=(r.match(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g)??[]).map(Number),a=0;return e.replace(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g,d=>{let m=parseFloat(d),M=n[a++]??m;return String(+(m+(M-m)*o).toFixed(4))})}function xe(e,r={}){if(typeof window>"u")return {destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>0};let o=window.matchMedia("(prefers-reduced-motion: reduce)").matches,{selector:n="path, polyline, line, polygon, rect, circle",speed:a=1,fade:d=false,easing:m="linear",trigger:M={},stagger:U=0,direction:y="forward",once:X=false,debug:Be=false,axis:S="y",scrollContainer:ce,autoReverse:Z=false,delay:me=0,strokeColor:R,strokeWidth:H,fillOpacity:V,waypoints:ee,velocityScale:te=false,threshold:qe=0,rootMargin:_e="0px",repeat:j=0,repeatDelay:Ae=0,morphTo:L,clip:pe,native:Ue=true,onProgress:de,onStart:ye,onComplete:J}=r,W=pe===true?"left":typeof pe=="string"?pe:false,re=typeof m=="function"?m:we[m]??we.linear,Te=ve(M.start??"top bottom"),De=ve(M.end??"bottom top"),b=typeof ce=="string"?document.querySelector(ce):ce??null,C=Array.isArray(R)?R[0]:null,$=Array.isArray(R)?R[1]:typeof R=="string"?R:null,E=Array.isArray(H)?H[0]:null,x=Array.isArray(H)?H[1]:typeof H=="number"?H:null,k=Array.isArray(V)?V[0]:null,A=Array.isArray(V)?V[1]:typeof V=="number"?V:null;function _(t){let s=t*100;switch(W){case "right":return `inset(0 0 0 ${100-s}%)`;case "top":return `inset(0 0 ${100-s}% 0)`;case "bottom":return `inset(${100-s}% 0 0 0)`;case "center":return `circle(${t*150}% at 50% 50%)`;default:return `inset(0 ${100-s}% 0 0)`}}let w=W?[]:Array.from(e.querySelectorAll(n)),g=[],O=[],F=0,I=0,P=false,G=false,N=0,ne=false,T=-1,oe=-1,z=false,se=0,K=0,ie,ge=null,le=new Set,ae=-1,Me=performance.now();function Q(){return b?S==="x"?b.scrollLeft:b.scrollTop:S==="x"?window.scrollX:window.scrollY}function Pe(){return b?S==="x"?b.clientWidth:b.clientHeight:S==="x"?window.innerWidth:window.innerHeight}function Le(){let t=e.getBoundingClientRect(),s,l,p;if(b){let v=b.getBoundingClientRect();s=S==="x"?t.left-v.left+b.scrollLeft:t.top-v.top+b.scrollTop,l=S==="x"?t.width:t.height,p=Q();}else s=S==="x"?t.left:t.top,l=S==="x"?t.width:t.height,p=Q();let B=$e({top:s,height:l},p,Pe(),Te,De);F=B.tStart,I=B.tEnd,Be&&process.env.NODE_ENV!=="production"&&(ge?.remove(),ge=et(F,I,S));}function Xe(t,s){if(e.style.setProperty("--scroll-draw-progress",String(t)),W){let l=s==="reverse"?1-t:t;e.style.clipPath=_(l);return}w.forEach((l,p)=>{l.style.strokeDashoffset=s==="reverse"?`${g[p]*t}`:`${g[p]*(1-t)}`,d&&(l.style.opacity=s==="reverse"?`${1-t}`:`${t}`),C&&$?l.style.stroke=Ee(C,$,t):$&&(l.style.stroke=$),E!==null&&x!==null?l.style.strokeWidth=`${E+(x-E)*t}`:x!==null&&(l.style.strokeWidth=`${x}`),k!==null&&A!==null?l.style.fillOpacity=`${k+(A-k)*t}`:A!==null&&(l.style.fillOpacity=`${A}`),L&&l.tagName.toLowerCase()==="path"&&O[p]&&l.setAttribute("d",Ve(O[p],L,t));});}function Ce(){if(e.style.setProperty("--scroll-draw-progress","0"),W){e.style.clipPath=_(0);return}w.forEach((t,s)=>{t.style.strokeDasharray=`${g[s]}`,t.style.strokeDashoffset=y==="reverse"?"0":`${g[s]}`,d?t.style.opacity=y==="reverse"?"1":"0":t.style.opacity="",C&&(t.style.stroke=C),E!==null&&(t.style.strokeWidth=`${E}`),k!==null&&(t.style.fillOpacity=`${k}`),L&&t.tagName.toLowerCase()==="path"&&O[s]&&t.setAttribute("d",O[s]);});}if(w.forEach(t=>{Ze(t);let s=Se(t);g.push(s),t.tagName.toLowerCase()==="path"?O.push(t.getAttribute("d")??""):O.push(""),o?(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=y==="reverse"?`${s}`:"0",d&&(t.style.opacity="1"),$&&(t.style.stroke=$),x!==null&&(t.style.strokeWidth=`${x}`),A!==null&&(t.style.fillOpacity=`${A}`),L&&t.tagName.toLowerCase()==="path"&&t.setAttribute("d",L)):(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=y==="reverse"?"0":`${s}`,d?t.style.opacity=y==="reverse"?"1":"0":t.style.opacity="",C&&(t.style.stroke=C),E!==null&&(t.style.strokeWidth=`${E}`),k!==null&&(t.style.fillOpacity=`${k}`));}),W){if(o)return e.style.clipPath=_(1),J?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};e.style.clipPath=_(0);}else if(o)return J?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};function je(){return !(Ue===false||!Ye()||!w.length||typeof m!="string"||!(m in He)||W||S!=="y"||b||a!==1||U!==0||X||Z||te!==false||L||ee||j||me>0||de||ye||J||R!=null||H!=null||V!=null||(M.start??"top bottom").trim()!=="top bottom"||(M.end??"bottom top").trim()!=="bottom top")}function Je(){let t=`svg-scroll-draw-${++Qe}`,s=y==="reverse"?"0":"var(--ssd-len)",l=y==="reverse"?"var(--ssd-len)":"0",p=`stroke-dashoffset:${s};`,B=`stroke-dashoffset:${l};`;d&&(p+=`opacity:${y==="reverse"?1:0};`,B+=`opacity:${y==="reverse"?0:1};`);let v=document.createElement("style");v.setAttribute("data-svg-scroll-draw",""),v.textContent=`@keyframes ${t}{from{${p}}to{${B}}}.${t}{animation-name:${t};animation-duration:auto;animation-timing-function:${He[m]};animation-fill-mode:both;animation-timeline:view();animation-range:cover 0% cover 100%;}`,document.head.appendChild(v);function i(f,D){f.style.setProperty("--ssd-len",String(g[D])),f.style.strokeDasharray=`${g[D]}`,f.style.strokeDashoffset="",f.style.opacity="",f.style.animationPlayState="",f.classList.add(t);}w.forEach(i);let u=false,h=-1;function c(){if(h>=0)return h;let f=e.getBoundingClientRect(),{tStart:D,tEnd:Y}=$e({top:f.top,height:f.height},Q(),Pe(),Te,De);return re(q(Q(),D,Y,a))}return {destroy(){w.forEach(f=>{f.classList.remove(t),f.style.removeProperty("--ssd-len"),f.style.animationPlayState="";}),v.remove();},replay(){u=false,h=-1,w.forEach(i);},pause(){u=true,w.forEach(f=>{f.style.animationPlayState="paused";});},resume(){u&&(u=false,w.forEach(f=>{f.style.animationPlayState="running";}));},seek(f){let D=Math.min(1,Math.max(0,f));h=D,u=true,w.forEach((Y,Oe)=>{Y.classList.remove(t),Y.style.strokeDashoffset=y==="reverse"?`${g[Oe]*D}`:`${g[Oe]*(1-D)}`,d&&(Y.style.opacity=y==="reverse"?`${1-D}`:`${D}`);});},getProgress(){return c()}}}if(je())return Je();Le();function ue(){if(!ne||z)return;let t=performance.now(),s=Q(),l=a;if(te!==false){let i=t-Me,u=i>0?Math.abs(s-(ae<0?s:ae))/i:0;l=a*Math.max(.2,1+u*(typeof te=="number"?te:1)*.04);}ae=s,Me=t;let p=Z?oe===-1||s>=oe?"forward":"reverse":y;oe=s;let B=I-F,v=true;if(W){let i=re(q(s,F,I,l));X&&!Z&&(T=Math.max(T,i),i=T),se=i,e.style.setProperty("--scroll-draw-progress",String(i));let u=p==="reverse"?1-i:i;e.style.clipPath=_(u),de?.(i),!G&&q(s,F,I,l)>0&&(G=true,ye?.()),i>=1&&!P?(P=true,J?.(),K<(j==="infinite"?1/0:j??0)&&(K++,ie=setTimeout(()=>{T=-1,G=false,P=false,e.style.clipPath=_(0);},Ae))):i<1&&!X&&(P=false),N=requestAnimationFrame(ue);return}if(w.forEach((i,u)=>{let h=u*U*B,c=re(q(s,F+h,I+h,l));X&&!Z&&(T=Math.max(T,c),c=T),se=c,i.style.strokeDashoffset=p==="reverse"?`${g[u]*c}`:`${g[u]*(1-c)}`,d&&(i.style.opacity=p==="reverse"?`${1-c}`:`${c}`),C&&$?i.style.stroke=Ee(C,$,c):$&&(i.style.stroke=$),E!==null&&x!==null?i.style.strokeWidth=`${E+(x-E)*c}`:x!==null&&(i.style.strokeWidth=`${x}`),k!==null&&A!==null?i.style.fillOpacity=`${k+(A-k)*c}`:A!==null&&(i.style.fillOpacity=`${A}`),L&&i.tagName.toLowerCase()==="path"&&O[u]&&i.setAttribute("d",Ve(O[u],L,c)),u===0&&(de?.(c),e.style.setProperty("--scroll-draw-progress",String(c))),c<1&&(v=false);}),ee){let i=re(q(s,F,I,l));for(let u in ee){let h=parseFloat(u);i>=h&&!le.has(h)&&(le.add(h),ee[u]?.());}}!G&&q(s,F,I,l)>0&&(G=true,ye?.()),v&&!P?(P=true,J?.(),K<(j==="infinite"?1/0:j??0)&&(K++,ie=setTimeout(()=>{T=-1,G=false,P=false,le.clear(),Ce();},Ae))):!v&&!X&&(P=false),N=requestAnimationFrame(ue);}let he=new IntersectionObserver(t=>{t.forEach(s=>{ne=s.isIntersecting,ne&&!z?N=requestAnimationFrame(ue):cancelAnimationFrame(N);});},{root:b??null,threshold:qe,rootMargin:_e}),be;function fe(){clearTimeout(be),be=setTimeout(()=>{w.forEach((t,s)=>{g[s]=Se(t),t.style.strokeDasharray=`${g[s]}`;}),Le();},150);}return window.addEventListener("resize",fe),window.addEventListener("orientationchange",fe),me>0?setTimeout(()=>he.observe(e),me):he.observe(e),{destroy(){cancelAnimationFrame(N),clearTimeout(ie),he.disconnect(),window.removeEventListener("resize",fe),window.removeEventListener("orientationchange",fe),clearTimeout(be),ge?.remove();},replay(){T=-1,oe=-1,ae=-1,G=false,P=false,K=0,z=false,le.clear(),clearTimeout(ie),Ce();},pause(){z=true,cancelAnimationFrame(N);},resume(){z&&(z=false,ne&&(N=requestAnimationFrame(ue)));},seek(t){let s=Math.min(1,Math.max(0,t));se=s,T=s,z=true,cancelAnimationFrame(N),Xe(s,y);},getProgress(){return se}}}function nt(e={}){let r=ref(null);return onMounted(()=>{if(!r.value)return;let o=xe(r.value,e);onUnmounted(()=>o.destroy());}),r}var ke=defineComponent({name:"ScrollDraw",props:{selector:{type:String},speed:{type:Number},fade:{type:Boolean},stagger:{type:Number},easing:{type:[String,Function]},direction:{type:String},trigger:{type:Object},onProgress:{type:Function},onStart:{type:Function},onComplete:{type:Function},once:{type:Boolean},debug:{type:Boolean}},setup(e,{slots:r}){let o=ref(null);return onMounted(()=>{if(!o.value)return;let n={};e.selector!=null&&(n.selector=e.selector),e.speed!=null&&(n.speed=e.speed),e.fade!=null&&(n.fade=e.fade),e.stagger!=null&&(n.stagger=e.stagger),e.easing!=null&&(n.easing=e.easing),e.direction!=null&&(n.direction=e.direction),e.trigger!=null&&(n.trigger=e.trigger),e.once!=null&&(n.once=e.once),e.debug!=null&&(n.debug=e.debug),e.onProgress!=null&&(n.onProgress=e.onProgress),e.onStart!=null&&(n.onStart=e.onStart),e.onComplete!=null&&(n.onComplete=e.onComplete);let a=xe(o.value,n);onUnmounted(()=>a.destroy());}),()=>h("div",{ref:o},r.default?.())}});function ct(){return {install(e){e.component("ScrollDraw",ke);}}}export{ke as ScrollDraw,ct as createScrollDrawPlugin,nt as useScrollDraw};
|
|
1
|
+
import {defineComponent,ref,onMounted,onUnmounted,h}from'vue';function Ke({bounces:e=3,decay:r=.5}={}){let n=Math.max(1,Math.round(e)),o=Math.max(.01,Math.min(.99,r)),u=Math.sqrt(o),c=0,p=[];for(let a=0;a<n;a++){let d=Math.pow(u,a);p.push(d),c+=d;}let g=[0],C=0;for(let a=0;a<n;a++)C+=p[a]/c,g.push(C);return a=>{if(a<=0)return 0;if(a>=1)return 1;for(let d=0;d<n;d++)if(a<=g[d+1]){let X=(a-g[d])/(g[d+1]-g[d]);if(d===0)return X*(2-X);let b=1-Math.pow(o,d);return b+(1-b)*(2*X-1)*(2*X-1)}return 1}}function Qe({amplitude:e=1,period:r=.4}={}){let n=Math.max(1,e),o=Math.max(.1,r),u=n<=1?o/4:o/(2*Math.PI)*Math.asin(1/n);return c=>c<=0?0:c>=1?1:n*Math.pow(2,-10*c)*Math.sin((c-u)*(2*Math.PI)/o)+1}var ve={linear:e=>e,"ease-in":e=>e*e,"ease-out":e=>e*(2-e),"ease-in-out":e=>e<.5?2*e*e:-1+(4-2*e)*e,spring:e=>1-Math.cos(e*Math.PI*2.5)*Math.pow(1-e,2.2),bounce:Ke(),elastic:Qe()};function Se(e="top bottom"){let r=e.trim();if(/^\d+(\.\d+)?%$/.test(r))return {element:"top",viewport:r};let[n="top",o="bottom"]=r.split(/\s+/).filter(Boolean);return {element:n,viewport:o}}function Fe(e,r,n,o){switch(o){case "top":return e+n;case "center":return e+n+r/2;case "bottom":return e+n+r;default:return e+n}}function Ne(e,r){if(/^\d+(\.\d+)?%$/.test(e))return r*(parseFloat(e)/100);switch(e){case "top":return 0;case "center":return r/2;case "bottom":return r;default:return r}}function xe(e){let r=e.tagName.toLowerCase();if(r==="rect"){let n=parseFloat(e.getAttribute("width")??"0"),o=parseFloat(e.getAttribute("height")??"0");return 2*(n+o)}if(r==="circle"){let n=parseFloat(e.getAttribute("r")??"0");return 2*Math.PI*n}return e.getTotalLength()}function Ye(e,r,n){return Math.min(n,Math.max(r,e))}function U(e,r,n,o){return n===r?0:Ye((e-r)/(n-r)*o,0,1)}function Ee(e,r,n,o,u){let c=Fe(e.top,e.height,r,o.element)-Ne(o.viewport,n),p=Fe(e.top,e.height,r,u.element)-Ne(u.viewport,n);return {tStart:c,tEnd:p}}function Re(e){let r=/^#([a-f\d])([a-f\d])([a-f\d])$/i.exec(e);if(r)return [parseInt(r[1]+r[1],16),parseInt(r[2]+r[2],16),parseInt(r[3]+r[3],16)];let n=/^#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(e);if(n)return [parseInt(n[1],16),parseInt(n[2],16),parseInt(n[3],16)];let o=/^rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i.exec(e);return o?[parseInt(o[1]),parseInt(o[2]),parseInt(o[3])]:null}function $e(e,r,n){let o=Re(e),u=Re(r);return !o||!u?e:`rgb(${Math.round(o[0]+(u[0]-o[0])*n)},${Math.round(o[1]+(u[1]-o[1])*n)},${Math.round(o[2]+(u[2]-o[2])*n)})`}function He(e,r){process.env.NODE_ENV!=="production"&&console.warn(`[svg-scroll-draw] ${e}`,r);}var We={linear:"linear","ease-in":"ease-in","ease-out":"ease-out","ease-in-out":"ease-in-out"},Ze=0;function et(){return typeof CSS<"u"&&typeof CSS.supports=="function"&&CSS.supports("animation-timeline: view()")}function tt(e){let r=e.getAttribute("stroke"),n=e.getAttribute("fill");!r||r==="none"?He("Element has no stroke \u2014 path will not be visible.",e):n&&n!=="none"&&n!=="transparent"&&He("Element has a fill \u2014 it may obscure the stroke animation.",e);}function rt(e,r,n){let o=document.createElement("div");o.setAttribute("data-svg-scroll-draw-debug",""),o.style.cssText="position:fixed;pointer-events:none;z-index:9999;font-family:monospace;font-size:11px;top:0;left:0;right:0;bottom:0;";function u(){let c=n==="x"?window.scrollX:window.scrollY,p=e-c,g=r-c,C=n==="x";o.innerHTML=`
|
|
2
|
+
<div style="position:absolute;${C?`left:${p}px;top:0;bottom:0;border-left:2px dashed #22c55e;`:`top:${p}px;left:0;right:0;border-top:2px dashed #22c55e;`}padding:2px 6px;color:#22c55e;background:rgba(0,0,0,.6)">\u25B6 start</div>
|
|
3
|
+
<div style="position:absolute;${C?`left:${g}px;top:0;bottom:0;border-left:2px dashed #ef4444;`:`top:${g}px;left:0;right:0;border-top:2px dashed #ef4444;`}padding:2px 6px;color:#ef4444;background:rgba(0,0,0,.6)">\u25A0 end</div>`;}return document.body.appendChild(o),window.addEventListener("scroll",u,{passive:true}),u(),o}function Ve(e,r,n){let o=(r.match(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g)??[]).map(Number),u=0;return e.replace(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g,c=>{let p=parseFloat(c),g=o[u++]??p;return String(+(p+(g-p)*n).toFixed(4))})}function Me(e,r={}){if(typeof window>"u")return {destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>0};let n=window.matchMedia("(prefers-reduced-motion: reduce)").matches,{selector:o="path, polyline, line, polygon, rect, circle",speed:u=1,fade:c=false,easing:p="linear",trigger:g={},stagger:C=0,direction:a="forward",once:d=false,debug:X=false,axis:b="y",scrollContainer:me,autoReverse:ee=false,delay:pe=0,strokeColor:W,strokeWidth:V,fillOpacity:B,waypoints:te,velocityScale:re=false,threshold:qe=0,rootMargin:_e="0px",repeat:J=0,repeatDelay:Ae=0,morphTo:O,clip:de,native:Ue=true,onProgress:ye,onStart:ge,onComplete:K}=r,G=de===true?"left":typeof de=="string"?de:false,ne=typeof p=="function"?p:ve[p]??ve.linear,De=Se(g.start??"top bottom"),Te=Se(g.end??"bottom top"),S=typeof me=="string"?document.querySelector(me):me??null,I=Array.isArray(W)?W[0]:null,$=Array.isArray(W)?W[1]:typeof W=="string"?W:null,M=Array.isArray(V)?V[0]:null,k=Array.isArray(V)?V[1]:typeof V=="number"?V:null,A=Array.isArray(B)?B[0]:null,D=Array.isArray(B)?B[1]:typeof B=="number"?B:null;function j(t){let s=t*100;switch(G){case "right":return `inset(0 0 0 ${100-s}%)`;case "top":return `inset(0 0 ${100-s}% 0)`;case "bottom":return `inset(${100-s}% 0 0 0)`;case "center":return `circle(${t*150}% at 50% 50%)`;default:return `inset(0 ${100-s}% 0 0)`}}let x=G?[]:Array.from(e.querySelectorAll(o)),w=[],F=[],N=0,R=0,L=false,z=false,H=0,oe=false,T=-1,se=-1,q=false,ie=0,Q=0,ae,he=null,le=new Set,ue=-1,Pe=performance.now();function Y(){return S?b==="x"?S.scrollLeft:S.scrollTop:b==="x"?window.scrollX:window.scrollY}function Le(){return S?b==="x"?S.clientWidth:S.clientHeight:b==="x"?window.innerWidth:window.innerHeight}function Ce(){let t=e.getBoundingClientRect(),s,l,h;if(S){let E=S.getBoundingClientRect();s=b==="x"?t.left-E.left+S.scrollLeft:t.top-E.top+S.scrollTop,l=b==="x"?t.width:t.height,h=Y();}else s=b==="x"?t.left:t.top,l=b==="x"?t.width:t.height,h=Y();let _=Ee({top:s,height:l},h,Le(),De,Te);N=_.tStart,R=_.tEnd,X&&process.env.NODE_ENV!=="production"&&(he?.remove(),he=rt(N,R,b));}function Xe(t,s){if(e.style.setProperty("--scroll-draw-progress",String(t)),G){let l=s==="reverse"?1-t:t;e.style.clipPath=j(l);return}x.forEach((l,h)=>{l.style.strokeDashoffset=s==="reverse"?`${w[h]*t}`:`${w[h]*(1-t)}`,c&&(l.style.opacity=s==="reverse"?`${1-t}`:`${t}`),I&&$?l.style.stroke=$e(I,$,t):$&&(l.style.stroke=$),M!==null&&k!==null?l.style.strokeWidth=`${M+(k-M)*t}`:k!==null&&(l.style.strokeWidth=`${k}`),A!==null&&D!==null?l.style.fillOpacity=`${A+(D-A)*t}`:D!==null&&(l.style.fillOpacity=`${D}`),O&&l.tagName.toLowerCase()==="path"&&F[h]&&l.setAttribute("d",Ve(F[h],O,t));});}function Oe(){if(e.style.setProperty("--scroll-draw-progress","0"),G){e.style.clipPath=j(0);return}x.forEach((t,s)=>{t.style.strokeDasharray=`${w[s]}`,t.style.strokeDashoffset=a==="reverse"?"0":`${w[s]}`,c?t.style.opacity=a==="reverse"?"1":"0":t.style.opacity="",I&&(t.style.stroke=I),M!==null&&(t.style.strokeWidth=`${M}`),A!==null&&(t.style.fillOpacity=`${A}`),O&&t.tagName.toLowerCase()==="path"&&F[s]&&t.setAttribute("d",F[s]);});}if(x.forEach(t=>{tt(t);let s=xe(t);w.push(s),t.tagName.toLowerCase()==="path"?F.push(t.getAttribute("d")??""):F.push(""),n?(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=a==="reverse"?`${s}`:"0",c&&(t.style.opacity="1"),$&&(t.style.stroke=$),k!==null&&(t.style.strokeWidth=`${k}`),D!==null&&(t.style.fillOpacity=`${D}`),O&&t.tagName.toLowerCase()==="path"&&t.setAttribute("d",O)):(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=a==="reverse"?"0":`${s}`,c?t.style.opacity=a==="reverse"?"1":"0":t.style.opacity="",I&&(t.style.stroke=I),M!==null&&(t.style.strokeWidth=`${M}`),A!==null&&(t.style.fillOpacity=`${A}`));}),G){if(n)return e.style.clipPath=j(1),K?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};e.style.clipPath=j(0);}else if(n)return K?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};function je(){return !(Ue===false||!et()||!x.length||typeof p!="string"||!(p in We)||G||b!=="y"||S||u!==1||C!==0||d||ee||re!==false||O||te||J||pe>0||ye||ge||K||W!=null||V!=null||B!=null||(g.start??"top bottom").trim()!=="top bottom"||(g.end??"bottom top").trim()!=="bottom top")}function Je(){let t=`svg-scroll-draw-${++Ze}`,s=a==="reverse"?"0":"var(--ssd-len)",l=a==="reverse"?"var(--ssd-len)":"0",h=`stroke-dashoffset:${s};`,_=`stroke-dashoffset:${l};`;c&&(h+=`opacity:${a==="reverse"?1:0};`,_+=`opacity:${a==="reverse"?0:1};`);let E=document.createElement("style");E.setAttribute("data-svg-scroll-draw",""),E.textContent=`@keyframes ${t}{from{${h}}to{${_}}}.${t}{animation-name:${t};animation-duration:auto;animation-timing-function:${We[p]};animation-fill-mode:both;animation-timeline:view();animation-range:cover 0% cover 100%;}`,document.head.appendChild(E);function i(m,P){m.style.setProperty("--ssd-len",String(w[P])),m.style.strokeDasharray=`${w[P]}`,m.style.strokeDashoffset="",m.style.opacity="",m.style.animationPlayState="",m.classList.add(t);}x.forEach(i);let f=false,v=-1;function y(){if(v>=0)return v;let m=e.getBoundingClientRect(),{tStart:P,tEnd:Z}=Ee({top:m.top,height:m.height},Y(),Le(),De,Te);return ne(U(Y(),P,Z,u))}return {destroy(){x.forEach(m=>{m.classList.remove(t),m.style.removeProperty("--ssd-len"),m.style.animationPlayState="";}),E.remove();},replay(){f=false,v=-1,x.forEach(i);},pause(){f=true,x.forEach(m=>{m.style.animationPlayState="paused";});},resume(){f&&(f=false,x.forEach(m=>{m.style.animationPlayState="running";}));},seek(m){let P=Math.min(1,Math.max(0,m));v=P,f=true,x.forEach((Z,Ie)=>{Z.classList.remove(t),Z.style.strokeDashoffset=a==="reverse"?`${w[Ie]*P}`:`${w[Ie]*(1-P)}`,c&&(Z.style.opacity=a==="reverse"?`${1-P}`:`${P}`);});},getProgress(){return y()}}}if(je())return Je();Ce();function ce(){if(!oe||q)return;let t=performance.now(),s=Y(),l=u;if(re!==false){let i=t-Pe,f=i>0?Math.abs(s-(ue<0?s:ue))/i:0;l=u*Math.max(.2,1+f*(typeof re=="number"?re:1)*.04);}ue=s,Pe=t;let h=ee?se===-1||s>=se?"forward":"reverse":a;se=s;let _=R-N,E=true;if(G){let i=ne(U(s,N,R,l));d&&!ee&&(T=Math.max(T,i),i=T),ie=i,e.style.setProperty("--scroll-draw-progress",String(i));let f=h==="reverse"?1-i:i;e.style.clipPath=j(f),ye?.(i),!z&&U(s,N,R,l)>0&&(z=true,ge?.()),i>=1&&!L?(L=true,K?.(),Q<(J==="infinite"?1/0:J??0)&&(Q++,ae=setTimeout(()=>{T=-1,z=false,L=false,e.style.clipPath=j(0);},Ae))):i<1&&!d&&(L=false),H=requestAnimationFrame(ce);return}if(x.forEach((i,f)=>{let v=f*C*_,y=ne(U(s,N+v,R+v,l));d&&!ee&&(T=Math.max(T,y),y=T),ie=y,i.style.strokeDashoffset=h==="reverse"?`${w[f]*y}`:`${w[f]*(1-y)}`,c&&(i.style.opacity=h==="reverse"?`${1-y}`:`${y}`),I&&$?i.style.stroke=$e(I,$,y):$&&(i.style.stroke=$),M!==null&&k!==null?i.style.strokeWidth=`${M+(k-M)*y}`:k!==null&&(i.style.strokeWidth=`${k}`),A!==null&&D!==null?i.style.fillOpacity=`${A+(D-A)*y}`:D!==null&&(i.style.fillOpacity=`${D}`),O&&i.tagName.toLowerCase()==="path"&&F[f]&&i.setAttribute("d",Ve(F[f],O,y)),f===0&&(ye?.(y),e.style.setProperty("--scroll-draw-progress",String(y))),y<1&&(E=false);}),te){let i=ne(U(s,N,R,l));for(let f in te){let v=parseFloat(f);i>=v&&!le.has(v)&&(le.add(v),te[f]?.());}}!z&&U(s,N,R,l)>0&&(z=true,ge?.()),E&&!L?(L=true,K?.(),Q<(J==="infinite"?1/0:J??0)&&(Q++,ae=setTimeout(()=>{T=-1,z=false,L=false,le.clear(),Oe();},Ae))):!E&&!d&&(L=false),H=requestAnimationFrame(ce);}let be=new IntersectionObserver(t=>{t.forEach(s=>{oe=s.isIntersecting,oe&&!q?H=requestAnimationFrame(ce):cancelAnimationFrame(H);});},{root:S??null,threshold:qe,rootMargin:_e}),we;function fe(){clearTimeout(we),we=setTimeout(()=>{x.forEach((t,s)=>{w[s]=xe(t),t.style.strokeDasharray=`${w[s]}`;}),Ce();},150);}return window.addEventListener("resize",fe),window.addEventListener("orientationchange",fe),pe>0?setTimeout(()=>be.observe(e),pe):be.observe(e),{destroy(){cancelAnimationFrame(H),clearTimeout(ae),be.disconnect(),window.removeEventListener("resize",fe),window.removeEventListener("orientationchange",fe),clearTimeout(we),he?.remove();},replay(){T=-1,se=-1,ue=-1,z=false,L=false,Q=0,q=false,le.clear(),clearTimeout(ae),Oe();},pause(){q=true,cancelAnimationFrame(H);},resume(){q&&(q=false,oe&&(H=requestAnimationFrame(ce)));},seek(t){let s=Math.min(1,Math.max(0,t));ie=s,T=s,q=true,cancelAnimationFrame(H),Xe(s,a);},getProgress(){return ie}}}function st(e={}){let r=ref(null);return onMounted(()=>{if(!r.value)return;let n=Me(r.value,e);onUnmounted(()=>n.destroy());}),r}var ke=defineComponent({name:"ScrollDraw",props:{selector:{type:String},speed:{type:Number},fade:{type:Boolean},stagger:{type:Number},easing:{type:[String,Function]},direction:{type:String},trigger:{type:Object},onProgress:{type:Function},onStart:{type:Function},onComplete:{type:Function},once:{type:Boolean},debug:{type:Boolean}},setup(e,{slots:r}){let n=ref(null);return onMounted(()=>{if(!n.value)return;let o={};e.selector!=null&&(o.selector=e.selector),e.speed!=null&&(o.speed=e.speed),e.fade!=null&&(o.fade=e.fade),e.stagger!=null&&(o.stagger=e.stagger),e.easing!=null&&(o.easing=e.easing),e.direction!=null&&(o.direction=e.direction),e.trigger!=null&&(o.trigger=e.trigger),e.once!=null&&(o.once=e.once),e.debug!=null&&(o.debug=e.debug),e.onProgress!=null&&(o.onProgress=e.onProgress),e.onStart!=null&&(o.onStart=e.onStart),e.onComplete!=null&&(o.onComplete=e.onComplete);let u=Me(n.value,o);onUnmounted(()=>u.destroy());}),()=>h("div",{ref:n},r.default?.())}});function pt(){return {install(e){e.component("ScrollDraw",ke);}}}export{ke as ScrollDraw,pt as createScrollDrawPlugin,st as useScrollDraw};
|
package/dist/react/index.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
'use strict';var react=require('react'),jsxRuntime=require('react/jsx-runtime');var oe={linear:e=>e,"ease-in":e=>e*e,"ease-out":e=>e*(2-e),"ease-in-out":e=>e<.5?2*e*e:-1+(4-2*e)*e,spring:e=>1-Math.cos(e*Math.PI*2.5)*Math.pow(1-e,2.2)};function ie(e="top bottom"){let r=e.trim();if(/^\d+(\.\d+)?%$/.test(r))return {element:"top",viewport:r};let[
|
|
2
|
-
<div style="position:absolute;${
|
|
3
|
-
<div style="position:absolute;${v?`left:${w}px;top:0;bottom:0;border-left:2px dashed #ef4444;`:`top:${w}px;left:0;right:0;border-top:2px dashed #ef4444;`}padding:2px 6px;color:#ef4444;background:rgba(0,0,0,.6)">\u25A0 end</div>`;}return document.body.appendChild(o),window.addEventListener("scroll",a,{passive:true}),a(),o}function Ve(e,r,s){let o=(r.match(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g)??[]).map(Number),a=0;return e.replace(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g,c=>{let m=parseFloat(c),w=o[a++]??m;return String(+(m+(w-m)*s).toFixed(4))})}function Ge(e,r={}){if(typeof window>"u")return {destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>0};let s=window.matchMedia("(prefers-reduced-motion: reduce)").matches,{selector:o="path, polyline, line, polygon, rect, circle",speed:a=1,fade:c=false,easing:m="linear",trigger:w={},stagger:v=0,direction:p="forward",once:V=false,debug:F=false,axis:E="y",scrollContainer:ae,autoReverse:j=false,delay:re=0,strokeColor:T,strokeWidth:x,fillOpacity:P,waypoints:ne,velocityScale:Y=false,threshold:pe=0,rootMargin:de="0px",repeat:N=0,repeatDelay:J=0,morphTo:d,clip:A,native:le=true,onProgress:K,onStart:G,onComplete:z}=r,Q=A===true?"left":typeof A=="string"?A:false,ye=typeof m=="function"?m:oe[m]??oe.linear,Le=ie(w.start??"top bottom"),Me=ie(w.end??"bottom top"),S=typeof ae=="string"?document.querySelector(ae):ae??null,q=Array.isArray(T)?T[0]:null,L=Array.isArray(T)?T[1]:typeof T=="string"?T:null,M=Array.isArray(x)?x[0]:null,D=Array.isArray(x)?x[1]:typeof x=="number"?x:null,C=Array.isArray(P)?P[0]:null,I=Array.isArray(P)?P[1]:typeof P=="number"?P:null;function se(t){let n=t*100;switch(Q){case "right":return `inset(0 0 0 ${100-n}%)`;case "top":return `inset(0 0 ${100-n}% 0)`;case "bottom":return `inset(${100-n}% 0 0 0)`;case "center":return `circle(${t*150}% at 50% 50%)`;default:return `inset(0 ${100-n}% 0 0)`}}let $=Q?[]:Array.from(e.querySelectorAll(o)),h=[],B=[],U=0,X=0,H=false,Z=false,_=0,ge=false,O=-1,he=-1,ee=false,be=0,ce=0,we,$e=null,ve=new Set,Ee=-1,De=performance.now();function ue(){return S?E==="x"?S.scrollLeft:S.scrollTop:E==="x"?window.scrollX:window.scrollY}function Ce(){return S?E==="x"?S.clientWidth:S.clientHeight:E==="x"?window.innerWidth:window.innerHeight}function Ie(){let t=e.getBoundingClientRect(),n,l,g;if(S){let k=S.getBoundingClientRect();n=E==="x"?t.left-k.left+S.scrollLeft:t.top-k.top+S.scrollTop,l=E==="x"?t.width:t.height,g=ue();}else n=E==="x"?t.left:t.top,l=E==="x"?t.width:t.height,g=ue();let te=me({top:n,height:l},g,Ce(),Le,Me);U=te.tStart,X=te.tEnd,F&&process.env.NODE_ENV!=="production"&&($e?.remove(),$e=Ke(U,X,E));}function Be(t,n){if(e.style.setProperty("--scroll-draw-progress",String(t)),Q){let l=n==="reverse"?1-t:t;e.style.clipPath=se(l);return}$.forEach((l,g)=>{l.style.strokeDashoffset=n==="reverse"?`${h[g]*t}`:`${h[g]*(1-t)}`,c&&(l.style.opacity=n==="reverse"?`${1-t}`:`${t}`),q&&L?l.style.stroke=Pe(q,L,t):L&&(l.style.stroke=L),M!==null&&D!==null?l.style.strokeWidth=`${M+(D-M)*t}`:D!==null&&(l.style.strokeWidth=`${D}`),C!==null&&I!==null?l.style.fillOpacity=`${C+(I-C)*t}`:I!==null&&(l.style.fillOpacity=`${I}`),d&&l.tagName.toLowerCase()==="path"&&B[g]&&l.setAttribute("d",Ve(B[g],d,t));});}function Oe(){if(e.style.setProperty("--scroll-draw-progress","0"),Q){e.style.clipPath=se(0);return}$.forEach((t,n)=>{t.style.strokeDasharray=`${h[n]}`,t.style.strokeDashoffset=p==="reverse"?"0":`${h[n]}`,c?t.style.opacity=p==="reverse"?"1":"0":t.style.opacity="",q&&(t.style.stroke=q),M!==null&&(t.style.strokeWidth=`${M}`),C!==null&&(t.style.fillOpacity=`${C}`),d&&t.tagName.toLowerCase()==="path"&&B[n]&&t.setAttribute("d",B[n]);});}if($.forEach(t=>{Je(t);let n=ke(t);h.push(n),t.tagName.toLowerCase()==="path"?B.push(t.getAttribute("d")??""):B.push(""),s?(t.style.strokeDasharray=`${n}`,t.style.strokeDashoffset=p==="reverse"?`${n}`:"0",c&&(t.style.opacity="1"),L&&(t.style.stroke=L),D!==null&&(t.style.strokeWidth=`${D}`),I!==null&&(t.style.fillOpacity=`${I}`),d&&t.tagName.toLowerCase()==="path"&&t.setAttribute("d",d)):(t.style.strokeDasharray=`${n}`,t.style.strokeDashoffset=p==="reverse"?"0":`${n}`,c?t.style.opacity=p==="reverse"?"1":"0":t.style.opacity="",q&&(t.style.stroke=q),M!==null&&(t.style.strokeWidth=`${M}`),C!==null&&(t.style.fillOpacity=`${C}`));}),Q){if(s)return e.style.clipPath=se(1),z?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};e.style.clipPath=se(0);}else if(s)return z?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};function Ue(){return !(le===false||!Ye()||!$.length||typeof m!="string"||!(m in We)||Q||E!=="y"||S||a!==1||v!==0||V||j||Y!==false||d||ne||N||re>0||K||G||z||T!=null||x!=null||P!=null||(w.start??"top bottom").trim()!=="top bottom"||(w.end??"bottom top").trim()!=="bottom top")}function Xe(){let t=`svg-scroll-draw-${++je}`,n=p==="reverse"?"0":"var(--ssd-len)",l=p==="reverse"?"var(--ssd-len)":"0",g=`stroke-dashoffset:${n};`,te=`stroke-dashoffset:${l};`;c&&(g+=`opacity:${p==="reverse"?1:0};`,te+=`opacity:${p==="reverse"?0:1};`);let k=document.createElement("style");k.setAttribute("data-svg-scroll-draw",""),k.textContent=`@keyframes ${t}{from{${g}}to{${te}}}.${t}{animation-name:${t};animation-duration:auto;animation-timing-function:${We[m]};animation-fill-mode:both;animation-timeline:view();animation-range:cover 0% cover 100%;}`,document.head.appendChild(k);function i(f,R){f.style.setProperty("--ssd-len",String(h[R])),f.style.strokeDasharray=`${h[R]}`,f.style.strokeDashoffset="",f.style.opacity="",f.style.animationPlayState="",f.classList.add(t);}$.forEach(i);let u=false,b=-1;function y(){if(b>=0)return b;let f=e.getBoundingClientRect(),{tStart:R,tEnd:fe}=me({top:f.top,height:f.height},ue(),Ce(),Le,Me);return ye(W(ue(),R,fe,a))}return {destroy(){$.forEach(f=>{f.classList.remove(t),f.style.removeProperty("--ssd-len"),f.style.animationPlayState="";}),k.remove();},replay(){u=false,b=-1,$.forEach(i);},pause(){u=true,$.forEach(f=>{f.style.animationPlayState="paused";});},resume(){u&&(u=false,$.forEach(f=>{f.style.animationPlayState="running";}));},seek(f){let R=Math.min(1,Math.max(0,f));b=R,u=true,$.forEach((fe,Re)=>{fe.classList.remove(t),fe.style.strokeDashoffset=p==="reverse"?`${h[Re]*R}`:`${h[Re]*(1-R)}`,c&&(fe.style.opacity=p==="reverse"?`${1-R}`:`${R}`);});},getProgress(){return y()}}}if(Ue())return Xe();Ie();function xe(){if(!ge||ee)return;let t=performance.now(),n=ue(),l=a;if(Y!==false){let i=t-De,u=i>0?Math.abs(n-(Ee<0?n:Ee))/i:0;l=a*Math.max(.2,1+u*(typeof Y=="number"?Y:1)*.04);}Ee=n,De=t;let g=j?he===-1||n>=he?"forward":"reverse":p;he=n;let te=X-U,k=true;if(Q){let i=ye(W(n,U,X,l));V&&!j&&(O=Math.max(O,i),i=O),be=i,e.style.setProperty("--scroll-draw-progress",String(i));let u=g==="reverse"?1-i:i;e.style.clipPath=se(u),K?.(i),!Z&&W(n,U,X,l)>0&&(Z=true,G?.()),i>=1&&!H?(H=true,z?.(),ce<(N==="infinite"?1/0:N??0)&&(ce++,we=setTimeout(()=>{O=-1,Z=false,H=false,e.style.clipPath=se(0);},J))):i<1&&!V&&(H=false),_=requestAnimationFrame(xe);return}if($.forEach((i,u)=>{let b=u*v*te,y=ye(W(n,U+b,X+b,l));V&&!j&&(O=Math.max(O,y),y=O),be=y,i.style.strokeDashoffset=g==="reverse"?`${h[u]*y}`:`${h[u]*(1-y)}`,c&&(i.style.opacity=g==="reverse"?`${1-y}`:`${y}`),q&&L?i.style.stroke=Pe(q,L,y):L&&(i.style.stroke=L),M!==null&&D!==null?i.style.strokeWidth=`${M+(D-M)*y}`:D!==null&&(i.style.strokeWidth=`${D}`),C!==null&&I!==null?i.style.fillOpacity=`${C+(I-C)*y}`:I!==null&&(i.style.fillOpacity=`${I}`),d&&i.tagName.toLowerCase()==="path"&&B[u]&&i.setAttribute("d",Ve(B[u],d,y)),u===0&&(K?.(y),e.style.setProperty("--scroll-draw-progress",String(y))),y<1&&(k=false);}),ne){let i=ye(W(n,U,X,l));for(let u in ne){let b=parseFloat(u);i>=b&&!ve.has(b)&&(ve.add(b),ne[u]?.());}}!Z&&W(n,U,X,l)>0&&(Z=true,G?.()),k&&!H?(H=true,z?.(),ce<(N==="infinite"?1/0:N??0)&&(ce++,we=setTimeout(()=>{O=-1,Z=false,H=false,ve.clear(),Oe();},J))):!k&&!V&&(H=false),_=requestAnimationFrame(xe);}let Te=new IntersectionObserver(t=>{t.forEach(n=>{ge=n.isIntersecting,ge&&!ee?_=requestAnimationFrame(xe):cancelAnimationFrame(_);});},{root:S??null,threshold:pe,rootMargin:de}),Ae;function Se(){clearTimeout(Ae),Ae=setTimeout(()=>{$.forEach((t,n)=>{h[n]=ke(t),t.style.strokeDasharray=`${h[n]}`;}),Ie();},150);}return window.addEventListener("resize",Se),window.addEventListener("orientationchange",Se),re>0?setTimeout(()=>Te.observe(e),re):Te.observe(e),{destroy(){cancelAnimationFrame(_),clearTimeout(we),Te.disconnect(),window.removeEventListener("resize",Se),window.removeEventListener("orientationchange",Se),clearTimeout(Ae),$e?.remove();},replay(){O=-1,he=-1,Ee=-1,Z=false,H=false,ce=0,ee=false,ve.clear(),clearTimeout(we),Oe();},pause(){ee=true,cancelAnimationFrame(_);},resume(){ee&&(ee=false,ge&&(_=requestAnimationFrame(xe)));},seek(t){let n=Math.min(1,Math.max(0,t));be=n,O=n,ee=true,cancelAnimationFrame(_),Be(n,p);},getProgress(){return be}}}function et(e,r={}){let[s,o]=react.useState(0),a=react.useRef(r);a.current=r;let c=react.useRef(-1);return react.useEffect(()=>{if(typeof window>"u")return;if(window.matchMedia("(prefers-reduced-motion: reduce)").matches){o(1);return}let m=typeof e=="string"?document.querySelector(e):e.current;if(!m)return;let w=m,{axis:v="y",scrollContainer:p,trigger:V={}}=a.current,F=typeof p=="string"?document.querySelector(p):p??null,E=ie(V.start??"top bottom"),ae=ie(V.end??"bottom top"),j=0,re=0,T=false,x=0;function P(){return F?v==="x"?F.scrollLeft:F.scrollTop:v==="x"?window.scrollX:window.scrollY}function ne(){return F?v==="x"?F.clientWidth:F.clientHeight:v==="x"?window.innerWidth:window.innerHeight}function Y(){let d=w.getBoundingClientRect(),A=P(),le=v==="x"?d.left:d.top,K=v==="x"?d.width:d.height,G=me({top:le,height:K},A,ne(),E,ae);j=G.tStart,re=G.tEnd;}function pe(){if(!T)return;let{speed:d=1,easing:A="linear",once:le=false}=a.current,K=typeof A=="function"?A:oe[A]??oe.linear,G=W(P(),j,re,d),z=K(G);le&&(c.current=Math.max(c.current,z),z=c.current),o(z),x=requestAnimationFrame(pe);}let de=new IntersectionObserver(d=>{d.forEach(A=>{T=A.isIntersecting,T?x=requestAnimationFrame(pe):cancelAnimationFrame(x);});},{root:F??null,threshold:0});Y(),de.observe(w);let N;function J(){clearTimeout(N),N=setTimeout(Y,150);}return window.addEventListener("resize",J),window.addEventListener("orientationchange",J),()=>{cancelAnimationFrame(x),clearTimeout(N),de.disconnect(),window.removeEventListener("resize",J),window.removeEventListener("orientationchange",J);}},[]),s}function pt({children:e,className:r,style:s,...o}){let a=react.useRef(null);return react.useEffect(()=>{if(!a.current)return;let c=Ge(a.current,o);return ()=>c.destroy()},[]),jsxRuntime.jsx("div",{ref:a,className:r,style:s,children:e})}exports.ScrollDraw=pt;exports.useScrollDrawProgress=et;
|
|
1
|
+
'use strict';var react=require('react'),jsxRuntime=require('react/jsx-runtime');function _e({bounces:e=3,decay:r=.5}={}){let n=Math.max(1,Math.round(e)),s=Math.max(.01,Math.min(.99,r)),a=Math.sqrt(s),u=0,f=[];for(let i=0;i<n;i++){let m=Math.pow(a,i);f.push(m),u+=m;}let y=[0],b=0;for(let i=0;i<n;i++)b+=f[i]/u,y.push(b);return i=>{if(i<=0)return 0;if(i>=1)return 1;for(let m=0;m<n;m++)if(i<=y[m+1]){let E=(i-y[m])/(y[m+1]-y[m]);if(m===0)return E*(2-E);let w=1-Math.pow(s,m);return w+(1-w)*(2*E-1)*(2*E-1)}return 1}}function je({amplitude:e=1,period:r=.4}={}){let n=Math.max(1,e),s=Math.max(.1,r),a=n<=1?s/4:s/(2*Math.PI)*Math.asin(1/n);return u=>u<=0?0:u>=1?1:n*Math.pow(2,-10*u)*Math.sin((u-a)*(2*Math.PI)/s)+1}var oe={linear:e=>e,"ease-in":e=>e*e,"ease-out":e=>e*(2-e),"ease-in-out":e=>e<.5?2*e*e:-1+(4-2*e)*e,spring:e=>1-Math.cos(e*Math.PI*2.5)*Math.pow(1-e,2.2),bounce:_e(),elastic:je()};function ie(e="top bottom"){let r=e.trim();if(/^\d+(\.\d+)?%$/.test(r))return {element:"top",viewport:r};let[n="top",s="bottom"]=r.split(/\s+/).filter(Boolean);return {element:n,viewport:s}}function Fe(e,r,n,s){switch(s){case "top":return e+n;case "center":return e+n+r/2;case "bottom":return e+n+r;default:return e+n}}function Ne(e,r){if(/^\d+(\.\d+)?%$/.test(e))return r*(parseFloat(e)/100);switch(e){case "top":return 0;case "center":return r/2;case "bottom":return r;default:return r}}function Ae(e){let r=e.tagName.toLowerCase();if(r==="rect"){let n=parseFloat(e.getAttribute("width")??"0"),s=parseFloat(e.getAttribute("height")??"0");return 2*(n+s)}if(r==="circle"){let n=parseFloat(e.getAttribute("r")??"0");return 2*Math.PI*n}return e.getTotalLength()}function Ye(e,r,n){return Math.min(n,Math.max(r,e))}function q(e,r,n,s){return n===r?0:Ye((e-r)/(n-r)*s,0,1)}function me(e,r,n,s,a){let u=Fe(e.top,e.height,r,s.element)-Ne(s.viewport,n),f=Fe(e.top,e.height,r,a.element)-Ne(a.viewport,n);return {tStart:u,tEnd:f}}function ze(e){let r=/^#([a-f\d])([a-f\d])([a-f\d])$/i.exec(e);if(r)return [parseInt(r[1]+r[1],16),parseInt(r[2]+r[2],16),parseInt(r[3]+r[3],16)];let n=/^#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(e);if(n)return [parseInt(n[1],16),parseInt(n[2],16),parseInt(n[3],16)];let s=/^rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i.exec(e);return s?[parseInt(s[1]),parseInt(s[2]),parseInt(s[3])]:null}function ke(e,r,n){let s=ze(e),a=ze(r);return !s||!a?e:`rgb(${Math.round(s[0]+(a[0]-s[0])*n)},${Math.round(s[1]+(a[1]-s[1])*n)},${Math.round(s[2]+(a[2]-s[2])*n)})`}function We(e,r){process.env.NODE_ENV!=="production"&&console.warn(`[svg-scroll-draw] ${e}`,r);}var He={linear:"linear","ease-in":"ease-in","ease-out":"ease-out","ease-in-out":"ease-in-out"},Je=0;function Ke(){return typeof CSS<"u"&&typeof CSS.supports=="function"&&CSS.supports("animation-timeline: view()")}function Qe(e){let r=e.getAttribute("stroke"),n=e.getAttribute("fill");!r||r==="none"?We("Element has no stroke \u2014 path will not be visible.",e):n&&n!=="none"&&n!=="transparent"&&We("Element has a fill \u2014 it may obscure the stroke animation.",e);}function Ze(e,r,n){let s=document.createElement("div");s.setAttribute("data-svg-scroll-draw-debug",""),s.style.cssText="position:fixed;pointer-events:none;z-index:9999;font-family:monospace;font-size:11px;top:0;left:0;right:0;bottom:0;";function a(){let u=n==="x"?window.scrollX:window.scrollY,f=e-u,y=r-u,b=n==="x";s.innerHTML=`
|
|
2
|
+
<div style="position:absolute;${b?`left:${f}px;top:0;bottom:0;border-left:2px dashed #22c55e;`:`top:${f}px;left:0;right:0;border-top:2px dashed #22c55e;`}padding:2px 6px;color:#22c55e;background:rgba(0,0,0,.6)">\u25B6 start</div>
|
|
3
|
+
<div style="position:absolute;${b?`left:${y}px;top:0;bottom:0;border-left:2px dashed #ef4444;`:`top:${y}px;left:0;right:0;border-top:2px dashed #ef4444;`}padding:2px 6px;color:#ef4444;background:rgba(0,0,0,.6)">\u25A0 end</div>`;}return document.body.appendChild(s),window.addEventListener("scroll",a,{passive:true}),a(),s}function qe(e,r,n){let s=(r.match(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g)??[]).map(Number),a=0;return e.replace(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g,u=>{let f=parseFloat(u),y=s[a++]??f;return String(+(f+(y-f)*n).toFixed(4))})}function Ve(e,r={}){if(typeof window>"u")return {destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>0};let n=window.matchMedia("(prefers-reduced-motion: reduce)").matches,{selector:s="path, polyline, line, polygon, rect, circle",speed:a=1,fade:u=false,easing:f="linear",trigger:y={},stagger:b=0,direction:i="forward",once:m=false,debug:E=false,axis:w="y",scrollContainer:ae,autoReverse:j=false,delay:re=0,strokeColor:A,strokeWidth:$,fillOpacity:D,waypoints:ne,velocityScale:Y=false,threshold:pe=0,rootMargin:de="0px",repeat:z=0,repeatDelay:J=0,morphTo:g,clip:k,native:le=true,onProgress:K,onStart:V,onComplete:W}=r,Q=k===true?"left":typeof k=="string"?k:false,ye=typeof f=="function"?f:oe[f]??oe.linear,Pe=ie(y.start??"top bottom"),De=ie(y.end??"bottom top"),T=typeof ae=="string"?document.querySelector(ae):ae??null,G=Array.isArray(A)?A[0]:null,L=Array.isArray(A)?A[1]:typeof A=="string"?A:null,C=Array.isArray($)?$[0]:null,I=Array.isArray($)?$[1]:typeof $=="number"?$:null,O=Array.isArray(D)?D[0]:null,R=Array.isArray(D)?D[1]:typeof D=="number"?D:null;function se(t){let o=t*100;switch(Q){case "right":return `inset(0 0 0 ${100-o}%)`;case "top":return `inset(0 0 ${100-o}% 0)`;case "bottom":return `inset(${100-o}% 0 0 0)`;case "center":return `circle(${t*150}% at 50% 50%)`;default:return `inset(0 ${100-o}% 0 0)`}}let M=Q?[]:Array.from(e.querySelectorAll(s)),x=[],B=[],U=0,X=0,H=false,Z=false,_=0,ge=false,F=-1,he=-1,ee=false,be=0,ue=0,we,$e=null,ve=new Set,Ee=-1,Le=performance.now();function ce(){return T?w==="x"?T.scrollLeft:T.scrollTop:w==="x"?window.scrollX:window.scrollY}function Ce(){return T?w==="x"?T.clientWidth:T.clientHeight:w==="x"?window.innerWidth:window.innerHeight}function Ie(){let t=e.getBoundingClientRect(),o,c,v;if(T){let P=T.getBoundingClientRect();o=w==="x"?t.left-P.left+T.scrollLeft:t.top-P.top+T.scrollTop,c=w==="x"?t.width:t.height,v=ce();}else o=w==="x"?t.left:t.top,c=w==="x"?t.width:t.height,v=ce();let te=me({top:o,height:c},v,Ce(),Pe,De);U=te.tStart,X=te.tEnd,E&&process.env.NODE_ENV!=="production"&&($e?.remove(),$e=Ze(U,X,w));}function Be(t,o){if(e.style.setProperty("--scroll-draw-progress",String(t)),Q){let c=o==="reverse"?1-t:t;e.style.clipPath=se(c);return}M.forEach((c,v)=>{c.style.strokeDashoffset=o==="reverse"?`${x[v]*t}`:`${x[v]*(1-t)}`,u&&(c.style.opacity=o==="reverse"?`${1-t}`:`${t}`),G&&L?c.style.stroke=ke(G,L,t):L&&(c.style.stroke=L),C!==null&&I!==null?c.style.strokeWidth=`${C+(I-C)*t}`:I!==null&&(c.style.strokeWidth=`${I}`),O!==null&&R!==null?c.style.fillOpacity=`${O+(R-O)*t}`:R!==null&&(c.style.fillOpacity=`${R}`),g&&c.tagName.toLowerCase()==="path"&&B[v]&&c.setAttribute("d",qe(B[v],g,t));});}function Oe(){if(e.style.setProperty("--scroll-draw-progress","0"),Q){e.style.clipPath=se(0);return}M.forEach((t,o)=>{t.style.strokeDasharray=`${x[o]}`,t.style.strokeDashoffset=i==="reverse"?"0":`${x[o]}`,u?t.style.opacity=i==="reverse"?"1":"0":t.style.opacity="",G&&(t.style.stroke=G),C!==null&&(t.style.strokeWidth=`${C}`),O!==null&&(t.style.fillOpacity=`${O}`),g&&t.tagName.toLowerCase()==="path"&&B[o]&&t.setAttribute("d",B[o]);});}if(M.forEach(t=>{Qe(t);let o=Ae(t);x.push(o),t.tagName.toLowerCase()==="path"?B.push(t.getAttribute("d")??""):B.push(""),n?(t.style.strokeDasharray=`${o}`,t.style.strokeDashoffset=i==="reverse"?`${o}`:"0",u&&(t.style.opacity="1"),L&&(t.style.stroke=L),I!==null&&(t.style.strokeWidth=`${I}`),R!==null&&(t.style.fillOpacity=`${R}`),g&&t.tagName.toLowerCase()==="path"&&t.setAttribute("d",g)):(t.style.strokeDasharray=`${o}`,t.style.strokeDashoffset=i==="reverse"?"0":`${o}`,u?t.style.opacity=i==="reverse"?"1":"0":t.style.opacity="",G&&(t.style.stroke=G),C!==null&&(t.style.strokeWidth=`${C}`),O!==null&&(t.style.fillOpacity=`${O}`));}),Q){if(n)return e.style.clipPath=se(1),W?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};e.style.clipPath=se(0);}else if(n)return W?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};function Ue(){return !(le===false||!Ke()||!M.length||typeof f!="string"||!(f in He)||Q||w!=="y"||T||a!==1||b!==0||m||j||Y!==false||g||ne||z||re>0||K||V||W||A!=null||$!=null||D!=null||(y.start??"top bottom").trim()!=="top bottom"||(y.end??"bottom top").trim()!=="bottom top")}function Xe(){let t=`svg-scroll-draw-${++Je}`,o=i==="reverse"?"0":"var(--ssd-len)",c=i==="reverse"?"var(--ssd-len)":"0",v=`stroke-dashoffset:${o};`,te=`stroke-dashoffset:${c};`;u&&(v+=`opacity:${i==="reverse"?1:0};`,te+=`opacity:${i==="reverse"?0:1};`);let P=document.createElement("style");P.setAttribute("data-svg-scroll-draw",""),P.textContent=`@keyframes ${t}{from{${v}}to{${te}}}.${t}{animation-name:${t};animation-duration:auto;animation-timing-function:${He[f]};animation-fill-mode:both;animation-timeline:view();animation-range:cover 0% cover 100%;}`,document.head.appendChild(P);function l(d,N){d.style.setProperty("--ssd-len",String(x[N])),d.style.strokeDasharray=`${x[N]}`,d.style.strokeDashoffset="",d.style.opacity="",d.style.animationPlayState="",d.classList.add(t);}M.forEach(l);let p=false,S=-1;function h(){if(S>=0)return S;let d=e.getBoundingClientRect(),{tStart:N,tEnd:fe}=me({top:d.top,height:d.height},ce(),Ce(),Pe,De);return ye(q(ce(),N,fe,a))}return {destroy(){M.forEach(d=>{d.classList.remove(t),d.style.removeProperty("--ssd-len"),d.style.animationPlayState="";}),P.remove();},replay(){p=false,S=-1,M.forEach(l);},pause(){p=true,M.forEach(d=>{d.style.animationPlayState="paused";});},resume(){p&&(p=false,M.forEach(d=>{d.style.animationPlayState="running";}));},seek(d){let N=Math.min(1,Math.max(0,d));S=N,p=true,M.forEach((fe,Re)=>{fe.classList.remove(t),fe.style.strokeDashoffset=i==="reverse"?`${x[Re]*N}`:`${x[Re]*(1-N)}`,u&&(fe.style.opacity=i==="reverse"?`${1-N}`:`${N}`);});},getProgress(){return h()}}}if(Ue())return Xe();Ie();function xe(){if(!ge||ee)return;let t=performance.now(),o=ce(),c=a;if(Y!==false){let l=t-Le,p=l>0?Math.abs(o-(Ee<0?o:Ee))/l:0;c=a*Math.max(.2,1+p*(typeof Y=="number"?Y:1)*.04);}Ee=o,Le=t;let v=j?he===-1||o>=he?"forward":"reverse":i;he=o;let te=X-U,P=true;if(Q){let l=ye(q(o,U,X,c));m&&!j&&(F=Math.max(F,l),l=F),be=l,e.style.setProperty("--scroll-draw-progress",String(l));let p=v==="reverse"?1-l:l;e.style.clipPath=se(p),K?.(l),!Z&&q(o,U,X,c)>0&&(Z=true,V?.()),l>=1&&!H?(H=true,W?.(),ue<(z==="infinite"?1/0:z??0)&&(ue++,we=setTimeout(()=>{F=-1,Z=false,H=false,e.style.clipPath=se(0);},J))):l<1&&!m&&(H=false),_=requestAnimationFrame(xe);return}if(M.forEach((l,p)=>{let S=p*b*te,h=ye(q(o,U+S,X+S,c));m&&!j&&(F=Math.max(F,h),h=F),be=h,l.style.strokeDashoffset=v==="reverse"?`${x[p]*h}`:`${x[p]*(1-h)}`,u&&(l.style.opacity=v==="reverse"?`${1-h}`:`${h}`),G&&L?l.style.stroke=ke(G,L,h):L&&(l.style.stroke=L),C!==null&&I!==null?l.style.strokeWidth=`${C+(I-C)*h}`:I!==null&&(l.style.strokeWidth=`${I}`),O!==null&&R!==null?l.style.fillOpacity=`${O+(R-O)*h}`:R!==null&&(l.style.fillOpacity=`${R}`),g&&l.tagName.toLowerCase()==="path"&&B[p]&&l.setAttribute("d",qe(B[p],g,h)),p===0&&(K?.(h),e.style.setProperty("--scroll-draw-progress",String(h))),h<1&&(P=false);}),ne){let l=ye(q(o,U,X,c));for(let p in ne){let S=parseFloat(p);l>=S&&!ve.has(S)&&(ve.add(S),ne[p]?.());}}!Z&&q(o,U,X,c)>0&&(Z=true,V?.()),P&&!H?(H=true,W?.(),ue<(z==="infinite"?1/0:z??0)&&(ue++,we=setTimeout(()=>{F=-1,Z=false,H=false,ve.clear(),Oe();},J))):!P&&!m&&(H=false),_=requestAnimationFrame(xe);}let Te=new IntersectionObserver(t=>{t.forEach(o=>{ge=o.isIntersecting,ge&&!ee?_=requestAnimationFrame(xe):cancelAnimationFrame(_);});},{root:T??null,threshold:pe,rootMargin:de}),Me;function Se(){clearTimeout(Me),Me=setTimeout(()=>{M.forEach((t,o)=>{x[o]=Ae(t),t.style.strokeDasharray=`${x[o]}`;}),Ie();},150);}return window.addEventListener("resize",Se),window.addEventListener("orientationchange",Se),re>0?setTimeout(()=>Te.observe(e),re):Te.observe(e),{destroy(){cancelAnimationFrame(_),clearTimeout(we),Te.disconnect(),window.removeEventListener("resize",Se),window.removeEventListener("orientationchange",Se),clearTimeout(Me),$e?.remove();},replay(){F=-1,he=-1,Ee=-1,Z=false,H=false,ue=0,ee=false,ve.clear(),clearTimeout(we),Oe();},pause(){ee=true,cancelAnimationFrame(_);},resume(){ee&&(ee=false,ge&&(_=requestAnimationFrame(xe)));},seek(t){let o=Math.min(1,Math.max(0,t));be=o,F=o,ee=true,cancelAnimationFrame(_),Be(o,i);},getProgress(){return be}}}function rt(e,r={}){let[n,s]=react.useState(0),a=react.useRef(r);a.current=r;let u=react.useRef(-1);return react.useEffect(()=>{if(typeof window>"u")return;if(window.matchMedia("(prefers-reduced-motion: reduce)").matches){s(1);return}let f=typeof e=="string"?document.querySelector(e):e.current;if(!f)return;let y=f,{axis:b="y",scrollContainer:i,trigger:m={}}=a.current,E=typeof i=="string"?document.querySelector(i):i??null,w=ie(m.start??"top bottom"),ae=ie(m.end??"bottom top"),j=0,re=0,A=false,$=0;function D(){return E?b==="x"?E.scrollLeft:E.scrollTop:b==="x"?window.scrollX:window.scrollY}function ne(){return E?b==="x"?E.clientWidth:E.clientHeight:b==="x"?window.innerWidth:window.innerHeight}function Y(){let g=y.getBoundingClientRect(),k=D(),le=b==="x"?g.left:g.top,K=b==="x"?g.width:g.height,V=me({top:le,height:K},k,ne(),w,ae);j=V.tStart,re=V.tEnd;}function pe(){if(!A)return;let{speed:g=1,easing:k="linear",once:le=false}=a.current,K=typeof k=="function"?k:oe[k]??oe.linear,V=q(D(),j,re,g),W=K(V);le&&(u.current=Math.max(u.current,W),W=u.current),s(W),$=requestAnimationFrame(pe);}let de=new IntersectionObserver(g=>{g.forEach(k=>{A=k.isIntersecting,A?$=requestAnimationFrame(pe):cancelAnimationFrame($);});},{root:E??null,threshold:0});Y(),de.observe(y);let z;function J(){clearTimeout(z),z=setTimeout(Y,150);}return window.addEventListener("resize",J),window.addEventListener("orientationchange",J),()=>{cancelAnimationFrame($),clearTimeout(z),de.disconnect(),window.removeEventListener("resize",J),window.removeEventListener("orientationchange",J);}},[]),n}function yt({children:e,className:r,style:n,...s}){let a=react.useRef(null);return react.useEffect(()=>{if(!a.current)return;let u=Ve(a.current,s);return ()=>u.destroy()},[]),jsxRuntime.jsx("div",{ref:a,className:r,style:n,children:e})}exports.ScrollDraw=yt;exports.useScrollDrawProgress=rt;
|
package/dist/react/index.d.mts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
import React, { RefObject } from 'react';
|
|
3
3
|
|
|
4
|
-
type EasingName = 'linear' | 'ease-in' | 'ease-out' | 'ease-in-out' | 'spring';
|
|
4
|
+
type EasingName = 'linear' | 'ease-in' | 'ease-out' | 'ease-in-out' | 'spring' | 'bounce' | 'elastic';
|
|
5
5
|
interface TriggerConfig {
|
|
6
6
|
start?: string;
|
|
7
7
|
end?: string;
|
package/dist/react/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
2
|
import React, { RefObject } from 'react';
|
|
3
3
|
|
|
4
|
-
type EasingName = 'linear' | 'ease-in' | 'ease-out' | 'ease-in-out' | 'spring';
|
|
4
|
+
type EasingName = 'linear' | 'ease-in' | 'ease-out' | 'ease-in-out' | 'spring' | 'bounce' | 'elastic';
|
|
5
5
|
interface TriggerConfig {
|
|
6
6
|
start?: string;
|
|
7
7
|
end?: string;
|
package/dist/react/index.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {useState,useRef,useEffect}from'react';import {jsx}from'react/jsx-runtime';var oe={linear:e=>e,"ease-in":e=>e*e,"ease-out":e=>e*(2-e),"ease-in-out":e=>e<.5?2*e*e:-1+(4-2*e)*e,spring:e=>1-Math.cos(e*Math.PI*2.5)*Math.pow(1-e,2.2)};function ie(e="top bottom"){let r=e.trim();if(/^\d+(\.\d+)?%$/.test(r))return {element:"top",viewport:r};let[
|
|
2
|
-
<div style="position:absolute;${
|
|
3
|
-
<div style="position:absolute;${v?`left:${w}px;top:0;bottom:0;border-left:2px dashed #ef4444;`:`top:${w}px;left:0;right:0;border-top:2px dashed #ef4444;`}padding:2px 6px;color:#ef4444;background:rgba(0,0,0,.6)">\u25A0 end</div>`;}return document.body.appendChild(o),window.addEventListener("scroll",a,{passive:true}),a(),o}function Ve(e,r,s){let o=(r.match(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g)??[]).map(Number),a=0;return e.replace(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g,c=>{let m=parseFloat(c),w=o[a++]??m;return String(+(m+(w-m)*s).toFixed(4))})}function Ge(e,r={}){if(typeof window>"u")return {destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>0};let s=window.matchMedia("(prefers-reduced-motion: reduce)").matches,{selector:o="path, polyline, line, polygon, rect, circle",speed:a=1,fade:c=false,easing:m="linear",trigger:w={},stagger:v=0,direction:p="forward",once:V=false,debug:F=false,axis:E="y",scrollContainer:ae,autoReverse:j=false,delay:re=0,strokeColor:T,strokeWidth:x,fillOpacity:P,waypoints:ne,velocityScale:Y=false,threshold:pe=0,rootMargin:de="0px",repeat:N=0,repeatDelay:J=0,morphTo:d,clip:A,native:le=true,onProgress:K,onStart:G,onComplete:z}=r,Q=A===true?"left":typeof A=="string"?A:false,ye=typeof m=="function"?m:oe[m]??oe.linear,Le=ie(w.start??"top bottom"),Me=ie(w.end??"bottom top"),S=typeof ae=="string"?document.querySelector(ae):ae??null,q=Array.isArray(T)?T[0]:null,L=Array.isArray(T)?T[1]:typeof T=="string"?T:null,M=Array.isArray(x)?x[0]:null,D=Array.isArray(x)?x[1]:typeof x=="number"?x:null,C=Array.isArray(P)?P[0]:null,I=Array.isArray(P)?P[1]:typeof P=="number"?P:null;function se(t){let n=t*100;switch(Q){case "right":return `inset(0 0 0 ${100-n}%)`;case "top":return `inset(0 0 ${100-n}% 0)`;case "bottom":return `inset(${100-n}% 0 0 0)`;case "center":return `circle(${t*150}% at 50% 50%)`;default:return `inset(0 ${100-n}% 0 0)`}}let $=Q?[]:Array.from(e.querySelectorAll(o)),h=[],B=[],U=0,X=0,H=false,Z=false,_=0,ge=false,O=-1,he=-1,ee=false,be=0,ce=0,we,$e=null,ve=new Set,Ee=-1,De=performance.now();function ue(){return S?E==="x"?S.scrollLeft:S.scrollTop:E==="x"?window.scrollX:window.scrollY}function Ce(){return S?E==="x"?S.clientWidth:S.clientHeight:E==="x"?window.innerWidth:window.innerHeight}function Ie(){let t=e.getBoundingClientRect(),n,l,g;if(S){let k=S.getBoundingClientRect();n=E==="x"?t.left-k.left+S.scrollLeft:t.top-k.top+S.scrollTop,l=E==="x"?t.width:t.height,g=ue();}else n=E==="x"?t.left:t.top,l=E==="x"?t.width:t.height,g=ue();let te=me({top:n,height:l},g,Ce(),Le,Me);U=te.tStart,X=te.tEnd,F&&process.env.NODE_ENV!=="production"&&($e?.remove(),$e=Ke(U,X,E));}function Be(t,n){if(e.style.setProperty("--scroll-draw-progress",String(t)),Q){let l=n==="reverse"?1-t:t;e.style.clipPath=se(l);return}$.forEach((l,g)=>{l.style.strokeDashoffset=n==="reverse"?`${h[g]*t}`:`${h[g]*(1-t)}`,c&&(l.style.opacity=n==="reverse"?`${1-t}`:`${t}`),q&&L?l.style.stroke=Pe(q,L,t):L&&(l.style.stroke=L),M!==null&&D!==null?l.style.strokeWidth=`${M+(D-M)*t}`:D!==null&&(l.style.strokeWidth=`${D}`),C!==null&&I!==null?l.style.fillOpacity=`${C+(I-C)*t}`:I!==null&&(l.style.fillOpacity=`${I}`),d&&l.tagName.toLowerCase()==="path"&&B[g]&&l.setAttribute("d",Ve(B[g],d,t));});}function Oe(){if(e.style.setProperty("--scroll-draw-progress","0"),Q){e.style.clipPath=se(0);return}$.forEach((t,n)=>{t.style.strokeDasharray=`${h[n]}`,t.style.strokeDashoffset=p==="reverse"?"0":`${h[n]}`,c?t.style.opacity=p==="reverse"?"1":"0":t.style.opacity="",q&&(t.style.stroke=q),M!==null&&(t.style.strokeWidth=`${M}`),C!==null&&(t.style.fillOpacity=`${C}`),d&&t.tagName.toLowerCase()==="path"&&B[n]&&t.setAttribute("d",B[n]);});}if($.forEach(t=>{Je(t);let n=ke(t);h.push(n),t.tagName.toLowerCase()==="path"?B.push(t.getAttribute("d")??""):B.push(""),s?(t.style.strokeDasharray=`${n}`,t.style.strokeDashoffset=p==="reverse"?`${n}`:"0",c&&(t.style.opacity="1"),L&&(t.style.stroke=L),D!==null&&(t.style.strokeWidth=`${D}`),I!==null&&(t.style.fillOpacity=`${I}`),d&&t.tagName.toLowerCase()==="path"&&t.setAttribute("d",d)):(t.style.strokeDasharray=`${n}`,t.style.strokeDashoffset=p==="reverse"?"0":`${n}`,c?t.style.opacity=p==="reverse"?"1":"0":t.style.opacity="",q&&(t.style.stroke=q),M!==null&&(t.style.strokeWidth=`${M}`),C!==null&&(t.style.fillOpacity=`${C}`));}),Q){if(s)return e.style.clipPath=se(1),z?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};e.style.clipPath=se(0);}else if(s)return z?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};function Ue(){return !(le===false||!Ye()||!$.length||typeof m!="string"||!(m in We)||Q||E!=="y"||S||a!==1||v!==0||V||j||Y!==false||d||ne||N||re>0||K||G||z||T!=null||x!=null||P!=null||(w.start??"top bottom").trim()!=="top bottom"||(w.end??"bottom top").trim()!=="bottom top")}function Xe(){let t=`svg-scroll-draw-${++je}`,n=p==="reverse"?"0":"var(--ssd-len)",l=p==="reverse"?"var(--ssd-len)":"0",g=`stroke-dashoffset:${n};`,te=`stroke-dashoffset:${l};`;c&&(g+=`opacity:${p==="reverse"?1:0};`,te+=`opacity:${p==="reverse"?0:1};`);let k=document.createElement("style");k.setAttribute("data-svg-scroll-draw",""),k.textContent=`@keyframes ${t}{from{${g}}to{${te}}}.${t}{animation-name:${t};animation-duration:auto;animation-timing-function:${We[m]};animation-fill-mode:both;animation-timeline:view();animation-range:cover 0% cover 100%;}`,document.head.appendChild(k);function i(f,R){f.style.setProperty("--ssd-len",String(h[R])),f.style.strokeDasharray=`${h[R]}`,f.style.strokeDashoffset="",f.style.opacity="",f.style.animationPlayState="",f.classList.add(t);}$.forEach(i);let u=false,b=-1;function y(){if(b>=0)return b;let f=e.getBoundingClientRect(),{tStart:R,tEnd:fe}=me({top:f.top,height:f.height},ue(),Ce(),Le,Me);return ye(W(ue(),R,fe,a))}return {destroy(){$.forEach(f=>{f.classList.remove(t),f.style.removeProperty("--ssd-len"),f.style.animationPlayState="";}),k.remove();},replay(){u=false,b=-1,$.forEach(i);},pause(){u=true,$.forEach(f=>{f.style.animationPlayState="paused";});},resume(){u&&(u=false,$.forEach(f=>{f.style.animationPlayState="running";}));},seek(f){let R=Math.min(1,Math.max(0,f));b=R,u=true,$.forEach((fe,Re)=>{fe.classList.remove(t),fe.style.strokeDashoffset=p==="reverse"?`${h[Re]*R}`:`${h[Re]*(1-R)}`,c&&(fe.style.opacity=p==="reverse"?`${1-R}`:`${R}`);});},getProgress(){return y()}}}if(Ue())return Xe();Ie();function xe(){if(!ge||ee)return;let t=performance.now(),n=ue(),l=a;if(Y!==false){let i=t-De,u=i>0?Math.abs(n-(Ee<0?n:Ee))/i:0;l=a*Math.max(.2,1+u*(typeof Y=="number"?Y:1)*.04);}Ee=n,De=t;let g=j?he===-1||n>=he?"forward":"reverse":p;he=n;let te=X-U,k=true;if(Q){let i=ye(W(n,U,X,l));V&&!j&&(O=Math.max(O,i),i=O),be=i,e.style.setProperty("--scroll-draw-progress",String(i));let u=g==="reverse"?1-i:i;e.style.clipPath=se(u),K?.(i),!Z&&W(n,U,X,l)>0&&(Z=true,G?.()),i>=1&&!H?(H=true,z?.(),ce<(N==="infinite"?1/0:N??0)&&(ce++,we=setTimeout(()=>{O=-1,Z=false,H=false,e.style.clipPath=se(0);},J))):i<1&&!V&&(H=false),_=requestAnimationFrame(xe);return}if($.forEach((i,u)=>{let b=u*v*te,y=ye(W(n,U+b,X+b,l));V&&!j&&(O=Math.max(O,y),y=O),be=y,i.style.strokeDashoffset=g==="reverse"?`${h[u]*y}`:`${h[u]*(1-y)}`,c&&(i.style.opacity=g==="reverse"?`${1-y}`:`${y}`),q&&L?i.style.stroke=Pe(q,L,y):L&&(i.style.stroke=L),M!==null&&D!==null?i.style.strokeWidth=`${M+(D-M)*y}`:D!==null&&(i.style.strokeWidth=`${D}`),C!==null&&I!==null?i.style.fillOpacity=`${C+(I-C)*y}`:I!==null&&(i.style.fillOpacity=`${I}`),d&&i.tagName.toLowerCase()==="path"&&B[u]&&i.setAttribute("d",Ve(B[u],d,y)),u===0&&(K?.(y),e.style.setProperty("--scroll-draw-progress",String(y))),y<1&&(k=false);}),ne){let i=ye(W(n,U,X,l));for(let u in ne){let b=parseFloat(u);i>=b&&!ve.has(b)&&(ve.add(b),ne[u]?.());}}!Z&&W(n,U,X,l)>0&&(Z=true,G?.()),k&&!H?(H=true,z?.(),ce<(N==="infinite"?1/0:N??0)&&(ce++,we=setTimeout(()=>{O=-1,Z=false,H=false,ve.clear(),Oe();},J))):!k&&!V&&(H=false),_=requestAnimationFrame(xe);}let Te=new IntersectionObserver(t=>{t.forEach(n=>{ge=n.isIntersecting,ge&&!ee?_=requestAnimationFrame(xe):cancelAnimationFrame(_);});},{root:S??null,threshold:pe,rootMargin:de}),Ae;function Se(){clearTimeout(Ae),Ae=setTimeout(()=>{$.forEach((t,n)=>{h[n]=ke(t),t.style.strokeDasharray=`${h[n]}`;}),Ie();},150);}return window.addEventListener("resize",Se),window.addEventListener("orientationchange",Se),re>0?setTimeout(()=>Te.observe(e),re):Te.observe(e),{destroy(){cancelAnimationFrame(_),clearTimeout(we),Te.disconnect(),window.removeEventListener("resize",Se),window.removeEventListener("orientationchange",Se),clearTimeout(Ae),$e?.remove();},replay(){O=-1,he=-1,Ee=-1,Z=false,H=false,ce=0,ee=false,ve.clear(),clearTimeout(we),Oe();},pause(){ee=true,cancelAnimationFrame(_);},resume(){ee&&(ee=false,ge&&(_=requestAnimationFrame(xe)));},seek(t){let n=Math.min(1,Math.max(0,t));be=n,O=n,ee=true,cancelAnimationFrame(_),Be(n,p);},getProgress(){return be}}}function et(e,r={}){let[s,o]=useState(0),a=useRef(r);a.current=r;let c=useRef(-1);return useEffect(()=>{if(typeof window>"u")return;if(window.matchMedia("(prefers-reduced-motion: reduce)").matches){o(1);return}let m=typeof e=="string"?document.querySelector(e):e.current;if(!m)return;let w=m,{axis:v="y",scrollContainer:p,trigger:V={}}=a.current,F=typeof p=="string"?document.querySelector(p):p??null,E=ie(V.start??"top bottom"),ae=ie(V.end??"bottom top"),j=0,re=0,T=false,x=0;function P(){return F?v==="x"?F.scrollLeft:F.scrollTop:v==="x"?window.scrollX:window.scrollY}function ne(){return F?v==="x"?F.clientWidth:F.clientHeight:v==="x"?window.innerWidth:window.innerHeight}function Y(){let d=w.getBoundingClientRect(),A=P(),le=v==="x"?d.left:d.top,K=v==="x"?d.width:d.height,G=me({top:le,height:K},A,ne(),E,ae);j=G.tStart,re=G.tEnd;}function pe(){if(!T)return;let{speed:d=1,easing:A="linear",once:le=false}=a.current,K=typeof A=="function"?A:oe[A]??oe.linear,G=W(P(),j,re,d),z=K(G);le&&(c.current=Math.max(c.current,z),z=c.current),o(z),x=requestAnimationFrame(pe);}let de=new IntersectionObserver(d=>{d.forEach(A=>{T=A.isIntersecting,T?x=requestAnimationFrame(pe):cancelAnimationFrame(x);});},{root:F??null,threshold:0});Y(),de.observe(w);let N;function J(){clearTimeout(N),N=setTimeout(Y,150);}return window.addEventListener("resize",J),window.addEventListener("orientationchange",J),()=>{cancelAnimationFrame(x),clearTimeout(N),de.disconnect(),window.removeEventListener("resize",J),window.removeEventListener("orientationchange",J);}},[]),s}function pt({children:e,className:r,style:s,...o}){let a=useRef(null);return useEffect(()=>{if(!a.current)return;let c=Ge(a.current,o);return ()=>c.destroy()},[]),jsx("div",{ref:a,className:r,style:s,children:e})}export{pt as ScrollDraw,et as useScrollDrawProgress};
|
|
1
|
+
import {useState,useRef,useEffect}from'react';import {jsx}from'react/jsx-runtime';function _e({bounces:e=3,decay:r=.5}={}){let n=Math.max(1,Math.round(e)),s=Math.max(.01,Math.min(.99,r)),a=Math.sqrt(s),u=0,f=[];for(let i=0;i<n;i++){let m=Math.pow(a,i);f.push(m),u+=m;}let y=[0],b=0;for(let i=0;i<n;i++)b+=f[i]/u,y.push(b);return i=>{if(i<=0)return 0;if(i>=1)return 1;for(let m=0;m<n;m++)if(i<=y[m+1]){let E=(i-y[m])/(y[m+1]-y[m]);if(m===0)return E*(2-E);let w=1-Math.pow(s,m);return w+(1-w)*(2*E-1)*(2*E-1)}return 1}}function je({amplitude:e=1,period:r=.4}={}){let n=Math.max(1,e),s=Math.max(.1,r),a=n<=1?s/4:s/(2*Math.PI)*Math.asin(1/n);return u=>u<=0?0:u>=1?1:n*Math.pow(2,-10*u)*Math.sin((u-a)*(2*Math.PI)/s)+1}var oe={linear:e=>e,"ease-in":e=>e*e,"ease-out":e=>e*(2-e),"ease-in-out":e=>e<.5?2*e*e:-1+(4-2*e)*e,spring:e=>1-Math.cos(e*Math.PI*2.5)*Math.pow(1-e,2.2),bounce:_e(),elastic:je()};function ie(e="top bottom"){let r=e.trim();if(/^\d+(\.\d+)?%$/.test(r))return {element:"top",viewport:r};let[n="top",s="bottom"]=r.split(/\s+/).filter(Boolean);return {element:n,viewport:s}}function Fe(e,r,n,s){switch(s){case "top":return e+n;case "center":return e+n+r/2;case "bottom":return e+n+r;default:return e+n}}function Ne(e,r){if(/^\d+(\.\d+)?%$/.test(e))return r*(parseFloat(e)/100);switch(e){case "top":return 0;case "center":return r/2;case "bottom":return r;default:return r}}function Ae(e){let r=e.tagName.toLowerCase();if(r==="rect"){let n=parseFloat(e.getAttribute("width")??"0"),s=parseFloat(e.getAttribute("height")??"0");return 2*(n+s)}if(r==="circle"){let n=parseFloat(e.getAttribute("r")??"0");return 2*Math.PI*n}return e.getTotalLength()}function Ye(e,r,n){return Math.min(n,Math.max(r,e))}function q(e,r,n,s){return n===r?0:Ye((e-r)/(n-r)*s,0,1)}function me(e,r,n,s,a){let u=Fe(e.top,e.height,r,s.element)-Ne(s.viewport,n),f=Fe(e.top,e.height,r,a.element)-Ne(a.viewport,n);return {tStart:u,tEnd:f}}function ze(e){let r=/^#([a-f\d])([a-f\d])([a-f\d])$/i.exec(e);if(r)return [parseInt(r[1]+r[1],16),parseInt(r[2]+r[2],16),parseInt(r[3]+r[3],16)];let n=/^#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(e);if(n)return [parseInt(n[1],16),parseInt(n[2],16),parseInt(n[3],16)];let s=/^rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i.exec(e);return s?[parseInt(s[1]),parseInt(s[2]),parseInt(s[3])]:null}function ke(e,r,n){let s=ze(e),a=ze(r);return !s||!a?e:`rgb(${Math.round(s[0]+(a[0]-s[0])*n)},${Math.round(s[1]+(a[1]-s[1])*n)},${Math.round(s[2]+(a[2]-s[2])*n)})`}function We(e,r){process.env.NODE_ENV!=="production"&&console.warn(`[svg-scroll-draw] ${e}`,r);}var He={linear:"linear","ease-in":"ease-in","ease-out":"ease-out","ease-in-out":"ease-in-out"},Je=0;function Ke(){return typeof CSS<"u"&&typeof CSS.supports=="function"&&CSS.supports("animation-timeline: view()")}function Qe(e){let r=e.getAttribute("stroke"),n=e.getAttribute("fill");!r||r==="none"?We("Element has no stroke \u2014 path will not be visible.",e):n&&n!=="none"&&n!=="transparent"&&We("Element has a fill \u2014 it may obscure the stroke animation.",e);}function Ze(e,r,n){let s=document.createElement("div");s.setAttribute("data-svg-scroll-draw-debug",""),s.style.cssText="position:fixed;pointer-events:none;z-index:9999;font-family:monospace;font-size:11px;top:0;left:0;right:0;bottom:0;";function a(){let u=n==="x"?window.scrollX:window.scrollY,f=e-u,y=r-u,b=n==="x";s.innerHTML=`
|
|
2
|
+
<div style="position:absolute;${b?`left:${f}px;top:0;bottom:0;border-left:2px dashed #22c55e;`:`top:${f}px;left:0;right:0;border-top:2px dashed #22c55e;`}padding:2px 6px;color:#22c55e;background:rgba(0,0,0,.6)">\u25B6 start</div>
|
|
3
|
+
<div style="position:absolute;${b?`left:${y}px;top:0;bottom:0;border-left:2px dashed #ef4444;`:`top:${y}px;left:0;right:0;border-top:2px dashed #ef4444;`}padding:2px 6px;color:#ef4444;background:rgba(0,0,0,.6)">\u25A0 end</div>`;}return document.body.appendChild(s),window.addEventListener("scroll",a,{passive:true}),a(),s}function qe(e,r,n){let s=(r.match(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g)??[]).map(Number),a=0;return e.replace(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g,u=>{let f=parseFloat(u),y=s[a++]??f;return String(+(f+(y-f)*n).toFixed(4))})}function Ve(e,r={}){if(typeof window>"u")return {destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>0};let n=window.matchMedia("(prefers-reduced-motion: reduce)").matches,{selector:s="path, polyline, line, polygon, rect, circle",speed:a=1,fade:u=false,easing:f="linear",trigger:y={},stagger:b=0,direction:i="forward",once:m=false,debug:E=false,axis:w="y",scrollContainer:ae,autoReverse:j=false,delay:re=0,strokeColor:A,strokeWidth:$,fillOpacity:D,waypoints:ne,velocityScale:Y=false,threshold:pe=0,rootMargin:de="0px",repeat:z=0,repeatDelay:J=0,morphTo:g,clip:k,native:le=true,onProgress:K,onStart:V,onComplete:W}=r,Q=k===true?"left":typeof k=="string"?k:false,ye=typeof f=="function"?f:oe[f]??oe.linear,Pe=ie(y.start??"top bottom"),De=ie(y.end??"bottom top"),T=typeof ae=="string"?document.querySelector(ae):ae??null,G=Array.isArray(A)?A[0]:null,L=Array.isArray(A)?A[1]:typeof A=="string"?A:null,C=Array.isArray($)?$[0]:null,I=Array.isArray($)?$[1]:typeof $=="number"?$:null,O=Array.isArray(D)?D[0]:null,R=Array.isArray(D)?D[1]:typeof D=="number"?D:null;function se(t){let o=t*100;switch(Q){case "right":return `inset(0 0 0 ${100-o}%)`;case "top":return `inset(0 0 ${100-o}% 0)`;case "bottom":return `inset(${100-o}% 0 0 0)`;case "center":return `circle(${t*150}% at 50% 50%)`;default:return `inset(0 ${100-o}% 0 0)`}}let M=Q?[]:Array.from(e.querySelectorAll(s)),x=[],B=[],U=0,X=0,H=false,Z=false,_=0,ge=false,F=-1,he=-1,ee=false,be=0,ue=0,we,$e=null,ve=new Set,Ee=-1,Le=performance.now();function ce(){return T?w==="x"?T.scrollLeft:T.scrollTop:w==="x"?window.scrollX:window.scrollY}function Ce(){return T?w==="x"?T.clientWidth:T.clientHeight:w==="x"?window.innerWidth:window.innerHeight}function Ie(){let t=e.getBoundingClientRect(),o,c,v;if(T){let P=T.getBoundingClientRect();o=w==="x"?t.left-P.left+T.scrollLeft:t.top-P.top+T.scrollTop,c=w==="x"?t.width:t.height,v=ce();}else o=w==="x"?t.left:t.top,c=w==="x"?t.width:t.height,v=ce();let te=me({top:o,height:c},v,Ce(),Pe,De);U=te.tStart,X=te.tEnd,E&&process.env.NODE_ENV!=="production"&&($e?.remove(),$e=Ze(U,X,w));}function Be(t,o){if(e.style.setProperty("--scroll-draw-progress",String(t)),Q){let c=o==="reverse"?1-t:t;e.style.clipPath=se(c);return}M.forEach((c,v)=>{c.style.strokeDashoffset=o==="reverse"?`${x[v]*t}`:`${x[v]*(1-t)}`,u&&(c.style.opacity=o==="reverse"?`${1-t}`:`${t}`),G&&L?c.style.stroke=ke(G,L,t):L&&(c.style.stroke=L),C!==null&&I!==null?c.style.strokeWidth=`${C+(I-C)*t}`:I!==null&&(c.style.strokeWidth=`${I}`),O!==null&&R!==null?c.style.fillOpacity=`${O+(R-O)*t}`:R!==null&&(c.style.fillOpacity=`${R}`),g&&c.tagName.toLowerCase()==="path"&&B[v]&&c.setAttribute("d",qe(B[v],g,t));});}function Oe(){if(e.style.setProperty("--scroll-draw-progress","0"),Q){e.style.clipPath=se(0);return}M.forEach((t,o)=>{t.style.strokeDasharray=`${x[o]}`,t.style.strokeDashoffset=i==="reverse"?"0":`${x[o]}`,u?t.style.opacity=i==="reverse"?"1":"0":t.style.opacity="",G&&(t.style.stroke=G),C!==null&&(t.style.strokeWidth=`${C}`),O!==null&&(t.style.fillOpacity=`${O}`),g&&t.tagName.toLowerCase()==="path"&&B[o]&&t.setAttribute("d",B[o]);});}if(M.forEach(t=>{Qe(t);let o=Ae(t);x.push(o),t.tagName.toLowerCase()==="path"?B.push(t.getAttribute("d")??""):B.push(""),n?(t.style.strokeDasharray=`${o}`,t.style.strokeDashoffset=i==="reverse"?`${o}`:"0",u&&(t.style.opacity="1"),L&&(t.style.stroke=L),I!==null&&(t.style.strokeWidth=`${I}`),R!==null&&(t.style.fillOpacity=`${R}`),g&&t.tagName.toLowerCase()==="path"&&t.setAttribute("d",g)):(t.style.strokeDasharray=`${o}`,t.style.strokeDashoffset=i==="reverse"?"0":`${o}`,u?t.style.opacity=i==="reverse"?"1":"0":t.style.opacity="",G&&(t.style.stroke=G),C!==null&&(t.style.strokeWidth=`${C}`),O!==null&&(t.style.fillOpacity=`${O}`));}),Q){if(n)return e.style.clipPath=se(1),W?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};e.style.clipPath=se(0);}else if(n)return W?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};function Ue(){return !(le===false||!Ke()||!M.length||typeof f!="string"||!(f in He)||Q||w!=="y"||T||a!==1||b!==0||m||j||Y!==false||g||ne||z||re>0||K||V||W||A!=null||$!=null||D!=null||(y.start??"top bottom").trim()!=="top bottom"||(y.end??"bottom top").trim()!=="bottom top")}function Xe(){let t=`svg-scroll-draw-${++Je}`,o=i==="reverse"?"0":"var(--ssd-len)",c=i==="reverse"?"var(--ssd-len)":"0",v=`stroke-dashoffset:${o};`,te=`stroke-dashoffset:${c};`;u&&(v+=`opacity:${i==="reverse"?1:0};`,te+=`opacity:${i==="reverse"?0:1};`);let P=document.createElement("style");P.setAttribute("data-svg-scroll-draw",""),P.textContent=`@keyframes ${t}{from{${v}}to{${te}}}.${t}{animation-name:${t};animation-duration:auto;animation-timing-function:${He[f]};animation-fill-mode:both;animation-timeline:view();animation-range:cover 0% cover 100%;}`,document.head.appendChild(P);function l(d,N){d.style.setProperty("--ssd-len",String(x[N])),d.style.strokeDasharray=`${x[N]}`,d.style.strokeDashoffset="",d.style.opacity="",d.style.animationPlayState="",d.classList.add(t);}M.forEach(l);let p=false,S=-1;function h(){if(S>=0)return S;let d=e.getBoundingClientRect(),{tStart:N,tEnd:fe}=me({top:d.top,height:d.height},ce(),Ce(),Pe,De);return ye(q(ce(),N,fe,a))}return {destroy(){M.forEach(d=>{d.classList.remove(t),d.style.removeProperty("--ssd-len"),d.style.animationPlayState="";}),P.remove();},replay(){p=false,S=-1,M.forEach(l);},pause(){p=true,M.forEach(d=>{d.style.animationPlayState="paused";});},resume(){p&&(p=false,M.forEach(d=>{d.style.animationPlayState="running";}));},seek(d){let N=Math.min(1,Math.max(0,d));S=N,p=true,M.forEach((fe,Re)=>{fe.classList.remove(t),fe.style.strokeDashoffset=i==="reverse"?`${x[Re]*N}`:`${x[Re]*(1-N)}`,u&&(fe.style.opacity=i==="reverse"?`${1-N}`:`${N}`);});},getProgress(){return h()}}}if(Ue())return Xe();Ie();function xe(){if(!ge||ee)return;let t=performance.now(),o=ce(),c=a;if(Y!==false){let l=t-Le,p=l>0?Math.abs(o-(Ee<0?o:Ee))/l:0;c=a*Math.max(.2,1+p*(typeof Y=="number"?Y:1)*.04);}Ee=o,Le=t;let v=j?he===-1||o>=he?"forward":"reverse":i;he=o;let te=X-U,P=true;if(Q){let l=ye(q(o,U,X,c));m&&!j&&(F=Math.max(F,l),l=F),be=l,e.style.setProperty("--scroll-draw-progress",String(l));let p=v==="reverse"?1-l:l;e.style.clipPath=se(p),K?.(l),!Z&&q(o,U,X,c)>0&&(Z=true,V?.()),l>=1&&!H?(H=true,W?.(),ue<(z==="infinite"?1/0:z??0)&&(ue++,we=setTimeout(()=>{F=-1,Z=false,H=false,e.style.clipPath=se(0);},J))):l<1&&!m&&(H=false),_=requestAnimationFrame(xe);return}if(M.forEach((l,p)=>{let S=p*b*te,h=ye(q(o,U+S,X+S,c));m&&!j&&(F=Math.max(F,h),h=F),be=h,l.style.strokeDashoffset=v==="reverse"?`${x[p]*h}`:`${x[p]*(1-h)}`,u&&(l.style.opacity=v==="reverse"?`${1-h}`:`${h}`),G&&L?l.style.stroke=ke(G,L,h):L&&(l.style.stroke=L),C!==null&&I!==null?l.style.strokeWidth=`${C+(I-C)*h}`:I!==null&&(l.style.strokeWidth=`${I}`),O!==null&&R!==null?l.style.fillOpacity=`${O+(R-O)*h}`:R!==null&&(l.style.fillOpacity=`${R}`),g&&l.tagName.toLowerCase()==="path"&&B[p]&&l.setAttribute("d",qe(B[p],g,h)),p===0&&(K?.(h),e.style.setProperty("--scroll-draw-progress",String(h))),h<1&&(P=false);}),ne){let l=ye(q(o,U,X,c));for(let p in ne){let S=parseFloat(p);l>=S&&!ve.has(S)&&(ve.add(S),ne[p]?.());}}!Z&&q(o,U,X,c)>0&&(Z=true,V?.()),P&&!H?(H=true,W?.(),ue<(z==="infinite"?1/0:z??0)&&(ue++,we=setTimeout(()=>{F=-1,Z=false,H=false,ve.clear(),Oe();},J))):!P&&!m&&(H=false),_=requestAnimationFrame(xe);}let Te=new IntersectionObserver(t=>{t.forEach(o=>{ge=o.isIntersecting,ge&&!ee?_=requestAnimationFrame(xe):cancelAnimationFrame(_);});},{root:T??null,threshold:pe,rootMargin:de}),Me;function Se(){clearTimeout(Me),Me=setTimeout(()=>{M.forEach((t,o)=>{x[o]=Ae(t),t.style.strokeDasharray=`${x[o]}`;}),Ie();},150);}return window.addEventListener("resize",Se),window.addEventListener("orientationchange",Se),re>0?setTimeout(()=>Te.observe(e),re):Te.observe(e),{destroy(){cancelAnimationFrame(_),clearTimeout(we),Te.disconnect(),window.removeEventListener("resize",Se),window.removeEventListener("orientationchange",Se),clearTimeout(Me),$e?.remove();},replay(){F=-1,he=-1,Ee=-1,Z=false,H=false,ue=0,ee=false,ve.clear(),clearTimeout(we),Oe();},pause(){ee=true,cancelAnimationFrame(_);},resume(){ee&&(ee=false,ge&&(_=requestAnimationFrame(xe)));},seek(t){let o=Math.min(1,Math.max(0,t));be=o,F=o,ee=true,cancelAnimationFrame(_),Be(o,i);},getProgress(){return be}}}function rt(e,r={}){let[n,s]=useState(0),a=useRef(r);a.current=r;let u=useRef(-1);return useEffect(()=>{if(typeof window>"u")return;if(window.matchMedia("(prefers-reduced-motion: reduce)").matches){s(1);return}let f=typeof e=="string"?document.querySelector(e):e.current;if(!f)return;let y=f,{axis:b="y",scrollContainer:i,trigger:m={}}=a.current,E=typeof i=="string"?document.querySelector(i):i??null,w=ie(m.start??"top bottom"),ae=ie(m.end??"bottom top"),j=0,re=0,A=false,$=0;function D(){return E?b==="x"?E.scrollLeft:E.scrollTop:b==="x"?window.scrollX:window.scrollY}function ne(){return E?b==="x"?E.clientWidth:E.clientHeight:b==="x"?window.innerWidth:window.innerHeight}function Y(){let g=y.getBoundingClientRect(),k=D(),le=b==="x"?g.left:g.top,K=b==="x"?g.width:g.height,V=me({top:le,height:K},k,ne(),w,ae);j=V.tStart,re=V.tEnd;}function pe(){if(!A)return;let{speed:g=1,easing:k="linear",once:le=false}=a.current,K=typeof k=="function"?k:oe[k]??oe.linear,V=q(D(),j,re,g),W=K(V);le&&(u.current=Math.max(u.current,W),W=u.current),s(W),$=requestAnimationFrame(pe);}let de=new IntersectionObserver(g=>{g.forEach(k=>{A=k.isIntersecting,A?$=requestAnimationFrame(pe):cancelAnimationFrame($);});},{root:E??null,threshold:0});Y(),de.observe(y);let z;function J(){clearTimeout(z),z=setTimeout(Y,150);}return window.addEventListener("resize",J),window.addEventListener("orientationchange",J),()=>{cancelAnimationFrame($),clearTimeout(z),de.disconnect(),window.removeEventListener("resize",J),window.removeEventListener("orientationchange",J);}},[]),n}function yt({children:e,className:r,style:n,...s}){let a=useRef(null);return useEffect(()=>{if(!a.current)return;let u=Ve(a.current,s);return ()=>u.destroy()},[]),jsx("div",{ref:a,className:r,style:n,children:e})}export{yt as ScrollDraw,rt as useScrollDrawProgress};
|
package/dist/solid/index.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
'use strict';var solidJs=require('solid-js');var ve={linear:e=>e,"ease-in":e=>e*e,"ease-out":e=>e*(2-e),"ease-in-out":e=>e<.5?2*e*e:-1+(4-2*e)*e,spring:e=>1-Math.cos(e*Math.PI*2.5)*Math.pow(1-e,2.2)};function
|
|
2
|
-
<div style="position:absolute;${
|
|
3
|
-
<div style="position:absolute;${
|
|
1
|
+
'use strict';var solidJs=require('solid-js');function je({bounces:e=3,decay:r=.5}={}){let n=Math.max(1,Math.round(e)),o=Math.max(.01,Math.min(.99,r)),f=Math.sqrt(o),u=0,p=[];for(let a=0;a<n;a++){let d=Math.pow(f,a);p.push(d),u+=d;}let h=[0],I=0;for(let a=0;a<n;a++)I+=p[a]/u,h.push(I);return a=>{if(a<=0)return 0;if(a>=1)return 1;for(let d=0;d<n;d++)if(a<=h[d+1]){let U=(a-h[d])/(h[d+1]-h[d]);if(d===0)return U*(2-U);let b=1-Math.pow(o,d);return b+(1-b)*(2*U-1)*(2*U-1)}return 1}}function Je({amplitude:e=1,period:r=.4}={}){let n=Math.max(1,e),o=Math.max(.1,r),f=n<=1?o/4:o/(2*Math.PI)*Math.asin(1/n);return u=>u<=0?0:u>=1?1:n*Math.pow(2,-10*u)*Math.sin((u-f)*(2*Math.PI)/o)+1}var ve={linear:e=>e,"ease-in":e=>e*e,"ease-out":e=>e*(2-e),"ease-in-out":e=>e<.5?2*e*e:-1+(4-2*e)*e,spring:e=>1-Math.cos(e*Math.PI*2.5)*Math.pow(1-e,2.2),bounce:je(),elastic:Je()};function Ee(e="top bottom"){let r=e.trim();if(/^\d+(\.\d+)?%$/.test(r))return {element:"top",viewport:r};let[n="top",o="bottom"]=r.split(/\s+/).filter(Boolean);return {element:n,viewport:o}}function Oe(e,r,n,o){switch(o){case "top":return e+n;case "center":return e+n+r/2;case "bottom":return e+n+r;default:return e+n}}function Fe(e,r){if(/^\d+(\.\d+)?%$/.test(e))return r*(parseFloat(e)/100);switch(e){case "top":return 0;case "center":return r/2;case "bottom":return r;default:return r}}function xe(e){let r=e.tagName.toLowerCase();if(r==="rect"){let n=parseFloat(e.getAttribute("width")??"0"),o=parseFloat(e.getAttribute("height")??"0");return 2*(n+o)}if(r==="circle"){let n=parseFloat(e.getAttribute("r")??"0");return 2*Math.PI*n}return e.getTotalLength()}function Ke(e,r,n){return Math.min(n,Math.max(r,e))}function X(e,r,n,o){return n===r?0:Ke((e-r)/(n-r)*o,0,1)}function $e(e,r,n,o,f){let u=Oe(e.top,e.height,r,o.element)-Fe(o.viewport,n),p=Oe(e.top,e.height,r,f.element)-Fe(f.viewport,n);return {tStart:u,tEnd:p}}function Ne(e){let r=/^#([a-f\d])([a-f\d])([a-f\d])$/i.exec(e);if(r)return [parseInt(r[1]+r[1],16),parseInt(r[2]+r[2],16),parseInt(r[3]+r[3],16)];let n=/^#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(e);if(n)return [parseInt(n[1],16),parseInt(n[2],16),parseInt(n[3],16)];let o=/^rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i.exec(e);return o?[parseInt(o[1]),parseInt(o[2]),parseInt(o[3])]:null}function Se(e,r,n){let o=Ne(e),f=Ne(r);return !o||!f?e:`rgb(${Math.round(o[0]+(f[0]-o[0])*n)},${Math.round(o[1]+(f[1]-o[1])*n)},${Math.round(o[2]+(f[2]-o[2])*n)})`}function He(e,r){process.env.NODE_ENV!=="production"&&console.warn(`[svg-scroll-draw] ${e}`,r);}var Re={linear:"linear","ease-in":"ease-in","ease-out":"ease-out","ease-in-out":"ease-in-out"},Qe=0;function Ye(){return typeof CSS<"u"&&typeof CSS.supports=="function"&&CSS.supports("animation-timeline: view()")}function Ze(e){let r=e.getAttribute("stroke"),n=e.getAttribute("fill");!r||r==="none"?He("Element has no stroke \u2014 path will not be visible.",e):n&&n!=="none"&&n!=="transparent"&&He("Element has a fill \u2014 it may obscure the stroke animation.",e);}function et(e,r,n){let o=document.createElement("div");o.setAttribute("data-svg-scroll-draw-debug",""),o.style.cssText="position:fixed;pointer-events:none;z-index:9999;font-family:monospace;font-size:11px;top:0;left:0;right:0;bottom:0;";function f(){let u=n==="x"?window.scrollX:window.scrollY,p=e-u,h=r-u,I=n==="x";o.innerHTML=`
|
|
2
|
+
<div style="position:absolute;${I?`left:${p}px;top:0;bottom:0;border-left:2px dashed #22c55e;`:`top:${p}px;left:0;right:0;border-top:2px dashed #22c55e;`}padding:2px 6px;color:#22c55e;background:rgba(0,0,0,.6)">\u25B6 start</div>
|
|
3
|
+
<div style="position:absolute;${I?`left:${h}px;top:0;bottom:0;border-left:2px dashed #ef4444;`:`top:${h}px;left:0;right:0;border-top:2px dashed #ef4444;`}padding:2px 6px;color:#ef4444;background:rgba(0,0,0,.6)">\u25A0 end</div>`;}return document.body.appendChild(o),window.addEventListener("scroll",f,{passive:true}),f(),o}function We(e,r,n){let o=(r.match(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g)??[]).map(Number),f=0;return e.replace(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g,u=>{let p=parseFloat(u),h=o[f++]??p;return String(+(p+(h-p)*n).toFixed(4))})}function Me(e,r={}){if(typeof window>"u")return {destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>0};let n=window.matchMedia("(prefers-reduced-motion: reduce)").matches,{selector:o="path, polyline, line, polygon, rect, circle",speed:f=1,fade:u=false,easing:p="linear",trigger:h={},stagger:I=0,direction:a="forward",once:d=false,debug:U=false,axis:b="y",scrollContainer:me,autoReverse:ee=false,delay:pe=0,strokeColor:W,strokeWidth:V,fillOpacity:G,waypoints:te,velocityScale:re=false,threshold:ze=0,rootMargin:qe="0px",repeat:J=0,repeatDelay:ke=0,morphTo:C,clip:de,native:Be=true,onProgress:ye,onStart:he,onComplete:K}=r,z=de===true?"left":typeof de=="string"?de:false,ne=typeof p=="function"?p:ve[p]??ve.linear,Te=Ee(h.start??"top bottom"),Ae=Ee(h.end??"bottom top"),E=typeof me=="string"?document.querySelector(me):me??null,O=Array.isArray(W)?W[0]:null,S=Array.isArray(W)?W[1]:typeof W=="string"?W:null,M=Array.isArray(V)?V[0]:null,k=Array.isArray(V)?V[1]:typeof V=="number"?V:null,T=Array.isArray(G)?G[0]:null,A=Array.isArray(G)?G[1]:typeof G=="number"?G:null;function j(t){let s=t*100;switch(z){case "right":return `inset(0 0 0 ${100-s}%)`;case "top":return `inset(0 0 ${100-s}% 0)`;case "bottom":return `inset(${100-s}% 0 0 0)`;case "center":return `circle(${t*150}% at 50% 50%)`;default:return `inset(0 ${100-s}% 0 0)`}}let x=z?[]:Array.from(e.querySelectorAll(o)),w=[],F=[],N=0,H=0,P=false,q=false,R=0,se=false,L=-1,oe=-1,B=false,ie=0,Q=0,ae,ge=null,le=new Set,ue=-1,Le=performance.now();function Y(){return E?b==="x"?E.scrollLeft:E.scrollTop:b==="x"?window.scrollX:window.scrollY}function De(){return E?b==="x"?E.clientWidth:E.clientHeight:b==="x"?window.innerWidth:window.innerHeight}function Pe(){let t=e.getBoundingClientRect(),s,l,g;if(E){let $=E.getBoundingClientRect();s=b==="x"?t.left-$.left+E.scrollLeft:t.top-$.top+E.scrollTop,l=b==="x"?t.width:t.height,g=Y();}else s=b==="x"?t.left:t.top,l=b==="x"?t.width:t.height,g=Y();let _=$e({top:s,height:l},g,De(),Te,Ae);N=_.tStart,H=_.tEnd,U&&process.env.NODE_ENV!=="production"&&(ge?.remove(),ge=et(N,H,b));}function _e(t,s){if(e.style.setProperty("--scroll-draw-progress",String(t)),z){let l=s==="reverse"?1-t:t;e.style.clipPath=j(l);return}x.forEach((l,g)=>{l.style.strokeDashoffset=s==="reverse"?`${w[g]*t}`:`${w[g]*(1-t)}`,u&&(l.style.opacity=s==="reverse"?`${1-t}`:`${t}`),O&&S?l.style.stroke=Se(O,S,t):S&&(l.style.stroke=S),M!==null&&k!==null?l.style.strokeWidth=`${M+(k-M)*t}`:k!==null&&(l.style.strokeWidth=`${k}`),T!==null&&A!==null?l.style.fillOpacity=`${T+(A-T)*t}`:A!==null&&(l.style.fillOpacity=`${A}`),C&&l.tagName.toLowerCase()==="path"&&F[g]&&l.setAttribute("d",We(F[g],C,t));});}function Ie(){if(e.style.setProperty("--scroll-draw-progress","0"),z){e.style.clipPath=j(0);return}x.forEach((t,s)=>{t.style.strokeDasharray=`${w[s]}`,t.style.strokeDashoffset=a==="reverse"?"0":`${w[s]}`,u?t.style.opacity=a==="reverse"?"1":"0":t.style.opacity="",O&&(t.style.stroke=O),M!==null&&(t.style.strokeWidth=`${M}`),T!==null&&(t.style.fillOpacity=`${T}`),C&&t.tagName.toLowerCase()==="path"&&F[s]&&t.setAttribute("d",F[s]);});}if(x.forEach(t=>{Ze(t);let s=xe(t);w.push(s),t.tagName.toLowerCase()==="path"?F.push(t.getAttribute("d")??""):F.push(""),n?(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=a==="reverse"?`${s}`:"0",u&&(t.style.opacity="1"),S&&(t.style.stroke=S),k!==null&&(t.style.strokeWidth=`${k}`),A!==null&&(t.style.fillOpacity=`${A}`),C&&t.tagName.toLowerCase()==="path"&&t.setAttribute("d",C)):(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=a==="reverse"?"0":`${s}`,u?t.style.opacity=a==="reverse"?"1":"0":t.style.opacity="",O&&(t.style.stroke=O),M!==null&&(t.style.strokeWidth=`${M}`),T!==null&&(t.style.fillOpacity=`${T}`));}),z){if(n)return e.style.clipPath=j(1),K?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};e.style.clipPath=j(0);}else if(n)return K?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};function Xe(){return !(Be===false||!Ye()||!x.length||typeof p!="string"||!(p in Re)||z||b!=="y"||E||f!==1||I!==0||d||ee||re!==false||C||te||J||pe>0||ye||he||K||W!=null||V!=null||G!=null||(h.start??"top bottom").trim()!=="top bottom"||(h.end??"bottom top").trim()!=="bottom top")}function Ue(){let t=`svg-scroll-draw-${++Qe}`,s=a==="reverse"?"0":"var(--ssd-len)",l=a==="reverse"?"var(--ssd-len)":"0",g=`stroke-dashoffset:${s};`,_=`stroke-dashoffset:${l};`;u&&(g+=`opacity:${a==="reverse"?1:0};`,_+=`opacity:${a==="reverse"?0:1};`);let $=document.createElement("style");$.setAttribute("data-svg-scroll-draw",""),$.textContent=`@keyframes ${t}{from{${g}}to{${_}}}.${t}{animation-name:${t};animation-duration:auto;animation-timing-function:${Re[p]};animation-fill-mode:both;animation-timeline:view();animation-range:cover 0% cover 100%;}`,document.head.appendChild($);function i(m,D){m.style.setProperty("--ssd-len",String(w[D])),m.style.strokeDasharray=`${w[D]}`,m.style.strokeDashoffset="",m.style.opacity="",m.style.animationPlayState="",m.classList.add(t);}x.forEach(i);let c=false,v=-1;function y(){if(v>=0)return v;let m=e.getBoundingClientRect(),{tStart:D,tEnd:Z}=$e({top:m.top,height:m.height},Y(),De(),Te,Ae);return ne(X(Y(),D,Z,f))}return {destroy(){x.forEach(m=>{m.classList.remove(t),m.style.removeProperty("--ssd-len"),m.style.animationPlayState="";}),$.remove();},replay(){c=false,v=-1,x.forEach(i);},pause(){c=true,x.forEach(m=>{m.style.animationPlayState="paused";});},resume(){c&&(c=false,x.forEach(m=>{m.style.animationPlayState="running";}));},seek(m){let D=Math.min(1,Math.max(0,m));v=D,c=true,x.forEach((Z,Ce)=>{Z.classList.remove(t),Z.style.strokeDashoffset=a==="reverse"?`${w[Ce]*D}`:`${w[Ce]*(1-D)}`,u&&(Z.style.opacity=a==="reverse"?`${1-D}`:`${D}`);});},getProgress(){return y()}}}if(Xe())return Ue();Pe();function fe(){if(!se||B)return;let t=performance.now(),s=Y(),l=f;if(re!==false){let i=t-Le,c=i>0?Math.abs(s-(ue<0?s:ue))/i:0;l=f*Math.max(.2,1+c*(typeof re=="number"?re:1)*.04);}ue=s,Le=t;let g=ee?oe===-1||s>=oe?"forward":"reverse":a;oe=s;let _=H-N,$=true;if(z){let i=ne(X(s,N,H,l));d&&!ee&&(L=Math.max(L,i),i=L),ie=i,e.style.setProperty("--scroll-draw-progress",String(i));let c=g==="reverse"?1-i:i;e.style.clipPath=j(c),ye?.(i),!q&&X(s,N,H,l)>0&&(q=true,he?.()),i>=1&&!P?(P=true,K?.(),Q<(J==="infinite"?1/0:J??0)&&(Q++,ae=setTimeout(()=>{L=-1,q=false,P=false,e.style.clipPath=j(0);},ke))):i<1&&!d&&(P=false),R=requestAnimationFrame(fe);return}if(x.forEach((i,c)=>{let v=c*I*_,y=ne(X(s,N+v,H+v,l));d&&!ee&&(L=Math.max(L,y),y=L),ie=y,i.style.strokeDashoffset=g==="reverse"?`${w[c]*y}`:`${w[c]*(1-y)}`,u&&(i.style.opacity=g==="reverse"?`${1-y}`:`${y}`),O&&S?i.style.stroke=Se(O,S,y):S&&(i.style.stroke=S),M!==null&&k!==null?i.style.strokeWidth=`${M+(k-M)*y}`:k!==null&&(i.style.strokeWidth=`${k}`),T!==null&&A!==null?i.style.fillOpacity=`${T+(A-T)*y}`:A!==null&&(i.style.fillOpacity=`${A}`),C&&i.tagName.toLowerCase()==="path"&&F[c]&&i.setAttribute("d",We(F[c],C,y)),c===0&&(ye?.(y),e.style.setProperty("--scroll-draw-progress",String(y))),y<1&&($=false);}),te){let i=ne(X(s,N,H,l));for(let c in te){let v=parseFloat(c);i>=v&&!le.has(v)&&(le.add(v),te[c]?.());}}!q&&X(s,N,H,l)>0&&(q=true,he?.()),$&&!P?(P=true,K?.(),Q<(J==="infinite"?1/0:J??0)&&(Q++,ae=setTimeout(()=>{L=-1,q=false,P=false,le.clear(),Ie();},ke))):!$&&!d&&(P=false),R=requestAnimationFrame(fe);}let be=new IntersectionObserver(t=>{t.forEach(s=>{se=s.isIntersecting,se&&!B?R=requestAnimationFrame(fe):cancelAnimationFrame(R);});},{root:E??null,threshold:ze,rootMargin:qe}),we;function ce(){clearTimeout(we),we=setTimeout(()=>{x.forEach((t,s)=>{w[s]=xe(t),t.style.strokeDasharray=`${w[s]}`;}),Pe();},150);}return window.addEventListener("resize",ce),window.addEventListener("orientationchange",ce),pe>0?setTimeout(()=>be.observe(e),pe):be.observe(e),{destroy(){cancelAnimationFrame(R),clearTimeout(ae),be.disconnect(),window.removeEventListener("resize",ce),window.removeEventListener("orientationchange",ce),clearTimeout(we),ge?.remove();},replay(){L=-1,oe=-1,ue=-1,q=false,P=false,Q=0,B=false,le.clear(),clearTimeout(ae),Ie();},pause(){B=true,cancelAnimationFrame(R);},resume(){B&&(B=false,se&&(R=requestAnimationFrame(fe)));},seek(t){let s=Math.min(1,Math.max(0,t));ie=s,L=s,B=true,cancelAnimationFrame(R),_e(s,a);},getProgress(){return ie}}}function it(e={}){let r,n;return solidJs.onMount(()=>{r&&(n=Me(r,e));}),solidJs.onCleanup(()=>{n?.destroy();}),o=>{r=o;}}function at(e={}){let r,n;return solidJs.onMount(()=>{r&&(n=Me(r,e));}),solidJs.onCleanup(()=>{n?.destroy(),n=void 0;}),{ref:o=>{r=o;},getInstance:()=>n}}exports.createScrollDraw=at;exports.useScrollDraw=it;
|
package/dist/solid/index.d.mts
CHANGED
package/dist/solid/index.d.ts
CHANGED
package/dist/solid/index.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {onMount,onCleanup}from'solid-js';var ve={linear:e=>e,"ease-in":e=>e*e,"ease-out":e=>e*(2-e),"ease-in-out":e=>e<.5?2*e*e:-1+(4-2*e)*e,spring:e=>1-Math.cos(e*Math.PI*2.5)*Math.pow(1-e,2.2)};function
|
|
2
|
-
<div style="position:absolute;${
|
|
3
|
-
<div style="position:absolute;${
|
|
1
|
+
import {onMount,onCleanup}from'solid-js';function je({bounces:e=3,decay:r=.5}={}){let n=Math.max(1,Math.round(e)),o=Math.max(.01,Math.min(.99,r)),f=Math.sqrt(o),u=0,p=[];for(let a=0;a<n;a++){let d=Math.pow(f,a);p.push(d),u+=d;}let h=[0],I=0;for(let a=0;a<n;a++)I+=p[a]/u,h.push(I);return a=>{if(a<=0)return 0;if(a>=1)return 1;for(let d=0;d<n;d++)if(a<=h[d+1]){let U=(a-h[d])/(h[d+1]-h[d]);if(d===0)return U*(2-U);let b=1-Math.pow(o,d);return b+(1-b)*(2*U-1)*(2*U-1)}return 1}}function Je({amplitude:e=1,period:r=.4}={}){let n=Math.max(1,e),o=Math.max(.1,r),f=n<=1?o/4:o/(2*Math.PI)*Math.asin(1/n);return u=>u<=0?0:u>=1?1:n*Math.pow(2,-10*u)*Math.sin((u-f)*(2*Math.PI)/o)+1}var ve={linear:e=>e,"ease-in":e=>e*e,"ease-out":e=>e*(2-e),"ease-in-out":e=>e<.5?2*e*e:-1+(4-2*e)*e,spring:e=>1-Math.cos(e*Math.PI*2.5)*Math.pow(1-e,2.2),bounce:je(),elastic:Je()};function Ee(e="top bottom"){let r=e.trim();if(/^\d+(\.\d+)?%$/.test(r))return {element:"top",viewport:r};let[n="top",o="bottom"]=r.split(/\s+/).filter(Boolean);return {element:n,viewport:o}}function Oe(e,r,n,o){switch(o){case "top":return e+n;case "center":return e+n+r/2;case "bottom":return e+n+r;default:return e+n}}function Fe(e,r){if(/^\d+(\.\d+)?%$/.test(e))return r*(parseFloat(e)/100);switch(e){case "top":return 0;case "center":return r/2;case "bottom":return r;default:return r}}function xe(e){let r=e.tagName.toLowerCase();if(r==="rect"){let n=parseFloat(e.getAttribute("width")??"0"),o=parseFloat(e.getAttribute("height")??"0");return 2*(n+o)}if(r==="circle"){let n=parseFloat(e.getAttribute("r")??"0");return 2*Math.PI*n}return e.getTotalLength()}function Ke(e,r,n){return Math.min(n,Math.max(r,e))}function X(e,r,n,o){return n===r?0:Ke((e-r)/(n-r)*o,0,1)}function $e(e,r,n,o,f){let u=Oe(e.top,e.height,r,o.element)-Fe(o.viewport,n),p=Oe(e.top,e.height,r,f.element)-Fe(f.viewport,n);return {tStart:u,tEnd:p}}function Ne(e){let r=/^#([a-f\d])([a-f\d])([a-f\d])$/i.exec(e);if(r)return [parseInt(r[1]+r[1],16),parseInt(r[2]+r[2],16),parseInt(r[3]+r[3],16)];let n=/^#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(e);if(n)return [parseInt(n[1],16),parseInt(n[2],16),parseInt(n[3],16)];let o=/^rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i.exec(e);return o?[parseInt(o[1]),parseInt(o[2]),parseInt(o[3])]:null}function Se(e,r,n){let o=Ne(e),f=Ne(r);return !o||!f?e:`rgb(${Math.round(o[0]+(f[0]-o[0])*n)},${Math.round(o[1]+(f[1]-o[1])*n)},${Math.round(o[2]+(f[2]-o[2])*n)})`}function He(e,r){process.env.NODE_ENV!=="production"&&console.warn(`[svg-scroll-draw] ${e}`,r);}var Re={linear:"linear","ease-in":"ease-in","ease-out":"ease-out","ease-in-out":"ease-in-out"},Qe=0;function Ye(){return typeof CSS<"u"&&typeof CSS.supports=="function"&&CSS.supports("animation-timeline: view()")}function Ze(e){let r=e.getAttribute("stroke"),n=e.getAttribute("fill");!r||r==="none"?He("Element has no stroke \u2014 path will not be visible.",e):n&&n!=="none"&&n!=="transparent"&&He("Element has a fill \u2014 it may obscure the stroke animation.",e);}function et(e,r,n){let o=document.createElement("div");o.setAttribute("data-svg-scroll-draw-debug",""),o.style.cssText="position:fixed;pointer-events:none;z-index:9999;font-family:monospace;font-size:11px;top:0;left:0;right:0;bottom:0;";function f(){let u=n==="x"?window.scrollX:window.scrollY,p=e-u,h=r-u,I=n==="x";o.innerHTML=`
|
|
2
|
+
<div style="position:absolute;${I?`left:${p}px;top:0;bottom:0;border-left:2px dashed #22c55e;`:`top:${p}px;left:0;right:0;border-top:2px dashed #22c55e;`}padding:2px 6px;color:#22c55e;background:rgba(0,0,0,.6)">\u25B6 start</div>
|
|
3
|
+
<div style="position:absolute;${I?`left:${h}px;top:0;bottom:0;border-left:2px dashed #ef4444;`:`top:${h}px;left:0;right:0;border-top:2px dashed #ef4444;`}padding:2px 6px;color:#ef4444;background:rgba(0,0,0,.6)">\u25A0 end</div>`;}return document.body.appendChild(o),window.addEventListener("scroll",f,{passive:true}),f(),o}function We(e,r,n){let o=(r.match(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g)??[]).map(Number),f=0;return e.replace(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g,u=>{let p=parseFloat(u),h=o[f++]??p;return String(+(p+(h-p)*n).toFixed(4))})}function Me(e,r={}){if(typeof window>"u")return {destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>0};let n=window.matchMedia("(prefers-reduced-motion: reduce)").matches,{selector:o="path, polyline, line, polygon, rect, circle",speed:f=1,fade:u=false,easing:p="linear",trigger:h={},stagger:I=0,direction:a="forward",once:d=false,debug:U=false,axis:b="y",scrollContainer:me,autoReverse:ee=false,delay:pe=0,strokeColor:W,strokeWidth:V,fillOpacity:G,waypoints:te,velocityScale:re=false,threshold:ze=0,rootMargin:qe="0px",repeat:J=0,repeatDelay:ke=0,morphTo:C,clip:de,native:Be=true,onProgress:ye,onStart:he,onComplete:K}=r,z=de===true?"left":typeof de=="string"?de:false,ne=typeof p=="function"?p:ve[p]??ve.linear,Te=Ee(h.start??"top bottom"),Ae=Ee(h.end??"bottom top"),E=typeof me=="string"?document.querySelector(me):me??null,O=Array.isArray(W)?W[0]:null,S=Array.isArray(W)?W[1]:typeof W=="string"?W:null,M=Array.isArray(V)?V[0]:null,k=Array.isArray(V)?V[1]:typeof V=="number"?V:null,T=Array.isArray(G)?G[0]:null,A=Array.isArray(G)?G[1]:typeof G=="number"?G:null;function j(t){let s=t*100;switch(z){case "right":return `inset(0 0 0 ${100-s}%)`;case "top":return `inset(0 0 ${100-s}% 0)`;case "bottom":return `inset(${100-s}% 0 0 0)`;case "center":return `circle(${t*150}% at 50% 50%)`;default:return `inset(0 ${100-s}% 0 0)`}}let x=z?[]:Array.from(e.querySelectorAll(o)),w=[],F=[],N=0,H=0,P=false,q=false,R=0,se=false,L=-1,oe=-1,B=false,ie=0,Q=0,ae,ge=null,le=new Set,ue=-1,Le=performance.now();function Y(){return E?b==="x"?E.scrollLeft:E.scrollTop:b==="x"?window.scrollX:window.scrollY}function De(){return E?b==="x"?E.clientWidth:E.clientHeight:b==="x"?window.innerWidth:window.innerHeight}function Pe(){let t=e.getBoundingClientRect(),s,l,g;if(E){let $=E.getBoundingClientRect();s=b==="x"?t.left-$.left+E.scrollLeft:t.top-$.top+E.scrollTop,l=b==="x"?t.width:t.height,g=Y();}else s=b==="x"?t.left:t.top,l=b==="x"?t.width:t.height,g=Y();let _=$e({top:s,height:l},g,De(),Te,Ae);N=_.tStart,H=_.tEnd,U&&process.env.NODE_ENV!=="production"&&(ge?.remove(),ge=et(N,H,b));}function _e(t,s){if(e.style.setProperty("--scroll-draw-progress",String(t)),z){let l=s==="reverse"?1-t:t;e.style.clipPath=j(l);return}x.forEach((l,g)=>{l.style.strokeDashoffset=s==="reverse"?`${w[g]*t}`:`${w[g]*(1-t)}`,u&&(l.style.opacity=s==="reverse"?`${1-t}`:`${t}`),O&&S?l.style.stroke=Se(O,S,t):S&&(l.style.stroke=S),M!==null&&k!==null?l.style.strokeWidth=`${M+(k-M)*t}`:k!==null&&(l.style.strokeWidth=`${k}`),T!==null&&A!==null?l.style.fillOpacity=`${T+(A-T)*t}`:A!==null&&(l.style.fillOpacity=`${A}`),C&&l.tagName.toLowerCase()==="path"&&F[g]&&l.setAttribute("d",We(F[g],C,t));});}function Ie(){if(e.style.setProperty("--scroll-draw-progress","0"),z){e.style.clipPath=j(0);return}x.forEach((t,s)=>{t.style.strokeDasharray=`${w[s]}`,t.style.strokeDashoffset=a==="reverse"?"0":`${w[s]}`,u?t.style.opacity=a==="reverse"?"1":"0":t.style.opacity="",O&&(t.style.stroke=O),M!==null&&(t.style.strokeWidth=`${M}`),T!==null&&(t.style.fillOpacity=`${T}`),C&&t.tagName.toLowerCase()==="path"&&F[s]&&t.setAttribute("d",F[s]);});}if(x.forEach(t=>{Ze(t);let s=xe(t);w.push(s),t.tagName.toLowerCase()==="path"?F.push(t.getAttribute("d")??""):F.push(""),n?(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=a==="reverse"?`${s}`:"0",u&&(t.style.opacity="1"),S&&(t.style.stroke=S),k!==null&&(t.style.strokeWidth=`${k}`),A!==null&&(t.style.fillOpacity=`${A}`),C&&t.tagName.toLowerCase()==="path"&&t.setAttribute("d",C)):(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=a==="reverse"?"0":`${s}`,u?t.style.opacity=a==="reverse"?"1":"0":t.style.opacity="",O&&(t.style.stroke=O),M!==null&&(t.style.strokeWidth=`${M}`),T!==null&&(t.style.fillOpacity=`${T}`));}),z){if(n)return e.style.clipPath=j(1),K?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};e.style.clipPath=j(0);}else if(n)return K?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};function Xe(){return !(Be===false||!Ye()||!x.length||typeof p!="string"||!(p in Re)||z||b!=="y"||E||f!==1||I!==0||d||ee||re!==false||C||te||J||pe>0||ye||he||K||W!=null||V!=null||G!=null||(h.start??"top bottom").trim()!=="top bottom"||(h.end??"bottom top").trim()!=="bottom top")}function Ue(){let t=`svg-scroll-draw-${++Qe}`,s=a==="reverse"?"0":"var(--ssd-len)",l=a==="reverse"?"var(--ssd-len)":"0",g=`stroke-dashoffset:${s};`,_=`stroke-dashoffset:${l};`;u&&(g+=`opacity:${a==="reverse"?1:0};`,_+=`opacity:${a==="reverse"?0:1};`);let $=document.createElement("style");$.setAttribute("data-svg-scroll-draw",""),$.textContent=`@keyframes ${t}{from{${g}}to{${_}}}.${t}{animation-name:${t};animation-duration:auto;animation-timing-function:${Re[p]};animation-fill-mode:both;animation-timeline:view();animation-range:cover 0% cover 100%;}`,document.head.appendChild($);function i(m,D){m.style.setProperty("--ssd-len",String(w[D])),m.style.strokeDasharray=`${w[D]}`,m.style.strokeDashoffset="",m.style.opacity="",m.style.animationPlayState="",m.classList.add(t);}x.forEach(i);let c=false,v=-1;function y(){if(v>=0)return v;let m=e.getBoundingClientRect(),{tStart:D,tEnd:Z}=$e({top:m.top,height:m.height},Y(),De(),Te,Ae);return ne(X(Y(),D,Z,f))}return {destroy(){x.forEach(m=>{m.classList.remove(t),m.style.removeProperty("--ssd-len"),m.style.animationPlayState="";}),$.remove();},replay(){c=false,v=-1,x.forEach(i);},pause(){c=true,x.forEach(m=>{m.style.animationPlayState="paused";});},resume(){c&&(c=false,x.forEach(m=>{m.style.animationPlayState="running";}));},seek(m){let D=Math.min(1,Math.max(0,m));v=D,c=true,x.forEach((Z,Ce)=>{Z.classList.remove(t),Z.style.strokeDashoffset=a==="reverse"?`${w[Ce]*D}`:`${w[Ce]*(1-D)}`,u&&(Z.style.opacity=a==="reverse"?`${1-D}`:`${D}`);});},getProgress(){return y()}}}if(Xe())return Ue();Pe();function fe(){if(!se||B)return;let t=performance.now(),s=Y(),l=f;if(re!==false){let i=t-Le,c=i>0?Math.abs(s-(ue<0?s:ue))/i:0;l=f*Math.max(.2,1+c*(typeof re=="number"?re:1)*.04);}ue=s,Le=t;let g=ee?oe===-1||s>=oe?"forward":"reverse":a;oe=s;let _=H-N,$=true;if(z){let i=ne(X(s,N,H,l));d&&!ee&&(L=Math.max(L,i),i=L),ie=i,e.style.setProperty("--scroll-draw-progress",String(i));let c=g==="reverse"?1-i:i;e.style.clipPath=j(c),ye?.(i),!q&&X(s,N,H,l)>0&&(q=true,he?.()),i>=1&&!P?(P=true,K?.(),Q<(J==="infinite"?1/0:J??0)&&(Q++,ae=setTimeout(()=>{L=-1,q=false,P=false,e.style.clipPath=j(0);},ke))):i<1&&!d&&(P=false),R=requestAnimationFrame(fe);return}if(x.forEach((i,c)=>{let v=c*I*_,y=ne(X(s,N+v,H+v,l));d&&!ee&&(L=Math.max(L,y),y=L),ie=y,i.style.strokeDashoffset=g==="reverse"?`${w[c]*y}`:`${w[c]*(1-y)}`,u&&(i.style.opacity=g==="reverse"?`${1-y}`:`${y}`),O&&S?i.style.stroke=Se(O,S,y):S&&(i.style.stroke=S),M!==null&&k!==null?i.style.strokeWidth=`${M+(k-M)*y}`:k!==null&&(i.style.strokeWidth=`${k}`),T!==null&&A!==null?i.style.fillOpacity=`${T+(A-T)*y}`:A!==null&&(i.style.fillOpacity=`${A}`),C&&i.tagName.toLowerCase()==="path"&&F[c]&&i.setAttribute("d",We(F[c],C,y)),c===0&&(ye?.(y),e.style.setProperty("--scroll-draw-progress",String(y))),y<1&&($=false);}),te){let i=ne(X(s,N,H,l));for(let c in te){let v=parseFloat(c);i>=v&&!le.has(v)&&(le.add(v),te[c]?.());}}!q&&X(s,N,H,l)>0&&(q=true,he?.()),$&&!P?(P=true,K?.(),Q<(J==="infinite"?1/0:J??0)&&(Q++,ae=setTimeout(()=>{L=-1,q=false,P=false,le.clear(),Ie();},ke))):!$&&!d&&(P=false),R=requestAnimationFrame(fe);}let be=new IntersectionObserver(t=>{t.forEach(s=>{se=s.isIntersecting,se&&!B?R=requestAnimationFrame(fe):cancelAnimationFrame(R);});},{root:E??null,threshold:ze,rootMargin:qe}),we;function ce(){clearTimeout(we),we=setTimeout(()=>{x.forEach((t,s)=>{w[s]=xe(t),t.style.strokeDasharray=`${w[s]}`;}),Pe();},150);}return window.addEventListener("resize",ce),window.addEventListener("orientationchange",ce),pe>0?setTimeout(()=>be.observe(e),pe):be.observe(e),{destroy(){cancelAnimationFrame(R),clearTimeout(ae),be.disconnect(),window.removeEventListener("resize",ce),window.removeEventListener("orientationchange",ce),clearTimeout(we),ge?.remove();},replay(){L=-1,oe=-1,ue=-1,q=false,P=false,Q=0,B=false,le.clear(),clearTimeout(ae),Ie();},pause(){B=true,cancelAnimationFrame(R);},resume(){B&&(B=false,se&&(R=requestAnimationFrame(fe)));},seek(t){let s=Math.min(1,Math.max(0,t));ie=s,L=s,B=true,cancelAnimationFrame(R),_e(s,a);},getProgress(){return ie}}}function it(e={}){let r,n;return onMount(()=>{r&&(n=Me(r,e));}),onCleanup(()=>{n?.destroy();}),o=>{r=o;}}function at(e={}){let r,n;return onMount(()=>{r&&(n=Me(r,e));}),onCleanup(()=>{n?.destroy(),n=void 0;}),{ref:o=>{r=o;},getInstance:()=>n}}export{at as createScrollDraw,it as useScrollDraw};
|