From b575c47ab351a187d484c28af179031851d6e03b Mon Sep 17 00:00:00 2001 From: "J. Neugebauer" Date: Fri, 8 Jul 2022 08:52:31 +0200 Subject: [PATCH] Neue Easing funktionen --- src/schule/ngb/zm/anim/Easing.java | 76 ++++++++++++++++++- .../schule/ngb/zm/anim/AnimationsTest.java | 26 ++++++- 2 files changed, 97 insertions(+), 5 deletions(-) diff --git a/src/schule/ngb/zm/anim/Easing.java b/src/schule/ngb/zm/anim/Easing.java index 8dea06b..7679e23 100644 --- a/src/schule/ngb/zm/anim/Easing.java +++ b/src/schule/ngb/zm/anim/Easing.java @@ -7,7 +7,7 @@ import java.util.function.DoubleUnaryOperator; */ public class Easing { - public static final DoubleUnaryOperator DEFAULT_EASING = Easing::cubicInOut; + public static final DoubleUnaryOperator DEFAULT_EASING = Easing::smooth; public static final DoubleUnaryOperator thereAndBack() { return Easing::thereAndBack; @@ -50,6 +50,10 @@ public class Easing { } + /* + * Functions taken from easings.net + */ + public static final DoubleUnaryOperator linear() { return Easing::linear; } @@ -190,11 +194,11 @@ public class Easing { double n1 = 7.5625; double d1 = 2.75; - if (t < 1.0 / d1) { + if( t < 1.0 / d1 ) { return n1 * t * t; - } else if (t < 2.0 / d1) { + } else if( t < 2.0 / d1 ) { return n1 * (t -= 1.5 / d1) * t + 0.75; - } else if (t < 2.5 / d1) { + } else if( t < 2.5 / d1 ) { return n1 * (t -= 2.25 / d1) * t + 0.9375; } else { return n1 * (t -= 2.625 / d1) * t + 0.984375; @@ -246,6 +250,70 @@ public class Easing { : (Math.pow(2 * t - 2, 2) * ((c2 + 1) * (t * 2 - 2) + c2) + 2) / 2; } + + /* + * Functions from manim community + */ + + public static final DoubleUnaryOperator smooth() { + return Easing::smooth; + } + + public static final double smooth( double t ) { + double error = sigmoid(-INFLECTION / 2.0); + return Math.min( + Math.max( + (sigmoid(INFLECTION * (t - 0.5)) - error) / (1 - 2 * error), + 0 + ), + 1.0 + ); + } + + public static final double rushIn( double t ) { + return 2 * smooth(t / 2.0); + } + + public static final double rushOut( double t ) { + return 2 * smooth(t / 2.0 + 0.5) - 1; + } + + public static final double doubleSmooth( double t ) { + if( t < 0.5 ) + return 0.5 * smooth(2 * t); + else + return 0.5 * (1 + smooth(2 * t - 1)); + } + + public static final double hobbit( double t ) { + double new_t = t < 0.5 ? 2 * t : 2 * (1 - t); + return smooth(new_t); + } + + + public static final DoubleUnaryOperator wiggle() { + return Easing::wiggle; + } + + public static final DoubleUnaryOperator wiggle( final int wiggles ) { + return (t) -> Easing.wiggle(t, wiggles); + } + + public static final double wiggle( double t ) { + return wiggle(t, 2); + } + + public static final double wiggle( double t, int wiggles ) { + return hobbit(t) * Math.sin(wiggles * Math.PI * t); + } + + public static double INFLECTION = 10.0; + + public static final double sigmoid( double x ) { + return 1.0 / (1 + Math.exp(-x)); + } + + private Easing() { } diff --git a/test/src/schule/ngb/zm/anim/AnimationsTest.java b/test/src/schule/ngb/zm/anim/AnimationsTest.java index 7aa9eb3..d4f0f89 100644 --- a/test/src/schule/ngb/zm/anim/AnimationsTest.java +++ b/test/src/schule/ngb/zm/anim/AnimationsTest.java @@ -7,6 +7,7 @@ import org.junit.jupiter.api.Test; import schule.ngb.zm.Color; import schule.ngb.zm.Constants; +import schule.ngb.zm.Options; import schule.ngb.zm.Zeichenmaschine; import schule.ngb.zm.shapes.*; @@ -62,7 +63,7 @@ class AnimationsTest { Future future = Animations.animate( s, runtime, easing, - ( e ) -> Constants.interpolate(0, 400, e), + ( e ) -> Constants.interpolate(0, zm.getWidth(), e), ( t, p ) -> { t.moveTo(p, p); } @@ -235,4 +236,27 @@ class AnimationsTest { } } + @Test + void animateManim() { + Shape s = new Circle(0, 0, 10); + shapes.add(s); + Text t = new Text(0, 0, "Easing"); + t.setAnchor(Options.Direction.EAST); + t.alignTo(Options.Direction.NORTHEAST, -20.0); + shapes.add(t); + + t.setText("rushIn"); + _animateMove(s, 2500, Easing::rushIn); + t.setText("rushOut"); + _animateMove(s, 2500, Easing::rushOut); + t.setText("hobbit"); + _animateMove(s, 2500, Easing::hobbit); + t.setText("wiggle(2)"); + _animateMove(s, 2500, Easing::wiggle); + t.setText("wiggle(4)"); + _animateMove(s, 2500, Easing.wiggle(4)); + t.setText("doubleSmooth"); + _animateMove(s, 2500, Easing::doubleSmooth); + } + }