Skip to content

Instantly share code, notes, and snippets.

@RustyKnight
Created May 27, 2018 04:04
Show Gist options
  • Save RustyKnight/2b116c4246fdbd2203a529ae2799d929 to your computer and use it in GitHub Desktop.
Save RustyKnight/2b116c4246fdbd2203a529ae2799d929 to your computer and use it in GitHub Desktop.
A duration based `TimeLine`
import java.time.Duration;
import java.time.LocalDateTime;
public class DurationTimeLine<T> extends TimeLine<T> {
private Duration duration;
private LocalDateTime startedAt;
public DurationTimeLine(Duration duration, Blender<T> blender) {
this(duration, blender, null);
}
public DurationTimeLine(Duration duration, Blender<T> blender, SplineInterpolator splineInterpolator) {
super(blender, splineInterpolator);
this.duration = duration;
}
public void start() {
startedAt = LocalDateTime.now();
}
public boolean isRunning() {
return startedAt != null;
}
public T getValue() {
if (startedAt == null) {
return getValueAt(0.0f);
}
Duration runningTime = Duration.between(startedAt, LocalDateTime.now());
if (runningTime.compareTo(duration) > 0) {
runningTime = runningTime.minus(duration);
startedAt = LocalDateTime.now().minus(runningTime);
}
long total = duration.toMillis();
long remaining = duration.minus(runningTime).toMillis();
float progress = remaining / (float) total;
return getValueAt(progress);
}
}
@RustyKnight
Copy link
Author

This is a TimeLine with a inbuilt Duration.

The TimeLine can then calculate a "value" based on the elapsed time.

This implementation allows for "over time", which simply takes the amount of time over the "duration" and adds it onto the next cycle (although this does need improvement). The intention is to stop the animation from "jumping" to different points when it exceeds the "expected" duration.

Additional options should be added, such as "stop after duration", so the animation will stop when it reaches the end of the "duration" period. Decisions about if it should return the value at "0" or "1" would also need to be added

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment