forked from sveltejs/svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspring.js
155 lines (123 loc) · 3.39 KB
/
spring.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { writable } from 'svelte/store';
import { loop } from 'svelte/internal';
import { is_date } from './utils.js';
function get_initial_velocity(value) {
if (typeof value === 'number' || is_date(value)) return 0;
if (Array.isArray(value)) return value.map(get_initial_velocity);
if (value && typeof value === 'object') {
const velocities = {};
for (const k in value) velocities[k] = get_initial_velocity(value[k]);
return velocities;
}
throw new Error(`Cannot spring ${typeof value} values`);
}
function get_threshold(value, target_value, precision) {
if (typeof value === 'number' || is_date(value)) return precision * Math.abs((target_value - value));
if (Array.isArray(value)) return value.map((v, i) => get_threshold(v, target_value[i], precision));
if (value && typeof value === 'object') {
const threshold = {};
for (const k in value) threshold[k] = get_threshold(value[k], target_value[k], precision);
return threshold;
}
throw new Error(`Cannot spring ${typeof value} values`);
}
function tick_spring(velocity, current_value, target_value, stiffness, damping, multiplier, threshold) {
let settled = true;
let value;
if (typeof current_value === 'number' || is_date(current_value)) {
const delta = target_value - current_value;
const spring = stiffness * delta;
const damper = damping * velocity;
const acceleration = spring - damper;
velocity += acceleration;
const d = velocity * multiplier;
if (is_date(current_value)) {
value = new Date(current_value.getTime() + d);
} else {
value = current_value + d;
}
if (Math.abs(d) > threshold) settled = false;
}
else if (Array.isArray(current_value)) {
value = current_value.map((v, i) => {
const result = tick_spring(
velocity[i],
v,
target_value[i],
stiffness,
damping,
multiplier,
threshold[i]
);
velocity[i] = result.velocity;
if (!result.settled) settled = false;
return result.value;
});
}
else if (typeof current_value === 'object') {
value = {};
for (const k in current_value) {
const result = tick_spring(
velocity[k],
current_value[k],
target_value[k],
stiffness,
damping,
multiplier,
threshold[k]
);
velocity[k] = result.velocity;
if (!result.settled) settled = false;
value[k] = result.value;
}
}
else {
throw new Error(`Cannot spring ${typeof value} values`);
}
return { velocity, value, settled };
}
export function spring(value, opts = {}) {
const store = writable(value);
const { stiffness = 0.15, damping = 0.8 } = opts;
const velocity = get_initial_velocity(value);
let task;
let target_value = value;
let last_time;
let settled;
let threshold;
function set(new_value) {
target_value = new_value;
threshold = get_threshold(value, target_value, 0.000001); // TODO make precision configurable?
if (!task) {
last_time = window.performance.now();
settled = false;
task = loop(now=> {
({ value, settled } = tick_spring(
velocity,
value,
target_value,
spring.stiffness,
spring.damping,
(now - last_time) * 60 / 1000,
threshold
));
last_time = now;
if (settled) {
value = target_value;
task = null;
}
store.set(value);
return !settled;
});
}
return task.promise;
}
const spring = {
set,
update: fn => set(fn(target_value, value)),
subscribe: store.subscribe,
stiffness,
damping
};
return spring;
}