fix bezier + add string interpolation

pull/4742/head
pushkine 5 years ago
parent 0587fe4089
commit 9c02cc3202

@ -47,11 +47,10 @@ export const circOut = (t: number) => Math.sin(Math.acos(1.0 - t));
export const circInOut = (t: number) =>
0.5 * (t >= 0.5 ? 2.0 - Math.sin(Math.acos(1.0 - 2.0 * (1.0 - t))) : Math.sin(Math.acos(1.0 - 2.0 * t)));
export const cubicBezier = (x1: number, y1: number, x2: number, y2: number) => {
if (!(x1 >= 0 && x1 <= 1 && x2 >= 0 && x2 <= 1)) {
if (!(x1 >= 0 && x1 <= 1 && x2 >= 0 && x2 <= 1))
throw new Error(`CubicBezier x1 & x2 values must be { 0 < x < 1 }, got { x1 : ${x1}, x2: ${x2} }`);
}
const ax = 1.0 - (x2 = 3.0 * (x2 - x1) - (x1 = 3.0 * x1)) - x1,
ay = 1.0 - (y2 = 3.0 * (y2 - y1) - (y1 = 3.0 * y1)) - y1;
const ax = 1.0 - (x2 = 3.0 * (x2 - x1) - (x1 *= 3.0)) - x1,
ay = 1.0 - (y2 = 3.0 * (y2 - y1) - (y1 *= 3.0)) - y1;
let i = 0,
r = 0.0,
s = 0.0,
@ -59,11 +58,11 @@ export const cubicBezier = (x1: number, y1: number, x2: number, y2: number) => {
x = 0.0;
return (t: number) => {
for (r = t, i = 0; 32 > i; i++)
if (1e-5 > Math.abs((x = r * r * (r * ax + x1 + x2) - t))) return r * (r * (r * ay + y2) + y1);
if (1e-5 > Math.abs((x = r * (r * (r * ax + x2) + x1) - t))) return r * (r * (r * ay + y2) + y1);
else if (1e-5 > Math.abs((d = r * (r * ax * 3.0 + x2 * 2.0) + x1))) break;
else r = r - x / d;
else r -= x / d;
if ((s = 0.0) > (r = t)) return 0;
else if ((d = 1.0) > r) return 1;
else if ((d = 1.0) < r) return 1;
while (d > s)
if (1e-5 > Math.abs((x = r * (r * (r * ax + x2) + x1)) - t)) break;
else t > x ? (s = r) : (d = r), (r = 0.5 * (d - s) + s);

@ -7,3 +7,39 @@ export function numbers(a, b) {
const d = (b = +b) - (a = +a);
return (t) => a + d * t;
}
export function strings(a, b) {
const re1 = /[-+]?(?:[0-9]+\.?[0-9]*|\.?[0-9]+)(?:[eE][-+]?[0-9]+)?/g,
re2 = /[-+]?(?:[0-9]+\.?[0-9]*|\.?[0-9]+)(?:[eE][-+]?[0-9]+)?/g,
strings = [],
interpolators = [];
let f,
m1 = '',
m2 = '',
mm1,
mm2,
s = -1,
n = 0,
i = 0,
l = 0;
a = a + '';
b = b + '';
while ((mm1 = re1.exec(a)) && (mm2 = re2.exec(b))) {
({ 0: m1 } = mm1), ({ 0: m2, index: i } = mm2);
if (i > l)
if (strings[s]) strings[s] += b.slice(l, i);
else strings[++s] = b.slice(l, i);
if (m2 === m1)
if (strings[s]) strings[s] += m2;
else strings[++s] = m2;
else (strings[++s] = ''), (interpolators[n++] = { s, f: numbers(m1, m2) });
({ lastIndex: l } = re2);
}
if (l < b.length)
if (strings[s]) strings[s] += b.slice(l);
else strings[++s] = b.slice(l);
return (t) => {
for (i = 0; i < n; i++) strings[(({ s, f } = interpolators[i]), s)] = f(t);
return strings.join('');
};
}

@ -103,7 +103,7 @@ export function tweened<T>(
});
const set = (next_value, params) => {
delay = (params && params.delay) || default_delay;
duration = +((params && 'duration' in params) || default_duration);
duration = (params && params.duration) || default_duration;
easing = (params && params.easing) || default_easing;
interpolate = (params && params.interpolate) || default_interpolate;
return store.set(next_value);

Loading…
Cancel
Save