From 9c02cc32022fa90b676d00626458504f85ef8fe8 Mon Sep 17 00:00:00 2001 From: pushkine Date: Tue, 19 May 2020 19:25:42 +0200 Subject: [PATCH] fix bezier + add string interpolation --- src/runtime/easing/index.ts | 13 ++++++------ src/runtime/interpolate/index.ts | 36 ++++++++++++++++++++++++++++++++ src/runtime/motion/index.ts | 2 +- 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/runtime/easing/index.ts b/src/runtime/easing/index.ts index 612c8ab082..99fb1b0d8f 100644 --- a/src/runtime/easing/index.ts +++ b/src/runtime/easing/index.ts @@ -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); diff --git a/src/runtime/interpolate/index.ts b/src/runtime/interpolate/index.ts index 668c88d99a..37cf3df5e6 100644 --- a/src/runtime/interpolate/index.ts +++ b/src/runtime/interpolate/index.ts @@ -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(''); + }; +} diff --git a/src/runtime/motion/index.ts b/src/runtime/motion/index.ts index c3dba69ce2..8f8a8a30f9 100644 --- a/src/runtime/motion/index.ts +++ b/src/runtime/motion/index.ts @@ -103,7 +103,7 @@ export function tweened( }); 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);