From b89e611a94d34e4c43666e5301190f76e473941b Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 13 Dec 2021 00:23:17 +0000 Subject: [PATCH 1/2] Initial commit --- src/runtime/motion/spring.ts | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/runtime/motion/spring.ts b/src/runtime/motion/spring.ts index 64d17a058b26..d85bc3ae30cc 100644 --- a/src/runtime/motion/spring.ts +++ b/src/runtime/motion/spring.ts @@ -49,6 +49,8 @@ interface SpringOpts { stiffness?: number; damping?: number; precision?: number; + tick?: number; + limit?: number; } interface SpringUpdateOpts { @@ -64,11 +66,13 @@ export interface Spring extends Readable{ precision: number; damping: number; stiffness: number; + tick: number; + limit: number; } export function spring(value?: T, opts: SpringOpts = {}): Spring { const store = writable(value); - const { stiffness = 0.15, damping = 0.8, precision = 0.01 } = opts; + const { stiffness = 0.15, damping = 0.8, precision = 0.01, tick = 16, limit = 30 } = opts; let last_time: number; let task: Task; @@ -110,22 +114,31 @@ export function spring(value?: T, opts: SpringOpts = {}): Spring { inv_mass = Math.min(inv_mass + inv_mass_recovery_rate, 1); + last_time = Math.max(last_time, now - tick * limit); + while ( last_time < now ) { + const elapsed = Math.min(tick, now - last_time); + last_time += elapsed; + const ctx: TickContext = { inv_mass, opts: spring, settled: true, // tick_spring may signal false - dt: (now - last_time) * 60 / 1000 + dt: elapsed * 60 / 1000 }; const next_value = tick_spring(ctx, last_value, value, target_value); last_time = now; last_value = value; - store.set(value = next_value); + value = next_value; if (ctx.settled) { task = null; + store.set(value); + return false; + } } - return !ctx.settled; + store.set(value); + return true; }); } @@ -142,7 +155,9 @@ export function spring(value?: T, opts: SpringOpts = {}): Spring { subscribe: store.subscribe, stiffness, damping, - precision + precision, + tick, + limit }; return spring; From 8ccfa7f53226453abe2c66e8298d0de9063cd7a9 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Thu, 30 Mar 2023 17:24:14 -0400 Subject: [PATCH 2/2] Update src/runtime/motion/spring.ts Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> --- src/runtime/motion/spring.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/motion/spring.ts b/src/runtime/motion/spring.ts index d85bc3ae30cc..ce93646dcca8 100644 --- a/src/runtime/motion/spring.ts +++ b/src/runtime/motion/spring.ts @@ -115,7 +115,7 @@ export function spring(value?: T, opts: SpringOpts = {}): Spring { inv_mass = Math.min(inv_mass + inv_mass_recovery_rate, 1); last_time = Math.max(last_time, now - tick * limit); - while ( last_time < now ) { + while (last_time < now) { const elapsed = Math.min(tick, now - last_time); last_time += elapsed;