@expofp/floorplan
Version:
Interactive floor plan library for expos and events
2 lines (1 loc) • 1.19 kB
JavaScript
export function animateLineSegments(d,b,v,h=0){let i=0,a=0,s=null,r=null;if(!d.length)return;const c=d.map(t=>{const n=t.points[1].x-t.points[0].x,e=t.points[1].y-t.points[0].y;return Math.sqrt(n*n+e*e)}),f=c.reduce((t,n)=>t+n,0),M=Math.max(20,Math.min(f/2,300));function q(t){const n=t%1*f;let e=0;i=0,a=0;for(let o=0;o<c.length;o++){if(e+c[o]>n){i=o,a=n-e;break}e+=c[o],o===c.length-1&&(i=0,a=0)}}function F(t,n){const e=t.points[0],o=t.points[1],l=o.x-e.x,u=o.y-e.y,S=Math.sqrt(l*l+u*u),x=Math.min(n/S,1);return{points:[{x:e.x,y:e.y},{x:e.x+l*x,y:e.y+u*x}],color:t.color,width:t.width}}function g(t){s||(s=t);const n=(t-s)/1e3;s=t;const e=v(),o=M*Math.sqrt(e);for(a+=o*n;a>=c[i];)a-=c[i],i++,i>=d.length&&(i=0);const l=d[i],u=F(l,a);b(u,i,!1),r=requestAnimationFrame(g)}function p(){m(),h<=0&&(i=0,a=0),s=null,r=requestAnimationFrame(g)}function m(){r&&(cancelAnimationFrame(r),r=null,s=null)}function L(){if(f<=0)return 0;let t=0;for(let n=0;n<i;n++)t+=c[n];return t+=a,t/f}function y(){document.visibilityState==="visible"?p():m()}return h>0&&q(h),document.addEventListener("visibilitychange",y),p(),{stop:()=>{m(),document.removeEventListener("visibilitychange",y)},getProgress:L}}