js-apis-animator.md 7.7 KB
Newer Older
1
# Animation
Z
zengyawen 已提交
2

3 4
>![](../../public_sys-resources/icon-note.gif) **NOTE:** 
>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.
Z
zengyawen 已提交
5

6
## Modules to Import
Z
zengyawen 已提交
7 8 9 10 11 12 13 14 15 16 17

**requestAnimationFrame**: none

**cancelAnimationFrame**: none

**createAnimator**:

```
import animator from '@ohos.animator';
```

18
## Required Permissions
Z
zengyawen 已提交
19 20 21

None

22
## requestAnimationFrame
Z
zengyawen 已提交
23 24 25 26 27

requestAnimationFrame\(handler\[, \[ ...args\]\]\): number

Requests an animation frame.

28
- Parameters
Z
zengyawen 已提交
29

30 31 32 33
  | Name    | Type        | Mandatory | Description                                                  |
  | ------- | ----------- | --------- | ------------------------------------------------------------ |
  | handler | Function    | Yes       | Handler used to request a frame. When **requestAnimationFrame** calls the **handler**, the timestamp is passed to the first parameter to indicate the time when **requestAnimationFrame** starts to execute the callback. |
  | ...args | Array\<any> | No        | Additional parameter, which is passed to the **handler** as a parameter during function callback. |
Z
zengyawen 已提交
34 35 36

-   Return values

37 38 39 40
    | Type   | Description |
    | ------ | ----------- |
    | number | Request ID. |
    
Z
zengyawen 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 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 88 89
-   Example

    ```
    <!-- xxx.hml -->
    <div class="container">
      <button type="capsule" class="btn" onclick="beginAnimation">beginAnimation</button>
    </div>
    ```

    ```
    /* xxx.css */
    .container {
      flex-direction: column;
      justify-content: center;
      align-items: center;
      width: 100%;
      height: 100%;
    }
    .btn{
      width: 300px;
      margin-top: 40px;
    }
    ```

    ```
    /* xxx.js */
    export default {
      data: {
        requestId: 0,
        startTime: 0,
      },
      beginAnimation() {
        cancelAnimationFrame(this.requestId);
        this.requestId = requestAnimationFrame(this.runAnimation);
      },
      runAnimation(timestamp) {
        if (this.startTime == 0) {
          this.startTime = timestamp;
        }
        var elapsed = timestamp - this.startTime;
        if (elapsed < 500) {
          console.log('callback handler timestamp: ' + timestamp);
          this.requestId = requestAnimationFrame(this.runAnimation);
        }
      }
    }
    ```


90
## cancelAnimationFrame
Z
zengyawen 已提交
91 92 93 94 95 96 97

cancelAnimationFrame\(requestId: number\): void

Cancels the animation frame request.

-   Parameters

98 99 100 101
   | Name      | Type   | Mandatory | Description              |
| --------- | ------ | --------- | ------------------------ |
   | requestId | number | Yes       | ID of the frame request. |
   
Z
zengyawen 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 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
-   Example

    ```
    <!-- xxx.hml -->
    <div class="container">
      <button type="capsule" class="btn" onclick="beginAnimation">beginAnimation</button>
      <button type="capsule" class="btn" onclick="beginAnimation">stopAnimation</button>
    </div>
    ```

    ```
    /* xxx.css */
    .container {
      flex-direction: column;
      justify-content: center;
      align-items: center;
      width: 100%;
      height: 100%;
    }
    .btn{
      width: 300px;
      margin-top: 40px;
    }
    ```

    ```
    /* xxx.js */
    export default {
      data: {
        requestId: 0,
        startTime: 0,
      },
      beginAnimation() {
        cancelAnimationFrame(this.requestId);
        this.requestId = requestAnimationFrame(this.runAnimation);
      },
      runAnimation(timestamp) {
        if (this.startTime == 0) {
          this.startTime = timestamp;
        }
        var elapsed = timestamp - this.startTime;
        if (elapsed < 500) {
          console.log('callback handler timestamp: ' + timestamp);
          this.requestId = requestAnimationFrame(this.runAnimation);
        }
      },
      stopAnimation() {
        cancelAnimationFrame(this.requestId);
      }
    }
    ```


155
## createAnimator
Z
zengyawen 已提交
156 157 158 159 160

createAnimator\(options\[...\]\): void

Creates an animation object.

161 162 163 164 165
- Parameters

  | Name    | Type   | Mandatory | Description                                                  |
  | ------- | ------ | --------- | ------------------------------------------------------------ |
  | options | Object | Yes       | Attributes of the **Animator** to be created. For details, see the options table. |
Z
zengyawen 已提交
166

167 168 169 170 171 172 173 174 175 176 177 178
- Description of options

  | Name       | Type   | Mandatory | Description                                                  |
  | ---------- | ------ | --------- | ------------------------------------------------------------ |
  | duration   | number | No        | Duration for playing an animation, in milliseconds. The default value is **0**. |
  | easing     | string | No        | Animation easing curve. The default value is **ease**.       |
  | delay      | number | No        | Animation delay duration, in milliseconds. The default value is **0**, indicating that there is no delay. |
  | fill       | string | No        | Animation start/stop mode. The default value is **none**.    |
  | direction  | string | No        | Animation playback mode. The default value is **normal**.    |
  | iterations | number | No        | Number of times that an animation is played. The default value is **1**. If this parameter is set to **0**, the animation is not played. If this parameter is set to **-1**, the animation is played for an unlimited number of times. |
  | begin      | number | No        | Start point of the animation easing. If this parameter is not set, the default value **0** is used. |
  | end        | number | No        | End point of the animation easing. If this parameter is not set, the default value **1** is used. |
Z
zengyawen 已提交
179 180 181

-   animator interfaces

182 183 184 185 186 187 188 189
    | Name    | Type    | Description                                                  |
    | ------- | ------- | ------------------------------------------------------------ |
    | update  | options | Updates the animation parameters. The input parameters are the same as those of **createAnimator**. |
    | play    | -       | Starts an animation.                                         |
    | finish  | -       | Ends an animation.                                           |
    | pause   | -       | Pauses an animation.                                         |
    | cancel  | -       | Cancels an animation.                                        |
    | reverse | -       | Reverses an animation.                                       |
Z
zengyawen 已提交
190 191 192 193


-   **animator**  supported events:

194 195 196 197 198 199 200
    | Name   | Type   | Description                         |
    | ------ | ------ | ----------------------------------- |
    | frame  | number | The frame is requested.             |
    | cancel | -      | The animation is forcibly canceled. |
    | finish | -      | The animation playback is complete. |
    | repeat | -      | The animation replays.              |
    
Z
zengyawen 已提交
201 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
-   Example

    ```
    <!-- hml -->
    <div class="container">
      <div class="Animation" style="height: {{divHeight}}px; width: {{divWidth}}px; background-color: red;" onclick="Show">
      </div>
    </div>
    ```

    ```
    // js
    export default {
      data : {
        divWidth: 200,
        divHeight: 200,
        animator: null
      },
      onInit() {
        var options = {
          duration: 1500,
          easing: 'friction',
          fill: 'forwards',
          iterations: 2,
          begin: 200.0,
          end: 400.0
        };
        this.animator = animator.createAnimator(options);
      },
      Show() {
        var options1 = {
          duration: 2000,
          easing: 'friction',
          fill: 'forwards',
          iterations: 1,
          begin: 200.0,
          end: 400.0
        };
        this.animator.update(options1);
        var _this = this;
        this.animator.onframe = function(value) {
          _this.divWidth = value;
          _this.divHeight = value;
        };
        this.animator.play();
      }
    }
248
    ```