From 2964882536afbca3e092eded0c1e350c04fea444 Mon Sep 17 00:00:00 2001 From: ThaUnknown <6506529+ThaUnknown@users.noreply.github.com> Date: Mon, 31 Jul 2023 14:20:41 +0200 Subject: [PATCH] fix: non-fps bound scrolling --- src/renderer/modules/scroll.js | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/renderer/modules/scroll.js b/src/renderer/modules/scroll.js index 1612986..7be6bfc 100644 --- a/src/renderer/modules/scroll.js +++ b/src/renderer/modules/scroll.js @@ -5,21 +5,35 @@ export default function (t, { speed = 120, smooth = 10 } = {}) { let moving = false let pos = 0 let scrollTop = 0 + let lastTime = null t.addEventListener('wheel', e => { e.preventDefault() pos = Math.max(0, Math.min(pos - Math.max(-1, Math.min(1, (e.delta || e.wheelDelta) ?? -e.detail)) * speed, (t.scrollHeight - t.clientHeight) + (smooth * 2))) - if (!moving) update() + if (!moving) { + lastTime = null + update() + } }, { capture: true, passive: false }) - // TODO: this needs to be the scrollend event once we update electron + function getDeltaTime () { + const now = performance.now() + if (!lastTime) { + lastTime = now + return 1 + } + const deltaTime = now - lastTime + lastTime = now + return deltaTime / 14 + } + t.addEventListener('pointerup', () => { pos = scrollTop = t.scrollTop }) function update () { - const delta = pos - scrollTop === smooth * 2 ? 0 : ((pos - scrollTop) / smooth) + const delta = pos - scrollTop === smooth * 2 ? 0 : ((pos - scrollTop) / smooth) * getDeltaTime() scrollTop += delta t.scrollTo(0, scrollTop) - moving = Math.abs(delta) > 0.5 && requestAnimationFrame(update) + moving = Math.abs(delta) > 0.3 && requestAnimationFrame(update) } }