UNPKG

one

Version:

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

93 lines (91 loc) • 2.78 kB
"use strict"; 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); var SAMPLE_N = 32; var PRESETS = { smooth: function smooth(t) { return Math.pow(1 - t, 3); }, sharp: function sharp(t) { return Math.pow(1 - t, 5); }, gentle: function gentle(t) { return Math.pow(1 - t, 2); }, soft: function soft(t) { return Math.cos(t * Math.PI / 2); }, smoother: function smoother(t) { return 1 - t * t * t * (t * (t * 6 - 15) + 10); }, linear: function linear(t) { return 1 - t; } }; function cubicBezierEval(x1, y1, x2, y2, x) { if (x <= 0) return 0; if (x >= 1) return 1; var cx = 3 * x1; var bx = 3 * (x2 - x1) - cx; var ax = 1 - cx - bx; var cy = 3 * y1; var by = 3 * (y2 - y1) - cy; var ay = 1 - cy - by; var t = x; for (var i = 0; i < 8; i++) { var bxt = ((ax * t + bx) * t + cx) * t; var 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) { var { x1, y1, x2, y2 } = curve; return Array.from({ length: SAMPLE_N }, function(_, i) { return Number.parseFloat((1 - cubicBezierEval(x1, y1, x2, y2, i / (SAMPLE_N - 1))).toFixed(4)); }); } function normalizeStops(curve) { return curve.values.map(function(v) { return Math.max(0, Math.min(1, v)); }); } function sampleCurve(curve) { if (typeof curve === "string") { var _PRESETS_curve; var fn = (_PRESETS_curve = PRESETS[curve]) !== null && _PRESETS_curve !== void 0 ? _PRESETS_curve : PRESETS.smooth; return Array.from({ length: SAMPLE_N }, function(_, i) { return 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(","); }