Laptop-Thing/web/node_modules/.vite/deps/svelte_motion.js
2024-11-27 12:35:48 -05:00

262 lines
6.7 KiB
JavaScript

import {
writable
} from "./chunk-XWMO4EC7.js";
import {
linear
} from "./chunk-YERFD2CZ.js";
import {
loop,
raf
} from "./chunk-XI64PIGS.js";
import "./chunk-FZEWDNFU.js";
import "./chunk-OTO7APUV.js";
import "./chunk-EXIRPMAV.js";
import "./chunk-UGBVNEQM.js";
// node_modules/svelte/src/motion/utils.js
function is_date(obj) {
return Object.prototype.toString.call(obj) === "[object Date]";
}
// node_modules/svelte/src/motion/spring.js
function tick_spring(ctx, last_value, current_value, target_value) {
if (typeof current_value === "number" || is_date(current_value)) {
const delta = target_value - current_value;
const velocity = (current_value - last_value) / (ctx.dt || 1 / 60);
const spring2 = ctx.opts.stiffness * delta;
const damper = ctx.opts.damping * velocity;
const acceleration = (spring2 - damper) * ctx.inv_mass;
const d = (velocity + acceleration) * ctx.dt;
if (Math.abs(d) < ctx.opts.precision && Math.abs(delta) < ctx.opts.precision) {
return target_value;
} else {
ctx.settled = false;
return is_date(current_value) ? new Date(current_value.getTime() + d) : current_value + d;
}
} else if (Array.isArray(current_value)) {
return current_value.map(
(_, i) => (
// @ts-ignore
tick_spring(ctx, last_value[i], current_value[i], target_value[i])
)
);
} else if (typeof current_value === "object") {
const next_value = {};
for (const k in current_value) {
next_value[k] = tick_spring(ctx, last_value[k], current_value[k], target_value[k]);
}
return next_value;
} else {
throw new Error(`Cannot spring ${typeof current_value} values`);
}
}
function spring(value, opts = {}) {
const store = writable(value);
const { stiffness = 0.15, damping = 0.8, precision = 0.01 } = opts;
let last_time;
let task;
let current_token;
let last_value = (
/** @type {T} */
value
);
let target_value = (
/** @type {T | undefined} */
value
);
let inv_mass = 1;
let inv_mass_recovery_rate = 0;
let cancel_task = false;
function set(new_value, opts2 = {}) {
target_value = new_value;
const token = current_token = {};
if (value == null || opts2.hard || spring2.stiffness >= 1 && spring2.damping >= 1) {
cancel_task = true;
last_time = raf.now();
last_value = new_value;
store.set(value = target_value);
return Promise.resolve();
} else if (opts2.soft) {
const rate = opts2.soft === true ? 0.5 : +opts2.soft;
inv_mass_recovery_rate = 1 / (rate * 60);
inv_mass = 0;
}
if (!task) {
last_time = raf.now();
cancel_task = false;
task = loop((now) => {
if (cancel_task) {
cancel_task = false;
task = null;
return false;
}
inv_mass = Math.min(inv_mass + inv_mass_recovery_rate, 1);
const ctx = {
inv_mass,
opts: spring2,
settled: true,
dt: (now - last_time) * 60 / 1e3
};
const next_value = tick_spring(ctx, last_value, value, target_value);
last_time = now;
last_value = /** @type {T} */
value;
store.set(value = /** @type {T} */
next_value);
if (ctx.settled) {
task = null;
}
return !ctx.settled;
});
}
return new Promise((fulfil) => {
task.promise.then(() => {
if (token === current_token) fulfil();
});
});
}
const spring2 = {
set,
update: (fn, opts2) => set(fn(
/** @type {T} */
target_value,
/** @type {T} */
value
), opts2),
subscribe: store.subscribe,
stiffness,
damping,
precision
};
return spring2;
}
// node_modules/svelte/src/motion/tweened.js
function get_interpolator(a, b) {
if (a === b || a !== a) return () => a;
const type = typeof a;
if (type !== typeof b || Array.isArray(a) !== Array.isArray(b)) {
throw new Error("Cannot interpolate values of different type");
}
if (Array.isArray(a)) {
const arr = (
/** @type {Array<any>} */
b.map((bi, i) => {
return get_interpolator(
/** @type {Array<any>} */
a[i],
bi
);
})
);
return (t) => arr.map((fn) => fn(t));
}
if (type === "object") {
if (!a || !b) {
throw new Error("Object cannot be null");
}
if (is_date(a) && is_date(b)) {
const an = a.getTime();
const bn = b.getTime();
const delta = bn - an;
return (t) => new Date(an + t * delta);
}
const keys = Object.keys(b);
const interpolators = {};
keys.forEach((key) => {
interpolators[key] = get_interpolator(a[key], b[key]);
});
return (t) => {
const result = {};
keys.forEach((key) => {
result[key] = interpolators[key](t);
});
return result;
};
}
if (type === "number") {
const delta = (
/** @type {number} */
b - /** @type {number} */
a
);
return (t) => a + t * delta;
}
throw new Error(`Cannot interpolate ${type} values`);
}
function tweened(value, defaults = {}) {
const store = writable(value);
let task;
let target_value = value;
function set(new_value, opts) {
target_value = new_value;
if (value == null) {
store.set(value = new_value);
return Promise.resolve();
}
let previous_task = task;
let started = false;
let {
delay = 0,
duration = 400,
easing = linear,
interpolate = get_interpolator
} = { ...defaults, ...opts };
if (duration === 0) {
if (previous_task) {
previous_task.abort();
previous_task = null;
}
store.set(value = target_value);
return Promise.resolve();
}
const start = raf.now() + delay;
let fn;
task = loop((now) => {
if (now < start) return true;
if (!started) {
fn = interpolate(
/** @type {any} */
value,
new_value
);
if (typeof duration === "function")
duration = duration(
/** @type {any} */
value,
new_value
);
started = true;
}
if (previous_task) {
previous_task.abort();
previous_task = null;
}
const elapsed = now - start;
if (elapsed > /** @type {number} */
duration) {
store.set(value = new_value);
return false;
}
store.set(value = fn(easing(elapsed / duration)));
return true;
});
return task.promise;
}
return {
set,
update: (fn, opts) => set(fn(
/** @type {any} */
target_value,
/** @type {any} */
value
), opts),
subscribe: store.subscribe
};
}
export {
spring,
tweened
};
//# sourceMappingURL=svelte_motion.js.map