curves.dart 11 KB
Newer Older
1 2 3 4
// 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.

5 6
import 'dart:math' as math;

7 8 9 10 11 12
double _evaluateCubic(double a, double b, double m) {
  return 3 * a * (1 - m) * (1 - m) * m + 3 * b * (1 - m) * m * m + m * m * m;
}

const double _kCubicErrorBound = 0.001;

13
/// A mapping of the unit interval to the unit interval.
14 15
///
/// A curve must map 0.0 to 0.0 and 1.0 to 1.0.
16 17
///
/// See [Curves] for a collection of common animation curves.
18
abstract class Curve {
19 20
  /// Abstract const constructor. This constructor enables subclasses to provide
  /// const constructors so that they can be used in const expressions.
21 22
  const Curve();

Florian Loitsch's avatar
Florian Loitsch committed
23
  /// Returns the value of the curve at point [t].
24
  ///
Florian Loitsch's avatar
Florian Loitsch committed
25
  /// The value of [t] must be between 0.0 and 1.0, inclusive.
26
  double transform(double t);
27 28 29 30

  /// 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);
31 32 33 34 35

  @override
  String toString() {
    return '$runtimeType';
  }
36 37
}

Florian Loitsch's avatar
Florian Loitsch committed
38
/// The identity map over the unit interval.
39 40
///
/// See [Curves.linear] for an instance of this class.
41
class Linear extends Curve {
42
  const Linear._();
43 44

  @override
45
  double transform(double t) => t;
46 47
}

Hixie's avatar
Hixie committed
48
/// A sawtooth curve that repeats a given number of times over the unit interval.
49 50 51
///
/// The curve rises linearly from 0.0 to 1.0 and then falls discontinuously back
/// to 0.0 each iteration.
52
class SawTooth extends Curve {
53 54 55
  /// Creates a sawtooth curve.
  ///
  /// The [count] argument must not be null.
Hixie's avatar
Hixie committed
56
  const SawTooth(this.count);
57

58
  /// The number of repetitions of the sawtooth pattern in the unit interval.
Hixie's avatar
Hixie committed
59
  final int count;
60 61

  @override
Hixie's avatar
Hixie committed
62
  double transform(double t) {
63 64
    if (t == 1.0)
      return 1.0;
Hixie's avatar
Hixie committed
65 66 67
    t *= count;
    return t - t.truncateToDouble();
  }
68 69 70 71 72

  @override
  String toString() {
    return '$runtimeType($count)';
  }
Hixie's avatar
Hixie committed
73 74
}

75
/// A curve that is 0.0 until [start], then curved from 0.0 to 1.0 at [end], then 1.0.
76
class Interval extends Curve {
77 78 79
  /// Creates an interval curve.
  ///
  /// The [start] and [end] arguments must not be null.
80
  const Interval(this.begin, this.end, { this.curve: Curves.linear });
81

Florian Loitsch's avatar
Florian Loitsch committed
82
  /// The smallest value for which this interval is 0.0.
83
  final double begin;
84

Florian Loitsch's avatar
Florian Loitsch committed
85
  /// The smallest value for which this interval is 1.0.
86 87
  final double end;

Florian Loitsch's avatar
Florian Loitsch committed
88
  /// The curve to apply between [start] and [end].
89 90
  final Curve curve;

91
  @override
92
  double transform(double t) {
93 94
    assert(begin >= 0.0);
    assert(begin <= 1.0);
95 96
    assert(end >= 0.0);
    assert(end <= 1.0);
97
    assert(end >= begin);
98 99
    if (t == 0.0 || t == 1.0)
      return t;
100
    t = ((t - begin) / (end - begin)).clamp(0.0, 1.0);
101 102 103
    if (t == 0.0 || t == 1.0)
      return t;
    return curve.transform(t);
104
  }
105 106 107 108

  @override
  String toString() {
    if (curve is! Linear)
109 110
      return '$runtimeType($begin\u22EF$end)\u27A9$curve';
    return '$runtimeType($begin\u22EF$end)';
111
  }
112 113
}

114
/// A curve that is 0.0 until it hits the threshold, then it jumps to 1.0.
115 116
class Threshold extends Curve {
  /// Creates a threshold curve.
117 118
  ///
  /// The [threshold] argument must not be null.
119
  const Threshold(this.threshold);
120 121 122

  /// The value before which the curve is 0.0 and after which the curve is 1.0.
  ///
123
  /// When t is exactly [threshold], the curve has the value 1.0.
124 125 126 127 128 129 130 131 132 133 134 135
  final double threshold;

  @override
  double transform(double t) {
    assert(threshold >= 0.0);
    assert(threshold <= 1.0);
    if (t == 0.0 || t == 1.0)
      return t;
    return t < threshold ? 0.0 : 1.0;
  }
}

Florian Loitsch's avatar
Florian Loitsch committed
136
/// A cubic polynomial mapping of the unit interval.
137 138 139 140 141 142 143 144 145
///
/// See [Curves] for a number of commonly used cubic curves.
///
/// See also:
///
///  * [Curves.ease]
///  * [Curves.easeIn]
///  * [Curves.easeOut]
///  * [Curves.easeInOut]
146
class Cubic extends Curve {
147 148 149 150 151 152
  /// Creates a cubic curve.
  ///
  /// Rather than creating a new instance, consider using one of the common
  /// cubic curves in [Curves].
  ///
  /// The [a], [b], [c], and [d] arguments must not be null.
153 154
  const Cubic(this.a, this.b, this.c, this.d);

155 156 157 158
  /// The x coordinate of the first control point.
  ///
  /// The line through the point (0, 0) and the first control point is tangent
  /// to the curve at the point (0, 0).
159
  final double a;
160 161 162 163 164

  /// The y coordinate of the first control point.
  ///
  /// The line through the point (0, 0) and the first control point is tangent
  /// to the curve at the point (0, 0).
165
  final double b;
166 167 168 169 170

  /// The x coordinate of the second control point.
  ///
  /// The line through the point (1, 1) and the second control point is tangent
  /// to the curve at the point (1, 1).
171
  final double c;
172 173 174 175 176

  /// The y coordinate of the second control point.
  ///
  /// The line through the point (1, 1) and the second control point is tangent
  /// to the curve at the point (1, 1).
177 178
  final double d;

179
  @override
180 181 182 183 184 185 186 187 188 189 190 191 192 193
  double transform(double t) {
    double start = 0.0;
    double end = 1.0;
    while (true) {
      double midpoint = (start + end) / 2;
      double estimate = _evaluateCubic(a, c, midpoint);
      if ((t - estimate).abs() < _kCubicErrorBound)
        return _evaluateCubic(b, d, midpoint);
      if (estimate < t)
        start = midpoint;
      else
        end = midpoint;
    }
  }
194 195 196 197 198

  @override
  String toString() {
    return '$runtimeType(${a.toStringAsFixed(2)}, ${b.toStringAsFixed(2)}, ${c.toStringAsFixed(2)}, ${d.toStringAsFixed(2)})';
  }
199 200
}

201
double _bounce(double t) {
202 203 204 205 206 207 208 209 210 211 212 213 214
  if (t < 1.0 / 2.75) {
    return 7.5625 * t * t;
  } else if (t < 2 / 2.75) {
    t -= 1.5 / 2.75;
    return 7.5625 * t * t + 0.75;
  } else if (t < 2.5 / 2.75) {
    t -= 2.25 / 2.75;
    return 7.5625 * t * t + 0.9375;
  }
  t -= 2.625 / 2.75;
  return 7.5625 * t * t + 0.984375;
}

215
/// A curve that is the reversed inversion of its given curve.
216 217 218 219
///
/// This curve evalutes the given curve in reverse (i.e., from 1.0 to 0.0 as t
/// increases from 0.0 to 1.0) and returns the inverse of the given curve's value
/// (i.e., 1.0 minus the given curve's value).
220
class FlippedCurve extends Curve {
221 222 223 224
  /// Creates a flipped curve.
  ///
  /// The [curve] argument must not be null.
  const FlippedCurve(this.curve);
225

226
  /// The curve that is being flipped.
227
  final Curve curve;
228 229

  @override
230
  double transform(double t) => 1.0 - curve.transform(1.0 - t);
231 232 233 234 235

  @override
  String toString() {
    return '$runtimeType($curve)';
  }
236 237
}

Florian Loitsch's avatar
Florian Loitsch committed
238
/// An oscillating curve that grows in magnitude.
239 240
///
/// See [Curves.bounceIn] for an instance of this class.
241
class BounceInCurve extends Curve {
242
  const BounceInCurve._();
243 244

  @override
245 246 247 248 249
  double transform(double t) {
    return 1.0 - _bounce(1.0 - t);
  }
}

Florian Loitsch's avatar
Florian Loitsch committed
250
/// An oscillating curve that shrink in magnitude.
251 252
///
/// See [Curves.bounceOut] for an instance of this class.
253
class BounceOutCurve extends Curve {
254
  const BounceOutCurve._();
255 256

  @override
257 258 259 260 261
  double transform(double t) {
    return _bounce(t);
  }
}

Florian Loitsch's avatar
Florian Loitsch committed
262
/// An oscillating curve that first grows and then shrink in magnitude.
263 264
///
/// See [Curves.bounceInOut] for an instance of this class.
265
class BounceInOutCurve extends Curve {
266
  const BounceInOutCurve._();
267 268

  @override
269 270 271 272 273 274 275 276
  double transform(double t) {
    if (t < 0.5)
      return (1.0 - _bounce(1.0 - t)) * 0.5;
    else
      return _bounce(t * 2.0 - 1.0) * 0.5 + 0.5;
  }
}

Florian Loitsch's avatar
Florian Loitsch committed
277
/// An oscillating curve that grows in magnitude while overshooting its bounds.
278
class ElasticInCurve extends Curve {
279 280 281
  /// Creates an elastic-in curve.
  ///
  /// Rather than creating a new instance, consider using [Curves.elasticIn].
282
  const ElasticInCurve([this.period = 0.4]);
283

284
  /// The duration of the oscillation.
285
  final double period;
286 287

  @override
288 289 290 291 292
  double transform(double t) {
    double s = period / 4.0;
    t = t - 1.0;
    return -math.pow(2.0, 10.0 * t) * math.sin((t - s) * (math.PI * 2.0) / period);
  }
293 294 295 296 297

  @override
  String toString() {
    return '$runtimeType($period)';
  }
298 299
}

Florian Loitsch's avatar
Florian Loitsch committed
300
/// An oscillating curve that shrinks in magnitude while overshooting its bounds.
301
class ElasticOutCurve extends Curve {
302 303 304
  /// Creates an elastic-out curve.
  ///
  /// Rather than creating a new instance, consider using [Curves.elasticOut].
305
  const ElasticOutCurve([this.period = 0.4]);
306

307
  /// The duration of the oscillation.
308
  final double period;
309 310

  @override
311 312 313 314
  double transform(double t) {
    double s = period / 4.0;
    return math.pow(2.0, -10 * t) * math.sin((t - s) * (math.PI * 2.0) / period) + 1.0;
  }
315 316 317 318 319

  @override
  String toString() {
    return '$runtimeType($period)';
  }
320 321
}

Florian Loitsch's avatar
Florian Loitsch committed
322
/// An oscillating curve that grows and then shrinks in magnitude while overshooting its bounds.
323
class ElasticInOutCurve extends Curve {
324 325 326
  /// Creates an elastic-in-out curve.
  ///
  /// Rather than creating a new instance, consider using [Curves.elasticInOut].
327
  const ElasticInOutCurve([this.period = 0.4]);
328

329
  /// The duration of the oscillation.
330
  final double period;
331 332

  @override
333 334
  double transform(double t) {
    double s = period / 4.0;
335
    t = 2.0 * t - 1.0;
336 337 338 339 340
    if (t < 0.0)
      return -0.5 * math.pow(2.0, 10.0 * t) * math.sin((t - s) * (math.PI * 2.0) / period);
    else
      return math.pow(2.0, -10.0 * t) * math.sin((t - s) * (math.PI * 2.0) / period) * 0.5 + 1.0;
  }
341 342 343 344 345

  @override
  String toString() {
    return '$runtimeType($period)';
  }
346 347
}

348 349 350
/// A collection of common animation curves.
class Curves {
  Curves._();
351

352
  /// A linear animation curve
353
  static const Linear linear = const Linear._();
354

Florian Loitsch's avatar
Florian Loitsch committed
355
  /// A cubic animation curve that speeds up quickly and ends slowly.
356
  static const Cubic ease = const Cubic(0.25, 0.1, 0.25, 1.0);
357

Florian Loitsch's avatar
Florian Loitsch committed
358
  /// A cubic animation curve that starts slowly and ends quickly.
359
  static const Cubic easeIn = const Cubic(0.42, 0.0, 1.0, 1.0);
360

Florian Loitsch's avatar
Florian Loitsch committed
361
  /// A cubic animation curve that starts quickly and ends slowly.
362
  static const Cubic easeOut = const Cubic(0.0, 0.0, 0.58, 1.0);
363

Florian Loitsch's avatar
Florian Loitsch committed
364
  /// A cubic animation curve that starts slowly, speeds up, and then and ends slowly.
365
  static const Cubic easeInOut = const Cubic(0.42, 0.0, 0.58, 1.0);
366

Florian Loitsch's avatar
Florian Loitsch committed
367
  /// An oscillating curve that grows in magnitude.
368
  static const BounceInCurve bounceIn = const BounceInCurve._();
369

Florian Loitsch's avatar
Florian Loitsch committed
370
  /// An oscillating curve that first grows and then shrink in magnitude.
371
  static const BounceOutCurve bounceOut = const BounceOutCurve._();
372

Florian Loitsch's avatar
Florian Loitsch committed
373
  /// An oscillating curve that first grows and then shrink in magnitude.
374
  static const BounceInOutCurve bounceInOut = const BounceInOutCurve._();
375

Florian Loitsch's avatar
Florian Loitsch committed
376
  /// An oscillating curve that grows in magnitude while overshootings its bounds.
377
  static const ElasticInCurve elasticIn = const ElasticInCurve();
378

Florian Loitsch's avatar
Florian Loitsch committed
379
  /// An oscillating curve that shrinks in magnitude while overshootings its bounds.
380
  static const ElasticOutCurve elasticOut = const ElasticOutCurve();
381

Florian Loitsch's avatar
Florian Loitsch committed
382
  /// An oscillating curve that grows and then shrinks in magnitude while overshootings its bounds.
383 384
  static const ElasticInOutCurve elasticInOut = const ElasticInOutCurve();

Florian Loitsch's avatar
Florian Loitsch committed
385 386 387 388 389
  /// A curve that starts quickly and eases into its final position.
  ///
  /// Over the course of the animation, the object spends more time near its
  /// final destination. As a result, the user isn’t left waiting for the
  /// animation to finish, and the negative effects of motion are minimized.
390 391
  static const Curve fastOutSlowIn = const Cubic(0.4, 0.0, 0.2, 1.0);
}