-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
272 lines (272 loc) · 7.97 KB
/
index.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
* Helper to prevent x➗0 (example)
*/
const epsilon = Number.EPSILON;
/**
* Maps bipolar numbers [-1, 1] to the unipolar closed unit interval [0, 1]
*/
const biToUni = (v) => v / 2 + 0.5;
/**
* Maps unipolar numbers [0, 1] to the bipolar closed interval [-1, 1]
*/
const uniToBi = (v) => v * 2 - 1;
/**
* Clamps overflowing numbers within the closed interval [min, max]
*/
const clamp = (x, min, max) => {
if (x < min) {
return min;
}
if (x > max) {
return max;
}
return x;
};
/**
* Linearly interpolates `input` in [0,1] between `a` and `b`
*/
const lerp = (input, a, b) => a + (b - a) * input;
/**
* Classic tanh function with a simple drive parameter
*/
const tanh = (x, gain = 1) => {
const y = Math.tanh(x * gain);
return y;
};
/**
* @see http://www.flong.com/archive/texts/code/shapers_poly/index.html
*/
const quadraticThroughAGivenPoint = (input, x, y, clamped = false) => {
const min_param_a = 0.0 + epsilon;
const max_param_a = 1.0 - epsilon;
const min_param_b = 0.0;
const max_param_b = 1.0;
x = Math.min(max_param_a, Math.max(min_param_a, x));
y = Math.min(max_param_b, Math.max(min_param_b, y));
const A = (1 - y) / (1 - x) - y / x;
const B = (A * (x * x) - y) / x;
let output = A * (input * input) - B * input;
output = clamped ? Math.min(1, Math.max(0, output)) : output;
return output;
};
/**
* Parametric easing function
*/
const ease = (input,
/**
* This determines the strength of the curve from 0-index
* (+1 is added to the value to derive the order, allowing for smooth through-0 transitions)
* - 0 = linear
* - 1 = quadratic
* - 2 = cubic
* - 3 = quartic
* - 4 = quintic
* Positive values bias towards ease-out, negative values bias towards ease-in
*/
order = 2) => {
const positiveBias = order >= 0;
order = (Math.abs(order) + 1);
input = positiveBias ? 1 - input : input;
const offset = positiveBias ? 1 : 0;
return offset - (positiveBias ? 1 : -1) * Math.pow(input, order);
};
/**
* @see http://www.flong.com/archive/texts/code/shapers_bez/index.html
*/
const quadraticBezier = (input, x, y) => {
// adapted from BEZMATH.PS (1993)
// by Don Lancaster, SYNERGETICS Inc.
// http://www.tinaja.com/text/bezmath.html
let a = { x, y };
a.x = clamp(a.x, 0.0, 1.0);
a.y = clamp(a.y, 0.0, 1.0);
if (a.x === 0.5) {
a.x = a.x + epsilon;
a.y = a.y + epsilon;
}
// solve t from x (an inverse operation)
let om2a = 1.0 - 2.0 * a.x;
let t = (Math.sqrt(a.x * a.x + om2a * input) - a.x) / om2a;
let output = (1.0 - 2.0 * a.y) * (t * t) + 2.0 * a.y * t;
return output;
};
/**
* Implements quadratic bezier curve with a simplified `bias` argument that
* weights values towards 0 or 1.
*/
const quadraticSlope = (input, bias) => {
return quadraticBezier(input, 1 - bias, bias);
};
/**
* @see http://www.flong.com/archive/texts/code/shapers_exp/
*/
const doubleExponentialSigmoid = (x, a) => {
const min_param_a = 0.0 + epsilon;
const max_param_a = 1.0 - epsilon;
a = Math.min(max_param_a, Math.max(min_param_a, a));
a = 1.0 - a; // for sensible results
let y = 0;
if (x <= 0.5) {
y = Math.pow(2.0 * x, 1.0 / a) / 2.0;
}
else {
y = 1.0 - Math.pow(2.0 * (1.0 - x), 1.0 / a) / 2.0;
}
return y;
};
/**
* @see http://www.flong.com/archive/texts/code/shapers_exp/index.html
*/
const doubleExponentialSeat = (input, a) => {
const min_param_a = 0.0 + epsilon;
const max_param_a = 1.0 - epsilon;
a = Math.min(max_param_a, Math.max(min_param_a, a));
let y = 0;
if (input <= 0.5) {
y = Math.pow(2.0 * input, 1 - a) / 2.0;
}
else {
y = 1.0 - Math.pow(2.0 * (1.0 - input), 1 - a) / 2.0;
}
return y;
};
/**
* Snaps values to nearest multiple of `step`
*/
const quantize = (input, step,
/** rounding algorithm used. `floor` or `ceil` may be more useful for data in closed unit intervals */
algorithm = "round") => {
step = Math.max(epsilon, step);
return Math[algorithm](input / step) * step;
};
const fold = (input, gain) => {
input *= gain + 1;
input = 0.25 * input;
return biToUni(4 * (Math.abs(input - Math.round(input)) - 0.25));
};
const sineFold = (input, gain) => {
input *= gain + 1;
return biToUni(Math.sin((Math.PI * input) / 2 - Math.PI / 2));
};
/**
* Calculates a circular arc through the source points with a variable radius.
* Mixes a couple of implementations, one using θ, the other using y
* @see https://math.stackexchange.com/questions/1779414/2d-parametric-equation-for-an-arc-between-two-points-with-a-start-angle
* @see https://math.stackexchange.com/questions/3286848/equation-of-an-arbitrary-circular-arc
*/
const circularArc = (input,
/** range from 0..1 */
bias) => {
const x0 = 0, y0 = 0, x1 = 1, y1 = 1;
// avoiding clipping around extremes
if (input === 0)
return 0;
if (input === 1)
return 1;
// outputs bias from [0,-0.5]
const computedBias = (bias * -1) / 2;
if (bias === 0.5) {
return input;
}
const a0 = computedBias * Math.PI;
const r = (Math.pow(x0 - x1 + epsilon, 2) + Math.pow(y0 - y1 + epsilon, 2)) /
(2 * (x0 - x1) * Math.cos(a0) + 2 * (y0 - y1) * Math.sin(a0));
// const xc = x0 - r * Math.cos(a0)
const yc = y0 - r * Math.sin(a0);
const theta = ((Math.acos((input - yc) / r) + Math.PI * 2) % Math.PI) + Math.PI;
return 1 - (yc + r * Math.sin(theta));
};
/**
* An automatic bi version of the logistic sigmoid function
*/
const logistic = (input, gain) => {
return uniToBi(1 / (1 + Math.exp(-gain * input)));
};
/**
*
* @see https://en.wikipedia.org/wiki/Smoothstep#cite_note-5
*/
const smoothStep = (input, edge0, edge1) => {
if (input < edge0)
return 0;
if (input >= edge1)
return 1;
// Scale/bias into [0..1] range
input = (input - edge0) / (edge1 - edge0);
return input * input * (3 - 2 * input);
};
/**
* Hard angle version of smoothStep
*/
const linearStep = (input, x, y) => {
return clamp((input - x) / (y - x), 0, 1);
};
/**
* 3-point polyline
*/
const polyline = (input, midpointX, midpointY) => {
const slopeA = (midpointY - 0) / (midpointX - 0);
const slopeB = (1 - midpointY) / (1 - midpointX);
return input < midpointX
? slopeA * input
: slopeB * input - slopeB * midpointX + midpointY;
};
/**
* Calls function `fn` while producing an output with Y symmetry around 0.
*/
const mirrorAcrossY = (
/** input */
input,
/** function to mirror */
fn,
/** args paszsed to the function */
...args) => fn(Math.abs(input), ...args);
/**
* Calls function `fn` while producing an output with X and Y symmetry around 0,0.
* Effectively turns any saturating function that maps in the range [0,1] into a sigmoid.
*/
const mirrorAcrossOrigin = (
/** input */
input,
/** function to mirror */
fn,
/** args paszsed to the function */
...args) => {
let absOut = fn(Math.abs(input), ...args);
return input < 0 ? absOut * -1 : absOut;
};
/**
* Calls function `fn` reflected across the point at `x`, `y`
*/
function inflectionThroughPoint(input, x, y, fn, ...args) {
if (input <= x) {
return fn(input / (x + epsilon), ...args) * y;
}
else {
return (1 - fn(1 - (input - x) / (1 - x + epsilon), ...args)) * (1 - y) + y;
// input scaled to 0 1
}
}
const functions = {
biToUni,
uniToBi,
clamp,
lerp,
tanh,
quadraticThroughAGivenPoint,
quadraticBezier,
quadraticSlope,
doubleExponentialSigmoid,
doubleExponentialSeat,
quantize,
fold,
sineFold,
circularArc,
logistic,
smoothStep,
linearStep,
polyline,
mirrorAcrossY,
mirrorAcrossOrigin,
inflectionThroughPoint
};export{biToUni,circularArc,clamp,functions as default,doubleExponentialSeat,doubleExponentialSigmoid,ease,fold,inflectionThroughPoint,lerp,linearStep,logistic,mirrorAcrossOrigin,mirrorAcrossY,polyline,quadraticBezier,quadraticSlope,quadraticThroughAGivenPoint,quantize,sineFold,smoothStep,tanh,uniToBi};