fix: non-fps bound scrolling

This commit is contained in:
ThaUnknown 2023-07-31 14:20:41 +02:00
parent fda8fb3d2a
commit 2964882536

View file

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