dsoihsnz85757 2016-08-12 11:17
浏览 63
已采纳

对循环延迟应用缓动

To speak simple, I am trying to figure out how to apply easing to a loop delay.

for (i := 0; i < 114; i++) {
    // do a task
    time.Sleep(//delay before next job)
}

As you can read it, this is very basic. Let say I want to complete the whole loop in 3s (job completion time is negligeable, t <= us). What is the proper method with Penner's Equations to calculate for each iteration the proper eased delay ?

So, with this function, to simulate an acceleration from zero-velocity, how should I use the t parameter each iteration of the loop to create the proper delay to sleep for ?

func easeInQuad(t float64) {
  return math.Pow(t, 2)
}

I would be very thankfull if you could help me about this. Equations has not been a problem so far, but how to use them with my use case instead.

My question could looks like this one at first : Applying easing to setTimeout delays, within a loop but this one does not take the total time of the loop in account.

However, I think it may be better to use equations rewritten to use only one parameter, in range [0,1] : https://gist.github.com/rezoner/713615dabedb59a15470

From my understanding, I have to calculate the abstract "percentage time elapsed", and somehow interpolate this value with an easing function.

This Node project seems to do just that : https://github.com/CharlotteGore/animation-timer, but again I can't figure out how to reproduce it.

  • 写回答

2条回答 默认 最新

  • duandianzhong8315 2016-08-12 18:37
    关注

    Penner's Equations fundamentally require two parameters: the current progress, and the total possible progress. Alternatively, however, you can instead provide it with percentage-of-total-progress as a single parameter (as a value between 0 and 1), since that's what it uses the current and total to calculate anyway.

    Using your original code, if you want 114 iterations to take place within 3 seconds, the easiest way is to use your iteration index as the current progress, and 114 as the total, then multiply the calculated delay factor by your total duration 3.0s.

    Note, Penner's Equations calculate total displacement from start position, NOT relative displacement for each step, so you actually need to calculate that difference yourself. Thus the delay for this iteration is the total displacement (delay) for this iteration minus the total displacement for the last iteration:

    func DoStuffWithEasing() {
        iterations := 114
        runTime := 3 * time.Second
    
        for i := 1; i <= iterations; i++ {
            // do a task
            time.Sleep(easeInQuadDelay(i, iterations, runTime))
        }
    }
    
    func easeInQuadDelay(c, t int, dur time.Duration) time.Duration {
        if c <= 0 || t == 0 { // invalid cases
            return 0
        }
    
        // This return can be a single-liner, but I split it up for clarity
        // Note that time.Durations are fundamentally int64s,
        // so we can easily type convert them to float64s and back
    
        this := math.Pow(float64(c)/float64(t), 2)
        last := math.Pow(float64(c-1)/float64(t), 2)
        return time.Duration((this - last) * float64(dur))
    }
    

    https://play.golang.org/p/TTgZUYUvxW

    Graph of the easing

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输