Algorithm to control acceleration until a position is reached

前端 未结 4 1157
[愿得一人]
[愿得一人] 2021-02-02 02:42

I have a point that moves (in one dimension), and I need it to move smoothly. So I think that it\'s velocity has to be a continuous function and I need to control the accelerati

4条回答
  •  无人共我
    2021-02-02 03:05

    You need an easing formula, which you would call at a set interval, passing in the time elapsed, start point, end point and duration you want the animation to be.

    Doing time-based calculations will account for slow clients and other random hiccups. Since it calculates on time elapsed vs. the time in which it has to compkete, it will account for slow intervals between calls when returning how far along your point should be in the animation.

    The jquery.easing plugin has a ton of easing functions you can look at:

    http://gsgd.co.uk/sandbox/jquery/easing/

    I've found it best to pass in 0 and 1 as my start and end point, since it will return a floating point between the two, you can easily apply it to the real value you are modifying using multiplication.

提交回复
热议问题