| OLD | NEW |
| 1 // | 1 // |
| 2 // Copyright 2014 Google Inc. All rights reserved. | 2 // Copyright 2014 Google Inc. All rights reserved. |
| 3 // | 3 // |
| 4 // Use of this source code is governed by a BSD-style | 4 // Use of this source code is governed by a BSD-style |
| 5 // license that can be found in the LICENSE file or at | 5 // license that can be found in the LICENSE file or at |
| 6 // https://developers.google.com/open-source/licenses/bsd | 6 // https://developers.google.com/open-source/licenses/bsd |
| 7 // | 7 // |
| 8 | 8 |
| 9 part of charted.core.interpolators; | 9 part of charted.core.interpolators; |
| 10 | 10 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 const String EASE_MODE_OUT_IN = 'out-in'; | 25 const String EASE_MODE_OUT_IN = 'out-in'; |
| 26 | 26 |
| 27 /// [EasingFunction] manipulates progression of an animation. The returned | 27 /// [EasingFunction] manipulates progression of an animation. The returned |
| 28 /// value is passed to an [Interpolator] to generate intermediate state. | 28 /// value is passed to an [Interpolator] to generate intermediate state. |
| 29 typedef num EasingFunction(num t); | 29 typedef num EasingFunction(num t); |
| 30 | 30 |
| 31 /// Alters behavior of the [EasingFunction]. Takes [fn] and returns an | 31 /// Alters behavior of the [EasingFunction]. Takes [fn] and returns an |
| 32 /// altered [EasingFunction]. | 32 /// altered [EasingFunction]. |
| 33 typedef EasingFunction EasingModeFunction(EasingFunction fn); | 33 typedef EasingFunction EasingModeFunction(EasingFunction fn); |
| 34 | 34 |
| 35 /// Creates an easing function based on type and mode. | |
| 36 EasingFunction easingFunctionByName(String type, | |
| 37 [String mode = EASE_MODE_IN, List params]) { | |
| 38 const Map easingTypes = const { | |
| 39 EASE_TYPE_LINEAR: identityFunction, | |
| 40 EASE_TYPE_POLY: easePoly, | |
| 41 EASE_TYPE_QUAD: easeQuad, | |
| 42 EASE_TYPE_CUBIC: easeCubic, | |
| 43 EASE_TYPE_SIN: easeSin, | |
| 44 EASE_TYPE_EXP: easeExp, | |
| 45 EASE_TYPE_CIRCLE: easeCircle, | |
| 46 EASE_TYPE_ELASTIC: easeElastic, | |
| 47 EASE_TYPE_BACK: easeBack, | |
| 48 EASE_TYPE_BOUNCE: easeBounce | |
| 49 }; | |
| 50 | |
| 51 const Map easingModes = const { | |
| 52 EASE_MODE_IN: identityFunction, | |
| 53 EASE_MODE_OUT: reverseEasingFn, | |
| 54 EASE_MODE_IN_OUT: reflectEasingFn, | |
| 55 EASE_MODE_OUT_IN: reflectReverseEasingFn | |
| 56 }; | |
| 57 | |
| 58 const Map customEasingFunctions = const { | |
| 59 '$EASE_TYPE_CUBIC-$EASE_MODE_IN_OUT': easeCubicInOut | |
| 60 }; | |
| 61 | |
| 62 assert(easingTypes.containsKey(type)); | |
| 63 assert(easingModes.containsKey(mode)); | |
| 64 | |
| 65 EasingFunction fn; | |
| 66 if (customEasingFunctions.containsKey('$type-$mode')) { | |
| 67 fn = Function.apply(customEasingFunctions['$type-$mode'], params); | |
| 68 } else { | |
| 69 fn = Function.apply(easingTypes[type], params); | |
| 70 fn = easingModes[mode](fn); | |
| 71 } | |
| 72 return clampEasingFn(fn); | |
| 73 } | |
| 74 | |
| 75 /// Clamps transition progress to stay between 0.0 and 1.0 | 35 /// Clamps transition progress to stay between 0.0 and 1.0 |
| 76 EasingFunction clampEasingFn(EasingFunction f) => | 36 EasingFunction clampEasingFn(EasingFunction f) => |
| 77 (t) => t <= 0 ? 0 : t >= 1 ? 1 : f(t); | 37 (t) => t <= 0 ? 0 : t >= 1 ? 1 : f(t); |
| 78 | 38 |
| 79 // | 39 // |
| 80 // Implementation of easing modes. | 40 // Implementation of easing modes. |
| 81 // | 41 // |
| 82 | 42 |
| 83 EasingFunction reverseEasingFn(EasingFunction f) => (t) => 1 - f(1 - t); | 43 EasingFunction reverseEasingFn(EasingFunction f) => (t) => 1 - f(1 - t); |
| 84 | 44 |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 | 79 |
| 120 EasingFunction easeCircle() => (num t) => 1 - math.sqrt(1 - t * t); | 80 EasingFunction easeCircle() => (num t) => 1 - math.sqrt(1 - t * t); |
| 121 | 81 |
| 122 EasingFunction easeBounce() => (num t) => t < 1 / 2.75 | 82 EasingFunction easeBounce() => (num t) => t < 1 / 2.75 |
| 123 ? 7.5625 * t * t | 83 ? 7.5625 * t * t |
| 124 : t < 2 / 2.75 | 84 : t < 2 / 2.75 |
| 125 ? 7.5625 * (t -= 1.5 / 2.75) * t + .75 | 85 ? 7.5625 * (t -= 1.5 / 2.75) * t + .75 |
| 126 : t < 2.5 / 2.75 | 86 : t < 2.5 / 2.75 |
| 127 ? 7.5625 * (t -= 2.25 / 2.75) * t + .9375 | 87 ? 7.5625 * (t -= 2.25 / 2.75) * t + .9375 |
| 128 : 7.5625 * (t -= 2.625 / 2.75) * t + .984375; | 88 : 7.5625 * (t -= 2.625 / 2.75) * t + .984375; |
| OLD | NEW |