1# @ohos.animator (Animator) 2 3The **Animator** module provides APIs for applying animation effects, including defining animations, starting animations, and playing animations in reverse order. 4 5> **NOTE** 6> 7> The initial APIs of this module are supported since API version 6. Newly added APIs will be marked with a superscript to indicate their earliest API version. 8> 9> This module cannot be used in the file declaration of the [UIAbility](./js-apis-app-ability-uiAbility.md). In other words, the APIs of this module can be used only after a component instance is created; they cannot be called in the lifecycle of the UIAbility. 10> 11> 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](./js-apis-arkui-UIContext.md#uicontext). 12> 13> Since API version 10, you can use the [createAnimator](./js-apis-arkui-UIContext.md#createanimator) API in [UIContext](./js-apis-arkui-UIContext.md#uicontext) to obtain the UI context. 14 15## Modules to Import 16 17```ts 18import animator, { AnimatorOptions,AnimatorResult } from '@ohos.animator'; 19import { BusinessError } from '@ohos.base'; 20``` 21## create<sup>9+</sup> 22 23create(options: AnimatorOptions): AnimatorResult 24 25Creates an **Animator** object. 26 27**System capability**: SystemCapability.ArkUI.ArkUI.Full 28 29**Parameters** 30 31| Name | Type | Mandatory | Description | 32| ------- | ----------------------------------- | ---- | ------- | 33| options | [AnimatorOptions](#animatoroptions) | Yes | Animator options.| 34 35**Return value** 36 37| Type | Description | 38| --------------------------------- | ------------- | 39| [AnimatorResult](#animatorresult) | Animator result.| 40 41**Example** 42 43 ```ts 44import animator, { AnimatorOptions,AnimatorResult } from '@ohos.animator'; 45let options: AnimatorOptions = { 46 duration: 1500, 47 easing: "friction", 48 delay: 0, 49 fill: "forwards", 50 direction: "normal", 51 iterations: 3, 52 begin: 200.0, 53 end: 400.0 54}; 55animator.create(options); 56 ``` 57 58## AnimatorResult 59 60Defines the animator result. 61 62### reset<sup>9+</sup> 63 64reset(options: AnimatorOptions): void 65 66Updates this animator. 67 68**System capability**: SystemCapability.ArkUI.ArkUI.Full 69 70**Parameters** 71 72| Name | Type | Mandatory | Description | 73| ------- | ----------------------------------- | ---- | ------- | 74| options | [AnimatorOptions](#animatoroptions) | Yes | Animator options.| 75 76**Error codes** 77 78For details about the error codes, see [Animator Error Codes](../errorcodes/errorcode-animator.md). 79 80| ID | Error Message| 81| --------- | ------- | 82| 100001 | if no page is found for pageId or fail to get object property list. | 83 84 85**Example** 86 87```ts 88import animator, { AnimatorOptions,AnimatorResult } from '@ohos.animator'; 89import { BusinessError } from '@ohos.base'; 90let options: AnimatorOptions = { 91 duration: 1500, 92 easing: "friction", 93 delay: 0, 94 fill: "forwards", 95 direction: "normal", 96 iterations: 3, 97 begin: 200.0, 98 end: 400.0 99}; 100try { 101 animator.reset(options); 102} catch(error) { 103 let message = (error as BusinessError).message 104 let code = (error as BusinessError).code 105 console.error(`Animator reset failed, error code: ${code}, message: ${message}.`); 106} 107``` 108 109### play 110 111play(): void 112 113Plays this animation. The animation retains the previous playback state. For example, if the animation is set to **reverse** and paused, it will remain in **reverse** when resumed. 114 115**System capability**: SystemCapability.ArkUI.ArkUI.Full 116 117**Example** 118 119```ts 120animator.play(); 121``` 122 123### finish 124 125finish(): void 126 127Ends this animation. 128 129**System capability**: SystemCapability.ArkUI.ArkUI.Full 130 131**Example** 132 133```ts 134animator.finish(); 135``` 136 137### pause 138 139pause(): void 140 141Pauses this animation. 142 143**System capability**: SystemCapability.ArkUI.ArkUI.Full 144 145**Example** 146 147```ts 148animator.pause(); 149``` 150 151### cancel 152 153cancel(): void 154 155Cancels this animation. 156 157**System capability**: SystemCapability.ArkUI.ArkUI.Full 158 159**Example** 160 161```ts 162animator.cancel(); 163``` 164 165### reverse 166 167reverse(): void 168 169Plays this animation in reverse order. 170 171**System capability**: SystemCapability.ArkUI.ArkUI.Full 172 173**Example** 174 175```ts 176animator.reverse(); 177``` 178 179### onframe 180 181onframe: (progress: number) => void 182 183Called when a frame is received. 184 185**System capability**: SystemCapability.ArkUI.ArkUI.Full 186 187**Parameters** 188 189| Name | Type | Mandatory | Description | 190| -------- | ------ | ---- | -------- | 191| progress | number | Yes | Current progress of the animation.| 192 193**Example** 194 195```ts 196import animator, { AnimatorResult } from '@ohos.animator'; 197let animatorResult:AnimatorResult|undefined = animator.create(options) 198animatorResult.onframe = (value)=> { 199 console.info("onframe callback") 200} 201``` 202 203### onfinish 204 205onfinish: () => void 206 207Called when this animation is finished. 208 209**System capability**: SystemCapability.ArkUI.ArkUI.Full 210 211**Example** 212 213```ts 214import animator, { AnimatorResult } from '@ohos.animator'; 215let animatorResult:AnimatorResult|undefined = animator.create(options) 216animatorResult.onfinish = ()=> { 217 console.info("onfinish callback") 218} 219``` 220 221### oncancel 222 223oncancel: () => void 224 225Called when this animation is canceled. 226 227**System capability**: SystemCapability.ArkUI.ArkUI.Full 228 229**Example** 230 231```ts 232import animator, { AnimatorResult } from '@ohos.animator'; 233let animatorResult:AnimatorResult|undefined = animator.create(options) 234animatorResult.oncancel = ()=> { 235 console.info("oncancel callback") 236} 237``` 238 239### onrepeat 240 241onrepeat: () => void 242 243Called when this animation repeats. 244 245**System capability**: SystemCapability.ArkUI.ArkUI.Full 246 247**Example** 248 249```ts 250import animator, { AnimatorResult } from '@ohos.animator'; 251let animatorResult:AnimatorResult|undefined = animator.create(options) 252animatorResult.onrepeat = ()=> { 253 console.info("onrepeat callback") 254} 255``` 256 257 258 259## AnimatorOptions 260 261Defines animator options. 262 263**System capability**: SystemCapability.ArkUI.ArkUI.Full 264 265| Name | Type | Mandatory| Description | 266| ---------- | ----------------------------------------------------------- | ---- | ------------------------------------------------------------ | 267| duration | number | Yes | Duration for playing the animation, in milliseconds. | 268| easing | string | Yes | Animation interpolation curve. Only the following values are supported:<br>**"linear"**: The animation speed keeps unchanged.<br>**"ease"**: The animation starts slowly, accelerates, and then slows down towards the end. The cubic-bezier curve (0.25, 0.1, 0.25, 1.0) is used.<br>**"ease-in"**: The animation starts at a low speed and then picks up speed until the end. The cubic-bezier curve (0.42, 0.0, 1.0, 1.0) is used.<br>**"ease-out"**: The animation ends at a low speed. The cubic-bezier curve (0.0, 0.0, 0.58, 1.0) is used.<br>**"ease-in-out"**: The animation starts and ends at a low speed. The cubic-bezier curve (0.42, 0.0, 0.58, 1.0) is used.<br>**"fast-out-slow-in"**: The animation uses the standard cubic-bezier curve (0.4, 0.0, 0.2, 1.0).<br>**"linear-out-slow-in"**: The animation uses the deceleration cubic-bezier curve (0.0, 0.0, 0.2, 1.0).<br>**"friction"**: The animation uses the damping cubic-bezier curve (0.2, 0.0, 0.2, 1.0).<br>**"extreme-deceleration"**: The animation uses the extreme deceleration cubic-bezier curve (0.0, 0.0, 0.0, 1.0).<br>**"rhythm"**: The animation uses the rhythm cubic-bezier curve (0.7, 0.0, 0.2, 1.0).<br>**"sharp"**: The animation uses the sharp cubic-bezier curve (0.33, 0.0, 0.67, 1.0).<br>**"smooth"**: The animation uses the smooth cubic-bezier curve (0.4, 0.0, 0.4, 1.0).<br>**"cubic-bezier(x1,y1,x2,y2)"**: The animation uses the defined cubic bezier curve, where the value of **x1** and **x2** must range from 0 to 1. For example, **"cubic-bezier(0.42,0.0,0.58,1.0)"**.<br>**"steps(number,step-position)"**: The animation uses a step curve. The **number** parameter is mandatory and must be set to a positive integer. The **step-position** parameter is optional and can be set to **start** or **end** (default value). For example, **"steps(3,start)"**.| 269| delay | number | Yes | Animation delay duration, in milliseconds. Value **0** means that there is no delay. | 270| fill | "none" \| "forwards" \| "backwards" \| "both" | Yes | State of the animated target after the animation is executed.<br>**"none"**: No style is applied to the target before or after the animation is executed.<br>**"forwards"**: The target keeps the state at the end of the animation (defined in the last key frame) after the animation is executed.<br>**"backwards"**: The animation uses the value defined in the first key frame during the **animation-delay**. When **animation-direction** is set to **normal** or **alternate**, the value in the **from** key frame is used. When **animation-direction** is set to **reverse** or **alternate-reverse**, the value in the **to** key frame is used.<br>**"both"**: The animation follows the **forwards** and **backwards** rules.| 271| direction | "normal" \| "reverse" \| "alternate" \| "alternate-reverse" | Yes | Animation playback mode.<br>**"normal"**: plays the animation in forward loop mode.<br>**"reverse"**: plays the animation in reverse loop mode.<br>**"alternate"**: plays the animation in alternating loop mode. When the animation is played for an odd number of times, the playback is in forward direction. When the animation is played for an even number of times, the playback is in reverse direction.<br>**"alternate-reverse"**: plays the animation in reverse alternating loop mode. When the animation is played for an odd number of times, the playback is in reverse direction. When the animation is played for an even number of times, the playback is in forward direction.| 272| iterations | number | Yes | Number of times that the animation is played. The value **0** means not to play the animation, and **-1** means to play the animation for an unlimited number of times.<br>**NOTE**<br>If this parameter is set to a negative value other than **-1**, the value is invalid. In this case, the animation is played once.| 273| begin | number | Yes | Start point of the animation interpolation. | 274| end | number | Yes | End point of animation interpolation. | 275 276 277## Example 278### JavaScript-compatible Web-like Development Paradigm 279 280```html 281<!-- hml --> 282<div class="container"> 283 <div class="Animation" style="height: {{divHeight}}px; width: {{divWidth}}px; background-color: red;" onclick="Show"> 284 </div> 285</div> 286``` 287 288```ts 289import animator, { AnimatorOptions,AnimatorResult } from '@ohos.animator'; 290import { BusinessError } from '@ohos.base'; 291let DataTmp:Record<string,animator> = { 292 'divWidth': 200, 293 'divHeight': 200, 294 'animator': animator 295} 296class Tmp{ 297 data:animator = DataTmp 298 onInit:Function = ()=>{} 299 Show:Function = ()=>{} 300} 301class DateT{ 302 divWidth:number = 0 303 divHeight:number = 0 304 animator:AnimatorResult | null = null 305} 306(Fn:(v:Tmp) => void) => {Fn({ 307 data: DataTmp, 308 onInit() { 309 let options:AnimatorOptions = { 310 duration: 1500, 311 easing: "friction", 312 delay: 0, 313 fill: "forwards", 314 direction: "normal", 315 iterations: 2, 316 begin: 200.0, 317 end: 400.0 318 }; 319 let DataTmp:DateT = { 320 divWidth: 200, 321 divHeight: 200, 322 animator: null 323 } 324 DataTmp.animator = animator.create(options); 325 }, 326 Show() { 327 let options1:AnimatorOptions = { 328 duration: 1500, 329 easing: "friction", 330 delay: 0, 331 fill: "forwards", 332 direction: "normal", 333 iterations: 2, 334 begin: 0, 335 end: 400.0, 336 }; 337 let DataTmp:DateT = { 338 divWidth: 200, 339 divHeight: 200, 340 animator: null 341 } 342 try { 343 DataTmp.animator = animator.create(options1); 344 DataTmp.animator.reset(options1); 345 } catch(error) { 346 let message = (error as BusinessError).message 347 let code = (error as BusinessError).code 348 console.error(`Animator reset failed, error code: ${code}, message: ${message}.`); 349 } 350 let _this = DataTmp; 351 if(DataTmp.animator){ 352 DataTmp.animator.onframe = (value:number)=> { 353 _this.divWidth = value; 354 _this.divHeight = value; 355 }; 356 DataTmp.animator.play(); 357 } 358 } 359})} 360``` 361 362  363 364### ArkTS-based Declarative Development Paradigm 365 366```ts 367import animator, { AnimatorResult } from '@ohos.animator'; 368 369@Entry 370@Component 371struct AnimatorTest { 372 private TAG: string = '[AnimatorTest]' 373 private backAnimator: AnimatorResult | undefined = undefined 374 private flag: boolean = false 375 @State wid: number = 100 376 @State hei: number = 100 377 378 create() { 379 let _this = this 380 this.backAnimator = animator.create({ 381 duration: 2000, 382 easing: "ease", 383 delay: 0, 384 fill: "none", 385 direction: "normal", 386 iterations: 1, 387 begin: 100, 388 end: 200 389 }) 390 this.backAnimator.onfinish = ()=> { 391 _this.flag = true 392 console.info(_this.TAG, 'backAnimator onfinish') 393 } 394 this.backAnimator.onrepeat = ()=> { 395 console.info(_this.TAG, 'backAnimator repeat') 396 } 397 this.backAnimator.oncancel = ()=> { 398 console.info(_this.TAG, 'backAnimator cancel') 399 } 400 this.backAnimator.onframe = (value:number)=> { 401 _this.wid = value 402 _this.hei = value 403 } 404 } 405 406 build() { 407 Column() { 408 Column() { 409 Column() 410 .width(this.wid) 411 .height(this.hei) 412 .backgroundColor(Color.Red) 413 } 414 .width('100%') 415 .height(300) 416 417 Column() { 418 Row() { 419 Button('create') 420 .fontSize(30) 421 .fontColor(Color.Black) 422 .onClick(() => { 423 this.create() 424 }) 425 } 426 .padding(10) 427 428 Row() { 429 Button('play') 430 .fontSize(30) 431 .fontColor(Color.Black) 432 .onClick(() => { 433 this.flag = false 434 if(this.backAnimator){ 435 this.backAnimator.play() 436 } 437 }) 438 } 439 .padding(10) 440 441 Row() { 442 Button('pause') 443 .fontSize(30) 444 .fontColor(Color.Black) 445 .onClick(() => { 446 if(this.backAnimator){ 447 this.backAnimator.pause() 448 } 449 }) 450 } 451 .padding(10) 452 453 Row() { 454 Button('finish') 455 .fontSize(30) 456 .fontColor(Color.Black) 457 .onClick(() => { 458 this.flag = true 459 if(this.backAnimator){ 460 this.backAnimator.finish() 461 } 462 }) 463 } 464 .padding(10) 465 466 Row() { 467 Button('reverse') 468 .fontSize(30) 469 .fontColor(Color.Black) 470 .onClick(() => { 471 this.flag = false 472 if(this.backAnimator){ 473 this.backAnimator.reverse() 474 } 475 }) 476 } 477 .padding(10) 478 479 Row() { 480 Button('cancel') 481 .fontSize(30) 482 .fontColor(Color.Black) 483 .onClick(() => { 484 if(this.backAnimator){ 485 this.backAnimator.cancel() 486 } 487 }) 488 } 489 .padding(10) 490 491 Row() { 492 Button('reset') 493 .fontSize(30) 494 .fontColor(Color.Black) 495 .onClick(() => { 496 if (this.flag) { 497 this.flag = false 498 if(this.backAnimator){ 499 this.backAnimator.reset({ 500 duration: 5000, 501 easing: "ease-in", 502 delay: 0, 503 fill: "none", 504 direction: "normal", 505 iterations: 4, 506 begin: 100, 507 end: 300 508 }) 509 } 510 } else { 511 console.info(this.TAG, 'Animation not ended') 512 } 513 }) 514 } 515 .padding(10) 516 } 517 } 518 } 519} 520``` 521 522## update<sup>(deprecated)</sup> 523 524update(options: AnimatorOptions): void 525 526Updates this animator. 527 528This API is deprecated since API version 9. You are advised to use [reset<sup>9+</sup>](#reset9) instead. 529 530**System capability**: SystemCapability.ArkUI.ArkUI.Full 531 532**Parameters** 533 534| Name | Type | Mandatory | Description | 535| ------- | ----------------------------------- | ---- | ------- | 536| options | [AnimatorOptions](#animatoroptions) | Yes | Animator options.| 537 538**Example** 539 540```ts 541animator.update(options); 542``` 543 544## createAnimator<sup>(deprecated)</sup> 545 546createAnimator(options: AnimatorOptions): AnimatorResult 547 548Creates an **Animator** object. 549 550This API is deprecated since API version 9. You are advised to use [create<sup>9+</sup>](#create9) instead. 551 552**System capability**: SystemCapability.ArkUI.ArkUI.Full 553 554**Parameters** 555 556| Name | Type | Mandatory | Description | 557| ------- | ----------------------------------- | ---- | ------- | 558| options | [AnimatorOptions](#animatoroptions) | Yes | Animator options.| 559 560**Return value** 561 562| Type | Description | 563| --------------------------------- | ------------- | 564| [AnimatorResult](#animatorresult) | Animator result.| 565 566**Example** 567 568```ts 569import animator, { AnimatorOptions,AnimatorResult } from '@ohos.animator'; 570let options: AnimatorOptions = { // The explicit type AnimatorOptions does not need to be emphasized in the xxx.js file. 571 duration: 1500, 572 easing: "friction", 573 delay: 0, 574 fill: "forwards", 575 direction: "normal", 576 iterations: 3, 577 begin: 200.0, 578 end: 400.0, 579}; 580this.animator = animator.createAnimator(options); 581``` 582