blob: a03d3cfcbc7304521616ad8729759c1e92830675 [file] [log] [blame]
<!--
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-->
<script>
function evaluateCubic(a, b, m) {
// TODO(abarth): Would Math.pow be faster?
return 3 * a * (1 - m) * (1 - m) * m + 3 * b * (1 - m) * m * m + m * m * m
}
const kCubicErrorBound = 0.001;
class Linear {
transform(t) {
return t;
}
};
class Cubic {
constructor(a, b, c, d) {
this.a_ = a;
this.b_ = b;
this.c_ = c;
this.d_ = d;
Object.preventExtensions(this);
}
transform(t) {
var start = 0, end = 1;
while (1) {
var midpoint = (start + end) / 2;
var estimate = evaluateCubic(this.a_, this.c_, midpoint);
if (Math.abs(t - estimate) < kCubicErrorBound)
return evaluateCubic(this.b_, this.d_, midpoint);
if (estimate < t)
start = midpoint;
else
end = midpoint;
}
}
}
module.exports = {
Linear: Linear,
Cubic: Cubic,
linear: new Linear(),
ease: new Cubic(0.25, 0.1, 0.25, 1),
easeIn: new Cubic(0.42, 0, 1, 1),
easeOut: new Cubic(0, 0, 0.58, 1),
easeInOut: new Cubic(0.42, 0, 0.58, 1),
};
</script>