|
1 | 1 | package indigoexamples |
2 | 2 |
|
3 | 3 | import indigo.* |
| 4 | +import indigo.syntax.* |
| 5 | +import indigo.syntax.animations.* |
4 | 6 | import generated.Config |
5 | 7 | import generated.Assets |
6 | 8 |
|
@@ -28,14 +30,59 @@ object AnimationPart3 extends IndigoSandbox[Unit, Unit]: |
28 | 30 | def updateModel(context: Context[Unit], model: Unit): GlobalEvent => Outcome[Unit] = |
29 | 31 | _ => Outcome(model) |
30 | 32 |
|
| 33 | + val circle = |
| 34 | + Shape.Circle( |
| 35 | + Circle(Point.zero, 25), |
| 36 | + Fill.Color(RGBA.Red), |
| 37 | + Stroke(2, RGBA.White) |
| 38 | + ) |
| 39 | + |
| 40 | + /** ## How to make a timeline animation |
| 41 | + * |
| 42 | + * In this animation, we have two layers. |
| 43 | + * |
| 44 | + * The first layer initially waits 2 seconds. Then over the next 5 seconds, it calculates a |
| 45 | + * points position diagonally (lerp means linear interpolation) from one corner of the viewport |
| 46 | + * to the other, and finally moves a circle to that position. All of this is performed using an |
| 47 | + * 'ease-in-out' function that accelerates the movement up initially and slows it down towards |
| 48 | + * the end. |
| 49 | + * |
| 50 | + * The second layer also waits 2 seconds for consistency, then fades the circles fill color in, |
| 51 | + * over time. |
| 52 | + * |
| 53 | + * The function inside the `animate` block is built up using `SignalFunction`s (see below) to |
| 54 | + * describe the value transformation that results in the animated movement. There are lots of |
| 55 | + * helpful signal functions available on the `SignalFunction` companion object for you to make |
| 56 | + * use of. |
| 57 | + */ |
| 58 | + // ```scala |
| 59 | + def myTimelineAnimation(viewportSize: Size): Timeline[Shape.Circle] = |
| 60 | + timeline( |
| 61 | + layer( |
| 62 | + startAfter(2.seconds), |
| 63 | + animate(10.seconds) { circle => |
| 64 | + easeInOut >>> |
| 65 | + lerp(Point(60), viewportSize.toPoint - Point(60)) >>> |
| 66 | + SignalFunction(pt => circle.moveTo(pt)) |
| 67 | + } |
| 68 | + ), |
| 69 | + layer( |
| 70 | + startAfter(2.seconds), |
| 71 | + animate(10.seconds) { circle => |
| 72 | + lerp >>> |
| 73 | + SignalFunction { alpha => |
| 74 | + circle.withFill(Fill.Color(RGBA.Green.withAlpha(alpha))) |
| 75 | + } |
| 76 | + } |
| 77 | + ) |
| 78 | + ) |
| 79 | + |
31 | 80 | def present(context: Context[Unit], model: Unit): Outcome[SceneUpdateFragment] = |
32 | 81 | Outcome( |
33 | 82 | SceneUpdateFragment( |
34 | | - Shape |
35 | | - .Circle( |
36 | | - Circle(Point.zero, 50), |
37 | | - Fill.Color(RGBA.Red), |
38 | | - Stroke(2, RGBA.White) |
39 | | - ) |
| 83 | + myTimelineAnimation(context.frame.viewport.bounds.size) |
| 84 | + .atOrLast(context.frame.time.running)(circle) |
| 85 | + .toBatch |
40 | 86 | ) |
41 | 87 | ) |
| 88 | + // ``` |
0 commit comments