Momentum Scrolling Script

Jonathan Lynch jonathandlynch at gmail.com
Wed Jun 28 12:05:38 EDT 2017


Hi everyone,

I just created and tested a momentum scrolling script on my iPhone, and it
appears to work quite well, so I thought I would share it here. It also
works on desktop with mouse dragging. If you see improvements, please share
them.

1. Make sure to have accelerated rendering on for the stack.
2. Make sure to set the layer mode of the group to "scrolling" for mobile
devices, so it will be fully responsive to the touch.
3. You can adjust the various constants in this script to alter the
duration, speed, and length of momentum scrolling. Just play with it as
needed.

Here is the script:
---------------


Local StartDrag

Local AllowDrag

Local StartDragMil

Local CumulativeMomentum


on mousedown

   focus on nothing

   if word 1 of the name of the target = "button" or the isbutton of the
target = 1 then

      exit mousedown

   end if

   put (the mouseV)+(the vScroll of me) into AllowDrag

   put the milliseconds into StartDragMil

   put (the mouseV) into StartDrag

   repeat with CM = 1 to 5

      put 0 into CumulativeMomentum[CM]

   end repeat

   send momentumAccumulate to me in 0 milliseconds

end mousedown


on momentumAccumulate

   if mouse(1) <> "down" then

      exit momentumAccumulate

   end if



   put the milliseconds-StartDragMil into tTimeForLastMove

   put (the mouseV) into tCurrentMouseV

   put tCurrentMouseV - StartDrag into tLastDistanceCovered



   if tTimeForLastMove = 0 then

      put 1 into tTimeForLastMove

   end if

   repeat with CM = 5 down to 2

      put CumulativeMomentum[CM-1] into CumulativeMomentum[CM]

   end repeat

   put (tLastDistanceCovered*10) / tTimeForLastMove into
CumulativeMomentum[1]

   put tCurrentMouseV into StartDrag

   put the milliseconds into StartDragMil

   send momentumAccumulate to me in 5 milliseconds

end momentumAccumulate


on mousemove

   if AllowDrag <> empty then

      set the vScroll of me to AllowDrag-(the mouseV)

   end if

end mousemove


on mouseup

   put empty into AllowDrag

   put 0 into tCMTotal

   Repeat with CM = 1 to 5

      add CumulativeMomentum[CM] to tCMTotal

   end Repeat

   put tCMTotal/5 into tAverageMomentum

   put tAverageMomentum * 30 into tRemainingDistance

   doMomentumScrolling tRemainingDistance,50

end mouseup


on doMomentumScrolling tRemainingDistance, tCount

   if tCount > 50 then

      exit doMomentumScrolling

   end if

   if mouse(1) is "down" then

      exit doMomentumScrolling

   end if

   put tRemainingDistance/15 into tDistanceToMove

   put the vScroll of me into tVScroll

   set the vScroll of me to tVScroll - tDistanceToMove

   put tRemainingDistance-tDistanceToMove into tRemainingDistance

   put tCount-1 into tCount

   send "doMomentumScrolling tRemainingDistance, tCount" to me in 5
milliseconds

end doMomentumScrolling

-- 
Do all things with love



More information about the use-livecode mailing list