(x: number | undefined | null, y: number | undefined | null, duration = 0)
| 377 | */ |
| 378 | @Method() |
| 379 | async scrollToPoint(x: number | undefined | null, y: number | undefined | null, duration = 0): Promise<void> { |
| 380 | const el = await this.getScrollElement(); |
| 381 | if (duration < 32) { |
| 382 | if (y != null) { |
| 383 | el.scrollTop = y; |
| 384 | } |
| 385 | if (x != null) { |
| 386 | el.scrollLeft = x; |
| 387 | } |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | let resolve!: () => void; |
| 392 | let startTime = 0; |
| 393 | const promise = new Promise<void>((r) => (resolve = r)); |
| 394 | const fromY = el.scrollTop; |
| 395 | const fromX = el.scrollLeft; |
| 396 | |
| 397 | const deltaY = y != null ? y - fromY : 0; |
| 398 | const deltaX = x != null ? x - fromX : 0; |
| 399 | |
| 400 | // scroll loop |
| 401 | const step = (timeStamp: number) => { |
| 402 | const linearTime = Math.min(1, (timeStamp - startTime) / duration) - 1; |
| 403 | const easedT = Math.pow(linearTime, 3) + 1; |
| 404 | |
| 405 | if (deltaY !== 0) { |
| 406 | el.scrollTop = Math.floor(easedT * deltaY + fromY); |
| 407 | } |
| 408 | if (deltaX !== 0) { |
| 409 | el.scrollLeft = Math.floor(easedT * deltaX + fromX); |
| 410 | } |
| 411 | |
| 412 | if (easedT < 1) { |
| 413 | // do not use DomController here |
| 414 | // must use nativeRaf in order to fire in the next frame |
| 415 | requestAnimationFrame(step); |
| 416 | } else { |
| 417 | resolve(); |
| 418 | } |
| 419 | }; |
| 420 | // chill out for a frame first |
| 421 | requestAnimationFrame((ts) => { |
| 422 | startTime = ts; |
| 423 | step(ts); |
| 424 | }); |
| 425 | return promise; |
| 426 | } |
| 427 | |
| 428 | private onScrollStart() { |
| 429 | this.isScrolling = true; |
no test coverage detected