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) } }