ts-components-canvas-lottie.md 27.3 KB
Newer Older
Z
zengyawen 已提交
1
# Lottie
Z
zengyawen 已提交
2 3


Z
zengyawen 已提交
4 5
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE:**
> This component is supported since API version 8. Updates will be marked with a superscript to indicate their earliest API version.
Z
zengyawen 已提交
6

Z
zengyawen 已提交
7 8

## Required Permissions
Z
zengyawen 已提交
9 10 11 12

None


Z
zengyawen 已提交
13 14 15
## Modules to Import

  
Z
zengyawen 已提交
16
```
Z
zengyawen 已提交
17
import lottie from 'lottie-ohos-ets'
Z
zengyawen 已提交
18 19
```

Z
zengyawen 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE:**
> In the **Terminal** window, run the **npm install'lottie-ohos-ets...** command to download Lottie.


## lottie.loadAnimation

loadAnimation(

path: string, container: object, render: string, loop: boolean, autoplay: boolean, name: string ): AnimationItem

Loads an animation. Before calling this method, declare the **Animator('__lottie_ets')** object and check that the canvas layout is complete. This method can be used together with a lifecycle callback of the **Canvas** component, for example, **onAppear()** and **onPageShow()**.

- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | path | string | Yes | Path of the animation resource file in the HAP file. The resource file must be in JSON format. Example: **path: "common/lottie/data.json"**. | 
  | container | object | Yes | Canvas drawing context. A **CanvasRenderingContext2D** object must be declared in advance. | 
  | render | string | Yes | Rendering type. The value can only be **"canvas"**. | 
  | loop | boolean \| number | No | If the value is of the Boolean type, this parameter indicates whether to repeat the animation cyclically after the animation ends; the default value is **true**. If the value is of the number type and is greater than or equal to 1, this parameter indicates the number of times the animation plays. | 
  | autoplay | boolean | No | Whether to automatically play the animation. The default value is **true**. | 
  | name | string | No | Custom animation name. In later versions, the name can be used to reference and control the animation. The default value is null. | 
  | initialSegment | [number, number] | No | Start frame and end frame of the animation, respectively. | 


## lottie.destroy

destroy(name: string): void

Destroys the animation. This method must be called when a page exits. This method can be used together with a lifecycle callback of the **Canvas** component, for example, **onDisappear()** and **onPageHide()**.

- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | name | string | Yes | Name of the animation to destroy, which is the same as the **name** in the **loadAnimation** interface. By default, all animations are destroyed. | 

- Example
Z
zengyawen 已提交
56
    
Z
zengyawen 已提交
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  ```
  import lottie from 'lottie-web'
  
  @Entry
  @Component
  struct Index {
    private controller: CanvasRenderingContext2D = new CanvasRenderingContext2D()
    private animateName: string = "animate"
    private animatePath: string = "common/lottie/data.json"
    private animateItem: any = null
  
    private onPageHide(): void {
      console.log('onPageHide')
      lottie.destroy()
    }
  
    build() {
      Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
        Canvas(this.controller)
        .width('30%')
        .height('20%')
        .backgroundColor('#0D9FFB')
        .onAppear(() => {
          console.log('canvas onAppear');
          this.animateItem = lottie.loadAnimation({
            container: this.controller,
            renderer: 'canvas',
            loop: true,
            autoplay: true,
            name: this.animateName,
            path: this.animatePath,
Z
zengyawen 已提交
88
          })
Z
zengyawen 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
        })
  
        Animator('__lottie_ets') // declare Animator('__lottie_ets') when use lottie
        Button('load animation')
          .onClick(() => {
          if (this.animateItem != null) {
            this.animateItem.destroy()
            this.animateItem = null
          }
          this.animateItem = lottie.loadAnimation({
            container: this.controller,
            renderer: 'canvas',
            loop: true,
            autoplay: true,
            name: this.animateName,
            path: this.animatePath,
            initialSegment: [10, 50],
          })
        })
  
        Button('destroy animation')
          .onClick(() => {
            lottie.destroy(this.animateName)
            this.animateItem = null
Z
zengyawen 已提交
113 114
          })
      }
Z
zengyawen 已提交
115 116
      .width('100%')
      .height('100%')
Z
zengyawen 已提交
117
    }
Z
zengyawen 已提交
118 119
  }
  ```
Z
zengyawen 已提交
120

Z
zengyawen 已提交
121
  ![en-us_image_0000001256978355](figures/en-us_image_0000001256978355.gif)
Z
zengyawen 已提交
122 123


Z
zengyawen 已提交
124
## lottie.play
Z
zengyawen 已提交
125

Z
zengyawen 已提交
126
play(name: string): void
Z
zengyawen 已提交
127 128 129

Plays a specified animation.

Z
zengyawen 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | name | string | Yes | Name of the animation to play, which is the same as the **name** in the **loadAnimation** interface. By default, all animations are played. | 

- Example
    
  ```
  lottie.play(this.animateName)
  ```


## lottie.pause

pause(name: string): void

Pauses a specified animation. The next time **lottie.play()** is called, the animation starts from the current frame.

- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | name | string | Yes | Name of the animation to pause, which is the same as the **name** in the **loadAnimation** interface. By default, all animations are paused. | 

- Example
    
  ```
  lottie.pause(this.animateName)
  ```


## lottie.togglePause

togglePause(name: string): void

Pauses or plays a specified animation. This method is equivalent to the switching between **lottie.play()** and **lottie.pause()**.

- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | name | string | Yes | Name of the target animation, which is the same as the **name** in the **loadAnimation** interface. By default, all animations are paused. | 

- Example
    
  ```
  lottie.togglePause(this.animateName)
  ```


## lottie.stop

stop(name: string): void

Stops the specified animation. The next time **lottie.play()** is called, the animation starts from the first frame.

- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | name | string | Yes | Name of the target animation, which is the same as the **name** in the **loadAnimation** interface. By default, all animations are paused. | 

- Example
    
  ```
  lottie.stop(this.animateName)
  ```


## lottie.setSpeed

setSpeed(speed: number, name: string): void
Z
zengyawen 已提交
199 200 201

Sets the playback speed of the specified animation.

Z
zengyawen 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | speed | number | Yes | Playback speed. The value is a floating-point number. If the value is greater than 0, the animation plays in forward direction. If the value is less than 0, the animation plays in reversed direction. If the value is 0, the animation is paused. If the value is 1.0 or -1.0, the animation plays at the normal speed. | 
  | name | string | Yes | Name of the target animation, which is the same as the **name** in the **loadAnimation** interface. By default, all animations are stopped. | 

- Example
    
  ```
  lottie.setSpeed(5, this.animateName)
  ```


## lottie.setDirection

setDirection(direction: AnimationDirection, name: string): void

Sets the direction in which the specified animation plays.

- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | direction | AnimationDirection | Yes | Direction&nbsp;in&nbsp;which&nbsp;the&nbsp;animation&nbsp;plays.&nbsp;**1**:&nbsp;forwards;&nbsp;**-1**:&nbsp;backwards.&nbsp;When&nbsp;set&nbsp;to&nbsp;play&nbsp;backwards,&nbsp;the&nbsp;animation&nbsp;plays&nbsp;from&nbsp;the&nbsp;current&nbsp;playback&nbsp;progress&nbsp;to&nbsp;the&nbsp;first&nbsp;frame.&nbsp;When&nbsp;this&nbsp;setting&nbsp;is&nbsp;combined&nbsp;with&nbsp;**loop**&nbsp;being&nbsp;set&nbsp;to&nbsp;**true**,&nbsp;the&nbsp;animation&nbsp;plays&nbsp;backwards&nbsp;continuously.&nbsp;When&nbsp;the&nbsp;value&nbsp;of&nbsp;**speed**&nbsp;is&nbsp;less&nbsp;than&nbsp;0,&nbsp;the&nbsp;animation&nbsp;also&nbsp;plays&nbsp;backwards.<br/>**AnimationDirection**:&nbsp;1&nbsp;\|&nbsp;-1. | 
  | name | string | Yes | Name&nbsp;of&nbsp;the&nbsp;target&nbsp;animation,&nbsp;which&nbsp;is&nbsp;the&nbsp;same&nbsp;as&nbsp;the&nbsp;**name**&nbsp;in&nbsp;the&nbsp;**loadAnimation**&nbsp;interface.&nbsp;By&nbsp;default,&nbsp;all&nbsp;animations&nbsp;are&nbsp;set. | 

- Example
    
  ```
  lottie.setDirection(-1, this.animateName)
  ```


## AnimationItem

Defines an **AnimationItem** object, which is returned by the **loadAnimation** interface and has attributes and interfaces. The attributes are described as follows:

  | Name | Type | Description | 
| -------- | -------- | -------- |
| name | string | Animation&nbsp;name. | 
| isLoaded | boolean | Whether&nbsp;the&nbsp;animation&nbsp;is&nbsp;loaded. | 
| currentFrame | number | Frame&nbsp;that&nbsp;is&nbsp;being&nbsp;played.&nbsp;The&nbsp;default&nbsp;precision&nbsp;is&nbsp;a&nbsp;floating-point&nbsp;number&nbsp;greater&nbsp;than&nbsp;or&nbsp;equal&nbsp;to&nbsp;0.0.&nbsp;After&nbsp;**setSubframe(false)**&nbsp;is&nbsp;called,&nbsp;the&nbsp;value&nbsp;is&nbsp;a&nbsp;positive&nbsp;integer&nbsp;without&nbsp;decimal&nbsp;points. | 
| currentRawFrame | number | Number&nbsp;of&nbsp;frames&nbsp;that&nbsp;are&nbsp;being&nbsp;played.&nbsp;The&nbsp;precision&nbsp;is&nbsp;a&nbsp;floating&nbsp;point&nbsp;number&nbsp;greater&nbsp;than&nbsp;or&nbsp;equal&nbsp;to&nbsp;0.0. | 
| firstFrame | number | First&nbsp;frame&nbsp;of&nbsp;the&nbsp;animation&nbsp;segment&nbsp;that&nbsp;is&nbsp;being&nbsp;played. | 
| totalFrames | number | Total&nbsp;number&nbsp;of&nbsp;frames&nbsp;in&nbsp;the&nbsp;animation&nbsp;segment&nbsp;that&nbsp;is&nbsp;being&nbsp;played. | 
| frameRate | number | Frame&nbsp;rate&nbsp;(frame/s). | 
| frameMult | number | Frame&nbsp;rate&nbsp;(frame/ms). | 
| playSpeed | number | Playback&nbsp;speed.&nbsp;The&nbsp;value&nbsp;is&nbsp;a&nbsp;floating-point&nbsp;number.&nbsp;If&nbsp;the&nbsp;value&nbsp;is&nbsp;greater&nbsp;than&nbsp;0,&nbsp;the&nbsp;animation&nbsp;plays&nbsp;in&nbsp;forward&nbsp;direction.&nbsp;If&nbsp;the&nbsp;value&nbsp;is&nbsp;less&nbsp;than&nbsp;0,&nbsp;the&nbsp;animation&nbsp;plays&nbsp;in&nbsp;reversed&nbsp;direction.&nbsp;If&nbsp;the&nbsp;value&nbsp;is&nbsp;0,&nbsp;the&nbsp;animation&nbsp;is&nbsp;paused.&nbsp;If&nbsp;the&nbsp;value&nbsp;is&nbsp;**1.0**&nbsp;or&nbsp;**-1.0**,&nbsp;the&nbsp;animation&nbsp;plays&nbsp;at&nbsp;the&nbsp;normal&nbsp;speed. | 
| playDirection | number | Playback&nbsp;direction.&nbsp;The&nbsp;options&nbsp;are&nbsp;**1**&nbsp;(forward)&nbsp;and&nbsp;**-1**&nbsp;(backward). | 
| playCount | number | Number&nbsp;of&nbsp;times&nbsp;the&nbsp;animation&nbsp;plays. | 
| isPaused | boolean | Whether&nbsp;the&nbsp;current&nbsp;animation&nbsp;is&nbsp;paused.&nbsp;The&nbsp;value&nbsp;**true**&nbsp;means&nbsp;that&nbsp;the&nbsp;animation&nbsp;is&nbsp;paused. | 
| autoplay | boolean | Whether&nbsp;to&nbsp;automatically&nbsp;play&nbsp;the&nbsp;animation&nbsp;upon&nbsp;completion&nbsp;of&nbsp;the&nbsp;loading.&nbsp;The&nbsp;value&nbsp;**false**&nbsp;means&nbsp;that&nbsp;the&nbsp;**play()**&nbsp;interface&nbsp;needs&nbsp;to&nbsp;be&nbsp;called&nbsp;to&nbsp;start&nbsp;playing. | 
| loop | boolean&nbsp;\|&nbsp;number | If&nbsp;the&nbsp;value&nbsp;is&nbsp;of&nbsp;the&nbsp;Boolean&nbsp;type,&nbsp;this&nbsp;parameter&nbsp;indicates&nbsp;whether&nbsp;to&nbsp;repeat&nbsp;the&nbsp;animation&nbsp;cyclically&nbsp;after&nbsp;the&nbsp;animation&nbsp;ends.&nbsp;If&nbsp;the&nbsp;value&nbsp;is&nbsp;of&nbsp;the&nbsp;number&nbsp;type&nbsp;and&nbsp;is&nbsp;greater&nbsp;than&nbsp;or&nbsp;equal&nbsp;to&nbsp;1,&nbsp;this&nbsp;parameter&nbsp;indicates&nbsp;the&nbsp;number&nbsp;of&nbsp;times&nbsp;the&nbsp;animation&nbsp;plays. | 
| renderer | any | Animation&nbsp;rendering&nbsp;object,&nbsp;which&nbsp;depends&nbsp;on&nbsp;the&nbsp;rendering&nbsp;type. | 
| animationID | string | Animation&nbsp;ID. | 
| timeCompleted | number | Number&nbsp;of&nbsp;frames&nbsp;that&nbsp;are&nbsp;played&nbsp;for&nbsp;an&nbsp;animation&nbsp;sequence.&nbsp;The&nbsp;value&nbsp;is&nbsp;affected&nbsp;by&nbsp;the&nbsp;setting&nbsp;of&nbsp;**AnimationSegment**&nbsp;and&nbsp;is&nbsp;the&nbsp;same&nbsp;as&nbsp;the&nbsp;value&nbsp;of&nbsp;**totalFrames**. | 
| segmentPos | number | ID&nbsp;of&nbsp;the&nbsp;current&nbsp;animation&nbsp;segment.&nbsp;The&nbsp;value&nbsp;is&nbsp;a&nbsp;positive&nbsp;integer&nbsp;greater&nbsp;than&nbsp;or&nbsp;equal&nbsp;to&nbsp;0. | 
| isSubframeEnabled | boolean | Whether&nbsp;the&nbsp;precision&nbsp;of&nbsp;**currentFrame**&nbsp;is&nbsp;a&nbsp;floating&nbsp;point&nbsp;number. | 
| segments | AnimationSegment&nbsp;\|&nbsp;AnimationSegment[] | Current&nbsp;segment&nbsp;of&nbsp;the&nbsp;animation. | 


## AnimationItem.play

play(name?: string): void
Z
zengyawen 已提交
265 266 267

Plays an animation.

Z
zengyawen 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | name | string | No | Name&nbsp;of&nbsp;the&nbsp;target&nbsp;animation.&nbsp;By&nbsp;default,&nbsp;the&nbsp;value&nbsp;is&nbsp;null. | 

- Example
    
  ```
  this.animateItem.play()
  ```


## AnimationItem.destroy

destroy(name?: string): void
Z
zengyawen 已提交
283 284 285

Destroys an animation.

Z
zengyawen 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | name | string | No | Name&nbsp;of&nbsp;the&nbsp;target&nbsp;animation.&nbsp;By&nbsp;default,&nbsp;the&nbsp;value&nbsp;is&nbsp;null. | 

- Example
    
  ```
  this.animateItem.destroy()
  ```


## AnimationItem.pause

pause(name?: string): void

Pauses an animation. When the **play** interface is called next time, the animation is played from the current frame.

- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | name | string | No | Name&nbsp;of&nbsp;the&nbsp;target&nbsp;animation.&nbsp;By&nbsp;default,&nbsp;the&nbsp;value&nbsp;is&nbsp;null. | 

- Example
    
  ```
  this.animateItem.pause()
  ```


## AnimationItem.togglePause

togglePause(name?: string): void

Pauses or plays an animation. This method is equivalent to the switching between **play** and **pause**.

- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | name | string | No | Name&nbsp;of&nbsp;the&nbsp;target&nbsp;animation.&nbsp;By&nbsp;default,&nbsp;the&nbsp;value&nbsp;is&nbsp;null. | 

- Example
    
  ```
  this.animateItem.togglePause()
  ```


## AnimationItem.stop

stop(name?: string): void

Stops an animation. When the **play** interface is called next time, the animation is played from the first frame.

- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | name | string | No | Name&nbsp;of&nbsp;the&nbsp;target&nbsp;animation.&nbsp;By&nbsp;default,&nbsp;the&nbsp;value&nbsp;is&nbsp;null. | 

- Example
    
  ```
  this.animateItem.stop()
  ```


## AnimationItem.setSpeed

setSpeed(speed: number): void
Z
zengyawen 已提交
355 356 357

Sets the playback speed of an animation.

Z
zengyawen 已提交
358 359 360 361 362 363 364 365 366 367
- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | speed | number | Yes | Playback&nbsp;speed.&nbsp;The&nbsp;value&nbsp;is&nbsp;a&nbsp;floating-point&nbsp;number.&nbsp;If&nbsp;the&nbsp;value&nbsp;is&nbsp;greater&nbsp;than&nbsp;0,&nbsp;the&nbsp;animation&nbsp;plays&nbsp;in&nbsp;forward&nbsp;direction.&nbsp;If&nbsp;the&nbsp;value&nbsp;is&nbsp;less&nbsp;than&nbsp;0,&nbsp;the&nbsp;animation&nbsp;plays&nbsp;in&nbsp;reversed&nbsp;direction.&nbsp;If&nbsp;the&nbsp;value&nbsp;is&nbsp;0,&nbsp;the&nbsp;animation&nbsp;is&nbsp;paused.&nbsp;If&nbsp;the&nbsp;value&nbsp;is&nbsp;**1.0**&nbsp;or&nbsp;**-1.0**,&nbsp;the&nbsp;animation&nbsp;plays&nbsp;at&nbsp;the&nbsp;normal&nbsp;speed. | 

- Example
    
  ```
  this.animateItem.setSpeed(5);
  ```
Z
zengyawen 已提交
368 369


Z
zengyawen 已提交
370
## AnimationItem.setDirection
Z
zengyawen 已提交
371

Z
zengyawen 已提交
372
setDirection(direction: AnimationDirection): void
Z
zengyawen 已提交
373

Z
zengyawen 已提交
374
Sets the playback direction of an animation.
Z
zengyawen 已提交
375

Z
zengyawen 已提交
376 377 378 379
- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | direction | AnimationDirection | Yes | Direction&nbsp;in&nbsp;which&nbsp;the&nbsp;animation&nbsp;plays.&nbsp;**1**:&nbsp;forwards;&nbsp;**-1**:&nbsp;backwards.&nbsp;When&nbsp;set&nbsp;to&nbsp;play&nbsp;backwards,&nbsp;the&nbsp;animation&nbsp;plays&nbsp;from&nbsp;the&nbsp;current&nbsp;playback&nbsp;progress&nbsp;to&nbsp;the&nbsp;first&nbsp;frame.&nbsp;When&nbsp;this&nbsp;setting&nbsp;is&nbsp;combined&nbsp;with&nbsp;**loop**&nbsp;being&nbsp;set&nbsp;to&nbsp;**true**,&nbsp;the&nbsp;animation&nbsp;plays&nbsp;backwards&nbsp;continuously.&nbsp;When&nbsp;the&nbsp;value&nbsp;of&nbsp;**speed**&nbsp;is&nbsp;less&nbsp;than&nbsp;0,&nbsp;the&nbsp;animation&nbsp;also&nbsp;plays&nbsp;backwards.<br/>**AnimationDirection**:&nbsp;1&nbsp;\|&nbsp;-1. | 
Z
zengyawen 已提交
380

Z
zengyawen 已提交
381 382 383 384 385
- Example
    
  ```
  this.animateItem.setDirection(-1)
  ```
Z
zengyawen 已提交
386 387


Z
zengyawen 已提交
388
## AnimationItem.goToAndStop
Z
zengyawen 已提交
389

Z
zengyawen 已提交
390
goToAndStop(value: number, isFrame: boolean): void
Z
zengyawen 已提交
391 392 393

Sets the animation to stop at the specified frame or time.

Z
zengyawen 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | value | number | Yes | Frame&nbsp;ID&nbsp;(greater&nbsp;than&nbsp;or&nbsp;equal&nbsp;to&nbsp;0)&nbsp;or&nbsp;time&nbsp;progress&nbsp;(ms)&nbsp;at&nbsp;which&nbsp;the&nbsp;animation&nbsp;will&nbsp;stop. | 
  | isFrame | boolean | No | Whether&nbsp;to&nbsp;set&nbsp;the&nbsp;animation&nbsp;to&nbsp;stop&nbsp;at&nbsp;the&nbsp;specified&nbsp;frame.&nbsp;The&nbsp;value&nbsp;**true**&nbsp;means&nbsp;to&nbsp;set&nbsp;the&nbsp;animation&nbsp;to&nbsp;stop&nbsp;at&nbsp;the&nbsp;specified&nbsp;frame,&nbsp;and&nbsp;**false**&nbsp;means&nbsp;to&nbsp;set&nbsp;the&nbsp;animation&nbsp;to&nbsp;stop&nbsp;at&nbsp;the&nbsp;specified&nbsp;time&nbsp;progress.&nbsp;The&nbsp;default&nbsp;value&nbsp;is&nbsp;**false**. | 
  | name | string | No | Name&nbsp;of&nbsp;the&nbsp;target&nbsp;animation.&nbsp;By&nbsp;default,&nbsp;the&nbsp;value&nbsp;is&nbsp;null. | 

- Example
    
  ```
  // Set the animation to stop at the specified frame.
  this.animateItem.goToAndStop(25, true)
  // Set the animation to stop at the specified time progress.
  this.animateItem.goToAndStop(300, false, this.animateName)
  ```


## AnimationItem.goToAndPlay

goToAndPlay(value: number, isFrame: boolean): void
Z
zengyawen 已提交
414 415 416

Sets the animation to start from the specified frame or time progress.

Z
zengyawen 已提交
417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | value | number | Yes | Frame&nbsp;ID&nbsp;(greater&nbsp;than&nbsp;or&nbsp;equal&nbsp;to&nbsp;0)&nbsp;or&nbsp;time&nbsp;progress&nbsp;(ms)&nbsp;at&nbsp;which&nbsp;the&nbsp;animation&nbsp;will&nbsp;start. | 
  | isFrame | boolean | Yes | Whether&nbsp;to&nbsp;set&nbsp;the&nbsp;animation&nbsp;to&nbsp;start&nbsp;from&nbsp;the&nbsp;specified&nbsp;frame.&nbsp;The&nbsp;value&nbsp;**true**&nbsp;means&nbsp;to&nbsp;set&nbsp;the&nbsp;animation&nbsp;to&nbsp;start&nbsp;from&nbsp;the&nbsp;specified&nbsp;frame,&nbsp;and&nbsp;**false**&nbsp;means&nbsp;to&nbsp;set&nbsp;the&nbsp;animation&nbsp;to&nbsp;start&nbsp;from&nbsp;the&nbsp;specified&nbsp;time&nbsp;progress.&nbsp;The&nbsp;default&nbsp;value&nbsp;is&nbsp;**false**. | 
  | name | string | No | Name&nbsp;of&nbsp;the&nbsp;target&nbsp;animation.&nbsp;By&nbsp;default,&nbsp;the&nbsp;value&nbsp;is&nbsp;null. | 

- Example
    
  ```
  // Set the animation to stop at the specified frame.
  this.animateItem.goToAndPlay(25, true)
  // Set the animation to stop at the specified time progress.
  this.animateItem.goToAndPlay(300, false, this.animateName)
  ```


## AnimationItem.playSegments

playSegments(segments: AnimationSegment | AnimationSegment[], forceFlag: boolean): void
Z
zengyawen 已提交
437 438 439

Sets the animation to play only the specified segment.

Z
zengyawen 已提交
440 441 442 443 444
- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | segments | AnimationSegment&nbsp;=&nbsp;[number,&nbsp;number]&nbsp;\|&nbsp;AnimationSegment[] | Yes | Segment&nbsp;or&nbsp;segment&nbsp;list.<br/>If&nbsp;all&nbsp;segments&nbsp;in&nbsp;the&nbsp;segment&nbsp;list&nbsp;are&nbsp;played,&nbsp;only&nbsp;the&nbsp;last&nbsp;segment&nbsp;is&nbsp;played&nbsp;in&nbsp;the&nbsp;next&nbsp;cycle. | 
  | forceFlag | boolean | Yes | Whether&nbsp;the&nbsp;settings&nbsp;take&nbsp;effect&nbsp;immediately.&nbsp;The&nbsp;value&nbsp;**true**&nbsp;means&nbsp;the&nbsp;settings&nbsp;take&nbsp;effect&nbsp;immediately,&nbsp;and&nbsp;**false**&nbsp;means&nbsp;the&nbsp;settings&nbsp;take&nbsp;effect&nbsp;until&nbsp;the&nbsp;current&nbsp;cycle&nbsp;of&nbsp;playback&nbsp;is&nbsp;completed. | 
Z
zengyawen 已提交
445

Z
zengyawen 已提交
446 447 448 449 450 451 452 453
- Example
    
  ```
  // Set the animation to play the specified segment.
  this.animateItem.playSegments([10, 20], false)
  // Set the animation to play the specified segment list.
  this.animateItem.playSegments([[0, 5], [20, 30]], true)
  ```
Z
zengyawen 已提交
454 455


Z
zengyawen 已提交
456
## AnimationItem.resetSegments
Z
zengyawen 已提交
457

Z
zengyawen 已提交
458
resetSegments(forceFlag: boolean): void
Z
zengyawen 已提交
459

Z
zengyawen 已提交
460
Resets the settings configured by the **playSegments** method to play all the frames.
Z
zengyawen 已提交
461

Z
zengyawen 已提交
462 463 464 465 466 467 468 469 470 471
- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | forceFlag | boolean | Yes | Whether&nbsp;the&nbsp;settings&nbsp;take&nbsp;effect&nbsp;immediately.&nbsp;The&nbsp;value&nbsp;**true**&nbsp;means&nbsp;the&nbsp;settings&nbsp;take&nbsp;effect&nbsp;immediately,&nbsp;and&nbsp;**false**&nbsp;means&nbsp;the&nbsp;settings&nbsp;take&nbsp;effect&nbsp;until&nbsp;the&nbsp;current&nbsp;cycle&nbsp;of&nbsp;playback&nbsp;is&nbsp;completed. | 

- Example
    
  ```
  this.animateItem.resetSegments(true)
  ```
Z
zengyawen 已提交
472 473


Z
zengyawen 已提交
474 475 476 477 478 479 480 481 482 483 484
## AnimationItem.resize

resize(): void

Resizes the animation layout.

- Example
    
  ```
  this.animateItem.resize()
  ```
Z
zengyawen 已提交
485 486


Z
zengyawen 已提交
487
## AnimationItem.setSubframe
Z
zengyawen 已提交
488

Z
zengyawen 已提交
489
setSubframe(useSubFrame: boolean): void
Z
zengyawen 已提交
490

Z
zengyawen 已提交
491
Sets the precision of the **currentFrame** attribute to display floating-point numbers.
Z
zengyawen 已提交
492

Z
zengyawen 已提交
493 494 495 496
- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | useSubFrames | boolean | Yes | Whether&nbsp;the&nbsp;**currentFrame**&nbsp;attribute&nbsp;displays&nbsp;floating-point&nbsp;numbers.&nbsp;By&nbsp;default,&nbsp;the&nbsp;attribute&nbsp;displays&nbsp;floating-point&nbsp;numbers.<br/>**true**:&nbsp;The&nbsp;**currentFrame**&nbsp;attribute&nbsp;displays&nbsp;floating-point&nbsp;numbers.<br/>**false**:&nbsp;The&nbsp;**currentFrame**&nbsp;attribute&nbsp;displays&nbsp;an&nbsp;integer&nbsp;and&nbsp;does&nbsp;not&nbsp;display&nbsp;floating-point&nbsp;numbers. | 
Z
zengyawen 已提交
497

Z
zengyawen 已提交
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
- Example
    
  ```
  this.animateItem.setSubframe(false)
  ```


## AnimationItem.getDuration

getDuration(inFrames?: boolean): void

Obtains the duration (irrelevant to the playback speed) or number of frames for playing an animation sequence. The settings are related to the input parameter **initialSegment** of the **Lottie.loadAnimation** interface.

- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | inFrames | boolean | No | Whether&nbsp;to&nbsp;obtain&nbsp;the&nbsp;duration&nbsp;or&nbsp;number&nbsp;of&nbsp;frames.&nbsp;**true**:&nbsp;number&nbsp;of&nbsp;frames.&nbsp;**false**:&nbsp;duration,&nbsp;in&nbsp;ms.&nbsp;The&nbsp;default&nbsp;value&nbsp;is&nbsp;**false**. | 

- Example
    
  ```
  this.animateItem.getDuration(true)
  ```
Z
zengyawen 已提交
521

Z
zengyawen 已提交
522 523 524 525

## AnimationItem.addEventListener

addEventListener&lt;T = any&gt;(name: AnimationEventName, callback: AnimationEventCallback&lt;T&gt;): () =&gt; void
Z
zengyawen 已提交
526 527 528

Adds an event listener. After the event is complete, the specified callback function is triggered. This method returns the function object that can delete the event listener.

Z
zengyawen 已提交
529 530 531 532 533 534 535
- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | name | AnimationEventName | Yes | Animation&nbsp;event&nbsp;type.&nbsp;The&nbsp;available&nbsp;options&nbsp;are&nbsp;as&nbsp;follows:<br/>'enterFrame',&nbsp;'loopComplete',&nbsp;'complete',&nbsp;'segmentStart',&nbsp;'destroy',&nbsp;'config_ready',&nbsp;'data_ready',&nbsp;'DOMLoaded',&nbsp;'error',&nbsp;'data_failed',&nbsp;'loaded_images' | 
  | callback | AnimationEventCallback&lt;T&gt; | Yes | Custom&nbsp;callback. | 

- Example
Z
zengyawen 已提交
536
    
Z
zengyawen 已提交
537 538 539 540 541 542 543 544 545
  ```
  private callbackItem: any = function() {
      console.log("grunt loopComplete")
  }
  let delFunction = this.animateItem.addEventListener('loopComplete', this.animateName)
  
  // Delete the event listener.
  delFunction()
  ```
Z
zengyawen 已提交
546 547


Z
zengyawen 已提交
548 549 550 551 552 553 554 555 556 557 558 559 560
## AnimationItem.removeEventListener

removeEventListener&lt;T = any&gt;(name: AnimationEventName, callback?: AnimationEventCallback&lt;T&gt;): void

Removes an event listener.

- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | name | AnimationEventName | Yes | Animation&nbsp;event&nbsp;type.&nbsp;The&nbsp;available&nbsp;options&nbsp;are&nbsp;as&nbsp;follows:<br/>'enterFrame',&nbsp;'loopComplete',&nbsp;'complete',&nbsp;'segmentStart',&nbsp;'destroy',&nbsp;'config_ready',&nbsp;'data_ready',&nbsp;'DOMLoaded',&nbsp;'error',&nbsp;'data_failed',&nbsp;'loaded_images' | 
  | callback | AnimationEventCallback&lt;T&gt; | Yes | Custom&nbsp;callback.&nbsp;By&nbsp;default,&nbsp;the&nbsp;value&nbsp;is&nbsp;null,&nbsp;meaning&nbsp;that&nbsp;all&nbsp;callbacks&nbsp;of&nbsp;the&nbsp;event&nbsp;will&nbsp;be&nbsp;removed. | 

- Example
Z
zengyawen 已提交
561
    
Z
zengyawen 已提交
562 563 564 565 566 567
  ```
  this.animateItem.removeEventListener('loopComplete', this.animateName)
  ```


## AnimationItem.triggerEvent
Z
zengyawen 已提交
568

Z
zengyawen 已提交
569
triggerEvent&lt;T = any&gt;(name: AnimationEventName, args: T): void
Z
zengyawen 已提交
570

Z
zengyawen 已提交
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589
Directly triggers all configured callbacks of a specified event.

- Parameters
    | Name | Type | Mandatory | Description | 
  | -------- | -------- | -------- | -------- |
  | name | AnimationEventName | Yes | Animation&nbsp;event&nbsp;type. | 
  | args | any | Yes | Custom&nbsp;callback&nbsp;parameters. | 

- Example
    
  ```
  private triggerCallBack: any = function(item) {
      console.log("trigger loopComplete, name:" + item.name)
  }
  
  this.animateItem.addEventListener('loopComplete', this.triggerCallBack)
  this.animateItem.triggerEvent('loopComplete', this.animateItem)
  this.animateItem.removeEventListener('loopComplete', this.triggerCallBack)
  ```