nezuhub/simple / lib /anim.ts
nezuhub's picture
download
raw
1.87 kB
export const clamp = (v: number, a = 0, b = 1) => Math.min(b, Math.max(a, v));
export const lerp = (a: number, b: number, t: number) => a + (b - a) * t;
export const fract = (v: number) => v - Math.floor(v);
/** Deterministic pseudo-random in 0..1. */
export const rnd = (seed: number, i: number) => fract(Math.abs(Math.sin(seed * 31.7 + i * 12.9898) * 43758.5453));
export const easeOut = (t: number) => 1 - Math.pow(1 - clamp(t), 3);
export const easeIn = (t: number) => Math.pow(clamp(t), 3);
export const easeInOut = (t: number) => {
const x = clamp(t);
return x < 0.5 ? 4 * x * x * x : 1 - Math.pow(-2 * x + 2, 3) / 2;
};
export const backOut = (t: number) => {
const x = clamp(t);
const c1 = 1.70158;
const c3 = c1 + 1;
return 1 + c3 * Math.pow(x - 1, 3) + c1 * Math.pow(x - 1, 2);
};
/** 0..1 progress across [from, to]. */
export const prog = (f: number, from: number, to: number) => clamp((f - from) / Math.max(1e-6, to - from));
/** Piecewise keys. */
export const kf = (f: number, keys: [number, number][], ease = easeInOut) => {
if (f <= keys[0][0]) return keys[0][1];
for (let i = 0; i < keys.length - 1; i++) {
const [f0, v0] = keys[i];
const [f1, v1] = keys[i + 1];
if (f <= f1) return lerp(v0, v1, ease((f - f0) / Math.max(1e-6, f1 - f0)));
}
return keys[keys.length - 1][1];
};
/** Reveal helper: opacity + translateY. */
export const rise = (f: number, start: number, len = 16, dist = 40) => {
const p = easeOut(prog(f, start, start + len));
return { opacity: p, transform: `translateY(${lerp(dist, 0, p)}px)` };
};
export const fmt = (n: number) => Math.round(n).toLocaleString('en-US');
/** A glowing text shadow. */
export const glow = (color: string, radius = 24, strength = 0.8) =>
`0 0 ${radius}px ${color}${Math.round(strength * 255).toString(16).padStart(2, '0')}, 0 0 ${radius * 2.4}px ${color}44`;

Xet Storage Details

Size:
1.87 kB
·
Xet hash:
f0bcaf213b7baa76216182420da5d8b7c5b210b3499ab3228c293c984a495ec1

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.