• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Explicit Animation (animateTo)
2
3With **animateTo**, you can build explicit animations for state changes caused by closure code. In an explicit animation that involves width and height changes, as in a property animation, a component's content (such as text, [canvas](ts-components-canvas-canvas.md) content, and [linear gradient](ts-universal-attributes-gradient-color.md)) is changed straight to the final state. To enable the content to change with the width and height during the animation process, you can use the [renderFit](ts-universal-attributes-renderfit.md) attribute.
4
5>  **NOTE**
6>
7>  The APIs of this module are supported since API version 7. Updates will be marked with a superscript to indicate their earliest API version.
8>
9>  The functionality of this module depends on UI context. This means that the APIs of this module cannot be used where the UI context is unclear. For details, see [UIContext](../apis/js-apis-arkui-UIContext.md#uicontext).
10>
11>Since API version 9, this API is supported in ArkTS widgets.
12>
13>  Since API version 10, you can use the [animateTo](../apis/js-apis-arkui-UIContext.md#animateto) API in [UIContext](../apis/js-apis-arkui-UIContext.md#uicontext) to obtain the UI context.
14
15## APIs
16animateTo(value: AnimateParam, event: () => void): void
17
18
19**Parameters**
20| Name   | Type                               | Mandatory| Description                                   |
21| ----- | --------------------------------- | ---- | ------------------------------------- |
22| value | [AnimateParam](#animateparam)| Yes   | Animation settings.                          |
23| event | () => void                        | Yes   | Closure function that displays the animation. The system automatically inserts the transition animation if the status changes in the closure function.|
24
25## AnimateParam
26
27| Name        | Type         | Mandatory                | Description                                      |
28| ---------- | ---------------|------------------------ | ---------------------------------------- |
29| duration   | number         |  No | Animation duration, in ms.<br>Default value: **1000**<br>Since API version 9, this API is supported in ArkTS widgets.<br>**NOTE**<br>- The maximum animation duration on an ArkTS widget is 1000 ms. If the set value exceeds the limit, the value **1000** will be used.<br>- A value less than 0 evaluates to the value **0**.<br>- Floating-point values will be rounded down to integers. For example, if the value set is 1.2, **1** will be used.|
30| tempo      | number         | No| Animation playback speed. A larger value indicates faster animation playback, and a smaller value indicates slower animation playback. The value **0** means that there is no animation.<br>Default value: **1.0**<br>**NOTE**<br>A value less than 0 evaluates to the value **1**.|
31| curve      | [Curve](ts-appendix-enums.md#curve) \| [ICurve<sup>9+</sup>](../apis/js-apis-curve.md#icurve) \| string | No| Animation curve.<br>Default value: **Curve.EaseInOut**<br>Since API version 9, this API is supported in ArkTS widgets.|
32| delay      | number         | No| Delay of animation playback, in ms. By default, the playback is not delayed.<br>Default value: **0**<br>Value range: (-∞, +∞)<br>**NOTE**<br>- A value greater than 0 means to begin the animation after the specified amount of time has elapsed.<br>A value less than 0 means to begin the animation in advance. If the absolute value of **delay** is less than the actual animation duration, the animation starts its first frame from the state at the absolute value. If the absolute value of **delay** is greater than or equal to the actual animation duration, the animation starts its first frame from the end state. The actual animation duration is equal to the duration of a single animation multiplied by the number of animation playback times.<br>- Floating-point values will be rounded down to integers. For example, if the value set is 1.2, **1** will be used.|
33| iterations | number         | No| Number of times that the animation is played. By default, the animation is played once. The value **-1** indicates that the animation is played for an unlimited number of times. The value **0** indicates that there is no animation.<br>Default value: **1**<br>Value range: [-1, +∞)         |
34| playMode   | [PlayMode](ts-appendix-enums.md#playmode)|No| Playback mode. By default, the animation is played from the beginning after the playback is complete.<br>Default value: **PlayMode.Normal**<br>Since API version 9, this API is supported in ArkTS widgets.<br>For details about the restrictions, see **Notes about PlayMode**.|
35| onFinish   | () =&gt; void      | No| Callback invoked when the animation playback is complete.<br>Since API version 9, this API is supported in ArkTS widgets.|
36| finishCallbackType<sup>11+</sup>   | [FinishCallbackType](ts-appendix-enums.md#finishcallbacktype11)|No| Type of the **onFinish** callback.<br>Default value: **FinishCallbackType.REMOVED**<br>Since API version 11, this API is supported in ArkTS widgets.|
37| expectedFrameRateRange<sup>11+</sup>   | [ExpectedFrameRateRange](#expectedframeraterange11) | No| Expected frame rate range of the animation.|
38
39## ExpectedFrameRateRange<sup>11+</sup>
40| Name | Type    | Description     |
41|-----|--------|---------|
42| min | number | Expected minimum frame rate.|
43| max | number | Expected maximum frame rate.|
44| expected | number | Expected optimal frame rate.|
45
46> **Notes about PlayMode**:
47> - **PlayMode.Normal** and **PlayMode.Alternate** are recommended. Under these settings, the first round of the animation is played forwards. If **PlayMode.Reverse** or **PlayMode.AlternateReverse** is used, the first round of the animation is played backwards. In this case, the animation jumps to the end state and then starts from there.
48> - When using **PlayMode.Alternate** or **PlayMode.AlternateReverse**, make sure the final state of the animation is the same as the value of the state variable. In other words, make sure the last round of the animation is played forwards. When **PlayMode.Alternate** is used, **iterations** must be set to an odd number. When **PlayMode.AlternateReverse** is used, **iterations** must be set to an even number.
49> - **PlayMode.Reverse** is not recommended. Under this setting, the animation jumps to the end state at the beginning, and its final state will be different from the value of the state variable.
50
51## Example
52
53```ts
54// xxx.ets
55@Entry
56@Component
57struct AnimateToExample {
58  @State widthSize: number = 250
59  @State heightSize: number = 100
60  @State rotateAngle: number = 0
61  private flag: boolean = true
62
63  build() {
64    Column() {
65      Button('change size')
66        .width(this.widthSize)
67        .height(this.heightSize)
68        .margin(30)
69        .onClick(() => {
70          if (this.flag) {
71            animateTo({
72              duration: 2000,
73              curve: Curve.EaseOut,
74              iterations: 3,
75              playMode: PlayMode.Normal,
76              onFinish: () => {
77                console.info('play end')
78              }
79            }, () => {
80              this.widthSize = 150
81              this.heightSize = 60
82            })
83          } else {
84            animateTo({}, () => {
85              this.widthSize = 250
86              this.heightSize = 100
87            })
88          }
89          this.flag = !this.flag
90        })
91      Button('change rotate angle')
92        .margin(50)
93        .rotate({ x: 0, y: 0, z: 1, angle: this.rotateAngle })
94        .onClick(() => {
95          animateTo({
96            duration: 1200,
97            curve: Curve.Friction,
98            delay: 500,
99            iterations: -1, // The value -1 indicates that the animation is played for an unlimited number of times.
100            playMode: PlayMode.Alternate,
101            onFinish: () => {
102              console.info('play end')
103            },
104            expectedFrameRateRange: {
105              min: 10,
106              max: 120,
107              expected: 60,
108            }
109          }, () => {
110            this.rotateAngle = 90
111          })
112        })
113    }.width('100%').margin({ top: 5 })
114  }
115}
116```
117
118![animation1](figures/animation1.gif)
119