Animator with zero duration returns NaN for currentValue

If I create an animator with a duration of 0, calls to currentValue() return NaN until after the delay times out.

Example:

anim = playdate.graphics.animator.new(0, 20, 210, nil, 2000)

function playdate.update() 
    print(anim:currentValue())
    playdate.graphics.clear()
    playdate.graphics.drawRect(10, anim:currentValue(), 10, 10)
end

Expected Behavior
I expect currentValue() to return the startValue until the delay times out, then return endValue.

Actual Behavior
currentValue() returns NaN until after the delay times out, then returns endValue.

Workaround
I can set the delay to 1 instead of 0, to get an animation so short it looks like an immediate change. But I’d prefer to actually just use 0 when I mean 0.

WHYYY?
Why would anyone ever want to create a zero-length animation?
In my project, animations are created dynamically with different values for duration and delay specified for each layer. Sometimes I want a layer to trigger an immediate change after a delay—zero duration.

Perhaps it would smarter to trap these cases and use a delay timer instead, but if possible I’d prefer to use an animator for all lengths of animations, including 0.

In any case, I thought I would at least report that this didn’t behave as I expected. If that’s the intended behavior, maybe just a note in the docs that duration must be > 0?

1 Like