Skip to content

Commit 5ce7289

Browse files
authored
update spring.js
1 parent d1f1ae0 commit 5ce7289

File tree

1 file changed

+47
-13
lines changed

1 file changed

+47
-13
lines changed

packages/svelte/src/motion/spring.js

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,17 @@ function tick_spring(ctx, last_value, current_value, target_value) {
2828
const damper = ctx.opts.damping * velocity;
2929
const acceleration = (spring - damper) * ctx.inv_mass;
3030
const d = (velocity + acceleration) * ctx.dt;
31-
if (Math.abs(d) < ctx.opts.precision && Math.abs(delta) < ctx.opts.precision) {
31+
if (
32+
Math.abs(d) < ctx.opts.precision &&
33+
Math.abs(delta) < ctx.opts.precision
34+
) {
3235
return target_value; // settled
3336
} else {
3437
ctx.settled = false; // signal loop to keep ticking
3538
// @ts-ignore
36-
return is_date(current_value) ? new Date(current_value.getTime() + d) : current_value + d;
39+
return is_date(current_value)
40+
? new Date(current_value.getTime() + d)
41+
: current_value + d;
3742
}
3843
} else if (Array.isArray(current_value)) {
3944
// @ts-ignore
@@ -45,7 +50,12 @@ function tick_spring(ctx, last_value, current_value, target_value) {
4550
const next_value = {};
4651
for (const k in current_value) {
4752
// @ts-ignore
48-
next_value[k] = tick_spring(ctx, last_value[k], current_value[k], target_value[k]);
53+
next_value[k] = tick_spring(
54+
ctx,
55+
last_value[k],
56+
current_value[k],
57+
target_value[k]
58+
);
4959
}
5060
// @ts-ignore
5161
return next_value;
@@ -87,7 +97,11 @@ export function spring(value, opts = {}) {
8797
function set(new_value, opts = {}) {
8898
target_value = new_value;
8999
const token = (current_token = {});
90-
if (value == null || opts.hard || (spring.stiffness >= 1 && spring.damping >= 1)) {
100+
if (
101+
value == null ||
102+
opts.hard ||
103+
(spring.stiffness >= 1 && spring.damping >= 1)
104+
) {
91105
cancel_task = true; // cancel any running animation
92106
last_time = raf.now();
93107
last_value = new_value;
@@ -113,11 +127,16 @@ export function spring(value, opts = {}) {
113127
inv_mass,
114128
opts: spring,
115129
settled: true,
116-
//@ts-ignore
130+
//@ts-ignore
117131
dt: ((now - last_time) * 60) / 1000
118132
};
119133
// @ts-ignore
120-
const next_value = tick_spring(ctx, last_value, value, target_value);
134+
const next_value = tick_spring(
135+
ctx,
136+
last_value,
137+
value,
138+
target_value
139+
);
121140
last_time = now;
122141
last_value = /** @type {T} */ (value);
123142
store.set((value = /** @type {T} */ (next_value)));
@@ -137,7 +156,11 @@ export function spring(value, opts = {}) {
137156
// @ts-expect-error - class-only properties are missing
138157
const spring = {
139158
set,
140-
update: (fn, opts) => set(fn(/** @type {T} */ (target_value), /** @type {T} */ (value)), opts),
159+
update: (fn, opts) =>
160+
set(
161+
fn(/** @type {T} */ (target_value), /** @type {T} */ (value)),
162+
opts
163+
),
141164
subscribe: store.subscribe,
142165
stiffness,
143166
damping,
@@ -190,9 +213,12 @@ export class Spring {
190213
constructor(value, options = {}) {
191214
this.#current.v = this.#target.v = value;
192215

193-
if (typeof options.stiffness === 'number') this.#stiffness.v = clamp(options.stiffness, 0, 1);
194-
if (typeof options.damping === 'number') this.#damping.v = clamp(options.damping, 0, 1);
195-
if (typeof options.precision === 'number') this.#precision.v = options.precision;
216+
if (typeof options.stiffness === 'number')
217+
this.#stiffness.v = clamp(options.stiffness, 0, 1);
218+
if (typeof options.damping === 'number')
219+
this.#damping.v = clamp(options.damping, 0, 1);
220+
if (typeof options.precision === 'number')
221+
this.#precision.v = options.precision;
196222
}
197223

198224
/**
@@ -235,7 +261,10 @@ export class Spring {
235261
var inv_mass_recovery_rate = 1000 / (this.#momentum * 60);
236262

237263
this.#task ??= loop((now) => {
238-
this.#inverse_mass = Math.min(this.#inverse_mass + inv_mass_recovery_rate, 1);
264+
this.#inverse_mass = Math.min(
265+
this.#inverse_mass + inv_mass_recovery_rate,
266+
1
267+
);
239268

240269
/** @type {import('./private').TickContext} */
241270
const ctx = {
@@ -249,7 +278,12 @@ export class Spring {
249278
dt: ((now - this.#last_time) * 60) / 1000
250279
};
251280

252-
var next = tick_spring(ctx, this.#last_value, this.#current.v, this.#target.v);
281+
var next = tick_spring(
282+
ctx,
283+
this.#last_value,
284+
this.#current.v,
285+
this.#target.v
286+
);
253287
this.#last_value = this.#current.v;
254288
this.#last_time = now;
255289
set(this.#current, next);
@@ -336,7 +370,7 @@ export class Spring {
336370
}
337371

338372
set target(v) {
339-
//@ts-ignore
373+
//@ts-ignore
340374
this.set(v);
341375
}
342376
}

0 commit comments

Comments
 (0)