From 9c2b0a1070c34a1b085199a6f173d673ea7228cb Mon Sep 17 00:00:00 2001 From: Jason Sturges Date: Wed, 13 Nov 2024 23:10:51 -0600 Subject: [PATCH] Parametric curve functions --- src/constants/ParametricCurve.js | 43 ++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/src/constants/ParametricCurve.js b/src/constants/ParametricCurve.js index 659653f..f1302a6 100644 --- a/src/constants/ParametricCurve.js +++ b/src/constants/ParametricCurve.js @@ -1,19 +1,3 @@ -export const parabolicCurve = (t, a = 1, b = 0, c = 0) => { - const clampedT = Math.max(0, Math.min(1, t)); - return a * clampedT * clampedT + b * clampedT + c; -}; - -export const dampedCurve = (t, damping = 1) => { - const clampedDamping = Math.max(0.001, damping); // Avoid zero or negative damping - return (1 - Math.exp(-clampedDamping * t)) / (1 - Math.exp(-clampedDamping)); -}; - -export const quadraticCurve = (t, p0, p1, p2) => { - const clampedT = Math.max(0, Math.min(1, t)); - const oneMinusT = 1 - clampedT; - return oneMinusT * oneMinusT * p0 + 2 * oneMinusT * clampedT * p1 + clampedT * clampedT * p2; -}; - export const cubicCurve = (t, p0, p1, p2, p3) => { const clampedT = Math.max(0, Math.min(1, t)); const oneMinusT = 1 - clampedT; @@ -25,6 +9,11 @@ export const cubicCurve = (t, p0, p1, p2, p3) => { ); }; +export const dampedCurve = (t, damping = 1) => { + const clampedDamping = Math.max(0.001, damping); // Avoid zero or negative damping + return (1 - Math.exp(-clampedDamping * t)) / (1 - Math.exp(-clampedDamping)); +}; + export const exponentialCurve = (t, base = 1, factor = 1) => { const clampedT = Math.max(0, Math.min(1, t)); return base * Math.pow(clampedT, factor); @@ -35,17 +24,33 @@ export const logarithmicCurve = (t, base = 1, factor = 1) => { return base * Math.log(factor * clampedT + 1); }; +export const parabolicCurve = (t, a = 1, b = 0, c = 0) => { + const clampedT = Math.max(0, Math.min(1, t)); + return a * clampedT * clampedT + b * clampedT + c; +}; + +export const quadraticCurve = (t, p0, p1, p2) => { + const clampedT = Math.max(0, Math.min(1, t)); + const oneMinusT = 1 - clampedT; + return oneMinusT * oneMinusT * p0 + 2 * oneMinusT * clampedT * p1 + clampedT * clampedT * p2; +}; + export const sigmoidCurve = (t, a = 10) => { const clampedT = Math.max(0, Math.min(1, t)); return 1 / (1 + Math.exp(-a * (clampedT - 0.5))); }; +const sinusoidalCurve = (t) => { + return Math.sin(t * Math.PI * 0.5); +}; + export const ParametricCurve = { - PARABOLIC: parabolicCurve, - DAMPED: dampedCurve, - QUADRATIC: quadraticCurve, CUBIC: cubicCurve, + DAMPED: dampedCurve, EXPONENTIAL: exponentialCurve, LOGARITHMIC: logarithmicCurve, + PARABOLIC: parabolicCurve, + QUADRATIC: quadraticCurve, SIGMOID: sigmoidCurve, + SINUSOIDAL: sinusoidalCurve, };