evaluate method

T evaluate(
  1. double t
)

Evaluate a value for the given time t.

The value will be lerped from the nearest keyframe before, and the nearest keyframe after.

If there's no keyframe before t, the value will be the value of the next keyframe.

If there's no keyframe after t, the value will be the value of the previous keyframe.

Implementation

T evaluate(double t) {
  if (t <= 0 || keyframes.length == 1) {
    return keyframes.first.value;
  }
  if (t >= 1) {
    return keyframes.last.value;
  }
  final afterIndex = keyframes.indexWhere((x) => t < x.time);
  if (afterIndex == -1) {
    return keyframes.last.value;
  }
  if (afterIndex == 0) {
    return keyframes.first.value;
  }

  final before = keyframes[afterIndex - 1];
  final after = keyframes[afterIndex];
  final intervalTime = (t - before.time) / (after.time - before.time);
  return (lerp ?? defaultLerp).call(
    before.value,
    after.value,
    after.curve.transform(intervalTime),
  );
}