1# Frame Animation 2 3Frame animation involves the use of the **onFrame** callback in applications, which allows you to adjust property values with each frame that is rendered, thereby creating animation effects for the components associated with those property values. For details about the APIs, see [@ohos.animator (Animator)](../reference/apis-arkui/js-apis-animator.md). 4 5 6## Implementing an Animation with animator 7 8To create a simple animator and print the current interpolation value in each frame callback: 9 101. Import dependencies. 11 12 ```ts 13 import { AnimatorOptions, AnimatorResult } from '@kit.ArkUI'; 14 ``` 15 162. Create an animator object. 17 18 ```ts 19 // Initial options for creating an animator object 20 let options: AnimatorOptions = { 21 duration: 1500, 22 easing: "friction", 23 delay: 0, 24 fill: "forwards", 25 direction: "normal", 26 iterations: 2, 27 // Initial frame value used for interpolation in the onFrame callback 28 begin: 200.0, 29 // End frame value used for interpolation in the onFrame callback 30 end: 400.0 31 }; 32 let result: AnimatorResult = this.getUIContext().createAnimator(options); 33 // Set up a callback for when a frame is received, so that the onFrame callback is called for every frame throughout the animation playback process. 34 result.onFrame = (value: number) => { 35 console.log("current value is :" + value); 36 } 37 ``` 38 393. Play the animation. 40 41 ```ts 42 // Play the animation. 43 result.play(); 44 ``` 45 464. After the animation has finished executing, manually release the **AnimatorResult** object. 47 48 ```ts 49 // Play the animation. 50 result = undefined; 51 ``` 52 53## Implementing a Ball's Parabolic Motion with Animator 54 551. Import dependencies. 56 57 ```ts 58 import { AnimatorOptions, AnimatorResult } from '@kit.ArkUI'; 59 ``` 60 612. Define the component to be animated. 62 63 ```ts 64 Button() 65 .width(60) 66 .height(60) 67 .translate({ x: this.translateX, y: this.translateY }) 68 ``` 69 703. Create an **AnimatorResult** Object in **onPageShow**. 71 72 ```ts 73 onPageShow(): void { 74 // Create an animatorResult object. 75 this.animatorOptions = this.getUIContext().createAnimator(options); 76 this.animatorOptions.onFrame = (progress: number) => { 77 this.translateX = progress; 78 if (progress > this.topWidth && this.translateY < this.bottomHeight) { 79 this.translateY = Math.pow(progress - this.topWidth, 2) * this.g; 80 } 81 } 82 // Invoked when the animation is canceled. 83 this.animatorOptions.onCancel = () => { 84 this.animatorStatus = 'Canceled' 85 } 86 // Invoked when the animation finishes playing. 87 this.animatorOptions.onFinish = () => { 88 this.animatorStatus = 'Finished' 89 } 90 // Invoked when the animation repeats. 91 this.animatorOptions.onRepeat = () => { 92 console.log("Animation repeating"); 93 } 94 } 95 ``` 96 974. Define buttons for controlling the animation. 98 99 ```ts 100 Button('Play').onClick(() => { 101 this.animatorOptions?.play(); 102 this.animatorStatus = 'Playing' 103 }).width(80).height(35) 104 Button("Reset").onClick(() => { 105 this.translateX = 0; 106 this.translateY = 0; 107 }).width(80).height(35) 108 Button("Pause").onClick(() => { 109 this.animatorOptions?.pause(); 110 this.animatorStatus = 'Paused' 111 }).width(80).height(35) 112 ``` 1135. Destroy the animation in the page's **onPageHide** lifecycle callback. 114 ```ts 115 onPageHide(): void { 116 this.animatorOptions = undefined; 117 } 118 ``` 119 120A complete example is as follows: 121 122```ts 123import { AnimatorOptions, AnimatorResult } from '@kit.ArkUI'; 124 125@Entry 126@Component 127struct Index { 128 @State animatorOptions: AnimatorResult | undefined = undefined; 129 @State animatorStatus: string =' Created' 130 begin: number = 0; 131 end: number = 300 132 topWidth: number = 150; 133 bottomHeight: number = 100; 134 g: number = 0.18 135 animatorOption: AnimatorOptions = { 136 duration: 4000, 137 delay: 0, 138 easing: 'linear', 139 iterations: 1, 140 fill: "forwards", 141 direction: 'normal', 142 begin: this.begin, 143 end: this.end 144 }; 145 @State translateX: number = 0; 146 @State translateY: number = 0; 147 148 onPageShow(): void { 149 this.animatorOptions = this.getUIContext().createAnimator(this.animatorOption) 150 this.animatorOptions.onFrame = (progress: number) => { 151 this.translateX = progress; 152 if (progress > this.topWidth && this.translateY < this.bottomHeight) { 153 this.translateY = Math.pow(progress - this.topWidth, 2) * this.g; 154 } 155 } 156 this.animatorOptions.onCancel = () => { 157 this.animatorStatus = 'Canceled' 158 } 159 this.animatorOptions.onFinish = () => { 160 this.animatorStatus = 'Finished' 161 } 162 this.animatorOptions.onRepeat = () => { 163 console.log("Animation repeating"); 164 } 165 } 166 167 onPageHide(): void { 168 this.animatorOptions = undefined; 169 } 170 171 build() { 172 Column() { 173 Column({ space: 30 }) { 174 Button('Play').onClick(() => { 175 this.animatorOptions?.play(); 176 this.animatorStatus = 'Playing'; 177 }).width(80).height(35) 178 Button("Reset").onClick(() => { 179 this.translateX = 0; 180 this.translateY = 0; 181 }).width(80).height(35) 182 Button("Pause").onClick(() => { 183 this.animatorOptions?.pause(); 184 this.animatorStatus = 'Paused'; 185 }).width(80).height(35) 186 }.width("100%").height('25%') 187 188 Stack() { 189 Button() 190 .width(60) 191 .height(60) 192 .translate({ x: this.translateX, y: this.translateY }) 193 } 194 .width("100%") 195 .height('45%') 196 .align(Alignment.Start) 197 198 Text("Current animation state: "+ this.animatorStatus) 199 }.width("100%").height('100%') 200 } 201} 202``` 203 204 205