my-animation-lib
Version:
A powerful animation library combining Three.js, GSAP, custom scroll triggers, and advanced effects with MathUtils integration
237 lines (191 loc) • 6.61 kB
JavaScript
export class MathUtils {
static clamp(value, min, max) {
return Math.min(Math.max(value, min), max);
}
static lerp(start, end, factor) {
return start + (end - start) * factor;
}
static map(value, inMin, inMax, outMin, outMax) {
return ((value - inMin) * (outMax - outMin)) / (inMax - inMin) + outMin;
}
static random(min, max) {
return Math.random() * (max - min) + min;
}
static randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
static randomChoice(array) {
return array[Math.floor(Math.random() * array.length)];
}
static distance(x1, y1, x2, y2) {
const dx = x2 - x1;
const dy = y2 - y1;
return Math.sqrt(dx * dx + dy * dy);
}
static distance3D(x1, y1, z1, x2, y2, z2) {
const dx = x2 - x1;
const dy = y2 - y1;
const dz = z2 - z1;
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
static angle(x1, y1, x2, y2) {
return Math.atan2(y2 - y1, x2 - x1);
}
static degreesToRadians(degrees) {
return degrees * (Math.PI / 180);
}
static radiansToDegrees(radians) {
return radians * (180 / Math.PI);
}
static normalize(value, min, max) {
return (value - min) / (max - min);
}
static smoothstep(edge0, edge1, x) {
const t = MathUtils.clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
return t * t * (3.0 - 2.0 * t);
}
static smootherstep(edge0, edge1, x) {
const t = MathUtils.clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
return t * t * t * (t * (t * 6 - 15) + 10);
}
static noise(x) {
return Math.sin(x * 12.9898) * Math.cos(x * 78.233) * 43758.5453 % 1;
}
static perlinNoise(x, y) {
const X = Math.floor(x) & 255;
const Y = Math.floor(y) & 255;
x -= Math.floor(x);
y -= Math.floor(y);
const u = this.fade(x);
const v = this.fade(y);
const A = this.p[X] + Y;
const AA = this.p[A];
const AB = this.p[A + 1];
const B = this.p[X + 1] + Y;
const BA = this.p[B];
const BB = this.p[B + 1];
return this.lerp(
this.lerp(this.grad(this.p[AA], x, y), this.grad(this.p[BA], x - 1, y), u),
this.lerp(this.grad(this.p[AB], x, y - 1), this.grad(this.p[BB], x - 1, y - 1), u),
v
);
}
static fade(t) {
return t * t * t * (t * (t * 6 - 15) + 10);
}
static lerp(a, b, t) {
return a + t * (b - a);
}
static grad(hash, x, y) {
const h = hash & 15;
const u = h < 8 ? x : y;
const v = h < 4 ? y : h === 12 || h === 14 ? x : 0;
return ((h & 1) === 0 ? u : -u) + ((h & 2) === 0 ? v : -v);
}
static p = new Array(512);
static permutation = [
151, 160, 137, 91, 90, 15, 131, 13, 201, 95, 96, 53, 194, 233, 7, 225, 140, 36, 103, 30, 69, 142, 8, 99, 37, 240, 21, 10, 23, 190, 6, 148, 247, 120, 234, 75, 0, 26, 197, 62, 94, 252, 219, 203, 117, 35, 11, 32, 57, 177, 33, 88, 237, 149, 56, 87, 174, 20, 125, 136, 171, 168, 68, 175, 74, 165, 71, 134, 139, 48, 27, 166, 77, 146, 158, 231, 83, 111, 229, 122, 60, 211, 133, 230, 220, 105, 92, 41, 55, 46, 245, 40, 244, 102, 143, 54, 65, 25, 63, 161, 1, 216, 80, 73, 209, 76, 132, 187, 208, 89, 18, 169, 200, 196, 135, 130, 116, 188, 159, 86, 164, 100, 109, 198, 173, 186, 3, 64, 52, 217, 226, 250, 124, 123, 5, 202, 38, 147, 118, 126, 255, 82, 85, 212, 207, 206, 59, 227, 47, 16, 58, 17, 182, 189, 28, 42, 223, 183, 170, 213, 119, 248, 152, 2, 44, 154, 163, 70, 221, 153, 101, 155, 167, 43, 172, 9, 129, 22, 39, 253, 19, 98, 108, 110, 79, 113, 224, 232, 178, 185, 112, 104, 218, 246, 97, 228, 251, 34, 242, 193, 238, 210, 144, 12, 191, 179, 162, 241, 81, 51, 145, 235, 249, 14, 239, 107, 49, 192, 214, 31, 181, 199, 106, 157, 184, 84, 204, 176, 115, 121, 50, 45, 127, 4, 150, 254, 138, 236, 205, 93, 222, 114, 67, 29, 24, 72, 243, 141, 128, 195, 78, 66, 215, 61, 156, 180
];
static {
for (let i = 0; i < 256; i++) {
this.p[i] = this.permutation[i];
this.p[i + 256] = this.permutation[i];
}
}
static bezier(t, p0, p1, p2, p3) {
const u = 1 - t;
const tt = t * t;
const uu = u * u;
const uuu = uu * u;
const ttt = tt * t;
return uuu * p0 + 3 * uu * t * p1 + 3 * u * tt * p2 + ttt * p3;
}
static catmullRom(t, p0, p1, p2, p3) {
const t2 = t * t;
const t3 = t2 * t;
// Catmull-Rom spline formula
return 0.5 * (
(2 * p1) +
(-p0 + p2) * t +
(2 * p0 - 5 * p1 + 4 * p2 - p3) * t2 +
(-p0 + 3 * p1 - 3 * p2 + p3) * t3
);
}
static fibonacci(n) {
if (n <= 1) return n;
return this.fibonacci(n - 1) + this.fibonacci(n - 2);
}
static factorial(n) {
if (n <= 1) return 1;
return n * this.factorial(n - 1);
}
static isPrime(n) {
if (n < 2) return false;
if (n === 2) return true;
if (n % 2 === 0) return false;
for (let i = 3; i <= Math.sqrt(n); i += 2) {
if (n % i === 0) return false;
}
return true;
}
static gcd(a, b) {
while (b !== 0) {
const temp = b;
b = a % b;
a = temp;
}
return a;
}
static lcm(a, b) {
return (a * b) / this.gcd(a, b);
}
// Additional animation-friendly math utilities
static bounce(t) {
return Math.abs(Math.sin(t * Math.PI * 2));
}
static elastic(t) {
return Math.pow(2, 10 * (t - 1)) * Math.cos(t * Math.PI * 2);
}
static back(t) {
const s = 1.70158;
return t * t * ((s + 1) * t - s);
}
static circular(t) {
return 1 - Math.sqrt(1 - t * t);
}
static exponential(t) {
return t === 0 ? 0 : Math.pow(2, 10 * (t - 1));
}
static sine(t) {
return 1 - Math.cos(t * Math.PI / 2);
}
static cubic(t) {
return t * t * t;
}
static quartic(t) {
return t * t * t * t;
}
static quintic(t) {
return t * t * t * t * t;
}
static bounceOut(t) {
if (t < 1 / 2.75) {
return 7.5625 * t * t;
} else if (t < 2 / 2.75) {
return 7.5625 * (t -= 1.5 / 2.75) * t + 0.75;
} else if (t < 2.5 / 2.75) {
return 7.5625 * (t -= 2.25 / 2.75) * t + 0.9375;
} else {
return 7.5625 * (t -= 2.625 / 2.75) * t + 0.984375;
}
}
static elasticOut(t) {
if (t === 0) return 0;
if (t === 1) return 1;
return Math.pow(2, -10 * t) * Math.sin((t - 0.075) * (2 * Math.PI) / 0.3) + 1;
}
static backOut(t) {
const s = 1.70158;
return --t * t * ((s + 1) * t + s) + 1;
}
}