kineticscroll - Reduce Date.now() calls

master
Martin Boonk 2024-03-26 10:26:35 +01:00
parent ae4b1b6206
commit ecf31ce94a
1 changed files with 7 additions and 12 deletions

View File

@ -114,30 +114,25 @@
}; };
const dragHandler = e=>{ const dragHandler = e=>{
let now=Date.now();
direction = e.dy > 0 ? 1 : -1; direction = e.dy > 0 ? 1 : -1;
s.scroll -= e.dy; s.scroll -= e.dy;
if (e.b > 0){ if (e.b > 0){
// Finger touches the display // Finger touches the display or direction has been reversed
lastTouchedDrag = Date.now(); lastTouchedDrag = now;
if (!lastDragStart){ if (!lastDragStart || (accDy * direction < 0 && e.dy * direction > 0)){
lastDragStart = lastTouchedDrag; lastDragStart = lastTouchedDrag;
velocity = 0; velocity = 0;
accDy = 0; accDy = 0;
} }
if (accDy * direction < 0 && e.dy * direction > 0){
// Direction has been reversed, reset accumulated y-values and time of first touch
lastDragStart = Date.now();
velocity = 0;
accDy = 0;
}
accDy += e.dy; accDy += e.dy;
} else { } else {
// Finger has left the display, only start scrolling kinetically when the last drag event is close enough // Finger has left the display, only start scrolling kinetically when the last drag event is close enough
if (Date.now() - lastTouchedDrag < LAST_DRAG_WAIT){ if (now - lastTouchedDrag < LAST_DRAG_WAIT){
velocity = direction * accDy / (Date.now() - lastDragStart) * SPEED; velocity = direction * accDy / (now - lastDragStart) * SPEED;
} }
lastDragStart = 0;
} }
if (!scheduledDraw){ if (!scheduledDraw){