one
Version:
One is a React framework for web, iOS, and Android apps.
72 lines (71 loc) • 2.46 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all) __defProp(target, name, {
get: all[name],
enumerable: true
});
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: () => from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var curves_exports = {};
__export(curves_exports, {
sampleCurve: () => sampleCurve,
serializeCurve: () => serializeCurve
});
module.exports = __toCommonJS(curves_exports);
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(",");
}