Commit 1ce3146d authored by Andrew Wilson's avatar Andrew Wilson

Merge pull request #2160 from apwilson/curves

Add flipped curved
parents 4860e63f 20341577
...@@ -16,20 +16,26 @@ const double _kCubicErrorBound = 0.001; ...@@ -16,20 +16,26 @@ const double _kCubicErrorBound = 0.001;
/// ///
/// See [Curves] for a collection of common animation curves. /// See [Curves] for a collection of common animation curves.
abstract class Curve { abstract class Curve {
const Curve();
/// Returns the value of the curve at point [t]. /// Returns the value of the curve at point [t].
/// ///
/// The value of [t] must be between 0.0 and 1.0, inclusive. /// The value of [t] must be between 0.0 and 1.0, inclusive.
double transform(double t); double transform(double t);
/// Returns a new curve that is the reversed inversion of this one.
/// This is often useful as the reverseCurve of an [Animation].
Curve get flipped => new FlippedCurve(this);
} }
/// The identity map over the unit interval. /// The identity map over the unit interval.
class Linear implements Curve { class Linear extends Curve {
const Linear(); const Linear();
double transform(double t) => t; double transform(double t) => t;
} }
/// A sawtooth curve that repeats a given number of times over the unit interval. /// A sawtooth curve that repeats a given number of times over the unit interval.
class SawTooth implements Curve { class SawTooth extends Curve {
const SawTooth(this.count); const SawTooth(this.count);
final int count; final int count;
double transform(double t) { double transform(double t) {
...@@ -39,7 +45,7 @@ class SawTooth implements Curve { ...@@ -39,7 +45,7 @@ class SawTooth implements Curve {
} }
/// A curve that is 0.0 until start, then curved from 0.0 to 1.0 at end, then 1.0. /// A curve that is 0.0 until start, then curved from 0.0 to 1.0 at end, then 1.0.
class Interval implements Curve { class Interval extends Curve {
const Interval(this.start, this.end, { this.curve: Curves.linear }); const Interval(this.start, this.end, { this.curve: Curves.linear });
/// The smallest value for which this interval is 0.0. /// The smallest value for which this interval is 0.0.
...@@ -65,7 +71,7 @@ class Interval implements Curve { ...@@ -65,7 +71,7 @@ class Interval implements Curve {
} }
/// A cubic polynomial mapping of the unit interval. /// A cubic polynomial mapping of the unit interval.
class Cubic implements Curve { class Cubic extends Curve {
const Cubic(this.a, this.b, this.c, this.d); const Cubic(this.a, this.b, this.c, this.d);
final double a; final double a;
...@@ -103,8 +109,15 @@ double _bounce(double t) { ...@@ -103,8 +109,15 @@ double _bounce(double t) {
return 7.5625 * t * t + 0.984375; return 7.5625 * t * t + 0.984375;
} }
/// A curve that is the reversed inversion of its given curve.
class FlippedCurve extends Curve {
FlippedCurve(this.curve);
final Curve curve;
double transform(double t) => 1.0 - curve.transform(1.0 - t);
}
/// An oscillating curve that grows in magnitude. /// An oscillating curve that grows in magnitude.
class BounceInCurve implements Curve { class BounceInCurve extends Curve {
const BounceInCurve(); const BounceInCurve();
double transform(double t) { double transform(double t) {
return 1.0 - _bounce(1.0 - t); return 1.0 - _bounce(1.0 - t);
...@@ -112,7 +125,7 @@ class BounceInCurve implements Curve { ...@@ -112,7 +125,7 @@ class BounceInCurve implements Curve {
} }
/// An oscillating curve that shrink in magnitude. /// An oscillating curve that shrink in magnitude.
class BounceOutCurve implements Curve { class BounceOutCurve extends Curve {
const BounceOutCurve(); const BounceOutCurve();
double transform(double t) { double transform(double t) {
return _bounce(t); return _bounce(t);
...@@ -120,7 +133,7 @@ class BounceOutCurve implements Curve { ...@@ -120,7 +133,7 @@ class BounceOutCurve implements Curve {
} }
/// An oscillating curve that first grows and then shrink in magnitude. /// An oscillating curve that first grows and then shrink in magnitude.
class BounceInOutCurve implements Curve { class BounceInOutCurve extends Curve {
const BounceInOutCurve(); const BounceInOutCurve();
double transform(double t) { double transform(double t) {
if (t < 0.5) if (t < 0.5)
...@@ -131,7 +144,7 @@ class BounceInOutCurve implements Curve { ...@@ -131,7 +144,7 @@ class BounceInOutCurve implements Curve {
} }
/// An oscillating curve that grows in magnitude while overshooting its bounds. /// An oscillating curve that grows in magnitude while overshooting its bounds.
class ElasticInCurve implements Curve { class ElasticInCurve extends Curve {
const ElasticInCurve([this.period = 0.4]); const ElasticInCurve([this.period = 0.4]);
final double period; final double period;
double transform(double t) { double transform(double t) {
...@@ -142,7 +155,7 @@ class ElasticInCurve implements Curve { ...@@ -142,7 +155,7 @@ class ElasticInCurve implements Curve {
} }
/// An oscillating curve that shrinks in magnitude while overshooting its bounds. /// An oscillating curve that shrinks in magnitude while overshooting its bounds.
class ElasticOutCurve implements Curve { class ElasticOutCurve extends Curve {
const ElasticOutCurve([this.period = 0.4]); const ElasticOutCurve([this.period = 0.4]);
final double period; final double period;
double transform(double t) { double transform(double t) {
...@@ -152,7 +165,7 @@ class ElasticOutCurve implements Curve { ...@@ -152,7 +165,7 @@ class ElasticOutCurve implements Curve {
} }
/// An oscillating curve that grows and then shrinks in magnitude while overshooting its bounds. /// An oscillating curve that grows and then shrinks in magnitude while overshooting its bounds.
class ElasticInOutCurve implements Curve { class ElasticInOutCurve extends Curve {
const ElasticInOutCurve([this.period = 0.4]); const ElasticInOutCurve([this.period = 0.4]);
final double period; final double period;
double transform(double t) { double transform(double t) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment