Permalink
Browse files

Add convenient animation for Interpolatable types.

  • Loading branch information...
TomasMikula committed Feb 15, 2015
1 parent b369264 commit 95b184cab9b3b4b81c1425a40d06c2de95075ef6
@@ -20,9 +20,10 @@
@Override
public void start(Stage primaryStage) {
Circle circle = new Circle(30.0, Color.BLUE);
Circle circle = new Circle(30.0);
Pane canvas = new Pane(circle);
// animate circle position
Var<Point2D> center = Var.newSimpleVar(new Point2D(W/2, H/2));
Val<Point2D> animCenter = center.animate(
(p1, p2) -> Duration.ofMillis((long) p1.distance(p2)),
@@ -31,12 +32,21 @@ public void start(Stage primaryStage) {
circle.centerXProperty().bind(animCenter.map(Point2D::getX));
circle.centerYProperty().bind(animCenter.map(Point2D::getY));
Random random = new Random();
// animate circle color
Var<Color> color = Var.newSimpleVar(Color.BLUE);
Val<Color> animColor = Val.animate(color, Duration.ofMillis(500));
circle.fillProperty().bind(animColor);
// on click, move to random position and transition to random color
Random random = new Random();
circle.setOnMouseClicked(click -> {
center.setValue(new Point2D(
random.nextInt(W),
random.nextInt(H)));
color.setValue(Color.rgb(
random.nextInt(240),
random.nextInt(240),
random.nextInt(240)));
});
primaryStage.setScene(new Scene(canvas, W, H));
@@ -1,6 +1,7 @@
package org.reactfx.util;
import static javafx.animation.Interpolator.*;
import javafx.animation.Interpolatable;
/**
* Interpolates values between two boundary values.
@@ -52,4 +53,8 @@
static final Interpolator<Long> EASE_OUT_LONG =
(a, b, frac) -> EASE_OUT.interpolate(a.longValue(), b.longValue(), frac);
static <T extends Interpolatable<T>> Interpolator<T> get() {
return (a, b, frac) -> a.interpolate(b, frac);
}
}
@@ -10,6 +10,7 @@
import java.util.function.Predicate;
import java.util.function.Supplier;
import javafx.animation.Interpolatable;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.property.Property;
@@ -464,6 +465,28 @@ static Subscription observeInvalidations(
return animate(obs, (a, b) -> duration, interpolator);
}
/**
* Like {@link #animate(ObservableValue, BiFunction, Interpolator)}, but
* uses the interpolation defined by the {@linkplain Interpolatable} type
* {@code T}.
*/
static <T extends Interpolatable<T>> Val<T> animate(
ObservableValue<T> obs,
BiFunction<? super T, ? super T, Duration> duration) {
return animate(obs, duration, Interpolator.get());
}
/**
* Like {@link #animate(ObservableValue, Duration, Interpolator)}, but
* uses the interpolation defined by the {@linkplain Interpolatable} type
* {@code T}.
*/
static <T extends Interpolatable<T>> Val<T> animate(
ObservableValue<T> obs,
Duration duration) {
return animate(obs, duration, Interpolator.get());
}
static <A, B, R> Val<R> combine(
ObservableValue<A> src1,

0 comments on commit 95b184c

Please sign in to comment.