1# Animation Effect 2 3 4You can set the interpolator to implement the animation effect. For details, see [Animation](../reference/apis/js-apis-animator.md). 5 6 7>  **NOTE**: 8> This feature is supported since API version 6. 9 10 11## Creating an Animation Object 12 13Use createAnimator to create an animation object and set the animation attributes by using the options parameter. 14 15 16``` 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 28``` 29/* xxx.css */ 30.container { 31 flex-direction: column; 32 align-items: center; 33 justify-content: center; 34} 35button{ 36 width: 200px; 37} 38.row{ 39 width: 65%; 40 height: 100px; 41 align-items: center; 42 justify-content: space-between; 43 margin-top: 50px; 44 margin-left: 260px; 45} 46``` 47 48 49``` 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 82 83>  **NOTE**: 84> - When you use createAnimator to create an animation object, you must pass the options parameter. 85> 86> - begin indicates the start point of the animation interpolation. If it is not set, the default value 0 is used. 87> 88> - end indicates the end point of the animation interpolation. If it is not set, the default value 1 is used. 89 90 91## Adding Animation Events and Calling Methods 92 93The 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). 94 95 96``` 97<!-- xxx.hml --> 98<div style="flex-direction: column;align-items: center;"> 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 120``` 121/* xxx.css */ 122.container { 123 flex-direction: column; 124 align-items: center; 125 justify-content: center; 126} 127button{ 128 width: 200px; 129} 130.row{ 131 width: 65%; 132 height: 100px; 133 align-items: center; 134 justify-content: space-between; 135 margin-top: 150px; 136 position: fixed; 137 top: 55%; 138 left: 120px; 139} 140.row1{ 141 width: 65%; 142 height: 100px; 143 align-items: center; 144 justify-content: space-between; 145 margin-top: 120px; 146 position: fixed; 147 top: 65%; 148 left: 120px; 149} 150.row2{ 151 width: 65%; 152 height: 100px; 153 align-items: center; 154 justify-content: space-between; 155 margin-top: 100px; 156 position: fixed; 157 top: 75%; 158 left: 120px; 159} 160``` 161 162 163``` 164/* xxx.js */ 165import animator from '@ohos.animator'; 166import prompt from '@system.prompt'; 167export default { 168 data: { 169 scaleVal:1, 170 DivWidth:200, 171 DivHeight:200, 172 translateVal:0, 173 animation: null 174 }, 175 onInit() { 176 var options = { 177 duration: 3000, 178 fill: 'forwards', 179 begin: 200, 180 end: 270 181 }; 182 this.animation = animator.createAnimator(options); 183 }, 184 onShow() { 185 var _this= this; 186 // Add an animation repeat event. 187 this.animation.onrepeat = function() { 188 prompt.showToast({ 189 message: 'repeat' 190 }); 191 var repeatoptions = { 192 duration: 2000, 193 iterations: 1, 194 direction: 'alternate', 195 begin: 180, 196 end: 240 197 }; 198 _this.animation.update(repeatoptions); 199 _this.animation.play(); 200 }; 201 }, 202 playAnimation() { 203 var _this= this; 204 // Add the frame-by-frame interpolation callback event for the animation. 205 this.animation.onframe = function(value) { 206 _this. scaleVal= value/150, 207 _this.DivWidth = value, 208 _this.DivHeight = value, 209 _this.translateVal = value-180 210 }; 211 this.animation.play(); 212 }, 213 updateAnimation() { 214 var newoptions = { 215 duration: 5000, 216 iterations: 2, 217 begin: 120, 218 end: 180 219 }; 220 this.animation.update(newoptions); 221 this.animation.play();// Play this animation. 222 }, 223 pauseAnimation() { 224 this.animation.pause();// Pause this animation. 225 }, 226 finishAnimation() { 227 var _this= this; 228 // Add an animation completion event. 229 this.animation.onfinish = function() { 230 prompt.showToast({ 231 message: 'finish' 232 }) 233 }; 234 this.animation.finish(); // Finish this animation. 235 }, 236 cancelAnimation() { 237 this.animation.cancel(); // Cancel this animation. 238 }, 239 reverseAnimation() { 240 this.animation.reverse(); // Reverse this animation. 241 } 242} 243``` 244 245 246 247>  **NOTE**: 248> When calling the update method, you can use it to update the animation parameters. The input parameters are the same as those of createAnimator. 249