|
|
|
@ -4,8 +4,7 @@ import { linear } from '../easing';
|
|
|
|
import { is_date } from './utils';
|
|
|
|
import { is_date } from './utils';
|
|
|
|
/** @returns {(t: any) => any} */
|
|
|
|
/** @returns {(t: any) => any} */
|
|
|
|
function get_interpolator(a, b) {
|
|
|
|
function get_interpolator(a, b) {
|
|
|
|
if (a === b || a !== a)
|
|
|
|
if (a === b || a !== a) return () => a;
|
|
|
|
return () => a;
|
|
|
|
|
|
|
|
const type = typeof a;
|
|
|
|
const type = typeof a;
|
|
|
|
if (type !== typeof b || Array.isArray(a) !== Array.isArray(b)) {
|
|
|
|
if (type !== typeof b || Array.isArray(a) !== Array.isArray(b)) {
|
|
|
|
throw new Error('Cannot interpolate values of different type');
|
|
|
|
throw new Error('Cannot interpolate values of different type');
|
|
|
|
@ -17,8 +16,7 @@ function get_interpolator(a, b) {
|
|
|
|
return (t) => arr.map((fn) => fn(t));
|
|
|
|
return (t) => arr.map((fn) => fn(t));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (type === 'object') {
|
|
|
|
if (type === 'object') {
|
|
|
|
if (!a || !b)
|
|
|
|
if (!a || !b) throw new Error('Object cannot be null');
|
|
|
|
throw new Error('Object cannot be null');
|
|
|
|
|
|
|
|
if (is_date(a) && is_date(b)) {
|
|
|
|
if (is_date(a) && is_date(b)) {
|
|
|
|
a = a.getTime();
|
|
|
|
a = a.getTime();
|
|
|
|
b = b.getTime();
|
|
|
|
b = b.getTime();
|
|
|
|
@ -65,7 +63,12 @@ export function tweened(value, defaults = {}) {
|
|
|
|
target_value = new_value;
|
|
|
|
target_value = new_value;
|
|
|
|
let previous_task = task;
|
|
|
|
let previous_task = task;
|
|
|
|
let started = false;
|
|
|
|
let started = false;
|
|
|
|
let { delay = 0, duration = 400, easing = linear, interpolate = get_interpolator } = assign(assign({}, defaults), opts);
|
|
|
|
let {
|
|
|
|
|
|
|
|
delay = 0,
|
|
|
|
|
|
|
|
duration = 400,
|
|
|
|
|
|
|
|
easing = linear,
|
|
|
|
|
|
|
|
interpolate = get_interpolator
|
|
|
|
|
|
|
|
} = assign(assign({}, defaults), opts);
|
|
|
|
if (duration === 0) {
|
|
|
|
if (duration === 0) {
|
|
|
|
if (previous_task) {
|
|
|
|
if (previous_task) {
|
|
|
|
previous_task.abort();
|
|
|
|
previous_task.abort();
|
|
|
|
@ -77,12 +80,10 @@ export function tweened(value, defaults = {}) {
|
|
|
|
const start = now() + delay;
|
|
|
|
const start = now() + delay;
|
|
|
|
let fn;
|
|
|
|
let fn;
|
|
|
|
task = loop((now) => {
|
|
|
|
task = loop((now) => {
|
|
|
|
if (now < start)
|
|
|
|
if (now < start) return true;
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if (!started) {
|
|
|
|
if (!started) {
|
|
|
|
fn = interpolate(value, new_value);
|
|
|
|
fn = interpolate(value, new_value);
|
|
|
|
if (typeof duration === 'function')
|
|
|
|
if (typeof duration === 'function') duration = duration(value, new_value);
|
|
|
|
duration = duration(value, new_value);
|
|
|
|
|
|
|
|
started = true;
|
|
|
|
started = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (previous_task) {
|
|
|
|
if (previous_task) {
|
|
|
|
@ -107,7 +108,6 @@ export function tweened(value, defaults = {}) {
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @typedef {(target_value: T, value: T) => T} Updater
|
|
|
|
* @typedef {(target_value: T, value: T) => T} Updater
|
|
|
|
* @template T
|
|
|
|
* @template T
|
|
|
|
|