Skip to content

Commit

Permalink
Parametric curve functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonsturges committed Nov 14, 2024
1 parent 58509d6 commit 9c2b0a1
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions src/constants/ParametricCurve.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand All @@ -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,
};

0 comments on commit 9c2b0a1

Please sign in to comment.