svg-scroll-draw 0.6.2 → 0.7.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 +2 -2
- package/dist/angular/index.mjs +2 -2
- package/dist/astro/index.cjs +2 -2
- package/dist/astro/index.mjs +2 -2
- package/dist/cdn/svg-scroll-draw.global.js +2 -2
- package/dist/group/index.cjs +2 -2
- package/dist/group/index.mjs +2 -2
- package/dist/index.cjs +2 -2
- package/dist/index.d.mts +14 -1
- package/dist/index.d.ts +14 -1
- package/dist/index.mjs +2 -2
- package/dist/nuxt/index.cjs +2 -2
- package/dist/nuxt/index.mjs +2 -2
- package/dist/react/index.cjs +2 -2
- package/dist/react/index.mjs +2 -2
- package/dist/solid/index.cjs +2 -2
- package/dist/solid/index.mjs +2 -2
- package/dist/svelte/index.cjs +2 -2
- package/dist/svelte/index.mjs +2 -2
- package/dist/timeline/index.cjs +1 -0
- package/dist/timeline/index.d.mts +64 -0
- package/dist/timeline/index.d.ts +64 -0
- package/dist/timeline/index.mjs +1 -0
- package/dist/vue/index.cjs +2 -2
- package/dist/vue/index.mjs +2 -2
- package/dist/web-component/index.cjs +2 -2
- package/dist/web-component/index.mjs +2 -2
- package/package.json +7 -2
package/dist/index.d.ts
CHANGED
|
@@ -65,6 +65,19 @@ interface ScrollDrawInstance {
|
|
|
65
65
|
getProgress: () => number;
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
+
/**
|
|
69
|
+
* Returns a custom spring easing function.
|
|
70
|
+
* - `tension` controls oscillation frequency (default 2.5 — higher = more bouncy)
|
|
71
|
+
* - `friction` controls damping (default 2.2 — higher = less bouncy)
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* scrollDraw('#svg', { easing: createSpring({ tension: 3, friction: 1.8 }) });
|
|
75
|
+
*/
|
|
76
|
+
declare function createSpring({ tension, friction, }?: {
|
|
77
|
+
tension?: number;
|
|
78
|
+
friction?: number;
|
|
79
|
+
}): (t: number) => number;
|
|
80
|
+
|
|
68
81
|
declare function scrollDraw(target: string | Element, options?: ScrollDrawOptions): ScrollDrawInstance;
|
|
69
82
|
|
|
70
|
-
export { type ScrollDrawInstance, type ScrollDrawOptions, scrollDraw };
|
|
83
|
+
export { type ScrollDrawInstance, type ScrollDrawOptions, createSpring, scrollDraw };
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
var me={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 de(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 Ae(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
|
|
1
|
+
var me={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 He({tension:e=2.5,friction:r=2.2}={}){return n=>1-Math.cos(n*Math.PI*e)*Math.pow(1-n,r)}function de(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 Ae(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 Te(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 ye(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 We(e,r,n){return Math.min(n,Math.max(r,e))}function z(e,r,n,o){return n===r?0:We((e-r)/(n-r)*o,0,1)}function Se(e,r,n,o,a){let f=Ae(e.top,e.height,r,o.element)-Te(o.viewport,n),p=Ae(e.top,e.height,r,a.element)-Te(a.viewport,n);return {tStart:f,tEnd:p}}function ke(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 ge(e,r,n){let o=ke(e),a=ke(r);return !o||!a?e:`rgb(${Math.round(o[0]+(a[0]-o[0])*n)},${Math.round(o[1]+(a[1]-o[1])*n)},${Math.round(o[2]+(a[2]-o[2])*n)})`}function Me(e,r){process.env.NODE_ENV!=="production"&&console.warn(`[svg-scroll-draw] ${e}`,r);}function Ve(e){let r=e.getAttribute("stroke"),n=e.getAttribute("fill");!r||r==="none"?Me("Element has no stroke \u2014 path will not be visible.",e):n&&n!=="none"&&n!=="transparent"&&Me("Element has a fill \u2014 it may obscure the stroke animation.",e);}function ze(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 a(){let f=n==="x"?window.scrollX:window.scrollY,p=e-f,L=r-f,B=n==="x";o.innerHTML=`
|
|
2
2
|
<div style="position:absolute;${B?`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;${B?`left:${L}px;top:0;bottom:0;border-left:2px dashed #ef4444;`:`top:${L}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
|
|
3
|
+
<div style="position:absolute;${B?`left:${L}px;top:0;bottom:0;border-left:2px dashed #ef4444;`:`top:${L}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 De(e,r,n){let o=(r.match(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g)??[]).map(Number),a=0;return e.replace(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g,f=>{let p=parseFloat(f),L=o[a++]??p;return String(+(p+(L-p)*n).toFixed(4))})}function Le(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:a=1,fade:f=false,easing:p="linear",trigger:L={},stagger:B=0,direction:I="forward",once:X=false,debug:Ie=false,axis:x="y",scrollContainer:ne,autoReverse:se=false,delay:he=0,strokeColor:F,strokeWidth:N,fillOpacity:R,waypoints:oe,velocityScale:ie=false,threshold:Pe=0,rootMargin:Oe="0px",repeat:_=0,repeatDelay:be=0,morphTo:P,clip:le,onProgress:we,onStart:ve,onComplete:j}=r,H=le===true?"left":typeof le=="string"?le:false,ae=typeof p=="function"?p:me[p]??me.linear,Ce=de(L.start??"top bottom"),Fe=de(L.end??"bottom top"),d=typeof ne=="string"?document.querySelector(ne):ne??null,A=Array.isArray(F)?F[0]:null,y=Array.isArray(F)?F[1]:typeof F=="string"?F:null,g=Array.isArray(N)?N[0]:null,h=Array.isArray(N)?N[1]:typeof N=="number"?N:null,b=Array.isArray(R)?R[0]:null,w=Array.isArray(R)?R[1]:typeof R=="number"?R:null;function W(t){let s=t*100;switch(H){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 G=H?[]:Array.from(e.querySelectorAll(o)),E=[],T=[],k=0,S=0,$=false,O=false,M=0,J=false,v=-1,K=-1,C=false,Q=0,q=0,U,ce=null,Y=new Set,Z=-1,xe=performance.now();function ue(){return d?x==="x"?d.scrollLeft:d.scrollTop:x==="x"?window.scrollX:window.scrollY}function Ne(){return d?x==="x"?d.clientWidth:d.clientHeight:x==="x"?window.innerWidth:window.innerHeight}function Ee(){let t=e.getBoundingClientRect(),s,l,m;if(d){let V=d.getBoundingClientRect();s=x==="x"?t.left-V.left+d.scrollLeft:t.top-V.top+d.scrollTop,l=x==="x"?t.width:t.height,m=ue();}else s=x==="x"?t.left:t.top,l=x==="x"?t.width:t.height,m=ue();let re=Se({top:s,height:l},m,Ne(),Ce,Fe);k=re.tStart,S=re.tEnd,Ie&&process.env.NODE_ENV!=="production"&&(ce?.remove(),ce=ze(k,S,x));}function Re(t,s){if(e.style.setProperty("--scroll-draw-progress",String(t)),H){let l=s==="reverse"?1-t:t;e.style.clipPath=W(l);return}G.forEach((l,m)=>{l.style.strokeDashoffset=s==="reverse"?`${E[m]*t}`:`${E[m]*(1-t)}`,f&&(l.style.opacity=s==="reverse"?`${1-t}`:`${t}`),A&&y?l.style.stroke=ge(A,y,t):y&&(l.style.stroke=y),g!==null&&h!==null?l.style.strokeWidth=`${g+(h-g)*t}`:h!==null&&(l.style.strokeWidth=`${h}`),b!==null&&w!==null?l.style.fillOpacity=`${b+(w-b)*t}`:w!==null&&(l.style.fillOpacity=`${w}`),P&&l.tagName.toLowerCase()==="path"&&T[m]&&l.setAttribute("d",De(T[m],P,t));});}function $e(){if(e.style.setProperty("--scroll-draw-progress","0"),H){e.style.clipPath=W(0);return}G.forEach((t,s)=>{t.style.strokeDasharray=`${E[s]}`,t.style.strokeDashoffset=I==="reverse"?"0":`${E[s]}`,f?t.style.opacity=I==="reverse"?"1":"0":t.style.opacity="",A&&(t.style.stroke=A),g!==null&&(t.style.strokeWidth=`${g}`),b!==null&&(t.style.fillOpacity=`${b}`),P&&t.tagName.toLowerCase()==="path"&&T[s]&&t.setAttribute("d",T[s]);});}if(G.forEach(t=>{Ve(t);let s=ye(t);E.push(s),t.tagName.toLowerCase()==="path"?T.push(t.getAttribute("d")??""):T.push(""),n?(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=I==="reverse"?`${s}`:"0",f&&(t.style.opacity="1"),y&&(t.style.stroke=y),h!==null&&(t.style.strokeWidth=`${h}`),w!==null&&(t.style.fillOpacity=`${w}`),P&&t.tagName.toLowerCase()==="path"&&t.setAttribute("d",P)):(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=I==="reverse"?"0":`${s}`,f?t.style.opacity=I==="reverse"?"1":"0":t.style.opacity="",A&&(t.style.stroke=A),g!==null&&(t.style.strokeWidth=`${g}`),b!==null&&(t.style.fillOpacity=`${b}`));}),H){if(n)return e.style.clipPath=W(1),j?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};e.style.clipPath=W(0);}else if(n)return j?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};Ee();function ee(){if(!J||C)return;let t=performance.now(),s=ue(),l=a;if(ie!==false){let i=t-xe,u=i>0?Math.abs(s-(Z<0?s:Z))/i:0;l=a*Math.max(.2,1+u*(typeof ie=="number"?ie:1)*.04);}Z=s,xe=t;let m=se?K===-1||s>=K?"forward":"reverse":I;K=s;let re=S-k,V=true;if(H){let i=ae(z(s,k,S,l));X&&!se&&(v=Math.max(v,i),i=v),Q=i,e.style.setProperty("--scroll-draw-progress",String(i));let u=m==="reverse"?1-i:i;e.style.clipPath=W(u),we?.(i),!O&&z(s,k,S,l)>0&&(O=true,ve?.()),i>=1&&!$?($=true,j?.(),q<(_==="infinite"?1/0:_??0)&&(q++,U=setTimeout(()=>{v=-1,O=false,$=false,e.style.clipPath=W(0);},be))):i<1&&!X&&($=false),M=requestAnimationFrame(ee);return}if(G.forEach((i,u)=>{let D=u*B*re,c=ae(z(s,k+D,S+D,l));X&&!se&&(v=Math.max(v,c),c=v),Q=c,i.style.strokeDashoffset=m==="reverse"?`${E[u]*c}`:`${E[u]*(1-c)}`,f&&(i.style.opacity=m==="reverse"?`${1-c}`:`${c}`),A&&y?i.style.stroke=ge(A,y,c):y&&(i.style.stroke=y),g!==null&&h!==null?i.style.strokeWidth=`${g+(h-g)*c}`:h!==null&&(i.style.strokeWidth=`${h}`),b!==null&&w!==null?i.style.fillOpacity=`${b+(w-b)*c}`:w!==null&&(i.style.fillOpacity=`${w}`),P&&i.tagName.toLowerCase()==="path"&&T[u]&&i.setAttribute("d",De(T[u],P,c)),u===0&&(we?.(c),e.style.setProperty("--scroll-draw-progress",String(c))),c<1&&(V=false);}),oe){let i=ae(z(s,k,S,l));for(let u in oe){let D=parseFloat(u);i>=D&&!Y.has(D)&&(Y.add(D),oe[u]?.());}}!O&&z(s,k,S,l)>0&&(O=true,ve?.()),V&&!$?($=true,j?.(),q<(_==="infinite"?1/0:_??0)&&(q++,U=setTimeout(()=>{v=-1,O=false,$=false,Y.clear(),$e();},be))):!V&&!X&&($=false),M=requestAnimationFrame(ee);}let fe=new IntersectionObserver(t=>{t.forEach(s=>{J=s.isIntersecting,J&&!C?M=requestAnimationFrame(ee):cancelAnimationFrame(M);});},{root:d??null,threshold:Pe,rootMargin:Oe}),pe;function te(){clearTimeout(pe),pe=setTimeout(()=>{G.forEach((t,s)=>{E[s]=ye(t),t.style.strokeDasharray=`${E[s]}`;}),Ee();},150);}return window.addEventListener("resize",te),window.addEventListener("orientationchange",te),he>0?setTimeout(()=>fe.observe(e),he):fe.observe(e),{destroy(){cancelAnimationFrame(M),clearTimeout(U),fe.disconnect(),window.removeEventListener("resize",te),window.removeEventListener("orientationchange",te),clearTimeout(pe),ce?.remove();},replay(){v=-1,K=-1,Z=-1,O=false,$=false,q=0,C=false,Y.clear(),clearTimeout(U),$e();},pause(){C=true,cancelAnimationFrame(M);},resume(){C&&(C=false,J&&(M=requestAnimationFrame(ee)));},seek(t){let s=Math.min(1,Math.max(0,t));Q=s,v=s,C=true,cancelAnimationFrame(M),Re(s,I);},getProgress(){return Q}}}function _e(e,r){let n={destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>0};if(typeof window>"u")return n;let o=typeof e=="string"?document.querySelector(e):e;return o?Le(o,r):(console.warn("[svg-scroll-draw] Container not found:",e),n)}export{He as createSpring,_e as scrollDraw};
|
package/dist/nuxt/index.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
'use strict';var vue=require('vue');var pe={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 de(e="top bottom"){let r=e.trim();if(/^\d+(\.\d+)?%$/.test(r))return {element:"top",viewport:r};let[o="top",n="bottom"]=r.split(/\s+/).filter(Boolean);return {element:o,viewport:n}}function
|
|
1
|
+
'use strict';var vue=require('vue');var pe={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 de(e="top bottom"){let r=e.trim();if(/^\d+(\.\d+)?%$/.test(r))return {element:"top",viewport:r};let[o="top",n="bottom"]=r.split(/\s+/).filter(Boolean);return {element:o,viewport:n}}function Te(e,r,o,n){switch(n){case "top":return e+o;case "center":return e+o+r/2;case "bottom":return e+o+r;default:return e+o}}function De(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 ge(e){let r=e.tagName.toLowerCase();if(r==="rect"){let o=parseFloat(e.getAttribute("width")??"0"),n=parseFloat(e.getAttribute("height")??"0");return 2*(o+n)}if(r==="circle"){let o=parseFloat(e.getAttribute("r")??"0");return 2*Math.PI*o}return e.getTotalLength()}function Ge(e,r,o){return Math.min(o,Math.max(r,e))}function z(e,r,o,n){return o===r?0:Ge((e-r)/(o-r)*n,0,1)}function Me(e,r,o,n,a){let f=Te(e.top,e.height,r,n.element)-De(n.viewport,o),m=Te(e.top,e.height,r,a.element)-De(a.viewport,o);return {tStart:f,tEnd:m}}function ke(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 o=/^#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(e);if(o)return [parseInt(o[1],16),parseInt(o[2],16),parseInt(o[3],16)];let n=/^rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i.exec(e);return n?[parseInt(n[1]),parseInt(n[2]),parseInt(n[3])]:null}function ye(e,r,o){let n=ke(e),a=ke(r);return !n||!a?e:`rgb(${Math.round(n[0]+(a[0]-n[0])*o)},${Math.round(n[1]+(a[1]-n[1])*o)},${Math.round(n[2]+(a[2]-n[2])*o)})`}function Le(e,r){process.env.NODE_ENV!=="production"&&console.warn(`[svg-scroll-draw] ${e}`,r);}function qe(e){let r=e.getAttribute("stroke"),o=e.getAttribute("fill");!r||r==="none"?Le("Element has no stroke \u2014 path will not be visible.",e):o&&o!=="none"&&o!=="transparent"&&Le("Element has a fill \u2014 it may obscure the stroke animation.",e);}function Be(e,r,o){let n=document.createElement("div");n.setAttribute("data-svg-scroll-draw-debug",""),n.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 f=o==="x"?window.scrollX:window.scrollY,m=e-f,L=r-f,B=o==="x";n.innerHTML=`
|
|
2
2
|
<div style="position:absolute;${B?`left:${m}px;top:0;bottom:0;border-left:2px dashed #22c55e;`:`top:${m}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:${
|
|
3
|
+
<div style="position:absolute;${B?`left:${L}px;top:0;bottom:0;border-left:2px dashed #ef4444;`:`top:${L}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 Pe(e,r,o){let n=(r.match(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g)??[]).map(Number),a=0;return e.replace(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g,f=>{let m=parseFloat(f),L=n[a++]??m;return String(+(m+(L-m)*o).toFixed(4))})}function be(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:f=false,easing:m="linear",trigger:L={},stagger:B=0,direction:P="forward",once:X=false,debug:Ie=false,axis:x="y",scrollContainer:ne,autoReverse:oe=false,delay:we=0,strokeColor:I,strokeWidth:N,fillOpacity:R,waypoints:se,velocityScale:ie=false,threshold:Ne=0,rootMargin:Re="0px",repeat:_=0,repeatDelay:ve=0,morphTo:O,clip:le,onProgress:xe,onStart:Se,onComplete:j}=r,H=le===true?"left":typeof le=="string"?le:false,ae=typeof m=="function"?m:pe[m]??pe.linear,He=de(L.start??"top bottom"),We=de(L.end??"bottom top"),d=typeof ne=="string"?document.querySelector(ne):ne??null,$=Array.isArray(I)?I[0]:null,g=Array.isArray(I)?I[1]:typeof I=="string"?I:null,y=Array.isArray(N)?N[0]:null,b=Array.isArray(N)?N[1]:typeof N=="number"?N:null,h=Array.isArray(R)?R[0]:null,w=Array.isArray(R)?R[1]:typeof R=="number"?R:null;function W(t){let s=t*100;switch(H){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 G=H?[]:Array.from(e.querySelectorAll(n)),S=[],A=[],T=0,D=0,E=false,C=false,k=0,U=false,v=-1,J=-1,F=false,K=0,q=0,Q,ue=null,Y=new Set,Z=-1,Ee=performance.now();function ce(){return d?x==="x"?d.scrollLeft:d.scrollTop:x==="x"?window.scrollX:window.scrollY}function Ve(){return d?x==="x"?d.clientWidth:d.clientHeight:x==="x"?window.innerWidth:window.innerHeight}function $e(){let t=e.getBoundingClientRect(),s,l,p;if(d){let V=d.getBoundingClientRect();s=x==="x"?t.left-V.left+d.scrollLeft:t.top-V.top+d.scrollTop,l=x==="x"?t.width:t.height,p=ce();}else s=x==="x"?t.left:t.top,l=x==="x"?t.width:t.height,p=ce();let re=Me({top:s,height:l},p,Ve(),He,We);T=re.tStart,D=re.tEnd,Ie&&process.env.NODE_ENV!=="production"&&(ue?.remove(),ue=Be(T,D,x));}function ze(t,s){if(e.style.setProperty("--scroll-draw-progress",String(t)),H){let l=s==="reverse"?1-t:t;e.style.clipPath=W(l);return}G.forEach((l,p)=>{l.style.strokeDashoffset=s==="reverse"?`${S[p]*t}`:`${S[p]*(1-t)}`,f&&(l.style.opacity=s==="reverse"?`${1-t}`:`${t}`),$&&g?l.style.stroke=ye($,g,t):g&&(l.style.stroke=g),y!==null&&b!==null?l.style.strokeWidth=`${y+(b-y)*t}`:b!==null&&(l.style.strokeWidth=`${b}`),h!==null&&w!==null?l.style.fillOpacity=`${h+(w-h)*t}`:w!==null&&(l.style.fillOpacity=`${w}`),O&&l.tagName.toLowerCase()==="path"&&A[p]&&l.setAttribute("d",Pe(A[p],O,t));});}function Ae(){if(e.style.setProperty("--scroll-draw-progress","0"),H){e.style.clipPath=W(0);return}G.forEach((t,s)=>{t.style.strokeDasharray=`${S[s]}`,t.style.strokeDashoffset=P==="reverse"?"0":`${S[s]}`,f?t.style.opacity=P==="reverse"?"1":"0":t.style.opacity="",$&&(t.style.stroke=$),y!==null&&(t.style.strokeWidth=`${y}`),h!==null&&(t.style.fillOpacity=`${h}`),O&&t.tagName.toLowerCase()==="path"&&A[s]&&t.setAttribute("d",A[s]);});}if(G.forEach(t=>{qe(t);let s=ge(t);S.push(s),t.tagName.toLowerCase()==="path"?A.push(t.getAttribute("d")??""):A.push(""),o?(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=P==="reverse"?`${s}`:"0",f&&(t.style.opacity="1"),g&&(t.style.stroke=g),b!==null&&(t.style.strokeWidth=`${b}`),w!==null&&(t.style.fillOpacity=`${w}`),O&&t.tagName.toLowerCase()==="path"&&t.setAttribute("d",O)):(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=P==="reverse"?"0":`${s}`,f?t.style.opacity=P==="reverse"?"1":"0":t.style.opacity="",$&&(t.style.stroke=$),y!==null&&(t.style.strokeWidth=`${y}`),h!==null&&(t.style.fillOpacity=`${h}`));}),H){if(o)return e.style.clipPath=W(1),j?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};e.style.clipPath=W(0);}else if(o)return j?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};$e();function ee(){if(!U||F)return;let t=performance.now(),s=ce(),l=a;if(ie!==false){let i=t-Ee,c=i>0?Math.abs(s-(Z<0?s:Z))/i:0;l=a*Math.max(.2,1+c*(typeof ie=="number"?ie:1)*.04);}Z=s,Ee=t;let p=oe?J===-1||s>=J?"forward":"reverse":P;J=s;let re=D-T,V=true;if(H){let i=ae(z(s,T,D,l));X&&!oe&&(v=Math.max(v,i),i=v),K=i,e.style.setProperty("--scroll-draw-progress",String(i));let c=p==="reverse"?1-i:i;e.style.clipPath=W(c),xe?.(i),!C&&z(s,T,D,l)>0&&(C=true,Se?.()),i>=1&&!E?(E=true,j?.(),q<(_==="infinite"?1/0:_??0)&&(q++,Q=setTimeout(()=>{v=-1,C=false,E=false,e.style.clipPath=W(0);},ve))):i<1&&!X&&(E=false),k=requestAnimationFrame(ee);return}if(G.forEach((i,c)=>{let M=c*B*re,u=ae(z(s,T+M,D+M,l));X&&!oe&&(v=Math.max(v,u),u=v),K=u,i.style.strokeDashoffset=p==="reverse"?`${S[c]*u}`:`${S[c]*(1-u)}`,f&&(i.style.opacity=p==="reverse"?`${1-u}`:`${u}`),$&&g?i.style.stroke=ye($,g,u):g&&(i.style.stroke=g),y!==null&&b!==null?i.style.strokeWidth=`${y+(b-y)*u}`:b!==null&&(i.style.strokeWidth=`${b}`),h!==null&&w!==null?i.style.fillOpacity=`${h+(w-h)*u}`:w!==null&&(i.style.fillOpacity=`${w}`),O&&i.tagName.toLowerCase()==="path"&&A[c]&&i.setAttribute("d",Pe(A[c],O,u)),c===0&&(xe?.(u),e.style.setProperty("--scroll-draw-progress",String(u))),u<1&&(V=false);}),se){let i=ae(z(s,T,D,l));for(let c in se){let M=parseFloat(c);i>=M&&!Y.has(M)&&(Y.add(M),se[c]?.());}}!C&&z(s,T,D,l)>0&&(C=true,Se?.()),V&&!E?(E=true,j?.(),q<(_==="infinite"?1/0:_??0)&&(q++,Q=setTimeout(()=>{v=-1,C=false,E=false,Y.clear(),Ae();},ve))):!V&&!X&&(E=false),k=requestAnimationFrame(ee);}let fe=new IntersectionObserver(t=>{t.forEach(s=>{U=s.isIntersecting,U&&!F?k=requestAnimationFrame(ee):cancelAnimationFrame(k);});},{root:d??null,threshold:Ne,rootMargin:Re}),me;function te(){clearTimeout(me),me=setTimeout(()=>{G.forEach((t,s)=>{S[s]=ge(t),t.style.strokeDasharray=`${S[s]}`;}),$e();},150);}return window.addEventListener("resize",te),window.addEventListener("orientationchange",te),we>0?setTimeout(()=>fe.observe(e),we):fe.observe(e),{destroy(){cancelAnimationFrame(k),clearTimeout(Q),fe.disconnect(),window.removeEventListener("resize",te),window.removeEventListener("orientationchange",te),clearTimeout(me),ue?.remove();},replay(){v=-1,J=-1,Z=-1,C=false,E=false,q=0,F=false,Y.clear(),clearTimeout(Q),Ae();},pause(){F=true,cancelAnimationFrame(k);},resume(){F&&(F=false,U&&(k=requestAnimationFrame(ee)));},seek(t){let s=Math.min(1,Math.max(0,t));K=s,v=s,F=true,cancelAnimationFrame(k),ze(s,P);},getProgress(){return K}}}function je(e={}){let r=vue.ref(null);return vue.onMounted(()=>{if(!r.value)return;let o=be(r.value,e);vue.onUnmounted(()=>o.destroy());}),r}var he=vue.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=vue.ref(null);return vue.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=be(o.value,n);vue.onUnmounted(()=>a.destroy());}),()=>vue.h("div",{ref:o},r.default?.())}});function tt(){return {install(e){e.component("ScrollDraw",he);}}}exports.ScrollDraw=he;exports.createScrollDrawPlugin=tt;exports.useScrollDraw=je;
|
package/dist/nuxt/index.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {defineComponent,ref,onMounted,onUnmounted,h}from'vue';var pe={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 de(e="top bottom"){let r=e.trim();if(/^\d+(\.\d+)?%$/.test(r))return {element:"top",viewport:r};let[o="top",n="bottom"]=r.split(/\s+/).filter(Boolean);return {element:o,viewport:n}}function
|
|
1
|
+
import {defineComponent,ref,onMounted,onUnmounted,h}from'vue';var pe={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 de(e="top bottom"){let r=e.trim();if(/^\d+(\.\d+)?%$/.test(r))return {element:"top",viewport:r};let[o="top",n="bottom"]=r.split(/\s+/).filter(Boolean);return {element:o,viewport:n}}function Te(e,r,o,n){switch(n){case "top":return e+o;case "center":return e+o+r/2;case "bottom":return e+o+r;default:return e+o}}function De(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 ge(e){let r=e.tagName.toLowerCase();if(r==="rect"){let o=parseFloat(e.getAttribute("width")??"0"),n=parseFloat(e.getAttribute("height")??"0");return 2*(o+n)}if(r==="circle"){let o=parseFloat(e.getAttribute("r")??"0");return 2*Math.PI*o}return e.getTotalLength()}function Ge(e,r,o){return Math.min(o,Math.max(r,e))}function z(e,r,o,n){return o===r?0:Ge((e-r)/(o-r)*n,0,1)}function Me(e,r,o,n,a){let f=Te(e.top,e.height,r,n.element)-De(n.viewport,o),m=Te(e.top,e.height,r,a.element)-De(a.viewport,o);return {tStart:f,tEnd:m}}function ke(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 o=/^#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(e);if(o)return [parseInt(o[1],16),parseInt(o[2],16),parseInt(o[3],16)];let n=/^rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i.exec(e);return n?[parseInt(n[1]),parseInt(n[2]),parseInt(n[3])]:null}function ye(e,r,o){let n=ke(e),a=ke(r);return !n||!a?e:`rgb(${Math.round(n[0]+(a[0]-n[0])*o)},${Math.round(n[1]+(a[1]-n[1])*o)},${Math.round(n[2]+(a[2]-n[2])*o)})`}function Le(e,r){process.env.NODE_ENV!=="production"&&console.warn(`[svg-scroll-draw] ${e}`,r);}function qe(e){let r=e.getAttribute("stroke"),o=e.getAttribute("fill");!r||r==="none"?Le("Element has no stroke \u2014 path will not be visible.",e):o&&o!=="none"&&o!=="transparent"&&Le("Element has a fill \u2014 it may obscure the stroke animation.",e);}function Be(e,r,o){let n=document.createElement("div");n.setAttribute("data-svg-scroll-draw-debug",""),n.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 f=o==="x"?window.scrollX:window.scrollY,m=e-f,L=r-f,B=o==="x";n.innerHTML=`
|
|
2
2
|
<div style="position:absolute;${B?`left:${m}px;top:0;bottom:0;border-left:2px dashed #22c55e;`:`top:${m}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:${
|
|
3
|
+
<div style="position:absolute;${B?`left:${L}px;top:0;bottom:0;border-left:2px dashed #ef4444;`:`top:${L}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 Pe(e,r,o){let n=(r.match(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g)??[]).map(Number),a=0;return e.replace(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g,f=>{let m=parseFloat(f),L=n[a++]??m;return String(+(m+(L-m)*o).toFixed(4))})}function be(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:f=false,easing:m="linear",trigger:L={},stagger:B=0,direction:P="forward",once:X=false,debug:Ie=false,axis:x="y",scrollContainer:ne,autoReverse:oe=false,delay:we=0,strokeColor:I,strokeWidth:N,fillOpacity:R,waypoints:se,velocityScale:ie=false,threshold:Ne=0,rootMargin:Re="0px",repeat:_=0,repeatDelay:ve=0,morphTo:O,clip:le,onProgress:xe,onStart:Se,onComplete:j}=r,H=le===true?"left":typeof le=="string"?le:false,ae=typeof m=="function"?m:pe[m]??pe.linear,He=de(L.start??"top bottom"),We=de(L.end??"bottom top"),d=typeof ne=="string"?document.querySelector(ne):ne??null,$=Array.isArray(I)?I[0]:null,g=Array.isArray(I)?I[1]:typeof I=="string"?I:null,y=Array.isArray(N)?N[0]:null,b=Array.isArray(N)?N[1]:typeof N=="number"?N:null,h=Array.isArray(R)?R[0]:null,w=Array.isArray(R)?R[1]:typeof R=="number"?R:null;function W(t){let s=t*100;switch(H){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 G=H?[]:Array.from(e.querySelectorAll(n)),S=[],A=[],T=0,D=0,E=false,C=false,k=0,U=false,v=-1,J=-1,F=false,K=0,q=0,Q,ue=null,Y=new Set,Z=-1,Ee=performance.now();function ce(){return d?x==="x"?d.scrollLeft:d.scrollTop:x==="x"?window.scrollX:window.scrollY}function Ve(){return d?x==="x"?d.clientWidth:d.clientHeight:x==="x"?window.innerWidth:window.innerHeight}function $e(){let t=e.getBoundingClientRect(),s,l,p;if(d){let V=d.getBoundingClientRect();s=x==="x"?t.left-V.left+d.scrollLeft:t.top-V.top+d.scrollTop,l=x==="x"?t.width:t.height,p=ce();}else s=x==="x"?t.left:t.top,l=x==="x"?t.width:t.height,p=ce();let re=Me({top:s,height:l},p,Ve(),He,We);T=re.tStart,D=re.tEnd,Ie&&process.env.NODE_ENV!=="production"&&(ue?.remove(),ue=Be(T,D,x));}function ze(t,s){if(e.style.setProperty("--scroll-draw-progress",String(t)),H){let l=s==="reverse"?1-t:t;e.style.clipPath=W(l);return}G.forEach((l,p)=>{l.style.strokeDashoffset=s==="reverse"?`${S[p]*t}`:`${S[p]*(1-t)}`,f&&(l.style.opacity=s==="reverse"?`${1-t}`:`${t}`),$&&g?l.style.stroke=ye($,g,t):g&&(l.style.stroke=g),y!==null&&b!==null?l.style.strokeWidth=`${y+(b-y)*t}`:b!==null&&(l.style.strokeWidth=`${b}`),h!==null&&w!==null?l.style.fillOpacity=`${h+(w-h)*t}`:w!==null&&(l.style.fillOpacity=`${w}`),O&&l.tagName.toLowerCase()==="path"&&A[p]&&l.setAttribute("d",Pe(A[p],O,t));});}function Ae(){if(e.style.setProperty("--scroll-draw-progress","0"),H){e.style.clipPath=W(0);return}G.forEach((t,s)=>{t.style.strokeDasharray=`${S[s]}`,t.style.strokeDashoffset=P==="reverse"?"0":`${S[s]}`,f?t.style.opacity=P==="reverse"?"1":"0":t.style.opacity="",$&&(t.style.stroke=$),y!==null&&(t.style.strokeWidth=`${y}`),h!==null&&(t.style.fillOpacity=`${h}`),O&&t.tagName.toLowerCase()==="path"&&A[s]&&t.setAttribute("d",A[s]);});}if(G.forEach(t=>{qe(t);let s=ge(t);S.push(s),t.tagName.toLowerCase()==="path"?A.push(t.getAttribute("d")??""):A.push(""),o?(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=P==="reverse"?`${s}`:"0",f&&(t.style.opacity="1"),g&&(t.style.stroke=g),b!==null&&(t.style.strokeWidth=`${b}`),w!==null&&(t.style.fillOpacity=`${w}`),O&&t.tagName.toLowerCase()==="path"&&t.setAttribute("d",O)):(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=P==="reverse"?"0":`${s}`,f?t.style.opacity=P==="reverse"?"1":"0":t.style.opacity="",$&&(t.style.stroke=$),y!==null&&(t.style.strokeWidth=`${y}`),h!==null&&(t.style.fillOpacity=`${h}`));}),H){if(o)return e.style.clipPath=W(1),j?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};e.style.clipPath=W(0);}else if(o)return j?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};$e();function ee(){if(!U||F)return;let t=performance.now(),s=ce(),l=a;if(ie!==false){let i=t-Ee,c=i>0?Math.abs(s-(Z<0?s:Z))/i:0;l=a*Math.max(.2,1+c*(typeof ie=="number"?ie:1)*.04);}Z=s,Ee=t;let p=oe?J===-1||s>=J?"forward":"reverse":P;J=s;let re=D-T,V=true;if(H){let i=ae(z(s,T,D,l));X&&!oe&&(v=Math.max(v,i),i=v),K=i,e.style.setProperty("--scroll-draw-progress",String(i));let c=p==="reverse"?1-i:i;e.style.clipPath=W(c),xe?.(i),!C&&z(s,T,D,l)>0&&(C=true,Se?.()),i>=1&&!E?(E=true,j?.(),q<(_==="infinite"?1/0:_??0)&&(q++,Q=setTimeout(()=>{v=-1,C=false,E=false,e.style.clipPath=W(0);},ve))):i<1&&!X&&(E=false),k=requestAnimationFrame(ee);return}if(G.forEach((i,c)=>{let M=c*B*re,u=ae(z(s,T+M,D+M,l));X&&!oe&&(v=Math.max(v,u),u=v),K=u,i.style.strokeDashoffset=p==="reverse"?`${S[c]*u}`:`${S[c]*(1-u)}`,f&&(i.style.opacity=p==="reverse"?`${1-u}`:`${u}`),$&&g?i.style.stroke=ye($,g,u):g&&(i.style.stroke=g),y!==null&&b!==null?i.style.strokeWidth=`${y+(b-y)*u}`:b!==null&&(i.style.strokeWidth=`${b}`),h!==null&&w!==null?i.style.fillOpacity=`${h+(w-h)*u}`:w!==null&&(i.style.fillOpacity=`${w}`),O&&i.tagName.toLowerCase()==="path"&&A[c]&&i.setAttribute("d",Pe(A[c],O,u)),c===0&&(xe?.(u),e.style.setProperty("--scroll-draw-progress",String(u))),u<1&&(V=false);}),se){let i=ae(z(s,T,D,l));for(let c in se){let M=parseFloat(c);i>=M&&!Y.has(M)&&(Y.add(M),se[c]?.());}}!C&&z(s,T,D,l)>0&&(C=true,Se?.()),V&&!E?(E=true,j?.(),q<(_==="infinite"?1/0:_??0)&&(q++,Q=setTimeout(()=>{v=-1,C=false,E=false,Y.clear(),Ae();},ve))):!V&&!X&&(E=false),k=requestAnimationFrame(ee);}let fe=new IntersectionObserver(t=>{t.forEach(s=>{U=s.isIntersecting,U&&!F?k=requestAnimationFrame(ee):cancelAnimationFrame(k);});},{root:d??null,threshold:Ne,rootMargin:Re}),me;function te(){clearTimeout(me),me=setTimeout(()=>{G.forEach((t,s)=>{S[s]=ge(t),t.style.strokeDasharray=`${S[s]}`;}),$e();},150);}return window.addEventListener("resize",te),window.addEventListener("orientationchange",te),we>0?setTimeout(()=>fe.observe(e),we):fe.observe(e),{destroy(){cancelAnimationFrame(k),clearTimeout(Q),fe.disconnect(),window.removeEventListener("resize",te),window.removeEventListener("orientationchange",te),clearTimeout(me),ue?.remove();},replay(){v=-1,J=-1,Z=-1,C=false,E=false,q=0,F=false,Y.clear(),clearTimeout(Q),Ae();},pause(){F=true,cancelAnimationFrame(k);},resume(){F&&(F=false,U&&(k=requestAnimationFrame(ee)));},seek(t){let s=Math.min(1,Math.max(0,t));K=s,v=s,F=true,cancelAnimationFrame(k),ze(s,P);},getProgress(){return K}}}function je(e={}){let r=ref(null);return onMounted(()=>{if(!r.value)return;let o=be(r.value,e);onUnmounted(()=>o.destroy());}),r}var he=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=be(o.value,n);onUnmounted(()=>a.destroy());}),()=>h("div",{ref:o},r.default?.())}});function tt(){return {install(e){e.component("ScrollDraw",he);}}}export{he as ScrollDraw,tt as createScrollDrawPlugin,je 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 Z={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 ee(e="top bottom"){let r=e.trim();if(/^\d+(\.\d+)?%$/.test(r))return {element:"top",viewport:r};let[n="top",
|
|
1
|
+
'use strict';var react=require('react'),jsxRuntime=require('react/jsx-runtime');var Z={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 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 Me(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 Pe(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 Te(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 He(e,r,n){return Math.min(n,Math.max(r,e))}function q(e,r,n,o){return n===r?0:He((e-r)/(n-r)*o,0,1)}function he(e,r,n,o,l){let c=Me(e.top,e.height,r,o.element)-Pe(o.viewport,n),f=Me(e.top,e.height,r,l.element)-Pe(l.viewport,n);return {tStart:c,tEnd:f}}function Le(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=Le(e),l=Le(r);return !o||!l?e:`rgb(${Math.round(o[0]+(l[0]-o[0])*n)},${Math.round(o[1]+(l[1]-o[1])*n)},${Math.round(o[2]+(l[2]-o[2])*n)})`}function De(e,r){process.env.NODE_ENV!=="production"&&console.warn(`[svg-scroll-draw] ${e}`,r);}function We(e){let r=e.getAttribute("stroke"),n=e.getAttribute("fill");!r||r==="none"?De("Element has no stroke \u2014 path will not be visible.",e):n&&n!=="none"&&n!=="transparent"&&De("Element has a fill \u2014 it may obscure the stroke animation.",e);}function Ve(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 l(){let c=n==="x"?window.scrollX:window.scrollY,f=e-c,E=r-c,g=n==="x";o.innerHTML=`
|
|
2
2
|
<div style="position:absolute;${g?`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;${g?`left:${
|
|
3
|
+
<div style="position:absolute;${g?`left:${E}px;top:0;bottom:0;border-left:2px dashed #ef4444;`:`top:${E}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",l,{passive:true}),l(),o}function Ce(e,r,n){let o=(r.match(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g)??[]).map(Number),l=0;return e.replace(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g,c=>{let f=parseFloat(c),E=o[l++]??f;return String(+(f+(E-f)*n).toFixed(4))})}function Ie(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:l=1,fade:c=false,easing:f="linear",trigger:E={},stagger:g=0,direction:y="forward",once:G=false,debug:L=false,axis:h="y",scrollContainer:te,autoReverse:_=false,delay:re=0,strokeColor:x,strokeWidth:b,fillOpacity:D,waypoints:ne,velocityScale:j=false,threshold:ie=0,rootMargin:le="0px",repeat:R=0,repeatDelay:B=0,morphTo:u,clip:w,onProgress:Y,onStart:J,onComplete:C}=r,T=w===true?"left":typeof w=="string"?w:false,be=typeof f=="function"?f:Z[f]??Z.linear,Re=ee(E.start??"top bottom"),Fe=ee(E.end??"bottom top"),v=typeof te=="string"?document.querySelector(te):te??null,F=Array.isArray(x)?x[0]:null,S=Array.isArray(x)?x[1]:typeof x=="string"?x:null,$=Array.isArray(b)?b[0]:null,A=Array.isArray(b)?b[1]:typeof b=="number"?b:null,k=Array.isArray(D)?D[0]:null,M=Array.isArray(D)?D[1]:typeof D=="number"?D:null;function K(t){let s=t*100;switch(T){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 se=T?[]:Array.from(e.querySelectorAll(o)),I=[],N=[],z=0,H=0,O=false,U=false,W=0,ae=false,P=-1,ce=-1,X=false,ue=0,oe=0,fe,we=null,pe=new Set,me=-1,$e=performance.now();function ve(){return v?h==="x"?v.scrollLeft:v.scrollTop:h==="x"?window.scrollX:window.scrollY}function Ne(){return v?h==="x"?v.clientWidth:v.clientHeight:h==="x"?window.innerWidth:window.innerHeight}function Ae(){let t=e.getBoundingClientRect(),s,a,d;if(v){let Q=v.getBoundingClientRect();s=h==="x"?t.left-Q.left+v.scrollLeft:t.top-Q.top+v.scrollTop,a=h==="x"?t.width:t.height,d=ve();}else s=h==="x"?t.left:t.top,a=h==="x"?t.width:t.height,d=ve();let ye=he({top:s,height:a},d,Ne(),Re,Fe);z=ye.tStart,H=ye.tEnd,L&&process.env.NODE_ENV!=="production"&&(we?.remove(),we=Ve(z,H,h));}function ze(t,s){if(e.style.setProperty("--scroll-draw-progress",String(t)),T){let a=s==="reverse"?1-t:t;e.style.clipPath=K(a);return}se.forEach((a,d)=>{a.style.strokeDashoffset=s==="reverse"?`${I[d]*t}`:`${I[d]*(1-t)}`,c&&(a.style.opacity=s==="reverse"?`${1-t}`:`${t}`),F&&S?a.style.stroke=Se(F,S,t):S&&(a.style.stroke=S),$!==null&&A!==null?a.style.strokeWidth=`${$+(A-$)*t}`:A!==null&&(a.style.strokeWidth=`${A}`),k!==null&&M!==null?a.style.fillOpacity=`${k+(M-k)*t}`:M!==null&&(a.style.fillOpacity=`${M}`),u&&a.tagName.toLowerCase()==="path"&&N[d]&&a.setAttribute("d",Ce(N[d],u,t));});}function ke(){if(e.style.setProperty("--scroll-draw-progress","0"),T){e.style.clipPath=K(0);return}se.forEach((t,s)=>{t.style.strokeDasharray=`${I[s]}`,t.style.strokeDashoffset=y==="reverse"?"0":`${I[s]}`,c?t.style.opacity=y==="reverse"?"1":"0":t.style.opacity="",F&&(t.style.stroke=F),$!==null&&(t.style.strokeWidth=`${$}`),k!==null&&(t.style.fillOpacity=`${k}`),u&&t.tagName.toLowerCase()==="path"&&N[s]&&t.setAttribute("d",N[s]);});}if(se.forEach(t=>{We(t);let s=Te(t);I.push(s),t.tagName.toLowerCase()==="path"?N.push(t.getAttribute("d")??""):N.push(""),n?(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=y==="reverse"?`${s}`:"0",c&&(t.style.opacity="1"),S&&(t.style.stroke=S),A!==null&&(t.style.strokeWidth=`${A}`),M!==null&&(t.style.fillOpacity=`${M}`),u&&t.tagName.toLowerCase()==="path"&&t.setAttribute("d",u)):(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=y==="reverse"?"0":`${s}`,c?t.style.opacity=y==="reverse"?"1":"0":t.style.opacity="",F&&(t.style.stroke=F),$!==null&&(t.style.strokeWidth=`${$}`),k!==null&&(t.style.fillOpacity=`${k}`));}),T){if(n)return e.style.clipPath=K(1),C?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};e.style.clipPath=K(0);}else if(n)return C?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};Ae();function de(){if(!ae||X)return;let t=performance.now(),s=ve(),a=l;if(j!==false){let i=t-$e,m=i>0?Math.abs(s-(me<0?s:me))/i:0;a=l*Math.max(.2,1+m*(typeof j=="number"?j:1)*.04);}me=s,$e=t;let d=_?ce===-1||s>=ce?"forward":"reverse":y;ce=s;let ye=H-z,Q=true;if(T){let i=be(q(s,z,H,a));G&&!_&&(P=Math.max(P,i),i=P),ue=i,e.style.setProperty("--scroll-draw-progress",String(i));let m=d==="reverse"?1-i:i;e.style.clipPath=K(m),Y?.(i),!U&&q(s,z,H,a)>0&&(U=true,J?.()),i>=1&&!O?(O=true,C?.(),oe<(R==="infinite"?1/0:R??0)&&(oe++,fe=setTimeout(()=>{P=-1,U=false,O=false,e.style.clipPath=K(0);},B))):i<1&&!G&&(O=false),W=requestAnimationFrame(de);return}if(se.forEach((i,m)=>{let V=m*g*ye,p=be(q(s,z+V,H+V,a));G&&!_&&(P=Math.max(P,p),p=P),ue=p,i.style.strokeDashoffset=d==="reverse"?`${I[m]*p}`:`${I[m]*(1-p)}`,c&&(i.style.opacity=d==="reverse"?`${1-p}`:`${p}`),F&&S?i.style.stroke=Se(F,S,p):S&&(i.style.stroke=S),$!==null&&A!==null?i.style.strokeWidth=`${$+(A-$)*p}`:A!==null&&(i.style.strokeWidth=`${A}`),k!==null&&M!==null?i.style.fillOpacity=`${k+(M-k)*p}`:M!==null&&(i.style.fillOpacity=`${M}`),u&&i.tagName.toLowerCase()==="path"&&N[m]&&i.setAttribute("d",Ce(N[m],u,p)),m===0&&(Y?.(p),e.style.setProperty("--scroll-draw-progress",String(p))),p<1&&(Q=false);}),ne){let i=be(q(s,z,H,a));for(let m in ne){let V=parseFloat(m);i>=V&&!pe.has(V)&&(pe.add(V),ne[m]?.());}}!U&&q(s,z,H,a)>0&&(U=true,J?.()),Q&&!O?(O=true,C?.(),oe<(R==="infinite"?1/0:R??0)&&(oe++,fe=setTimeout(()=>{P=-1,U=false,O=false,pe.clear(),ke();},B))):!Q&&!G&&(O=false),W=requestAnimationFrame(de);}let Ee=new IntersectionObserver(t=>{t.forEach(s=>{ae=s.isIntersecting,ae&&!X?W=requestAnimationFrame(de):cancelAnimationFrame(W);});},{root:v??null,threshold:ie,rootMargin:le}),xe;function ge(){clearTimeout(xe),xe=setTimeout(()=>{se.forEach((t,s)=>{I[s]=Te(t),t.style.strokeDasharray=`${I[s]}`;}),Ae();},150);}return window.addEventListener("resize",ge),window.addEventListener("orientationchange",ge),re>0?setTimeout(()=>Ee.observe(e),re):Ee.observe(e),{destroy(){cancelAnimationFrame(W),clearTimeout(fe),Ee.disconnect(),window.removeEventListener("resize",ge),window.removeEventListener("orientationchange",ge),clearTimeout(xe),we?.remove();},replay(){P=-1,ce=-1,me=-1,U=false,O=false,oe=0,X=false,pe.clear(),clearTimeout(fe),ke();},pause(){X=true,cancelAnimationFrame(W);},resume(){X&&(X=false,ae&&(W=requestAnimationFrame(de)));},seek(t){let s=Math.min(1,Math.max(0,t));ue=s,P=s,X=true,cancelAnimationFrame(W),ze(s,y);},getProgress(){return ue}}}function Be(e,r={}){let[n,o]=react.useState(0),l=react.useRef(r);l.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 f=typeof e=="string"?document.querySelector(e):e.current;if(!f)return;let E=f,{axis:g="y",scrollContainer:y,trigger:G={}}=l.current,L=typeof y=="string"?document.querySelector(y):y??null,h=ee(G.start??"top bottom"),te=ee(G.end??"bottom top"),_=0,re=0,x=false,b=0;function D(){return L?g==="x"?L.scrollLeft:L.scrollTop:g==="x"?window.scrollX:window.scrollY}function ne(){return L?g==="x"?L.clientWidth:L.clientHeight:g==="x"?window.innerWidth:window.innerHeight}function j(){let u=E.getBoundingClientRect(),w=D(),Y=g==="x"?u.left:u.top,J=g==="x"?u.width:u.height,C=he({top:Y,height:J},w,ne(),h,te);_=C.tStart,re=C.tEnd;}function ie(){if(!x)return;let{speed:u=1,easing:w="linear",once:Y=false}=l.current,J=typeof w=="function"?w:Z[w]??Z.linear,C=q(D(),_,re,u),T=J(C);Y&&(c.current=Math.max(c.current,T),T=c.current),o(T),b=requestAnimationFrame(ie);}let le=new IntersectionObserver(u=>{u.forEach(w=>{x=w.isIntersecting,x?b=requestAnimationFrame(ie):cancelAnimationFrame(b);});},{root:L??null,threshold:0});j(),le.observe(E);let R;function B(){clearTimeout(R),R=setTimeout(j,150);}return window.addEventListener("resize",B),window.addEventListener("orientationchange",B),()=>{cancelAnimationFrame(b),clearTimeout(R),le.disconnect(),window.removeEventListener("resize",B),window.removeEventListener("orientationchange",B);}},[]),n}function nt({children:e,className:r,style:n,...o}){let l=react.useRef(null);return react.useEffect(()=>{if(!l.current)return;let c=Ie(l.current,o);return ()=>c.destroy()},[]),jsxRuntime.jsx("div",{ref:l,className:r,style:n,children:e})}exports.ScrollDraw=nt;exports.useScrollDrawProgress=Be;
|
package/dist/react/index.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {useState,useRef,useEffect}from'react';import {jsx}from'react/jsx-runtime';var Z={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 ee(e="top bottom"){let r=e.trim();if(/^\d+(\.\d+)?%$/.test(r))return {element:"top",viewport:r};let[n="top",
|
|
1
|
+
import {useState,useRef,useEffect}from'react';import {jsx}from'react/jsx-runtime';var Z={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 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 Me(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 Pe(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 Te(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 He(e,r,n){return Math.min(n,Math.max(r,e))}function q(e,r,n,o){return n===r?0:He((e-r)/(n-r)*o,0,1)}function he(e,r,n,o,l){let c=Me(e.top,e.height,r,o.element)-Pe(o.viewport,n),f=Me(e.top,e.height,r,l.element)-Pe(l.viewport,n);return {tStart:c,tEnd:f}}function Le(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=Le(e),l=Le(r);return !o||!l?e:`rgb(${Math.round(o[0]+(l[0]-o[0])*n)},${Math.round(o[1]+(l[1]-o[1])*n)},${Math.round(o[2]+(l[2]-o[2])*n)})`}function De(e,r){process.env.NODE_ENV!=="production"&&console.warn(`[svg-scroll-draw] ${e}`,r);}function We(e){let r=e.getAttribute("stroke"),n=e.getAttribute("fill");!r||r==="none"?De("Element has no stroke \u2014 path will not be visible.",e):n&&n!=="none"&&n!=="transparent"&&De("Element has a fill \u2014 it may obscure the stroke animation.",e);}function Ve(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 l(){let c=n==="x"?window.scrollX:window.scrollY,f=e-c,E=r-c,g=n==="x";o.innerHTML=`
|
|
2
2
|
<div style="position:absolute;${g?`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;${g?`left:${
|
|
3
|
+
<div style="position:absolute;${g?`left:${E}px;top:0;bottom:0;border-left:2px dashed #ef4444;`:`top:${E}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",l,{passive:true}),l(),o}function Ce(e,r,n){let o=(r.match(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g)??[]).map(Number),l=0;return e.replace(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g,c=>{let f=parseFloat(c),E=o[l++]??f;return String(+(f+(E-f)*n).toFixed(4))})}function Ie(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:l=1,fade:c=false,easing:f="linear",trigger:E={},stagger:g=0,direction:y="forward",once:G=false,debug:L=false,axis:h="y",scrollContainer:te,autoReverse:_=false,delay:re=0,strokeColor:x,strokeWidth:b,fillOpacity:D,waypoints:ne,velocityScale:j=false,threshold:ie=0,rootMargin:le="0px",repeat:R=0,repeatDelay:B=0,morphTo:u,clip:w,onProgress:Y,onStart:J,onComplete:C}=r,T=w===true?"left":typeof w=="string"?w:false,be=typeof f=="function"?f:Z[f]??Z.linear,Re=ee(E.start??"top bottom"),Fe=ee(E.end??"bottom top"),v=typeof te=="string"?document.querySelector(te):te??null,F=Array.isArray(x)?x[0]:null,S=Array.isArray(x)?x[1]:typeof x=="string"?x:null,$=Array.isArray(b)?b[0]:null,A=Array.isArray(b)?b[1]:typeof b=="number"?b:null,k=Array.isArray(D)?D[0]:null,M=Array.isArray(D)?D[1]:typeof D=="number"?D:null;function K(t){let s=t*100;switch(T){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 se=T?[]:Array.from(e.querySelectorAll(o)),I=[],N=[],z=0,H=0,O=false,U=false,W=0,ae=false,P=-1,ce=-1,X=false,ue=0,oe=0,fe,we=null,pe=new Set,me=-1,$e=performance.now();function ve(){return v?h==="x"?v.scrollLeft:v.scrollTop:h==="x"?window.scrollX:window.scrollY}function Ne(){return v?h==="x"?v.clientWidth:v.clientHeight:h==="x"?window.innerWidth:window.innerHeight}function Ae(){let t=e.getBoundingClientRect(),s,a,d;if(v){let Q=v.getBoundingClientRect();s=h==="x"?t.left-Q.left+v.scrollLeft:t.top-Q.top+v.scrollTop,a=h==="x"?t.width:t.height,d=ve();}else s=h==="x"?t.left:t.top,a=h==="x"?t.width:t.height,d=ve();let ye=he({top:s,height:a},d,Ne(),Re,Fe);z=ye.tStart,H=ye.tEnd,L&&process.env.NODE_ENV!=="production"&&(we?.remove(),we=Ve(z,H,h));}function ze(t,s){if(e.style.setProperty("--scroll-draw-progress",String(t)),T){let a=s==="reverse"?1-t:t;e.style.clipPath=K(a);return}se.forEach((a,d)=>{a.style.strokeDashoffset=s==="reverse"?`${I[d]*t}`:`${I[d]*(1-t)}`,c&&(a.style.opacity=s==="reverse"?`${1-t}`:`${t}`),F&&S?a.style.stroke=Se(F,S,t):S&&(a.style.stroke=S),$!==null&&A!==null?a.style.strokeWidth=`${$+(A-$)*t}`:A!==null&&(a.style.strokeWidth=`${A}`),k!==null&&M!==null?a.style.fillOpacity=`${k+(M-k)*t}`:M!==null&&(a.style.fillOpacity=`${M}`),u&&a.tagName.toLowerCase()==="path"&&N[d]&&a.setAttribute("d",Ce(N[d],u,t));});}function ke(){if(e.style.setProperty("--scroll-draw-progress","0"),T){e.style.clipPath=K(0);return}se.forEach((t,s)=>{t.style.strokeDasharray=`${I[s]}`,t.style.strokeDashoffset=y==="reverse"?"0":`${I[s]}`,c?t.style.opacity=y==="reverse"?"1":"0":t.style.opacity="",F&&(t.style.stroke=F),$!==null&&(t.style.strokeWidth=`${$}`),k!==null&&(t.style.fillOpacity=`${k}`),u&&t.tagName.toLowerCase()==="path"&&N[s]&&t.setAttribute("d",N[s]);});}if(se.forEach(t=>{We(t);let s=Te(t);I.push(s),t.tagName.toLowerCase()==="path"?N.push(t.getAttribute("d")??""):N.push(""),n?(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=y==="reverse"?`${s}`:"0",c&&(t.style.opacity="1"),S&&(t.style.stroke=S),A!==null&&(t.style.strokeWidth=`${A}`),M!==null&&(t.style.fillOpacity=`${M}`),u&&t.tagName.toLowerCase()==="path"&&t.setAttribute("d",u)):(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=y==="reverse"?"0":`${s}`,c?t.style.opacity=y==="reverse"?"1":"0":t.style.opacity="",F&&(t.style.stroke=F),$!==null&&(t.style.strokeWidth=`${$}`),k!==null&&(t.style.fillOpacity=`${k}`));}),T){if(n)return e.style.clipPath=K(1),C?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};e.style.clipPath=K(0);}else if(n)return C?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};Ae();function de(){if(!ae||X)return;let t=performance.now(),s=ve(),a=l;if(j!==false){let i=t-$e,m=i>0?Math.abs(s-(me<0?s:me))/i:0;a=l*Math.max(.2,1+m*(typeof j=="number"?j:1)*.04);}me=s,$e=t;let d=_?ce===-1||s>=ce?"forward":"reverse":y;ce=s;let ye=H-z,Q=true;if(T){let i=be(q(s,z,H,a));G&&!_&&(P=Math.max(P,i),i=P),ue=i,e.style.setProperty("--scroll-draw-progress",String(i));let m=d==="reverse"?1-i:i;e.style.clipPath=K(m),Y?.(i),!U&&q(s,z,H,a)>0&&(U=true,J?.()),i>=1&&!O?(O=true,C?.(),oe<(R==="infinite"?1/0:R??0)&&(oe++,fe=setTimeout(()=>{P=-1,U=false,O=false,e.style.clipPath=K(0);},B))):i<1&&!G&&(O=false),W=requestAnimationFrame(de);return}if(se.forEach((i,m)=>{let V=m*g*ye,p=be(q(s,z+V,H+V,a));G&&!_&&(P=Math.max(P,p),p=P),ue=p,i.style.strokeDashoffset=d==="reverse"?`${I[m]*p}`:`${I[m]*(1-p)}`,c&&(i.style.opacity=d==="reverse"?`${1-p}`:`${p}`),F&&S?i.style.stroke=Se(F,S,p):S&&(i.style.stroke=S),$!==null&&A!==null?i.style.strokeWidth=`${$+(A-$)*p}`:A!==null&&(i.style.strokeWidth=`${A}`),k!==null&&M!==null?i.style.fillOpacity=`${k+(M-k)*p}`:M!==null&&(i.style.fillOpacity=`${M}`),u&&i.tagName.toLowerCase()==="path"&&N[m]&&i.setAttribute("d",Ce(N[m],u,p)),m===0&&(Y?.(p),e.style.setProperty("--scroll-draw-progress",String(p))),p<1&&(Q=false);}),ne){let i=be(q(s,z,H,a));for(let m in ne){let V=parseFloat(m);i>=V&&!pe.has(V)&&(pe.add(V),ne[m]?.());}}!U&&q(s,z,H,a)>0&&(U=true,J?.()),Q&&!O?(O=true,C?.(),oe<(R==="infinite"?1/0:R??0)&&(oe++,fe=setTimeout(()=>{P=-1,U=false,O=false,pe.clear(),ke();},B))):!Q&&!G&&(O=false),W=requestAnimationFrame(de);}let Ee=new IntersectionObserver(t=>{t.forEach(s=>{ae=s.isIntersecting,ae&&!X?W=requestAnimationFrame(de):cancelAnimationFrame(W);});},{root:v??null,threshold:ie,rootMargin:le}),xe;function ge(){clearTimeout(xe),xe=setTimeout(()=>{se.forEach((t,s)=>{I[s]=Te(t),t.style.strokeDasharray=`${I[s]}`;}),Ae();},150);}return window.addEventListener("resize",ge),window.addEventListener("orientationchange",ge),re>0?setTimeout(()=>Ee.observe(e),re):Ee.observe(e),{destroy(){cancelAnimationFrame(W),clearTimeout(fe),Ee.disconnect(),window.removeEventListener("resize",ge),window.removeEventListener("orientationchange",ge),clearTimeout(xe),we?.remove();},replay(){P=-1,ce=-1,me=-1,U=false,O=false,oe=0,X=false,pe.clear(),clearTimeout(fe),ke();},pause(){X=true,cancelAnimationFrame(W);},resume(){X&&(X=false,ae&&(W=requestAnimationFrame(de)));},seek(t){let s=Math.min(1,Math.max(0,t));ue=s,P=s,X=true,cancelAnimationFrame(W),ze(s,y);},getProgress(){return ue}}}function Be(e,r={}){let[n,o]=useState(0),l=useRef(r);l.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 f=typeof e=="string"?document.querySelector(e):e.current;if(!f)return;let E=f,{axis:g="y",scrollContainer:y,trigger:G={}}=l.current,L=typeof y=="string"?document.querySelector(y):y??null,h=ee(G.start??"top bottom"),te=ee(G.end??"bottom top"),_=0,re=0,x=false,b=0;function D(){return L?g==="x"?L.scrollLeft:L.scrollTop:g==="x"?window.scrollX:window.scrollY}function ne(){return L?g==="x"?L.clientWidth:L.clientHeight:g==="x"?window.innerWidth:window.innerHeight}function j(){let u=E.getBoundingClientRect(),w=D(),Y=g==="x"?u.left:u.top,J=g==="x"?u.width:u.height,C=he({top:Y,height:J},w,ne(),h,te);_=C.tStart,re=C.tEnd;}function ie(){if(!x)return;let{speed:u=1,easing:w="linear",once:Y=false}=l.current,J=typeof w=="function"?w:Z[w]??Z.linear,C=q(D(),_,re,u),T=J(C);Y&&(c.current=Math.max(c.current,T),T=c.current),o(T),b=requestAnimationFrame(ie);}let le=new IntersectionObserver(u=>{u.forEach(w=>{x=w.isIntersecting,x?b=requestAnimationFrame(ie):cancelAnimationFrame(b);});},{root:L??null,threshold:0});j(),le.observe(E);let R;function B(){clearTimeout(R),R=setTimeout(j,150);}return window.addEventListener("resize",B),window.addEventListener("orientationchange",B),()=>{cancelAnimationFrame(b),clearTimeout(R),le.disconnect(),window.removeEventListener("resize",B),window.removeEventListener("orientationchange",B);}},[]),n}function nt({children:e,className:r,style:n,...o}){let l=useRef(null);return useEffect(()=>{if(!l.current)return;let c=Ie(l.current,o);return ()=>c.destroy()},[]),jsx("div",{ref:l,className:r,style:n,children:e})}export{nt as ScrollDraw,Be as useScrollDrawProgress};
|
package/dist/solid/index.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
'use strict';var solidJs=require('solid-js');var me={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 de(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
|
|
1
|
+
'use strict';var solidJs=require('solid-js');var me={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 de(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 Ae(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 Se(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 ye(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 Ve(e,r,n){return Math.min(n,Math.max(r,e))}function z(e,r,n,o){return n===r?0:Ve((e-r)/(n-r)*o,0,1)}function Me(e,r,n,o,a){let f=Ae(e.top,e.height,r,o.element)-Se(o.viewport,n),p=Ae(e.top,e.height,r,a.element)-Se(a.viewport,n);return {tStart:f,tEnd:p}}function ke(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 ge(e,r,n){let o=ke(e),a=ke(r);return !o||!a?e:`rgb(${Math.round(o[0]+(a[0]-o[0])*n)},${Math.round(o[1]+(a[1]-o[1])*n)},${Math.round(o[2]+(a[2]-o[2])*n)})`}function Le(e,r){process.env.NODE_ENV!=="production"&&console.warn(`[svg-scroll-draw] ${e}`,r);}function ze(e){let r=e.getAttribute("stroke"),n=e.getAttribute("fill");!r||r==="none"?Le("Element has no stroke \u2014 path will not be visible.",e):n&&n!=="none"&&n!=="transparent"&&Le("Element has a fill \u2014 it may obscure the stroke animation.",e);}function Ge(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 a(){let f=n==="x"?window.scrollX:window.scrollY,p=e-f,D=r-f,B=n==="x";o.innerHTML=`
|
|
2
2
|
<div style="position:absolute;${B?`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;${B?`left:${
|
|
3
|
+
<div style="position:absolute;${B?`left:${D}px;top:0;bottom:0;border-left:2px dashed #ef4444;`:`top:${D}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 De(e,r,n){let o=(r.match(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g)??[]).map(Number),a=0;return e.replace(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g,f=>{let p=parseFloat(f),D=o[a++]??p;return String(+(p+(D-p)*n).toFixed(4))})}function he(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:a=1,fade:f=false,easing:p="linear",trigger:D={},stagger:B=0,direction:I="forward",once:X=false,debug:Oe=false,axis:x="y",scrollContainer:ne,autoReverse:se=false,delay:be=0,strokeColor:F,strokeWidth:H,fillOpacity:N,waypoints:oe,velocityScale:ie=false,threshold:Ce=0,rootMargin:Fe="0px",repeat:_=0,repeatDelay:we=0,morphTo:P,clip:le,onProgress:ve,onStart:xe,onComplete:j}=r,R=le===true?"left":typeof le=="string"?le:false,ae=typeof p=="function"?p:me[p]??me.linear,He=de(D.start??"top bottom"),Ne=de(D.end??"bottom top"),d=typeof ne=="string"?document.querySelector(ne):ne??null,T=Array.isArray(F)?F[0]:null,y=Array.isArray(F)?F[1]:typeof F=="string"?F:null,g=Array.isArray(H)?H[0]:null,h=Array.isArray(H)?H[1]:typeof H=="number"?H:null,b=Array.isArray(N)?N[0]:null,w=Array.isArray(N)?N[1]:typeof N=="number"?N:null;function W(t){let s=t*100;switch(R){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 G=R?[]:Array.from(e.querySelectorAll(o)),E=[],A=[],S=0,k=0,$=false,O=false,M=0,J=false,v=-1,K=-1,C=false,Q=0,q=0,U,ue=null,Y=new Set,Z=-1,Ee=performance.now();function ce(){return d?x==="x"?d.scrollLeft:d.scrollTop:x==="x"?window.scrollX:window.scrollY}function Re(){return d?x==="x"?d.clientWidth:d.clientHeight:x==="x"?window.innerWidth:window.innerHeight}function $e(){let t=e.getBoundingClientRect(),s,l,m;if(d){let V=d.getBoundingClientRect();s=x==="x"?t.left-V.left+d.scrollLeft:t.top-V.top+d.scrollTop,l=x==="x"?t.width:t.height,m=ce();}else s=x==="x"?t.left:t.top,l=x==="x"?t.width:t.height,m=ce();let re=Me({top:s,height:l},m,Re(),He,Ne);S=re.tStart,k=re.tEnd,Oe&&process.env.NODE_ENV!=="production"&&(ue?.remove(),ue=Ge(S,k,x));}function We(t,s){if(e.style.setProperty("--scroll-draw-progress",String(t)),R){let l=s==="reverse"?1-t:t;e.style.clipPath=W(l);return}G.forEach((l,m)=>{l.style.strokeDashoffset=s==="reverse"?`${E[m]*t}`:`${E[m]*(1-t)}`,f&&(l.style.opacity=s==="reverse"?`${1-t}`:`${t}`),T&&y?l.style.stroke=ge(T,y,t):y&&(l.style.stroke=y),g!==null&&h!==null?l.style.strokeWidth=`${g+(h-g)*t}`:h!==null&&(l.style.strokeWidth=`${h}`),b!==null&&w!==null?l.style.fillOpacity=`${b+(w-b)*t}`:w!==null&&(l.style.fillOpacity=`${w}`),P&&l.tagName.toLowerCase()==="path"&&A[m]&&l.setAttribute("d",De(A[m],P,t));});}function Te(){if(e.style.setProperty("--scroll-draw-progress","0"),R){e.style.clipPath=W(0);return}G.forEach((t,s)=>{t.style.strokeDasharray=`${E[s]}`,t.style.strokeDashoffset=I==="reverse"?"0":`${E[s]}`,f?t.style.opacity=I==="reverse"?"1":"0":t.style.opacity="",T&&(t.style.stroke=T),g!==null&&(t.style.strokeWidth=`${g}`),b!==null&&(t.style.fillOpacity=`${b}`),P&&t.tagName.toLowerCase()==="path"&&A[s]&&t.setAttribute("d",A[s]);});}if(G.forEach(t=>{ze(t);let s=ye(t);E.push(s),t.tagName.toLowerCase()==="path"?A.push(t.getAttribute("d")??""):A.push(""),n?(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=I==="reverse"?`${s}`:"0",f&&(t.style.opacity="1"),y&&(t.style.stroke=y),h!==null&&(t.style.strokeWidth=`${h}`),w!==null&&(t.style.fillOpacity=`${w}`),P&&t.tagName.toLowerCase()==="path"&&t.setAttribute("d",P)):(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=I==="reverse"?"0":`${s}`,f?t.style.opacity=I==="reverse"?"1":"0":t.style.opacity="",T&&(t.style.stroke=T),g!==null&&(t.style.strokeWidth=`${g}`),b!==null&&(t.style.fillOpacity=`${b}`));}),R){if(n)return e.style.clipPath=W(1),j?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};e.style.clipPath=W(0);}else if(n)return j?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};$e();function ee(){if(!J||C)return;let t=performance.now(),s=ce(),l=a;if(ie!==false){let i=t-Ee,c=i>0?Math.abs(s-(Z<0?s:Z))/i:0;l=a*Math.max(.2,1+c*(typeof ie=="number"?ie:1)*.04);}Z=s,Ee=t;let m=se?K===-1||s>=K?"forward":"reverse":I;K=s;let re=k-S,V=true;if(R){let i=ae(z(s,S,k,l));X&&!se&&(v=Math.max(v,i),i=v),Q=i,e.style.setProperty("--scroll-draw-progress",String(i));let c=m==="reverse"?1-i:i;e.style.clipPath=W(c),ve?.(i),!O&&z(s,S,k,l)>0&&(O=true,xe?.()),i>=1&&!$?($=true,j?.(),q<(_==="infinite"?1/0:_??0)&&(q++,U=setTimeout(()=>{v=-1,O=false,$=false,e.style.clipPath=W(0);},we))):i<1&&!X&&($=false),M=requestAnimationFrame(ee);return}if(G.forEach((i,c)=>{let L=c*B*re,u=ae(z(s,S+L,k+L,l));X&&!se&&(v=Math.max(v,u),u=v),Q=u,i.style.strokeDashoffset=m==="reverse"?`${E[c]*u}`:`${E[c]*(1-u)}`,f&&(i.style.opacity=m==="reverse"?`${1-u}`:`${u}`),T&&y?i.style.stroke=ge(T,y,u):y&&(i.style.stroke=y),g!==null&&h!==null?i.style.strokeWidth=`${g+(h-g)*u}`:h!==null&&(i.style.strokeWidth=`${h}`),b!==null&&w!==null?i.style.fillOpacity=`${b+(w-b)*u}`:w!==null&&(i.style.fillOpacity=`${w}`),P&&i.tagName.toLowerCase()==="path"&&A[c]&&i.setAttribute("d",De(A[c],P,u)),c===0&&(ve?.(u),e.style.setProperty("--scroll-draw-progress",String(u))),u<1&&(V=false);}),oe){let i=ae(z(s,S,k,l));for(let c in oe){let L=parseFloat(c);i>=L&&!Y.has(L)&&(Y.add(L),oe[c]?.());}}!O&&z(s,S,k,l)>0&&(O=true,xe?.()),V&&!$?($=true,j?.(),q<(_==="infinite"?1/0:_??0)&&(q++,U=setTimeout(()=>{v=-1,O=false,$=false,Y.clear(),Te();},we))):!V&&!X&&($=false),M=requestAnimationFrame(ee);}let fe=new IntersectionObserver(t=>{t.forEach(s=>{J=s.isIntersecting,J&&!C?M=requestAnimationFrame(ee):cancelAnimationFrame(M);});},{root:d??null,threshold:Ce,rootMargin:Fe}),pe;function te(){clearTimeout(pe),pe=setTimeout(()=>{G.forEach((t,s)=>{E[s]=ye(t),t.style.strokeDasharray=`${E[s]}`;}),$e();},150);}return window.addEventListener("resize",te),window.addEventListener("orientationchange",te),be>0?setTimeout(()=>fe.observe(e),be):fe.observe(e),{destroy(){cancelAnimationFrame(M),clearTimeout(U),fe.disconnect(),window.removeEventListener("resize",te),window.removeEventListener("orientationchange",te),clearTimeout(pe),ue?.remove();},replay(){v=-1,K=-1,Z=-1,O=false,$=false,q=0,C=false,Y.clear(),clearTimeout(U),Te();},pause(){C=true,cancelAnimationFrame(M);},resume(){C&&(C=false,J&&(M=requestAnimationFrame(ee)));},seek(t){let s=Math.min(1,Math.max(0,t));Q=s,v=s,C=true,cancelAnimationFrame(M),We(s,I);},getProgress(){return Q}}}function Je(e={}){let r,n;return solidJs.onMount(()=>{r&&(n=he(r,e));}),solidJs.onCleanup(()=>{n?.destroy();}),o=>{r=o;}}function Ke(e={}){let r,n;return solidJs.onMount(()=>{r&&(n=he(r,e));}),solidJs.onCleanup(()=>{n?.destroy(),n=void 0;}),{ref:o=>{r=o;},getInstance:()=>n}}exports.createScrollDraw=Ke;exports.useScrollDraw=Je;
|
package/dist/solid/index.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import {onMount,onCleanup}from'solid-js';var me={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 de(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
|
|
1
|
+
import {onMount,onCleanup}from'solid-js';var me={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 de(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 Ae(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 Se(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 ye(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 Ve(e,r,n){return Math.min(n,Math.max(r,e))}function z(e,r,n,o){return n===r?0:Ve((e-r)/(n-r)*o,0,1)}function Me(e,r,n,o,a){let f=Ae(e.top,e.height,r,o.element)-Se(o.viewport,n),p=Ae(e.top,e.height,r,a.element)-Se(a.viewport,n);return {tStart:f,tEnd:p}}function ke(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 ge(e,r,n){let o=ke(e),a=ke(r);return !o||!a?e:`rgb(${Math.round(o[0]+(a[0]-o[0])*n)},${Math.round(o[1]+(a[1]-o[1])*n)},${Math.round(o[2]+(a[2]-o[2])*n)})`}function Le(e,r){process.env.NODE_ENV!=="production"&&console.warn(`[svg-scroll-draw] ${e}`,r);}function ze(e){let r=e.getAttribute("stroke"),n=e.getAttribute("fill");!r||r==="none"?Le("Element has no stroke \u2014 path will not be visible.",e):n&&n!=="none"&&n!=="transparent"&&Le("Element has a fill \u2014 it may obscure the stroke animation.",e);}function Ge(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 a(){let f=n==="x"?window.scrollX:window.scrollY,p=e-f,D=r-f,B=n==="x";o.innerHTML=`
|
|
2
2
|
<div style="position:absolute;${B?`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;${B?`left:${
|
|
3
|
+
<div style="position:absolute;${B?`left:${D}px;top:0;bottom:0;border-left:2px dashed #ef4444;`:`top:${D}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 De(e,r,n){let o=(r.match(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g)??[]).map(Number),a=0;return e.replace(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g,f=>{let p=parseFloat(f),D=o[a++]??p;return String(+(p+(D-p)*n).toFixed(4))})}function he(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:a=1,fade:f=false,easing:p="linear",trigger:D={},stagger:B=0,direction:I="forward",once:X=false,debug:Oe=false,axis:x="y",scrollContainer:ne,autoReverse:se=false,delay:be=0,strokeColor:F,strokeWidth:H,fillOpacity:N,waypoints:oe,velocityScale:ie=false,threshold:Ce=0,rootMargin:Fe="0px",repeat:_=0,repeatDelay:we=0,morphTo:P,clip:le,onProgress:ve,onStart:xe,onComplete:j}=r,R=le===true?"left":typeof le=="string"?le:false,ae=typeof p=="function"?p:me[p]??me.linear,He=de(D.start??"top bottom"),Ne=de(D.end??"bottom top"),d=typeof ne=="string"?document.querySelector(ne):ne??null,T=Array.isArray(F)?F[0]:null,y=Array.isArray(F)?F[1]:typeof F=="string"?F:null,g=Array.isArray(H)?H[0]:null,h=Array.isArray(H)?H[1]:typeof H=="number"?H:null,b=Array.isArray(N)?N[0]:null,w=Array.isArray(N)?N[1]:typeof N=="number"?N:null;function W(t){let s=t*100;switch(R){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 G=R?[]:Array.from(e.querySelectorAll(o)),E=[],A=[],S=0,k=0,$=false,O=false,M=0,J=false,v=-1,K=-1,C=false,Q=0,q=0,U,ue=null,Y=new Set,Z=-1,Ee=performance.now();function ce(){return d?x==="x"?d.scrollLeft:d.scrollTop:x==="x"?window.scrollX:window.scrollY}function Re(){return d?x==="x"?d.clientWidth:d.clientHeight:x==="x"?window.innerWidth:window.innerHeight}function $e(){let t=e.getBoundingClientRect(),s,l,m;if(d){let V=d.getBoundingClientRect();s=x==="x"?t.left-V.left+d.scrollLeft:t.top-V.top+d.scrollTop,l=x==="x"?t.width:t.height,m=ce();}else s=x==="x"?t.left:t.top,l=x==="x"?t.width:t.height,m=ce();let re=Me({top:s,height:l},m,Re(),He,Ne);S=re.tStart,k=re.tEnd,Oe&&process.env.NODE_ENV!=="production"&&(ue?.remove(),ue=Ge(S,k,x));}function We(t,s){if(e.style.setProperty("--scroll-draw-progress",String(t)),R){let l=s==="reverse"?1-t:t;e.style.clipPath=W(l);return}G.forEach((l,m)=>{l.style.strokeDashoffset=s==="reverse"?`${E[m]*t}`:`${E[m]*(1-t)}`,f&&(l.style.opacity=s==="reverse"?`${1-t}`:`${t}`),T&&y?l.style.stroke=ge(T,y,t):y&&(l.style.stroke=y),g!==null&&h!==null?l.style.strokeWidth=`${g+(h-g)*t}`:h!==null&&(l.style.strokeWidth=`${h}`),b!==null&&w!==null?l.style.fillOpacity=`${b+(w-b)*t}`:w!==null&&(l.style.fillOpacity=`${w}`),P&&l.tagName.toLowerCase()==="path"&&A[m]&&l.setAttribute("d",De(A[m],P,t));});}function Te(){if(e.style.setProperty("--scroll-draw-progress","0"),R){e.style.clipPath=W(0);return}G.forEach((t,s)=>{t.style.strokeDasharray=`${E[s]}`,t.style.strokeDashoffset=I==="reverse"?"0":`${E[s]}`,f?t.style.opacity=I==="reverse"?"1":"0":t.style.opacity="",T&&(t.style.stroke=T),g!==null&&(t.style.strokeWidth=`${g}`),b!==null&&(t.style.fillOpacity=`${b}`),P&&t.tagName.toLowerCase()==="path"&&A[s]&&t.setAttribute("d",A[s]);});}if(G.forEach(t=>{ze(t);let s=ye(t);E.push(s),t.tagName.toLowerCase()==="path"?A.push(t.getAttribute("d")??""):A.push(""),n?(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=I==="reverse"?`${s}`:"0",f&&(t.style.opacity="1"),y&&(t.style.stroke=y),h!==null&&(t.style.strokeWidth=`${h}`),w!==null&&(t.style.fillOpacity=`${w}`),P&&t.tagName.toLowerCase()==="path"&&t.setAttribute("d",P)):(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=I==="reverse"?"0":`${s}`,f?t.style.opacity=I==="reverse"?"1":"0":t.style.opacity="",T&&(t.style.stroke=T),g!==null&&(t.style.strokeWidth=`${g}`),b!==null&&(t.style.fillOpacity=`${b}`));}),R){if(n)return e.style.clipPath=W(1),j?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};e.style.clipPath=W(0);}else if(n)return j?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};$e();function ee(){if(!J||C)return;let t=performance.now(),s=ce(),l=a;if(ie!==false){let i=t-Ee,c=i>0?Math.abs(s-(Z<0?s:Z))/i:0;l=a*Math.max(.2,1+c*(typeof ie=="number"?ie:1)*.04);}Z=s,Ee=t;let m=se?K===-1||s>=K?"forward":"reverse":I;K=s;let re=k-S,V=true;if(R){let i=ae(z(s,S,k,l));X&&!se&&(v=Math.max(v,i),i=v),Q=i,e.style.setProperty("--scroll-draw-progress",String(i));let c=m==="reverse"?1-i:i;e.style.clipPath=W(c),ve?.(i),!O&&z(s,S,k,l)>0&&(O=true,xe?.()),i>=1&&!$?($=true,j?.(),q<(_==="infinite"?1/0:_??0)&&(q++,U=setTimeout(()=>{v=-1,O=false,$=false,e.style.clipPath=W(0);},we))):i<1&&!X&&($=false),M=requestAnimationFrame(ee);return}if(G.forEach((i,c)=>{let L=c*B*re,u=ae(z(s,S+L,k+L,l));X&&!se&&(v=Math.max(v,u),u=v),Q=u,i.style.strokeDashoffset=m==="reverse"?`${E[c]*u}`:`${E[c]*(1-u)}`,f&&(i.style.opacity=m==="reverse"?`${1-u}`:`${u}`),T&&y?i.style.stroke=ge(T,y,u):y&&(i.style.stroke=y),g!==null&&h!==null?i.style.strokeWidth=`${g+(h-g)*u}`:h!==null&&(i.style.strokeWidth=`${h}`),b!==null&&w!==null?i.style.fillOpacity=`${b+(w-b)*u}`:w!==null&&(i.style.fillOpacity=`${w}`),P&&i.tagName.toLowerCase()==="path"&&A[c]&&i.setAttribute("d",De(A[c],P,u)),c===0&&(ve?.(u),e.style.setProperty("--scroll-draw-progress",String(u))),u<1&&(V=false);}),oe){let i=ae(z(s,S,k,l));for(let c in oe){let L=parseFloat(c);i>=L&&!Y.has(L)&&(Y.add(L),oe[c]?.());}}!O&&z(s,S,k,l)>0&&(O=true,xe?.()),V&&!$?($=true,j?.(),q<(_==="infinite"?1/0:_??0)&&(q++,U=setTimeout(()=>{v=-1,O=false,$=false,Y.clear(),Te();},we))):!V&&!X&&($=false),M=requestAnimationFrame(ee);}let fe=new IntersectionObserver(t=>{t.forEach(s=>{J=s.isIntersecting,J&&!C?M=requestAnimationFrame(ee):cancelAnimationFrame(M);});},{root:d??null,threshold:Ce,rootMargin:Fe}),pe;function te(){clearTimeout(pe),pe=setTimeout(()=>{G.forEach((t,s)=>{E[s]=ye(t),t.style.strokeDasharray=`${E[s]}`;}),$e();},150);}return window.addEventListener("resize",te),window.addEventListener("orientationchange",te),be>0?setTimeout(()=>fe.observe(e),be):fe.observe(e),{destroy(){cancelAnimationFrame(M),clearTimeout(U),fe.disconnect(),window.removeEventListener("resize",te),window.removeEventListener("orientationchange",te),clearTimeout(pe),ue?.remove();},replay(){v=-1,K=-1,Z=-1,O=false,$=false,q=0,C=false,Y.clear(),clearTimeout(U),Te();},pause(){C=true,cancelAnimationFrame(M);},resume(){C&&(C=false,J&&(M=requestAnimationFrame(ee)));},seek(t){let s=Math.min(1,Math.max(0,t));Q=s,v=s,C=true,cancelAnimationFrame(M),We(s,I);},getProgress(){return Q}}}function Je(e={}){let r,n;return onMount(()=>{r&&(n=he(r,e));}),onCleanup(()=>{n?.destroy();}),o=>{r=o;}}function Ke(e={}){let r,n;return onMount(()=>{r&&(n=he(r,e));}),onCleanup(()=>{n?.destroy(),n=void 0;}),{ref:o=>{r=o;},getInstance:()=>n}}export{Ke as createScrollDraw,Je as useScrollDraw};
|