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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog