Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Easing Between Two Values Over Time With An Interval

I'm experimenting with the Web Audio API. I'm capturing start and end points for volume changes and want to apply them with an easing function. The goal is to apply tweaks every 10ms. I needed a way to determine the eased values at that interval. This is what I came up with:

Details

The function takes 5 integer values: a starting time, an ending time, a starting value, an ending value, and an interval. The return value is an array that has a timestamp and volume level for each entry. Everything in the array is spaced at the interval with a possible exception of the last entry. It's always the endTime which means it might not line up exctly if the interval split the time evenly.

It took a while to figure this one out. I feel like I had to have missed hitting good search terms, but I wasn't able to find an example. At some point I'd like to add an animation to this page. That's an exercise for another day.

Playground

This form updates live to show the output from the function based on the inputs.

References

  • This is a good intro article to easing functions if you're not already familiar

  • Very nice collection of easing functions and examples

  • This has a formula to do a reverse lerp. I didn't end up needing it. Keeping it here for reference anyway