export interface Scrollable { scrollToOffset ( x , y , animate ) : void ; } export class AutoScroll { public static scrollNow ( scrollable , fromX , fromY , toX , toY , speedMultiplier = 1 ) { return new Promise ( ( resolve ) => { scrollable . scrollToOffset ( fromX , fromY , false ) ; const incrementPerMs = 0.1 * speedMultiplier ; let startTime = Date . now ( ) ; let startX = fromX ; let startY = fromY ; const animationLoop = ( ) => { requestAnimationFrame ( ( ) => { const currentTime = Date . now ( ) ; const timeElapsed = currentTime - startTime ; const distanceToCover = incrementPerMs * timeElapsed ; startX += distanceToCover ; startY += distanceToCover ; scrollable . scrollToOffset ( Math . min ( toX , startX ) , Math . min ( toY , startY ) , false ) ; startTime = currentTime ; if ( Math . min ( toX , startX ) !== toX || Math . min ( toY , startY ) !== toY ) { animationLoop ( ) ; return ; } resolve ( ) ; } ) ; } ; animationLoop ( ) ; } ) ; } }