This is a read-only archive of the Framer Community on Facebook.

What is Framer? Join the Community
Return to index
Marc Krenn
Posted Apr 27 - Read on Facebook

What's the best way to calculate a layer's velocity along one axis (X in this case) during an animation?


Doug Tramill

the general equation for velocity is displacement / time passed

Doug Tramill

so you to need to pick a time epoch... 0

Doug Tramill

and in your case it will be (x1 - x0)/time elapsed

Doug Tramill

where x1 is where you ended up and x0 is where you began

Doug Tramill

time elapsed coukd be acieved with two date time stamps from system

Doug Tramill

t0 == 0 and t1 == time elapsed

Marc Krenn

Got it, thanks. Must be something like that then, right?

savelayerXposition = layer.x

Utils.interval 0.5, -> #every 0.5 seconds
.....velocityX = (layer.x - savelayerXposition)/0.5 #calculation
.....savelayerXposition = 0 #clears savelayerXposition

Marc Krenn

Here is what works for me:

velocityX = 0
saveX = 0
refreshrate = 0.06

Utils.interval refreshrate, ->
....saveX = layer.x

.....Utils.delay refreshrate/2, ->
..........velocityX = (saveX - layer.x)/refreshrate
..........print velocityX

Thanks again, Doug!

Read the entire post on Facebook