Friday, February 3, 2023

Tanh - a nice S-curve function

I was just made aware by a colleague that tanh makes for a nice ease-in/ease-out/S-curve function!

Here is a quick example that takes a normalised value x (0-1) and gives a nice curved version out. In this case, map is a function that moves the input from one range [0, 1] to another [-1, 1]

function easeInOutCool(steepness, x) {
  const mappedX = map(x, 0, 1, -1, 1);
  return (1 + Math.tanh(steepness * mappedX)) / 2;
}

No comments:

Post a Comment