forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurve.js
314 lines (282 loc) · 8.27 KB
/
curve.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/**
* Copyright 2015 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Imported just for the side effect of getting the `types` it exports into
// the type system during compile time.
import './time';
/**
* A CurveDef is a function that returns a normtime value (0 to 1) for another
* normtime value.
* @typedef {function(./time.normtimeDef): ./time.normtimeDef}
*/
export let CurveDef;
/**
* Returns a cubic bezier curve.
* @param {number} x1 X coordinate of the first control point.
* @param {number} y1 Y coordinate of the first control point.
* @param {number} x2 X coordinate of the second control point.
* @param {number} y2 Y coordinate of the second control point.
* @return {!CurveDef}
*/
export function bezierCurve(x1, y1, x2, y2) {
const bezier = new Bezier(0, 0, x1, y1, x2, y2, 1, 1);
return bezier.solveYValueFromXValue.bind(bezier);
}
/**
* Thanks to
* https://closure-library.googlecode.com/git-history/docs/local_closure_goog_math_bezier.js.source.html
*/
class Bezier {
/**
* @param {number} x0 X coordinate of the start point.
* @param {number} y0 Y coordinate of the start point.
* @param {number} x1 X coordinate of the first control point.
* @param {number} y1 Y coordinate of the first control point.
* @param {number} x2 X coordinate of the second control point.
* @param {number} y2 Y coordinate of the second control point.
* @param {number} x3 X coordinate of the end point.
* @param {number} y3 Y coordinate of the end point.
*/
constructor(x0, y0, x1, y1, x2, y2, x3, y3) {
/**
* X coordinate of the first point.
* @type {number}
*/
this.x0 = x0;
/**
* Y coordinate of the first point.
* @type {number}
*/
this.y0 = y0;
/**
* X coordinate of the first control point.
* @type {number}
*/
this.x1 = x1;
/**
* Y coordinate of the first control point.
* @type {number}
*/
this.y1 = y1;
/**
* X coordinate of the second control point.
* @type {number}
*/
this.x2 = x2;
/**
* Y coordinate of the second control point.
* @type {number}
*/
this.y2 = y2;
/**
* X coordinate of the end point.
* @type {number}
*/
this.x3 = x3;
/**
* Y coordinate of the end point.
* @type {number}
*/
this.y3 = y3;
}
/**
* Computes the y coordinate of a point on the curve given its x coordinate.
* @param {number} xVal The x coordinate of the point on the curve.
* @return {number} The y coordinate of the point on the curve.
*/
solveYValueFromXValue(xVal) {
return this.getPointY(this.solvePositionFromXValue(xVal));
}
/**
* Computes the position t of a point on the curve given its x coordinate.
* That is, for an input xVal, finds t s.t. getPointX(t) = xVal.
* As such, the following should always be true up to some small epsilon:
* t ~ solvePositionFromXValue(getPointX(t)) for t in [0, 1].
* @param {number} xVal The x coordinate of the point to find on the curve.
* @return {number} The position t.
*/
solvePositionFromXValue(xVal) {
// Desired precision on the computation.
const epsilon = 1e-6;
// Initial estimate of t using linear interpolation.
let t = (xVal - this.x0) / (this.x3 - this.x0);
if (t <= 0) {
return 0;
} else if (t >= 1) {
return 1;
}
// Try gradient descent to solve for t. If it works, it is very fast.
let tMin = 0;
let tMax = 1;
let value = 0;
for (let i = 0; i < 8; i++) {
value = this.getPointX(t);
const derivative = (this.getPointX(t + epsilon) - value) / epsilon;
if (Math.abs(value - xVal) < epsilon) {
return t;
} else if (Math.abs(derivative) < epsilon) {
break;
} else {
if (value < xVal) {
tMin = t;
} else {
tMax = t;
}
t -= (value - xVal) / derivative;
}
}
// If the gradient descent got stuck in a local minimum, e.g. because
// the derivative was close to 0, use a Dichotomy refinement instead.
// We limit the number of iterations to 8.
for (let i = 0; Math.abs(value - xVal) > epsilon && i < 8; i++) {
if (value < xVal) {
tMin = t;
t = (t + tMax) / 2;
} else {
tMax = t;
t = (t + tMin) / 2;
}
value = this.getPointX(t);
}
return t;
}
/**
* Computes the curve's X coordinate at a point between 0 and 1.
* @param {number} t The point on the curve to find.
* @return {number} The computed coordinate.
*/
getPointX(t) {
// Special case start and end.
if (t == 0) {
return this.x0;
} else if (t == 1) {
return this.x3;
}
// Step one - from 4 points to 3
let ix0 = this.lerp(this.x0, this.x1, t);
let ix1 = this.lerp(this.x1, this.x2, t);
const ix2 = this.lerp(this.x2, this.x3, t);
// Step two - from 3 points to 2
ix0 = this.lerp(ix0, ix1, t);
ix1 = this.lerp(ix1, ix2, t);
// Final step - last point
return this.lerp(ix0, ix1, t);
}
/**
* Computes the curve's Y coordinate at a point between 0 and 1.
* @param {number} t The point on the curve to find.
* @return {number} The computed coordinate.
*/
getPointY(t) {
// Special case start and end.
if (t == 0) {
return this.y0;
} else if (t == 1) {
return this.y3;
}
// Step one - from 4 points to 3
let iy0 = this.lerp(this.y0, this.y1, t);
let iy1 = this.lerp(this.y1, this.y2, t);
const iy2 = this.lerp(this.y2, this.y3, t);
// Step two - from 3 points to 2
iy0 = this.lerp(iy0, iy1, t);
iy1 = this.lerp(iy1, iy2, t);
// Final step - last point
return this.lerp(iy0, iy1, t);
}
/**
* Performs linear interpolation between values a and b. Returns the value
* between a and b proportional to x (when x is between 0 and 1. When x is
* outside this range, the return value is a linear extrapolation).
* @param {number} a A number.
* @param {number} b A number.
* @param {number} x The proportion between a and b.
* @return {number} The interpolated value between a and b.
*/
lerp(a, b, x) {
return a + x * (b - a);
}
}
/**
* A collection of common curves.
* See https://developer.mozilla.org/en-US/docs/Web/CSS/timing-function
* @enum {!CurveDef}
*/
export const Curves = {
/**
* linear
* @param {number} n
* @return {number}
*/
LINEAR(n) {
return n;
},
/**
* ease
*/
EASE: bezierCurve(0.25, 0.1, 0.25, 1.0),
/**
* ease-in: slow out, fast in
*/
EASE_IN: bezierCurve(0.42, 0.0, 1.0, 1.0),
/**
* ease-out: fast out, slow in
*/
EASE_OUT: bezierCurve(0.0, 0.0, 0.58, 1.0),
/**
* ease-in-out
*/
EASE_IN_OUT: bezierCurve(0.42, 0.0, 0.58, 1.0),
};
/**
* @const {!Object<string, !CurveDef>}
*/
const NAME_MAP = {
'linear': Curves.LINEAR,
'ease': Curves.EASE,
'ease-in': Curves.EASE_IN,
'ease-out': Curves.EASE_OUT,
'ease-in-out': Curves.EASE_IN_OUT,
};
/**
* If the argument is a string, this methods matches an existing curve by name.
* @param {?CurveDef|string|undefined} curve
* @return {?CurveDef}
*/
export function getCurve(curve) {
if (!curve) {
return null;
}
if (typeof curve == 'string') {
// If the curve is a custom cubic-bezier curve
if (curve.indexOf('cubic-bezier') != -1) {
const match = curve.match(/cubic-bezier\((.+)\)/);
if (match) {
const values = match[1].split(',').map(parseFloat);
if (values.length == 4) {
for (let i = 0; i < 4; i++) {
if (isNaN(values[i])) {
return null;
}
}
return bezierCurve(values[0], values[1], values[2], values[3]);
}
}
return null;
}
return NAME_MAP[curve];
}
return curve;
}