UNPKG

one

Version:

One is a React framework for web, iOS, and Android apps.

49 lines (47 loc) • 1.58 kB
const SAMPLE_N = 32; const PRESETS = { smooth: (t) => (1 - t) ** 3, sharp: (t) => (1 - t) ** 5, gentle: (t) => (1 - t) ** 2, soft: (t) => Math.cos(t * Math.PI / 2), smoother: (t) => 1 - t * t * t * (t * (t * 6 - 15) + 10), linear: (t) => 1 - t }; function cubicBezierEval(x1, y1, x2, y2, x) { if (x <= 0) return 0; if (x >= 1) return 1; const cx = 3 * x1; const bx = 3 * (x2 - x1) - cx; const ax = 1 - cx - bx; const cy = 3 * y1; const by = 3 * (y2 - y1) - cy; const ay = 1 - cy - by; let t = x; for (let i = 0; i < 8; i++) { const bxt = ((ax * t + bx) * t + cx) * t; const dbxt = (3 * ax * t + 2 * bx) * t + cx; if (Math.abs(dbxt) < 1e-6) break; t = Math.max(0, Math.min(1, t - (bxt - x) / dbxt)); } return ((ay * t + by) * t + cy) * t; } function sampleCubicBezier(curve) { const { x1, y1, x2, y2 } = curve; return Array.from({ length: SAMPLE_N }, (_, i) => Number.parseFloat((1 - cubicBezierEval(x1, y1, x2, y2, i / (SAMPLE_N - 1))).toFixed(4))); } function normalizeStops(curve) { return curve.values.map((v) => Math.max(0, Math.min(1, v))); } function sampleCurve(curve) { if (typeof curve === "string") { const fn = PRESETS[curve] ?? PRESETS.smooth; return Array.from({ length: SAMPLE_N }, (_, i) => Number.parseFloat(fn(i / (SAMPLE_N - 1)).toFixed(4))); } return curve.type === "cubicBezier" ? sampleCubicBezier(curve) : normalizeStops(curve); } function serializeCurve(curve) { if (typeof curve === "string") return curve; return sampleCurve(curve).join(","); } export { sampleCurve, serializeCurve }; //# sourceMappingURL=curves.mjs.map