Skip to content

Instantly share code, notes, and snippets.

@arifd
Last active October 18, 2020 09:34
Show Gist options
  • Save arifd/d90b1b44335a1c629da5b544c0c6d7f9 to your computer and use it in GitHub Desktop.
Save arifd/d90b1b44335a1c629da5b544c0c6d7f9 to your computer and use it in GitHub Desktop.
Useful common mathematical functions
// Clamp val between min and max
const clamp = (val, min, max) => Math.min(Math.max(min, val), max);
// Supply a value and a range, return val normalized to 0 and 1
const normalize = (val, min, max) => (val - min) / (max - min);
// Shaping Functions ///////////////////////////////////////
// Parabola val between 0 and 1.
// Original function by Iñigo Quiles
const parabola = val => 4.0 * val * (1.0 - val);
// ease-in-out
// val between 0 and 1.
const sigmoid = (val) => 1 / (1 + Math.exp(5+val*(-10)));
// fast-in-out
// val between 0 and 1.
const middle = (val) => {const val2 = val-0.5; return 4*val2*val2*val2+0.5;};
// balanced parabola
(x + 1) * (1 - x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment