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?

8 Comments

Victolander Bomeyer

the general equation for velocity is displacement / time passed

Victolander Bomeyer

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

Victolander Bomeyer

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

Victolander Bomeyer

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

Victolander Bomeyer

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

Victolander Bomeyer

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