svg-scroll-draw 1.0.0 → 1.1.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/README.md +27 -6
- package/dist/angular/index.cjs +3 -3
- package/dist/angular/index.d.mts +14 -0
- package/dist/angular/index.d.ts +14 -0
- package/dist/angular/index.mjs +3 -3
- package/dist/astro/index.cjs +3 -3
- package/dist/astro/index.d.mts +14 -0
- package/dist/astro/index.d.ts +14 -0
- package/dist/astro/index.mjs +3 -3
- package/dist/cdn/svg-scroll-draw.global.js +3 -3
- package/dist/group/index.cjs +3 -3
- package/dist/group/index.d.mts +14 -0
- package/dist/group/index.d.ts +14 -0
- package/dist/group/index.mjs +3 -3
- package/dist/index.cjs +3 -3
- package/dist/index.d.mts +14 -0
- package/dist/index.d.ts +14 -0
- package/dist/index.mjs +3 -3
- package/dist/nuxt/index.cjs +3 -3
- package/dist/nuxt/index.d.mts +14 -0
- package/dist/nuxt/index.d.ts +14 -0
- package/dist/nuxt/index.mjs +3 -3
- package/dist/react/index.cjs +3 -3
- package/dist/react/index.d.mts +14 -0
- package/dist/react/index.d.ts +14 -0
- package/dist/react/index.mjs +3 -3
- package/dist/solid/index.cjs +3 -3
- package/dist/solid/index.d.mts +14 -0
- package/dist/solid/index.d.ts +14 -0
- package/dist/solid/index.mjs +3 -3
- package/dist/svelte/index.cjs +3 -3
- package/dist/svelte/index.d.mts +14 -0
- package/dist/svelte/index.d.ts +14 -0
- package/dist/svelte/index.mjs +3 -3
- package/dist/vue/index.cjs +3 -3
- package/dist/vue/index.d.mts +14 -0
- package/dist/vue/index.d.ts +14 -0
- 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 +2 -2
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
[](./LICENSE)
|
|
8
8
|
[](https://github.com/DhruvilChauahan0210/ink-scroll/stargazers)
|
|
9
9
|
|
|
10
|
-
> Scroll-driven SVG path animation. Zero dependencies. ~
|
|
10
|
+
> Scroll-driven SVG path animation. Zero dependencies. ~4.4 KB gzipped — vs 35–40 KB for Framer Motion / GSAP DrawSVG.
|
|
11
11
|
|
|
12
12
|
**[Live Demo](https://ink-scroll.vercel.app)** · [npm](https://www.npmjs.com/package/svg-scroll-draw) · [Report a bug](https://github.com/DhruvilChauahan0210/ink-scroll/issues)
|
|
13
13
|
|
|
@@ -137,6 +137,24 @@ const containerRef = useScrollDraw({ easing: 'ease-out', speed: 1.2 });
|
|
|
137
137
|
| `trigger.end` | `string` | `"bottom top"` | When animation ends |
|
|
138
138
|
| `onProgress` | `(alpha: number) => void` | — | Called every animation frame with current draw progress (0–1) |
|
|
139
139
|
| `onComplete` | `() => void` | — | Fires once when all paths reach 100% draw progress |
|
|
140
|
+
| `native` | `boolean` | `true` | Run the draw as a native CSS scroll-driven animation when the browser supports it and the config is simple. Falls back to the JS engine automatically. Set `false` to always use the JS engine |
|
|
141
|
+
|
|
142
|
+
### Native CSS rendering
|
|
143
|
+
|
|
144
|
+
For the common case — a default trigger, a named easing, optional `fade`, forward or
|
|
145
|
+
reverse — `svg-scroll-draw` hands the animation to the browser's native
|
|
146
|
+
[`animation-timeline: view()`](https://developer.mozilla.org/en-US/docs/Web/CSS/animation-timeline).
|
|
147
|
+
The draw then runs on the compositor with **zero per-frame JavaScript and no scroll or
|
|
148
|
+
resize listeners**.
|
|
149
|
+
|
|
150
|
+
It falls back to the JS engine automatically when the browser lacks support, or when the
|
|
151
|
+
config uses something CSS can't express declaratively — callbacks (`onProgress` /
|
|
152
|
+
`onComplete` / `waypoints`), `stagger`, `morphTo`, `velocityScale`, `autoReverse`,
|
|
153
|
+
`once`, `repeat`, a custom trigger, a custom scroll container, `speed ≠ 1`, a
|
|
154
|
+
custom-function or `spring` easing, or animated color/width/fill. The full instance API
|
|
155
|
+
(`pause`, `resume`, `seek`, `replay`, `getProgress`, `destroy`) works on both paths.
|
|
156
|
+
|
|
157
|
+
Pass `native: false` to force the JS engine regardless.
|
|
140
158
|
|
|
141
159
|
### Trigger anchors
|
|
142
160
|
|
|
@@ -160,11 +178,14 @@ Available named anchors: `top`, `center`, `bottom`.
|
|
|
160
178
|
|
|
161
179
|
## Bundle sizes
|
|
162
180
|
|
|
163
|
-
| Format |
|
|
164
|
-
|
|
165
|
-
| ESM (`.mjs`) | ~
|
|
166
|
-
| CJS (`.cjs`) | ~
|
|
167
|
-
|
|
|
181
|
+
| Format | Minified | Gzipped |
|
|
182
|
+
|---|---|---|
|
|
183
|
+
| ESM (`.mjs`) | 11.9 KB | ~4.4 KB |
|
|
184
|
+
| CJS (`.cjs`) | 11.9 KB | ~4.4 KB |
|
|
185
|
+
| React (`/react`) | 13.4 KB | ~4.8 KB |
|
|
186
|
+
| IIFE / CDN (`.global.js`) | 12.9 KB | ~4.8 KB (includes Web Component) |
|
|
187
|
+
|
|
188
|
+
Still 8–9× smaller than Framer Motion (~35 KB) or GSAP DrawSVG (~40 KB), and on supporting browsers the simple case runs as a native CSS scroll animation with zero per-frame JavaScript.
|
|
168
189
|
|
|
169
190
|
---
|
|
170
191
|
|
package/dist/angular/index.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
'use strict';var
|
|
2
|
-
<div style="position:absolute;${
|
|
3
|
-
<div style="position:absolute;${
|
|
1
|
+
'use strict';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 we(e="top bottom"){let r=e.trim();if(/^\d+(\.\d+)?%$/.test(r))return {element:"top",viewport:r};let[s="top",o="bottom"]=r.split(/\s+/).filter(Boolean);return {element:s,viewport:o}}function Ie(e,r,s,o){switch(o){case "top":return e+s;case "center":return e+s+r/2;case "bottom":return e+s+r;default:return e+s}}function Ce(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 $e(e){let r=e.tagName.toLowerCase();if(r==="rect"){let s=parseFloat(e.getAttribute("width")??"0"),o=parseFloat(e.getAttribute("height")??"0");return 2*(s+o)}if(r==="circle"){let s=parseFloat(e.getAttribute("r")??"0");return 2*Math.PI*s}return e.getTotalLength()}function Ue(e,r,s){return Math.min(s,Math.max(r,e))}function B(e,r,s,o){return s===r?0:Ue((e-r)/(s-r)*o,0,1)}function Ee(e,r,s,o,f){let d=Ie(e.top,e.height,r,o.element)-Ce(o.viewport,s),p=Ie(e.top,e.height,r,f.element)-Ce(f.viewport,s);return {tStart:d,tEnd:p}}function Oe(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 s=/^#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(e);if(s)return [parseInt(s[1],16),parseInt(s[2],16),parseInt(s[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 xe(e,r,s){let o=Oe(e),f=Oe(r);return !o||!f?e:`rgb(${Math.round(o[0]+(f[0]-o[0])*s)},${Math.round(o[1]+(f[1]-o[1])*s)},${Math.round(o[2]+(f[2]-o[2])*s)})`}function Fe(e,r){process.env.NODE_ENV!=="production"&&console.warn(`[svg-scroll-draw] ${e}`,r);}var Ne={linear:"linear","ease-in":"ease-in","ease-out":"ease-out","ease-in-out":"ease-in-out"},je=0;function Je(){return typeof CSS<"u"&&typeof CSS.supports=="function"&&CSS.supports("animation-timeline: view()")}function Ke(e){let r=e.getAttribute("stroke"),s=e.getAttribute("fill");!r||r==="none"?Fe("Element has no stroke \u2014 path will not be visible.",e):s&&s!=="none"&&s!=="transparent"&&Fe("Element has a fill \u2014 it may obscure the stroke animation.",e);}function Qe(e,r,s){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 d=s==="x"?window.scrollX:window.scrollY,p=e-d,L=r-d,X=s==="x";o.innerHTML=`
|
|
2
|
+
<div style="position:absolute;${X?`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;${X?`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",f,{passive:true}),f(),o}function Re(e,r,s){let o=(r.match(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g)??[]).map(Number),f=0;return e.replace(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g,d=>{let p=parseFloat(d),L=o[f++]??p;return String(+(p+(L-p)*s).toFixed(4))})}function He(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:f=1,fade:d=false,easing:p="linear",trigger:L={},stagger:X=0,direction:y="forward",once:U=false,debug:We=false,axis:$="y",scrollContainer:ce,autoReverse:Z=false,delay:pe=0,strokeColor:R,strokeWidth:H,fillOpacity:V,waypoints:ee,velocityScale:te=false,threshold:Ge=0,rootMargin:ze="0px",repeat:j=0,repeatDelay:Se=0,morphTo:D,clip:me,native:qe=true,onProgress:de,onStart:ye,onComplete:J}=r,W=me===true?"left":typeof me=="string"?me:false,re=typeof p=="function"?p:ve[p]??ve.linear,ke=we(L.start??"top bottom"),Ae=we(L.end??"bottom top"),b=typeof ce=="string"?document.querySelector(ce):ce??null,I=Array.isArray(R)?R[0]:null,E=Array.isArray(R)?R[1]:typeof R=="string"?R:null,x=Array.isArray(H)?H[0]:null,S=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 n=t*100;switch(W){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 v=W?[]:Array.from(e.querySelectorAll(o)),h=[],C=[],O=0,F=0,P=false,G=false,N=0,ne=false,T=-1,se=-1,z=false,oe=0,K=0,ie,he=null,ae=new Set,le=-1,Te=performance.now();function Q(){return b?$==="x"?b.scrollLeft:b.scrollTop:$==="x"?window.scrollX:window.scrollY}function Me(){return b?$==="x"?b.clientWidth:b.clientHeight:$==="x"?window.innerWidth:window.innerHeight}function Le(){let t=e.getBoundingClientRect(),n,a,m;if(b){let w=b.getBoundingClientRect();n=$==="x"?t.left-w.left+b.scrollLeft:t.top-w.top+b.scrollTop,a=$==="x"?t.width:t.height,m=Q();}else n=$==="x"?t.left:t.top,a=$==="x"?t.width:t.height,m=Q();let q=Ee({top:n,height:a},m,Me(),ke,Ae);O=q.tStart,F=q.tEnd,We&&process.env.NODE_ENV!=="production"&&(he?.remove(),he=Qe(O,F,$));}function Be(t,n){if(e.style.setProperty("--scroll-draw-progress",String(t)),W){let a=n==="reverse"?1-t:t;e.style.clipPath=_(a);return}v.forEach((a,m)=>{a.style.strokeDashoffset=n==="reverse"?`${h[m]*t}`:`${h[m]*(1-t)}`,d&&(a.style.opacity=n==="reverse"?`${1-t}`:`${t}`),I&&E?a.style.stroke=xe(I,E,t):E&&(a.style.stroke=E),x!==null&&S!==null?a.style.strokeWidth=`${x+(S-x)*t}`:S!==null&&(a.style.strokeWidth=`${S}`),k!==null&&A!==null?a.style.fillOpacity=`${k+(A-k)*t}`:A!==null&&(a.style.fillOpacity=`${A}`),D&&a.tagName.toLowerCase()==="path"&&C[m]&&a.setAttribute("d",Re(C[m],D,t));});}function Pe(){if(e.style.setProperty("--scroll-draw-progress","0"),W){e.style.clipPath=_(0);return}v.forEach((t,n)=>{t.style.strokeDasharray=`${h[n]}`,t.style.strokeDashoffset=y==="reverse"?"0":`${h[n]}`,d?t.style.opacity=y==="reverse"?"1":"0":t.style.opacity="",I&&(t.style.stroke=I),x!==null&&(t.style.strokeWidth=`${x}`),k!==null&&(t.style.fillOpacity=`${k}`),D&&t.tagName.toLowerCase()==="path"&&C[n]&&t.setAttribute("d",C[n]);});}if(v.forEach(t=>{Ke(t);let n=$e(t);h.push(n),t.tagName.toLowerCase()==="path"?C.push(t.getAttribute("d")??""):C.push(""),s?(t.style.strokeDasharray=`${n}`,t.style.strokeDashoffset=y==="reverse"?`${n}`:"0",d&&(t.style.opacity="1"),E&&(t.style.stroke=E),S!==null&&(t.style.strokeWidth=`${S}`),A!==null&&(t.style.fillOpacity=`${A}`),D&&t.tagName.toLowerCase()==="path"&&t.setAttribute("d",D)):(t.style.strokeDasharray=`${n}`,t.style.strokeDashoffset=y==="reverse"?"0":`${n}`,d?t.style.opacity=y==="reverse"?"1":"0":t.style.opacity="",I&&(t.style.stroke=I),x!==null&&(t.style.strokeWidth=`${x}`),k!==null&&(t.style.fillOpacity=`${k}`));}),W){if(s)return e.style.clipPath=_(1),J?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};e.style.clipPath=_(0);}else if(s)return J?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};function _e(){return !(qe===false||!Je()||!v.length||typeof p!="string"||!(p in Ne)||W||$!=="y"||b||f!==1||X!==0||U||Z||te!==false||D||ee||j||pe>0||de||ye||J||R!=null||H!=null||V!=null||(L.start??"top bottom").trim()!=="top bottom"||(L.end??"bottom top").trim()!=="bottom top")}function Xe(){let t=`svg-scroll-draw-${++je}`,n=y==="reverse"?"0":"var(--ssd-len)",a=y==="reverse"?"var(--ssd-len)":"0",m=`stroke-dashoffset:${n};`,q=`stroke-dashoffset:${a};`;d&&(m+=`opacity:${y==="reverse"?1:0};`,q+=`opacity:${y==="reverse"?0:1};`);let w=document.createElement("style");w.setAttribute("data-svg-scroll-draw",""),w.textContent=`@keyframes ${t}{from{${m}}to{${q}}}.${t}{animation-name:${t};animation-duration:auto;animation-timing-function:${Ne[p]};animation-fill-mode:both;animation-timeline:view();animation-range:cover 0% cover 100%;}`,document.head.appendChild(w);function i(u,M){u.style.setProperty("--ssd-len",String(h[M])),u.style.strokeDasharray=`${h[M]}`,u.style.strokeDashoffset="",u.style.opacity="",u.style.animationPlayState="",u.classList.add(t);}v.forEach(i);let l=false,g=-1;function c(){if(g>=0)return g;let u=e.getBoundingClientRect(),{tStart:M,tEnd:Y}=Ee({top:u.top,height:u.height},Q(),Me(),ke,Ae);return re(B(Q(),M,Y,f))}return {destroy(){v.forEach(u=>{u.classList.remove(t),u.style.removeProperty("--ssd-len"),u.style.animationPlayState="";}),w.remove();},replay(){l=false,g=-1,v.forEach(i);},pause(){l=true,v.forEach(u=>{u.style.animationPlayState="paused";});},resume(){l&&(l=false,v.forEach(u=>{u.style.animationPlayState="running";}));},seek(u){let M=Math.min(1,Math.max(0,u));g=M,l=true,v.forEach((Y,De)=>{Y.classList.remove(t),Y.style.strokeDashoffset=y==="reverse"?`${h[De]*M}`:`${h[De]*(1-M)}`,d&&(Y.style.opacity=y==="reverse"?`${1-M}`:`${M}`);});},getProgress(){return c()}}}if(_e())return Xe();Le();function ue(){if(!ne||z)return;let t=performance.now(),n=Q(),a=f;if(te!==false){let i=t-Te,l=i>0?Math.abs(n-(le<0?n:le))/i:0;a=f*Math.max(.2,1+l*(typeof te=="number"?te:1)*.04);}le=n,Te=t;let m=Z?se===-1||n>=se?"forward":"reverse":y;se=n;let q=F-O,w=true;if(W){let i=re(B(n,O,F,a));U&&!Z&&(T=Math.max(T,i),i=T),oe=i,e.style.setProperty("--scroll-draw-progress",String(i));let l=m==="reverse"?1-i:i;e.style.clipPath=_(l),de?.(i),!G&&B(n,O,F,a)>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);},Se))):i<1&&!U&&(P=false),N=requestAnimationFrame(ue);return}if(v.forEach((i,l)=>{let g=l*X*q,c=re(B(n,O+g,F+g,a));U&&!Z&&(T=Math.max(T,c),c=T),oe=c,i.style.strokeDashoffset=m==="reverse"?`${h[l]*c}`:`${h[l]*(1-c)}`,d&&(i.style.opacity=m==="reverse"?`${1-c}`:`${c}`),I&&E?i.style.stroke=xe(I,E,c):E&&(i.style.stroke=E),x!==null&&S!==null?i.style.strokeWidth=`${x+(S-x)*c}`:S!==null&&(i.style.strokeWidth=`${S}`),k!==null&&A!==null?i.style.fillOpacity=`${k+(A-k)*c}`:A!==null&&(i.style.fillOpacity=`${A}`),D&&i.tagName.toLowerCase()==="path"&&C[l]&&i.setAttribute("d",Re(C[l],D,c)),l===0&&(de?.(c),e.style.setProperty("--scroll-draw-progress",String(c))),c<1&&(w=false);}),ee){let i=re(B(n,O,F,a));for(let l in ee){let g=parseFloat(l);i>=g&&!ae.has(g)&&(ae.add(g),ee[l]?.());}}!G&&B(n,O,F,a)>0&&(G=true,ye?.()),w&&!P?(P=true,J?.(),K<(j==="infinite"?1/0:j??0)&&(K++,ie=setTimeout(()=>{T=-1,G=false,P=false,ae.clear(),Pe();},Se))):!w&&!U&&(P=false),N=requestAnimationFrame(ue);}let ge=new IntersectionObserver(t=>{t.forEach(n=>{ne=n.isIntersecting,ne&&!z?N=requestAnimationFrame(ue):cancelAnimationFrame(N);});},{root:b??null,threshold:Ge,rootMargin:ze}),be;function fe(){clearTimeout(be),be=setTimeout(()=>{v.forEach((t,n)=>{h[n]=$e(t),t.style.strokeDasharray=`${h[n]}`;}),Le();},150);}return window.addEventListener("resize",fe),window.addEventListener("orientationchange",fe),pe>0?setTimeout(()=>ge.observe(e),pe):ge.observe(e),{destroy(){cancelAnimationFrame(N),clearTimeout(ie),ge.disconnect(),window.removeEventListener("resize",fe),window.removeEventListener("orientationchange",fe),clearTimeout(be),he?.remove();},replay(){T=-1,se=-1,le=-1,G=false,P=false,K=0,z=false,ae.clear(),clearTimeout(ie),Pe();},pause(){z=true,cancelAnimationFrame(N);},resume(){z&&(z=false,ne&&(N=requestAnimationFrame(ue)));},seek(t){let n=Math.min(1,Math.max(0,t));oe=n,T=n,z=true,cancelAnimationFrame(N),Be(n,y);},getProgress(){return oe}}}var Ve=class{constructor(){this.instance=null;}init(r,s={}){return this.destroy(),this.instance=He(r,s),this}replay(){return this.instance?.replay(),this}destroy(){return this.instance?.destroy(),this.instance=null,this}};exports.ScrollDrawRef=Ve;
|
package/dist/angular/index.d.mts
CHANGED
|
@@ -60,6 +60,20 @@ interface ScrollDrawOptions {
|
|
|
60
60
|
onProgress?: (alpha: number) => void;
|
|
61
61
|
onStart?: () => void;
|
|
62
62
|
onComplete?: () => void;
|
|
63
|
+
/**
|
|
64
|
+
* Use the browser's native CSS scroll-driven animation
|
|
65
|
+
* (`animation-timeline: view()`) when the configuration is simple enough and
|
|
66
|
+
* the browser supports it. This runs the draw entirely on the compositor —
|
|
67
|
+
* zero per-frame JavaScript, zero scroll/resize listeners.
|
|
68
|
+
*
|
|
69
|
+
* Falls back to the JS engine automatically when unsupported or when the
|
|
70
|
+
* config uses a feature native CSS can't express (callbacks, stagger, morph,
|
|
71
|
+
* velocity scaling, custom triggers, `once`, custom easing functions, etc.).
|
|
72
|
+
*
|
|
73
|
+
* - `undefined` / `true` (default): use native when eligible.
|
|
74
|
+
* - `false`: always use the JS engine.
|
|
75
|
+
*/
|
|
76
|
+
native?: boolean;
|
|
63
77
|
}
|
|
64
78
|
interface ScrollDrawInstance {
|
|
65
79
|
destroy: () => void;
|
package/dist/angular/index.d.ts
CHANGED
|
@@ -60,6 +60,20 @@ interface ScrollDrawOptions {
|
|
|
60
60
|
onProgress?: (alpha: number) => void;
|
|
61
61
|
onStart?: () => void;
|
|
62
62
|
onComplete?: () => void;
|
|
63
|
+
/**
|
|
64
|
+
* Use the browser's native CSS scroll-driven animation
|
|
65
|
+
* (`animation-timeline: view()`) when the configuration is simple enough and
|
|
66
|
+
* the browser supports it. This runs the draw entirely on the compositor —
|
|
67
|
+
* zero per-frame JavaScript, zero scroll/resize listeners.
|
|
68
|
+
*
|
|
69
|
+
* Falls back to the JS engine automatically when unsupported or when the
|
|
70
|
+
* config uses a feature native CSS can't express (callbacks, stagger, morph,
|
|
71
|
+
* velocity scaling, custom triggers, `once`, custom easing functions, etc.).
|
|
72
|
+
*
|
|
73
|
+
* - `undefined` / `true` (default): use native when eligible.
|
|
74
|
+
* - `false`: always use the JS engine.
|
|
75
|
+
*/
|
|
76
|
+
native?: boolean;
|
|
63
77
|
}
|
|
64
78
|
interface ScrollDrawInstance {
|
|
65
79
|
destroy: () => void;
|
package/dist/angular/index.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
var
|
|
2
|
-
<div style="position:absolute;${
|
|
3
|
-
<div style="position:absolute;${
|
|
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)};function we(e="top bottom"){let r=e.trim();if(/^\d+(\.\d+)?%$/.test(r))return {element:"top",viewport:r};let[s="top",o="bottom"]=r.split(/\s+/).filter(Boolean);return {element:s,viewport:o}}function Ie(e,r,s,o){switch(o){case "top":return e+s;case "center":return e+s+r/2;case "bottom":return e+s+r;default:return e+s}}function Ce(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 $e(e){let r=e.tagName.toLowerCase();if(r==="rect"){let s=parseFloat(e.getAttribute("width")??"0"),o=parseFloat(e.getAttribute("height")??"0");return 2*(s+o)}if(r==="circle"){let s=parseFloat(e.getAttribute("r")??"0");return 2*Math.PI*s}return e.getTotalLength()}function Ue(e,r,s){return Math.min(s,Math.max(r,e))}function B(e,r,s,o){return s===r?0:Ue((e-r)/(s-r)*o,0,1)}function Ee(e,r,s,o,f){let d=Ie(e.top,e.height,r,o.element)-Ce(o.viewport,s),p=Ie(e.top,e.height,r,f.element)-Ce(f.viewport,s);return {tStart:d,tEnd:p}}function Oe(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 s=/^#([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(e);if(s)return [parseInt(s[1],16),parseInt(s[2],16),parseInt(s[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 xe(e,r,s){let o=Oe(e),f=Oe(r);return !o||!f?e:`rgb(${Math.round(o[0]+(f[0]-o[0])*s)},${Math.round(o[1]+(f[1]-o[1])*s)},${Math.round(o[2]+(f[2]-o[2])*s)})`}function Fe(e,r){process.env.NODE_ENV!=="production"&&console.warn(`[svg-scroll-draw] ${e}`,r);}var Ne={linear:"linear","ease-in":"ease-in","ease-out":"ease-out","ease-in-out":"ease-in-out"},je=0;function Je(){return typeof CSS<"u"&&typeof CSS.supports=="function"&&CSS.supports("animation-timeline: view()")}function Ke(e){let r=e.getAttribute("stroke"),s=e.getAttribute("fill");!r||r==="none"?Fe("Element has no stroke \u2014 path will not be visible.",e):s&&s!=="none"&&s!=="transparent"&&Fe("Element has a fill \u2014 it may obscure the stroke animation.",e);}function Qe(e,r,s){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 d=s==="x"?window.scrollX:window.scrollY,p=e-d,L=r-d,X=s==="x";o.innerHTML=`
|
|
2
|
+
<div style="position:absolute;${X?`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;${X?`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",f,{passive:true}),f(),o}function Re(e,r,s){let o=(r.match(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g)??[]).map(Number),f=0;return e.replace(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g,d=>{let p=parseFloat(d),L=o[f++]??p;return String(+(p+(L-p)*s).toFixed(4))})}function He(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:f=1,fade:d=false,easing:p="linear",trigger:L={},stagger:X=0,direction:y="forward",once:U=false,debug:We=false,axis:$="y",scrollContainer:ce,autoReverse:Z=false,delay:pe=0,strokeColor:R,strokeWidth:H,fillOpacity:V,waypoints:ee,velocityScale:te=false,threshold:Ge=0,rootMargin:ze="0px",repeat:j=0,repeatDelay:Se=0,morphTo:D,clip:me,native:qe=true,onProgress:de,onStart:ye,onComplete:J}=r,W=me===true?"left":typeof me=="string"?me:false,re=typeof p=="function"?p:ve[p]??ve.linear,ke=we(L.start??"top bottom"),Ae=we(L.end??"bottom top"),b=typeof ce=="string"?document.querySelector(ce):ce??null,I=Array.isArray(R)?R[0]:null,E=Array.isArray(R)?R[1]:typeof R=="string"?R:null,x=Array.isArray(H)?H[0]:null,S=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 n=t*100;switch(W){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 v=W?[]:Array.from(e.querySelectorAll(o)),h=[],C=[],O=0,F=0,P=false,G=false,N=0,ne=false,T=-1,se=-1,z=false,oe=0,K=0,ie,he=null,ae=new Set,le=-1,Te=performance.now();function Q(){return b?$==="x"?b.scrollLeft:b.scrollTop:$==="x"?window.scrollX:window.scrollY}function Me(){return b?$==="x"?b.clientWidth:b.clientHeight:$==="x"?window.innerWidth:window.innerHeight}function Le(){let t=e.getBoundingClientRect(),n,a,m;if(b){let w=b.getBoundingClientRect();n=$==="x"?t.left-w.left+b.scrollLeft:t.top-w.top+b.scrollTop,a=$==="x"?t.width:t.height,m=Q();}else n=$==="x"?t.left:t.top,a=$==="x"?t.width:t.height,m=Q();let q=Ee({top:n,height:a},m,Me(),ke,Ae);O=q.tStart,F=q.tEnd,We&&process.env.NODE_ENV!=="production"&&(he?.remove(),he=Qe(O,F,$));}function Be(t,n){if(e.style.setProperty("--scroll-draw-progress",String(t)),W){let a=n==="reverse"?1-t:t;e.style.clipPath=_(a);return}v.forEach((a,m)=>{a.style.strokeDashoffset=n==="reverse"?`${h[m]*t}`:`${h[m]*(1-t)}`,d&&(a.style.opacity=n==="reverse"?`${1-t}`:`${t}`),I&&E?a.style.stroke=xe(I,E,t):E&&(a.style.stroke=E),x!==null&&S!==null?a.style.strokeWidth=`${x+(S-x)*t}`:S!==null&&(a.style.strokeWidth=`${S}`),k!==null&&A!==null?a.style.fillOpacity=`${k+(A-k)*t}`:A!==null&&(a.style.fillOpacity=`${A}`),D&&a.tagName.toLowerCase()==="path"&&C[m]&&a.setAttribute("d",Re(C[m],D,t));});}function Pe(){if(e.style.setProperty("--scroll-draw-progress","0"),W){e.style.clipPath=_(0);return}v.forEach((t,n)=>{t.style.strokeDasharray=`${h[n]}`,t.style.strokeDashoffset=y==="reverse"?"0":`${h[n]}`,d?t.style.opacity=y==="reverse"?"1":"0":t.style.opacity="",I&&(t.style.stroke=I),x!==null&&(t.style.strokeWidth=`${x}`),k!==null&&(t.style.fillOpacity=`${k}`),D&&t.tagName.toLowerCase()==="path"&&C[n]&&t.setAttribute("d",C[n]);});}if(v.forEach(t=>{Ke(t);let n=$e(t);h.push(n),t.tagName.toLowerCase()==="path"?C.push(t.getAttribute("d")??""):C.push(""),s?(t.style.strokeDasharray=`${n}`,t.style.strokeDashoffset=y==="reverse"?`${n}`:"0",d&&(t.style.opacity="1"),E&&(t.style.stroke=E),S!==null&&(t.style.strokeWidth=`${S}`),A!==null&&(t.style.fillOpacity=`${A}`),D&&t.tagName.toLowerCase()==="path"&&t.setAttribute("d",D)):(t.style.strokeDasharray=`${n}`,t.style.strokeDashoffset=y==="reverse"?"0":`${n}`,d?t.style.opacity=y==="reverse"?"1":"0":t.style.opacity="",I&&(t.style.stroke=I),x!==null&&(t.style.strokeWidth=`${x}`),k!==null&&(t.style.fillOpacity=`${k}`));}),W){if(s)return e.style.clipPath=_(1),J?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};e.style.clipPath=_(0);}else if(s)return J?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};function _e(){return !(qe===false||!Je()||!v.length||typeof p!="string"||!(p in Ne)||W||$!=="y"||b||f!==1||X!==0||U||Z||te!==false||D||ee||j||pe>0||de||ye||J||R!=null||H!=null||V!=null||(L.start??"top bottom").trim()!=="top bottom"||(L.end??"bottom top").trim()!=="bottom top")}function Xe(){let t=`svg-scroll-draw-${++je}`,n=y==="reverse"?"0":"var(--ssd-len)",a=y==="reverse"?"var(--ssd-len)":"0",m=`stroke-dashoffset:${n};`,q=`stroke-dashoffset:${a};`;d&&(m+=`opacity:${y==="reverse"?1:0};`,q+=`opacity:${y==="reverse"?0:1};`);let w=document.createElement("style");w.setAttribute("data-svg-scroll-draw",""),w.textContent=`@keyframes ${t}{from{${m}}to{${q}}}.${t}{animation-name:${t};animation-duration:auto;animation-timing-function:${Ne[p]};animation-fill-mode:both;animation-timeline:view();animation-range:cover 0% cover 100%;}`,document.head.appendChild(w);function i(u,M){u.style.setProperty("--ssd-len",String(h[M])),u.style.strokeDasharray=`${h[M]}`,u.style.strokeDashoffset="",u.style.opacity="",u.style.animationPlayState="",u.classList.add(t);}v.forEach(i);let l=false,g=-1;function c(){if(g>=0)return g;let u=e.getBoundingClientRect(),{tStart:M,tEnd:Y}=Ee({top:u.top,height:u.height},Q(),Me(),ke,Ae);return re(B(Q(),M,Y,f))}return {destroy(){v.forEach(u=>{u.classList.remove(t),u.style.removeProperty("--ssd-len"),u.style.animationPlayState="";}),w.remove();},replay(){l=false,g=-1,v.forEach(i);},pause(){l=true,v.forEach(u=>{u.style.animationPlayState="paused";});},resume(){l&&(l=false,v.forEach(u=>{u.style.animationPlayState="running";}));},seek(u){let M=Math.min(1,Math.max(0,u));g=M,l=true,v.forEach((Y,De)=>{Y.classList.remove(t),Y.style.strokeDashoffset=y==="reverse"?`${h[De]*M}`:`${h[De]*(1-M)}`,d&&(Y.style.opacity=y==="reverse"?`${1-M}`:`${M}`);});},getProgress(){return c()}}}if(_e())return Xe();Le();function ue(){if(!ne||z)return;let t=performance.now(),n=Q(),a=f;if(te!==false){let i=t-Te,l=i>0?Math.abs(n-(le<0?n:le))/i:0;a=f*Math.max(.2,1+l*(typeof te=="number"?te:1)*.04);}le=n,Te=t;let m=Z?se===-1||n>=se?"forward":"reverse":y;se=n;let q=F-O,w=true;if(W){let i=re(B(n,O,F,a));U&&!Z&&(T=Math.max(T,i),i=T),oe=i,e.style.setProperty("--scroll-draw-progress",String(i));let l=m==="reverse"?1-i:i;e.style.clipPath=_(l),de?.(i),!G&&B(n,O,F,a)>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);},Se))):i<1&&!U&&(P=false),N=requestAnimationFrame(ue);return}if(v.forEach((i,l)=>{let g=l*X*q,c=re(B(n,O+g,F+g,a));U&&!Z&&(T=Math.max(T,c),c=T),oe=c,i.style.strokeDashoffset=m==="reverse"?`${h[l]*c}`:`${h[l]*(1-c)}`,d&&(i.style.opacity=m==="reverse"?`${1-c}`:`${c}`),I&&E?i.style.stroke=xe(I,E,c):E&&(i.style.stroke=E),x!==null&&S!==null?i.style.strokeWidth=`${x+(S-x)*c}`:S!==null&&(i.style.strokeWidth=`${S}`),k!==null&&A!==null?i.style.fillOpacity=`${k+(A-k)*c}`:A!==null&&(i.style.fillOpacity=`${A}`),D&&i.tagName.toLowerCase()==="path"&&C[l]&&i.setAttribute("d",Re(C[l],D,c)),l===0&&(de?.(c),e.style.setProperty("--scroll-draw-progress",String(c))),c<1&&(w=false);}),ee){let i=re(B(n,O,F,a));for(let l in ee){let g=parseFloat(l);i>=g&&!ae.has(g)&&(ae.add(g),ee[l]?.());}}!G&&B(n,O,F,a)>0&&(G=true,ye?.()),w&&!P?(P=true,J?.(),K<(j==="infinite"?1/0:j??0)&&(K++,ie=setTimeout(()=>{T=-1,G=false,P=false,ae.clear(),Pe();},Se))):!w&&!U&&(P=false),N=requestAnimationFrame(ue);}let ge=new IntersectionObserver(t=>{t.forEach(n=>{ne=n.isIntersecting,ne&&!z?N=requestAnimationFrame(ue):cancelAnimationFrame(N);});},{root:b??null,threshold:Ge,rootMargin:ze}),be;function fe(){clearTimeout(be),be=setTimeout(()=>{v.forEach((t,n)=>{h[n]=$e(t),t.style.strokeDasharray=`${h[n]}`;}),Le();},150);}return window.addEventListener("resize",fe),window.addEventListener("orientationchange",fe),pe>0?setTimeout(()=>ge.observe(e),pe):ge.observe(e),{destroy(){cancelAnimationFrame(N),clearTimeout(ie),ge.disconnect(),window.removeEventListener("resize",fe),window.removeEventListener("orientationchange",fe),clearTimeout(be),he?.remove();},replay(){T=-1,se=-1,le=-1,G=false,P=false,K=0,z=false,ae.clear(),clearTimeout(ie),Pe();},pause(){z=true,cancelAnimationFrame(N);},resume(){z&&(z=false,ne&&(N=requestAnimationFrame(ue)));},seek(t){let n=Math.min(1,Math.max(0,t));oe=n,T=n,z=true,cancelAnimationFrame(N),Be(n,y);},getProgress(){return oe}}}var Ve=class{constructor(){this.instance=null;}init(r,s={}){return this.destroy(),this.instance=He(r,s),this}replay(){return this.instance?.replay(),this}destroy(){return this.instance?.destroy(),this.instance=null,this}};export{Ve as ScrollDrawRef};
|
package/dist/astro/index.cjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
'use strict';var
|
|
2
|
-
<div style="position:absolute;${
|
|
3
|
-
<div style="position:absolute;${
|
|
1
|
+
'use strict';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 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 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 Ce(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 Ee(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 Xe(e,r,n){return Math.min(n,Math.max(r,e))}function B(e,r,n,o){return n===r?0:Xe((e-r)/(n-r)*o,0,1)}function $e(e,r,n,o,c){let d=Oe(e.top,e.height,r,o.element)-Ce(o.viewport,n),p=Oe(e.top,e.height,r,c.element)-Ce(c.viewport,n);return {tStart:d,tEnd:p}}function Fe(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 xe(e,r,n){let o=Fe(e),c=Fe(r);return !o||!c?e:`rgb(${Math.round(o[0]+(c[0]-o[0])*n)},${Math.round(o[1]+(c[1]-o[1])*n)},${Math.round(o[2]+(c[2]-o[2])*n)})`}function Ne(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"},Je=0;function Ue(){return typeof CSS<"u"&&typeof CSS.supports=="function"&&CSS.supports("animation-timeline: view()")}function je(e){let r=e.getAttribute("stroke"),n=e.getAttribute("fill");!r||r==="none"?Ne("Element has no stroke \u2014 path will not be visible.",e):n&&n!=="none"&&n!=="transparent"&&Ne("Element has a fill \u2014 it may obscure the stroke animation.",e);}function Ke(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 c(){let d=n==="x"?window.scrollX:window.scrollY,p=e-d,M=r-d,X=n==="x";o.innerHTML=`
|
|
2
|
+
<div style="position:absolute;${X?`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;${X?`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(o),window.addEventListener("scroll",c,{passive:true}),c(),o}function He(e,r,n){let o=(r.match(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g)??[]).map(Number),c=0;return e.replace(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g,d=>{let p=parseFloat(d),M=o[c++]??p;return String(+(p+(M-p)*n).toFixed(4))})}function fe(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:c=1,fade:d=false,easing:p="linear",trigger:M={},stagger:X=0,direction:y="forward",once:J=false,debug:Ve=false,axis:S="y",scrollContainer:pe,autoReverse:Z=false,delay:me=0,strokeColor:R,strokeWidth:H,fillOpacity:V,waypoints:ee,velocityScale:te=false,threshold:We=0,rootMargin:Ge="0px",repeat:U=0,repeatDelay:ke=0,morphTo:P,clip:de,native:ze=true,onProgress:ye,onStart:ge,onComplete:j}=r,W=de===true?"left":typeof de=="string"?de:false,re=typeof p=="function"?p:ve[p]??ve.linear,Ae=Se(M.start??"top bottom"),De=Se(M.end??"bottom top"),b=typeof pe=="string"?document.querySelector(pe):pe??null,I=Array.isArray(R)?R[0]:null,E=Array.isArray(R)?R[1]:typeof R=="string"?R:null,$=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(o)),g=[],O=[],C=0,F=0,L=false,G=false,N=0,ne=false,D=-1,se=-1,z=false,oe=0,K=0,ie,he=null,ae=new Set,le=-1,Te=performance.now();function Q(){return b?S==="x"?b.scrollLeft:b.scrollTop:S==="x"?window.scrollX:window.scrollY}function Me(){return b?S==="x"?b.clientWidth:b.clientHeight:S==="x"?window.innerWidth:window.innerHeight}function Le(){let t=e.getBoundingClientRect(),s,a,m;if(b){let v=b.getBoundingClientRect();s=S==="x"?t.left-v.left+b.scrollLeft:t.top-v.top+b.scrollTop,a=S==="x"?t.width:t.height,m=Q();}else s=S==="x"?t.left:t.top,a=S==="x"?t.width:t.height,m=Q();let q=$e({top:s,height:a},m,Me(),Ae,De);C=q.tStart,F=q.tEnd,Ve&&process.env.NODE_ENV!=="production"&&(he?.remove(),he=Ke(C,F,S));}function qe(t,s){if(e.style.setProperty("--scroll-draw-progress",String(t)),W){let a=s==="reverse"?1-t:t;e.style.clipPath=_(a);return}w.forEach((a,m)=>{a.style.strokeDashoffset=s==="reverse"?`${g[m]*t}`:`${g[m]*(1-t)}`,d&&(a.style.opacity=s==="reverse"?`${1-t}`:`${t}`),I&&E?a.style.stroke=xe(I,E,t):E&&(a.style.stroke=E),$!==null&&x!==null?a.style.strokeWidth=`${$+(x-$)*t}`:x!==null&&(a.style.strokeWidth=`${x}`),k!==null&&A!==null?a.style.fillOpacity=`${k+(A-k)*t}`:A!==null&&(a.style.fillOpacity=`${A}`),P&&a.tagName.toLowerCase()==="path"&&O[m]&&a.setAttribute("d",He(O[m],P,t));});}function Pe(){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="",I&&(t.style.stroke=I),$!==null&&(t.style.strokeWidth=`${$}`),k!==null&&(t.style.fillOpacity=`${k}`),P&&t.tagName.toLowerCase()==="path"&&O[s]&&t.setAttribute("d",O[s]);});}if(w.forEach(t=>{je(t);let s=Ee(t);g.push(s),t.tagName.toLowerCase()==="path"?O.push(t.getAttribute("d")??""):O.push(""),n?(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=y==="reverse"?`${s}`:"0",d&&(t.style.opacity="1"),E&&(t.style.stroke=E),x!==null&&(t.style.strokeWidth=`${x}`),A!==null&&(t.style.fillOpacity=`${A}`),P&&t.tagName.toLowerCase()==="path"&&t.setAttribute("d",P)):(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=y==="reverse"?"0":`${s}`,d?t.style.opacity=y==="reverse"?"1":"0":t.style.opacity="",I&&(t.style.stroke=I),$!==null&&(t.style.strokeWidth=`${$}`),k!==null&&(t.style.fillOpacity=`${k}`));}),W){if(n)return e.style.clipPath=_(1),j?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};e.style.clipPath=_(0);}else if(n)return j?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};function Be(){return !(ze===false||!Ue()||!w.length||typeof p!="string"||!(p in Re)||W||S!=="y"||b||c!==1||X!==0||J||Z||te!==false||P||ee||U||me>0||ye||ge||j||R!=null||H!=null||V!=null||(M.start??"top bottom").trim()!=="top bottom"||(M.end??"bottom top").trim()!=="bottom top")}function _e(){let t=`svg-scroll-draw-${++Je}`,s=y==="reverse"?"0":"var(--ssd-len)",a=y==="reverse"?"var(--ssd-len)":"0",m=`stroke-dashoffset:${s};`,q=`stroke-dashoffset:${a};`;d&&(m+=`opacity:${y==="reverse"?1:0};`,q+=`opacity:${y==="reverse"?0:1};`);let v=document.createElement("style");v.setAttribute("data-svg-scroll-draw",""),v.textContent=`@keyframes ${t}{from{${m}}to{${q}}}.${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(v);function i(u,T){u.style.setProperty("--ssd-len",String(g[T])),u.style.strokeDasharray=`${g[T]}`,u.style.strokeDashoffset="",u.style.opacity="",u.style.animationPlayState="",u.classList.add(t);}w.forEach(i);let l=false,h=-1;function f(){if(h>=0)return h;let u=e.getBoundingClientRect(),{tStart:T,tEnd:Y}=$e({top:u.top,height:u.height},Q(),Me(),Ae,De);return re(B(Q(),T,Y,c))}return {destroy(){w.forEach(u=>{u.classList.remove(t),u.style.removeProperty("--ssd-len"),u.style.animationPlayState="";}),v.remove();},replay(){l=false,h=-1,w.forEach(i);},pause(){l=true,w.forEach(u=>{u.style.animationPlayState="paused";});},resume(){l&&(l=false,w.forEach(u=>{u.style.animationPlayState="running";}));},seek(u){let T=Math.min(1,Math.max(0,u));h=T,l=true,w.forEach((Y,Ie)=>{Y.classList.remove(t),Y.style.strokeDashoffset=y==="reverse"?`${g[Ie]*T}`:`${g[Ie]*(1-T)}`,d&&(Y.style.opacity=y==="reverse"?`${1-T}`:`${T}`);});},getProgress(){return f()}}}if(Be())return _e();Le();function ue(){if(!ne||z)return;let t=performance.now(),s=Q(),a=c;if(te!==false){let i=t-Te,l=i>0?Math.abs(s-(le<0?s:le))/i:0;a=c*Math.max(.2,1+l*(typeof te=="number"?te:1)*.04);}le=s,Te=t;let m=Z?se===-1||s>=se?"forward":"reverse":y;se=s;let q=F-C,v=true;if(W){let i=re(B(s,C,F,a));J&&!Z&&(D=Math.max(D,i),i=D),oe=i,e.style.setProperty("--scroll-draw-progress",String(i));let l=m==="reverse"?1-i:i;e.style.clipPath=_(l),ye?.(i),!G&&B(s,C,F,a)>0&&(G=true,ge?.()),i>=1&&!L?(L=true,j?.(),K<(U==="infinite"?1/0:U??0)&&(K++,ie=setTimeout(()=>{D=-1,G=false,L=false,e.style.clipPath=_(0);},ke))):i<1&&!J&&(L=false),N=requestAnimationFrame(ue);return}if(w.forEach((i,l)=>{let h=l*X*q,f=re(B(s,C+h,F+h,a));J&&!Z&&(D=Math.max(D,f),f=D),oe=f,i.style.strokeDashoffset=m==="reverse"?`${g[l]*f}`:`${g[l]*(1-f)}`,d&&(i.style.opacity=m==="reverse"?`${1-f}`:`${f}`),I&&E?i.style.stroke=xe(I,E,f):E&&(i.style.stroke=E),$!==null&&x!==null?i.style.strokeWidth=`${$+(x-$)*f}`:x!==null&&(i.style.strokeWidth=`${x}`),k!==null&&A!==null?i.style.fillOpacity=`${k+(A-k)*f}`:A!==null&&(i.style.fillOpacity=`${A}`),P&&i.tagName.toLowerCase()==="path"&&O[l]&&i.setAttribute("d",He(O[l],P,f)),l===0&&(ye?.(f),e.style.setProperty("--scroll-draw-progress",String(f))),f<1&&(v=false);}),ee){let i=re(B(s,C,F,a));for(let l in ee){let h=parseFloat(l);i>=h&&!ae.has(h)&&(ae.add(h),ee[l]?.());}}!G&&B(s,C,F,a)>0&&(G=true,ge?.()),v&&!L?(L=true,j?.(),K<(U==="infinite"?1/0:U??0)&&(K++,ie=setTimeout(()=>{D=-1,G=false,L=false,ae.clear(),Pe();},ke))):!v&&!J&&(L=false),N=requestAnimationFrame(ue);}let be=new IntersectionObserver(t=>{t.forEach(s=>{ne=s.isIntersecting,ne&&!z?N=requestAnimationFrame(ue):cancelAnimationFrame(N);});},{root:b??null,threshold:We,rootMargin:Ge}),we;function ce(){clearTimeout(we),we=setTimeout(()=>{w.forEach((t,s)=>{g[s]=Ee(t),t.style.strokeDasharray=`${g[s]}`;}),Le();},150);}return window.addEventListener("resize",ce),window.addEventListener("orientationchange",ce),me>0?setTimeout(()=>be.observe(e),me):be.observe(e),{destroy(){cancelAnimationFrame(N),clearTimeout(ie),be.disconnect(),window.removeEventListener("resize",ce),window.removeEventListener("orientationchange",ce),clearTimeout(we),he?.remove();},replay(){D=-1,se=-1,le=-1,G=false,L=false,K=0,z=false,ae.clear(),clearTimeout(ie),Pe();},pause(){z=true,cancelAnimationFrame(N);},resume(){z&&(z=false,ne&&(N=requestAnimationFrame(ue)));},seek(t){let s=Math.min(1,Math.max(0,t));oe=s,D=s,z=true,cancelAnimationFrame(N),qe(s,y);},getProgress(){return oe}}}function Qe(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?fe(o,r):(console.warn("[svg-scroll-draw] Container not found:",e),n)}function it(e=document){return Array.from(e.querySelectorAll("[data-scroll-draw]")).map(r=>{let n={};try{let o=r.dataset.scrollDrawOptions??r.dataset.scrollDrawoptions??"";o&&(n=JSON.parse(o));}catch{}return fe(r,n)})}exports.initScrollDraw=it;exports.scrollDraw=Qe;
|
package/dist/astro/index.d.mts
CHANGED
|
@@ -60,6 +60,20 @@ interface ScrollDrawOptions {
|
|
|
60
60
|
onProgress?: (alpha: number) => void;
|
|
61
61
|
onStart?: () => void;
|
|
62
62
|
onComplete?: () => void;
|
|
63
|
+
/**
|
|
64
|
+
* Use the browser's native CSS scroll-driven animation
|
|
65
|
+
* (`animation-timeline: view()`) when the configuration is simple enough and
|
|
66
|
+
* the browser supports it. This runs the draw entirely on the compositor —
|
|
67
|
+
* zero per-frame JavaScript, zero scroll/resize listeners.
|
|
68
|
+
*
|
|
69
|
+
* Falls back to the JS engine automatically when unsupported or when the
|
|
70
|
+
* config uses a feature native CSS can't express (callbacks, stagger, morph,
|
|
71
|
+
* velocity scaling, custom triggers, `once`, custom easing functions, etc.).
|
|
72
|
+
*
|
|
73
|
+
* - `undefined` / `true` (default): use native when eligible.
|
|
74
|
+
* - `false`: always use the JS engine.
|
|
75
|
+
*/
|
|
76
|
+
native?: boolean;
|
|
63
77
|
}
|
|
64
78
|
interface ScrollDrawInstance {
|
|
65
79
|
destroy: () => void;
|
package/dist/astro/index.d.ts
CHANGED
|
@@ -60,6 +60,20 @@ interface ScrollDrawOptions {
|
|
|
60
60
|
onProgress?: (alpha: number) => void;
|
|
61
61
|
onStart?: () => void;
|
|
62
62
|
onComplete?: () => void;
|
|
63
|
+
/**
|
|
64
|
+
* Use the browser's native CSS scroll-driven animation
|
|
65
|
+
* (`animation-timeline: view()`) when the configuration is simple enough and
|
|
66
|
+
* the browser supports it. This runs the draw entirely on the compositor —
|
|
67
|
+
* zero per-frame JavaScript, zero scroll/resize listeners.
|
|
68
|
+
*
|
|
69
|
+
* Falls back to the JS engine automatically when unsupported or when the
|
|
70
|
+
* config uses a feature native CSS can't express (callbacks, stagger, morph,
|
|
71
|
+
* velocity scaling, custom triggers, `once`, custom easing functions, etc.).
|
|
72
|
+
*
|
|
73
|
+
* - `undefined` / `true` (default): use native when eligible.
|
|
74
|
+
* - `false`: always use the JS engine.
|
|
75
|
+
*/
|
|
76
|
+
native?: boolean;
|
|
63
77
|
}
|
|
64
78
|
interface ScrollDrawInstance {
|
|
65
79
|
destroy: () => void;
|
package/dist/astro/index.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
var
|
|
2
|
-
<div style="position:absolute;${
|
|
3
|
-
<div style="position:absolute;${
|
|
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)};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 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 Ce(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 Ee(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 Xe(e,r,n){return Math.min(n,Math.max(r,e))}function B(e,r,n,o){return n===r?0:Xe((e-r)/(n-r)*o,0,1)}function $e(e,r,n,o,c){let d=Oe(e.top,e.height,r,o.element)-Ce(o.viewport,n),p=Oe(e.top,e.height,r,c.element)-Ce(c.viewport,n);return {tStart:d,tEnd:p}}function Fe(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 xe(e,r,n){let o=Fe(e),c=Fe(r);return !o||!c?e:`rgb(${Math.round(o[0]+(c[0]-o[0])*n)},${Math.round(o[1]+(c[1]-o[1])*n)},${Math.round(o[2]+(c[2]-o[2])*n)})`}function Ne(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"},Je=0;function Ue(){return typeof CSS<"u"&&typeof CSS.supports=="function"&&CSS.supports("animation-timeline: view()")}function je(e){let r=e.getAttribute("stroke"),n=e.getAttribute("fill");!r||r==="none"?Ne("Element has no stroke \u2014 path will not be visible.",e):n&&n!=="none"&&n!=="transparent"&&Ne("Element has a fill \u2014 it may obscure the stroke animation.",e);}function Ke(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 c(){let d=n==="x"?window.scrollX:window.scrollY,p=e-d,M=r-d,X=n==="x";o.innerHTML=`
|
|
2
|
+
<div style="position:absolute;${X?`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;${X?`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(o),window.addEventListener("scroll",c,{passive:true}),c(),o}function He(e,r,n){let o=(r.match(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g)??[]).map(Number),c=0;return e.replace(/[-+]?(?:\d*\.)?\d+(?:[eE][-+]?\d+)?/g,d=>{let p=parseFloat(d),M=o[c++]??p;return String(+(p+(M-p)*n).toFixed(4))})}function fe(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:c=1,fade:d=false,easing:p="linear",trigger:M={},stagger:X=0,direction:y="forward",once:J=false,debug:Ve=false,axis:S="y",scrollContainer:pe,autoReverse:Z=false,delay:me=0,strokeColor:R,strokeWidth:H,fillOpacity:V,waypoints:ee,velocityScale:te=false,threshold:We=0,rootMargin:Ge="0px",repeat:U=0,repeatDelay:ke=0,morphTo:P,clip:de,native:ze=true,onProgress:ye,onStart:ge,onComplete:j}=r,W=de===true?"left":typeof de=="string"?de:false,re=typeof p=="function"?p:ve[p]??ve.linear,Ae=Se(M.start??"top bottom"),De=Se(M.end??"bottom top"),b=typeof pe=="string"?document.querySelector(pe):pe??null,I=Array.isArray(R)?R[0]:null,E=Array.isArray(R)?R[1]:typeof R=="string"?R:null,$=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(o)),g=[],O=[],C=0,F=0,L=false,G=false,N=0,ne=false,D=-1,se=-1,z=false,oe=0,K=0,ie,he=null,ae=new Set,le=-1,Te=performance.now();function Q(){return b?S==="x"?b.scrollLeft:b.scrollTop:S==="x"?window.scrollX:window.scrollY}function Me(){return b?S==="x"?b.clientWidth:b.clientHeight:S==="x"?window.innerWidth:window.innerHeight}function Le(){let t=e.getBoundingClientRect(),s,a,m;if(b){let v=b.getBoundingClientRect();s=S==="x"?t.left-v.left+b.scrollLeft:t.top-v.top+b.scrollTop,a=S==="x"?t.width:t.height,m=Q();}else s=S==="x"?t.left:t.top,a=S==="x"?t.width:t.height,m=Q();let q=$e({top:s,height:a},m,Me(),Ae,De);C=q.tStart,F=q.tEnd,Ve&&process.env.NODE_ENV!=="production"&&(he?.remove(),he=Ke(C,F,S));}function qe(t,s){if(e.style.setProperty("--scroll-draw-progress",String(t)),W){let a=s==="reverse"?1-t:t;e.style.clipPath=_(a);return}w.forEach((a,m)=>{a.style.strokeDashoffset=s==="reverse"?`${g[m]*t}`:`${g[m]*(1-t)}`,d&&(a.style.opacity=s==="reverse"?`${1-t}`:`${t}`),I&&E?a.style.stroke=xe(I,E,t):E&&(a.style.stroke=E),$!==null&&x!==null?a.style.strokeWidth=`${$+(x-$)*t}`:x!==null&&(a.style.strokeWidth=`${x}`),k!==null&&A!==null?a.style.fillOpacity=`${k+(A-k)*t}`:A!==null&&(a.style.fillOpacity=`${A}`),P&&a.tagName.toLowerCase()==="path"&&O[m]&&a.setAttribute("d",He(O[m],P,t));});}function Pe(){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="",I&&(t.style.stroke=I),$!==null&&(t.style.strokeWidth=`${$}`),k!==null&&(t.style.fillOpacity=`${k}`),P&&t.tagName.toLowerCase()==="path"&&O[s]&&t.setAttribute("d",O[s]);});}if(w.forEach(t=>{je(t);let s=Ee(t);g.push(s),t.tagName.toLowerCase()==="path"?O.push(t.getAttribute("d")??""):O.push(""),n?(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=y==="reverse"?`${s}`:"0",d&&(t.style.opacity="1"),E&&(t.style.stroke=E),x!==null&&(t.style.strokeWidth=`${x}`),A!==null&&(t.style.fillOpacity=`${A}`),P&&t.tagName.toLowerCase()==="path"&&t.setAttribute("d",P)):(t.style.strokeDasharray=`${s}`,t.style.strokeDashoffset=y==="reverse"?"0":`${s}`,d?t.style.opacity=y==="reverse"?"1":"0":t.style.opacity="",I&&(t.style.stroke=I),$!==null&&(t.style.strokeWidth=`${$}`),k!==null&&(t.style.fillOpacity=`${k}`));}),W){if(n)return e.style.clipPath=_(1),j?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};e.style.clipPath=_(0);}else if(n)return j?.(),{destroy:()=>{},replay:()=>{},pause:()=>{},resume:()=>{},seek:()=>{},getProgress:()=>1};function Be(){return !(ze===false||!Ue()||!w.length||typeof p!="string"||!(p in Re)||W||S!=="y"||b||c!==1||X!==0||J||Z||te!==false||P||ee||U||me>0||ye||ge||j||R!=null||H!=null||V!=null||(M.start??"top bottom").trim()!=="top bottom"||(M.end??"bottom top").trim()!=="bottom top")}function _e(){let t=`svg-scroll-draw-${++Je}`,s=y==="reverse"?"0":"var(--ssd-len)",a=y==="reverse"?"var(--ssd-len)":"0",m=`stroke-dashoffset:${s};`,q=`stroke-dashoffset:${a};`;d&&(m+=`opacity:${y==="reverse"?1:0};`,q+=`opacity:${y==="reverse"?0:1};`);let v=document.createElement("style");v.setAttribute("data-svg-scroll-draw",""),v.textContent=`@keyframes ${t}{from{${m}}to{${q}}}.${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(v);function i(u,T){u.style.setProperty("--ssd-len",String(g[T])),u.style.strokeDasharray=`${g[T]}`,u.style.strokeDashoffset="",u.style.opacity="",u.style.animationPlayState="",u.classList.add(t);}w.forEach(i);let l=false,h=-1;function f(){if(h>=0)return h;let u=e.getBoundingClientRect(),{tStart:T,tEnd:Y}=$e({top:u.top,height:u.height},Q(),Me(),Ae,De);return re(B(Q(),T,Y,c))}return {destroy(){w.forEach(u=>{u.classList.remove(t),u.style.removeProperty("--ssd-len"),u.style.animationPlayState="";}),v.remove();},replay(){l=false,h=-1,w.forEach(i);},pause(){l=true,w.forEach(u=>{u.style.animationPlayState="paused";});},resume(){l&&(l=false,w.forEach(u=>{u.style.animationPlayState="running";}));},seek(u){let T=Math.min(1,Math.max(0,u));h=T,l=true,w.forEach((Y,Ie)=>{Y.classList.remove(t),Y.style.strokeDashoffset=y==="reverse"?`${g[Ie]*T}`:`${g[Ie]*(1-T)}`,d&&(Y.style.opacity=y==="reverse"?`${1-T}`:`${T}`);});},getProgress(){return f()}}}if(Be())return _e();Le();function ue(){if(!ne||z)return;let t=performance.now(),s=Q(),a=c;if(te!==false){let i=t-Te,l=i>0?Math.abs(s-(le<0?s:le))/i:0;a=c*Math.max(.2,1+l*(typeof te=="number"?te:1)*.04);}le=s,Te=t;let m=Z?se===-1||s>=se?"forward":"reverse":y;se=s;let q=F-C,v=true;if(W){let i=re(B(s,C,F,a));J&&!Z&&(D=Math.max(D,i),i=D),oe=i,e.style.setProperty("--scroll-draw-progress",String(i));let l=m==="reverse"?1-i:i;e.style.clipPath=_(l),ye?.(i),!G&&B(s,C,F,a)>0&&(G=true,ge?.()),i>=1&&!L?(L=true,j?.(),K<(U==="infinite"?1/0:U??0)&&(K++,ie=setTimeout(()=>{D=-1,G=false,L=false,e.style.clipPath=_(0);},ke))):i<1&&!J&&(L=false),N=requestAnimationFrame(ue);return}if(w.forEach((i,l)=>{let h=l*X*q,f=re(B(s,C+h,F+h,a));J&&!Z&&(D=Math.max(D,f),f=D),oe=f,i.style.strokeDashoffset=m==="reverse"?`${g[l]*f}`:`${g[l]*(1-f)}`,d&&(i.style.opacity=m==="reverse"?`${1-f}`:`${f}`),I&&E?i.style.stroke=xe(I,E,f):E&&(i.style.stroke=E),$!==null&&x!==null?i.style.strokeWidth=`${$+(x-$)*f}`:x!==null&&(i.style.strokeWidth=`${x}`),k!==null&&A!==null?i.style.fillOpacity=`${k+(A-k)*f}`:A!==null&&(i.style.fillOpacity=`${A}`),P&&i.tagName.toLowerCase()==="path"&&O[l]&&i.setAttribute("d",He(O[l],P,f)),l===0&&(ye?.(f),e.style.setProperty("--scroll-draw-progress",String(f))),f<1&&(v=false);}),ee){let i=re(B(s,C,F,a));for(let l in ee){let h=parseFloat(l);i>=h&&!ae.has(h)&&(ae.add(h),ee[l]?.());}}!G&&B(s,C,F,a)>0&&(G=true,ge?.()),v&&!L?(L=true,j?.(),K<(U==="infinite"?1/0:U??0)&&(K++,ie=setTimeout(()=>{D=-1,G=false,L=false,ae.clear(),Pe();},ke))):!v&&!J&&(L=false),N=requestAnimationFrame(ue);}let be=new IntersectionObserver(t=>{t.forEach(s=>{ne=s.isIntersecting,ne&&!z?N=requestAnimationFrame(ue):cancelAnimationFrame(N);});},{root:b??null,threshold:We,rootMargin:Ge}),we;function ce(){clearTimeout(we),we=setTimeout(()=>{w.forEach((t,s)=>{g[s]=Ee(t),t.style.strokeDasharray=`${g[s]}`;}),Le();},150);}return window.addEventListener("resize",ce),window.addEventListener("orientationchange",ce),me>0?setTimeout(()=>be.observe(e),me):be.observe(e),{destroy(){cancelAnimationFrame(N),clearTimeout(ie),be.disconnect(),window.removeEventListener("resize",ce),window.removeEventListener("orientationchange",ce),clearTimeout(we),he?.remove();},replay(){D=-1,se=-1,le=-1,G=false,L=false,K=0,z=false,ae.clear(),clearTimeout(ie),Pe();},pause(){z=true,cancelAnimationFrame(N);},resume(){z&&(z=false,ne&&(N=requestAnimationFrame(ue)));},seek(t){let s=Math.min(1,Math.max(0,t));oe=s,D=s,z=true,cancelAnimationFrame(N),qe(s,y);},getProgress(){return oe}}}function Qe(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?fe(o,r):(console.warn("[svg-scroll-draw] Container not found:",e),n)}function it(e=document){return Array.from(e.querySelectorAll("[data-scroll-draw]")).map(r=>{let n={};try{let o=r.dataset.scrollDrawOptions??r.dataset.scrollDrawoptions??"";o&&(n=JSON.parse(o));}catch{}return fe(r,n)})}export{it as initScrollDraw,Qe as scrollDraw};
|