• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Animation Effect
2
3
4You can set the interpolator to implement the animation effect.
5
6
7> **NOTE**
8>
9> This feature is supported since API version 6.
10
11
12## Creating an Animation Object
13
14Use createAnimator to create an animation object and set the animation attributes by using the options parameter.
15
16```html
17<!-- xxx.hml -->
18<div class="container">
19  <div style="width: 300px;height: 300px;margin-top: 100px;background: linear-gradient(pink, purple);transform: translate({{translateVal}});">
20  </div>
21  <div class="row">
22    <button type="capsule" value="play" onclick="playAnimation"></button>
23  </div>
24</div>
25```
26
27```css
28/* xxx.css */
29.container {
30  width:100%;
31  height:100%;
32  flex-direction: column;
33  align-items: center;
34  justify-content: center;
35}
36button{
37  width: 200px;
38}
39.row{
40  width: 65%;
41  height: 100px;
42  align-items: center;
43  justify-content: space-between;
44  margin-top: 50px;
45  margin-left: 260px;
46}
47```
48
49```js
50// xxx.js
51import animator from '@ohos.animator';
52export default {
53  data: {
54    translateVal: 0,
55    animation: null
56  },
57  onInit() {},
58  onShow(){
59    var options = {
60      duration: 3000,
61      easing:"friction",
62      delay:"1000",
63      fill: 'forwards',
64      direction:'alternate',
65      iterations: 2,
66      begin: 0,
67      end: 180
68    };// Set attributes.
69    this.animation = animator.createAnimator(options)// Create an animation.
70  },
71  playAnimation() {
72    var _this = this;
73    this.animation.onframe = function(value) {
74      _this.translateVal= value
75    };
76    this.animation.play();
77  }
78}
79```
80
81![en-us_image_0000001267887885](figures/en-us_image_0000001267887885.gif)
82
83> **NOTE**
84>
85> - When you use createAnimator to create an animation object, you must pass the options parameter.
86>
87> - begin indicates the start point of the animation interpolation. If it is not set, the default value 0 is used.
88>
89> - end indicates the end point of the animation interpolation. If it is not set, the default value 1 is used.
90
91
92## Adding Animation Events and Calling Methods
93
94The animator supports events and methods, which you can use to customize the animation effect. Events include frame, cancel, repeat, and finish. Methods include update, play, pause, cancel, reverse, and finish. For details about the supported events and methods, see [animator supported events and animator supported APIs](../reference/apis/js-apis-animator.md).
95
96```html
97<!-- xxx.hml -->
98<div style="flex-direction: column;align-items: center;width: 100%;height: 100%;">
99  <div style="width:200px;height: 200px;margin-top: 100px;background: linear-gradient(#b30d29, #dcac1b);
100  transform: scale({{scaleVal}});"></div>
101  <div style="width: {{DivWidth}};height: {{DivHeight}};margin-top: 200px;
102  background: linear-gradient(pink, purple);margin-top: 200px;transform:translateY({{translateVal}});">
103  </div>
104  <div class="row">
105    <button type="capsule" value="play" onclick="playAnimation"></button>
106    <button type="capsule" value="update" onclick="updateAnimation"></button>
107  </div>
108  <div class="row1">
109    <button type="capsule" value="pause" onclick="pauseAnimation"></button>
110    <button type="capsule" value="finish" onclick="finishAnimation"></button>
111  </div>
112  <div class="row2">
113    <button type="capsule" value="cancel" onclick="cancelAnimation"></button>
114    <button type="capsule" value="reverse" onclick="reverseAnimation"></button>
115  </div>
116</div>
117```
118
119```css
120/* xxx.css */
121button{
122  width: 200px;
123}
124.row{
125  width: 65%;
126  height: 100px;
127  align-items: center;
128  justify-content: space-between;
129  margin-top: 150px;
130  position: fixed;
131  top: 52%;
132  left: 120px;
133}
134.row1{
135  width: 65%;
136  height: 100px;
137  align-items: center;
138  justify-content: space-between;
139  margin-top: 120px;
140  position: fixed;
141  top: 65%;
142  left: 120px;
143}
144.row2{
145  width: 65%;
146  height: 100px;
147  align-items: center;
148  justify-content: space-between;
149  margin-top: 100px;
150  position: fixed;
151  top: 75%;
152  left: 120px;
153}
154```
155
156```js
157// xxx.js
158import animator from '@ohos.animator';
159import prompt from '@system.prompt';
160export default {
161  data: {
162    scaleVal:1,
163    DivWidth:200,
164    DivHeight:200,
165    translateVal:0,
166    animation: null
167  },
168  onInit() {
169    var options = {
170      duration: 3000,
171      fill: 'forwards',
172      begin: 200,
173      end: 270
174    };
175    this.animation = animator.createAnimator(options);
176  },
177  onShow() {
178    var _this= this;
179    // Add an animation repeat event.
180    this.animation.onrepeat = function() {
181      prompt.showToast({
182        message: 'repeat'
183      });
184      var repeatoptions = {
185        duration: 2000,
186        iterations: 1,
187         direction: 'alternate',
188         begin: 180,
189         end: 240
190       };
191        _this.animation.update(repeatoptions);
192        _this.animation.play();
193      };
194  },
195  playAnimation() {
196    var _this= this;
197    // Add the frame-by-frame interpolation callback event for the animation.
198    this.animation.onframe = function(value) {
199      _this. scaleVal= value/150,
200      _this.DivWidth = value,
201      _this.DivHeight = value,
202      _this.translateVal = value-180
203    };
204    this.animation.play();
205  },
206  updateAnimation() {
207    var newoptions = {
208      duration: 5000,
209      iterations: 2,
210      begin: 120,
211      end: 180
212    };
213    this.animation.update(newoptions);
214    this.animation.play();// Play this animation.
215  },
216  pauseAnimation() {
217    this.animation.pause();// Pause this animation.
218  },
219  finishAnimation() {
220    var _this= this;
221   // Add an animation completion event.
222    this.animation.onfinish = function() {
223      prompt.showToast({
224        message: 'finish'
225      })
226    };
227    this.animation.finish(); // Finish this animation.
228  },
229  cancelAnimation() {
230    this.animation.cancel(); // Cancel this animation.
231  },
232  reverseAnimation() {
233    this.animation.reverse(); // Reverse this animation.
234  }
235}
236```
237
238![en-us_image_0000001223287724](figures/en-us_image_0000001223287724.gif)
239
240> **NOTE**
241>
242> When calling the update method, you can use it to update the animation parameters. The input parameters are the same as those of createAnimator.
243