Skip to content

Instantly share code, notes, and snippets.

@jniac
Last active April 30, 2021 21:50
Show Gist options
  • Save jniac/c34d5f6621064edad847610b441b1881 to your computer and use it in GitHub Desktop.
Save jniac/c34d5f6621064edad847610b441b1881 to your computer and use it in GitHub Desktop.
easeInout, pcurve and math utilities
export const clamp01 = x => x < 0 ? 0 : x > 1 ? 1 : x
export const clamp = (x, min = 0, max = 1) => x < min ? min : x > max ? max : x
export const lerp = (a, b, x) => a + (b - a) * clamp01(x)
export const inverseLerp = (x, min, max) => clamp01((x - min) / (max - min))
export const map = (x, inMin, inMax, outMin, outMax) => lerp(outMin, outMax, inverseLerp(x, inMin, inMax))
export const easeIn = (x, power = 3) => clamp01(x) ** power
export const easeOut = (x, power = 3) => 1 - (1 - clamp01(x)) ** power
// https://www.desmos.com/calculator/ynzjoms155
export const easeInout = (x, power = 3, inflexion = 0.33) => {
x = clamp01(x)
if (x <= inflexion) {
return (inflexion ** (1 - power)) * (x ** power)
}
return 1 - ((1 - inflexion) ** (1 - power)) * ((1 - x) ** power)
}
export const easeInoutBind = (power = 3, inflexion = 0.33) => {
const i1 = inflexion ** (1 - power)
const i2 = (1 - inflexion) ** (1 - power)
return x => {
x = clamp01(x)
if (x <= inflexion) {
return i1 * (x ** power)
}
return 1 - i2 * ((1 - x) ** power)
}
}
// https://www.desmos.com/calculator/9h6o072i43 (from Inigo Quilez)
export const pcurve = (x, a = 3, b = 3) => {
x = clamp01(x)
const k = ((a + b) ** (a + b)) / ((a ** a) * (b ** b))
return k * (x ** a) * ((1 - x) ** b)
}
export const pcurveBind = (a = 3, b = 3) => {
const k = ((a + b) ** (a + b)) / ((a ** a) * (b ** b))
return x => {
x = clamp01(x)
return k * (x ** a) * ((1 - x) ** b)
}
}
/* eslint: i-want-to-break-free */
/* eslint-disable-next-line no-nested-ternary */
export const clamp01 = (x: number) => (x < 0 ? 0 : x > 1 ? 1 : x);
/* eslint-disable-next-line no-nested-ternary */
export const clamp = (x: number, min = 0, max = 1) => (x < min ? min : x > max ? max : x);
export const lerp = (a: number, b: number, x: number) => a + (b - a) * clamp01(x);
export const inverseLerp = (x: number, min: number, max: number) => clamp01((x - min) / (max - min));
export const map = (
x: number,
inMin: number,
inMax: number,
outMin: number,
outMax: number,
) => lerp(outMin, outMax, inverseLerp(x, inMin, inMax));
export const easeIn = (x: number, power = 3) => clamp01(x) ** power;
export const easeOut = (x: number, power = 3) => 1 - (1 - clamp01(x)) ** power;
// https://www.desmos.com/calculator/ynzjoms155
export const easeInout = (x: number, power = 3, inflexion = 0.33) => {
x = clamp01(x);
if (x <= inflexion) {
return (inflexion ** (1 - power)) * (x ** power);
}
return 1 - ((1 - inflexion) ** (1 - power)) * ((1 - x) ** power);
};
export const easeInoutBind = (power = 3, inflexion = 0.33) => {
const i1 = inflexion ** (1 - power);
const i2 = (1 - inflexion) ** (1 - power);
return (x: number) => {
x = clamp01(x);
if (x <= inflexion) {
return i1 * (x ** power);
}
return 1 - i2 * ((1 - x) ** power);
};
};
// https://www.desmos.com/calculator/9h6o072i43 (from Inigo Quilez)
export const pcurve = (x: number, a = 3, b = 3) => {
x = clamp01(x);
const k = ((a + b) ** (a + b)) / ((a ** a) * (b ** b));
return k * (x ** a) * ((1 - x) ** b);
};
export const pcurveBind = (a = 3, b = 3) => {
const k = ((a + b) ** (a + b)) / ((a ** a) * (b ** b));
return (x: number) => {
x = clamp01(x);
return k * (x ** a) * ((1 - x) ** b);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment