1e41f4b71Sopenharmony_ci# Controlling and Managing ArkGraphics 3D Scene Animations
2e41f4b71Sopenharmony_ciAnimations, an important resource type in a 3D scene, is used to control the motion of elements in the scene. For example, to simulate a scene where a person walks, it is difficult to calculate and set the rotation angle of every joint of the person in each frame. When making such an animation, the resource producer saves key frame data of the animation and the interpolator type between key frames in a model file. ArkGraphics 3D provides APIs for you to play and control animations to achieve the expected rendering effect in the scene.
3e41f4b71Sopenharmony_ci
4e41f4b71Sopenharmony_ci## Creating Animation Resources
5e41f4b71Sopenharmony_ciAnimation resources are made and saved to model files by model resource producers when they make the model. ArkGraphics 3D provides the capability of extracting and playing animations from glTF model resources.
6e41f4b71Sopenharmony_ci```ts
7e41f4b71Sopenharmony_ciimport { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters,
8e41f4b71Sopenharmony_ci  LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D';
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_cifunction createAnimation() : void {
11e41f4b71Sopenharmony_ci  let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.gltf"));
12e41f4b71Sopenharmony_ci  scene.then(async (result: Scene) => {
13e41f4b71Sopenharmony_ci    if (result) {
14e41f4b71Sopenharmony_ci      // Obtain animation resources.
15e41f4b71Sopenharmony_ci      let anim: Animation = result.animations[0];
16e41f4b71Sopenharmony_ci    }
17e41f4b71Sopenharmony_ci  });
18e41f4b71Sopenharmony_ci}
19e41f4b71Sopenharmony_ci```
20e41f4b71Sopenharmony_ci
21e41f4b71Sopenharmony_ci## Controlling the Animation Status
22e41f4b71Sopenharmony_ciArkGraphics 3D provides the following APIs to control the animation status:
23e41f4b71Sopenharmony_ci- **start**: plays an animation based on the current progress.
24e41f4b71Sopenharmony_ci- **stop**: stops playing an animation and sets its progress to **0** (not started).
25e41f4b71Sopenharmony_ci- **finish**: finishes the playing of an animation and sets its progress of **1** (finished).
26e41f4b71Sopenharmony_ci- **pause**: pauses an animation. The animation remains in the current playing progress.
27e41f4b71Sopenharmony_ci- **restart**: plays an animation from the beginning.
28e41f4b71Sopenharmony_ci
29e41f4b71Sopenharmony_ciThe sample code is as follows:
30e41f4b71Sopenharmony_ci```ts
31e41f4b71Sopenharmony_ciimport { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters,
32e41f4b71Sopenharmony_ci  LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D';
33e41f4b71Sopenharmony_ci
34e41f4b71Sopenharmony_cifunction animationControl() : void {
35e41f4b71Sopenharmony_ci  let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.gltf"));
36e41f4b71Sopenharmony_ci  scene.then(async (result: Scene) => {
37e41f4b71Sopenharmony_ci    if (result) {
38e41f4b71Sopenharmony_ci      let anim: Animation = result.animations[0];
39e41f4b71Sopenharmony_ci      // Control the animation status.
40e41f4b71Sopenharmony_ci      anim.start();
41e41f4b71Sopenharmony_ci      anim.pause();
42e41f4b71Sopenharmony_ci      anim.stop();
43e41f4b71Sopenharmony_ci      anim.restart();
44e41f4b71Sopenharmony_ci      anim.finish();
45e41f4b71Sopenharmony_ci    }
46e41f4b71Sopenharmony_ci  });
47e41f4b71Sopenharmony_ci}
48e41f4b71Sopenharmony_ci```
49e41f4b71Sopenharmony_ci
50e41f4b71Sopenharmony_ci## Using Animation Callbacks
51e41f4b71Sopenharmony_ci
52e41f4b71Sopenharmony_ciAn animation callback function is triggered when an animation enters a certain state. You can perform logic control based on the animation state. ArkGraphics 3D provides the following callback functions:
53e41f4b71Sopenharmony_ci- **onStarted()**: called when an animation starts to play. The start operation is triggered by calling **start** or **restart**.
54e41f4b71Sopenharmony_ci- **onFinished()**: called when an animation playback is complete or the **finish** API is called.
55e41f4b71Sopenharmony_ci
56e41f4b71Sopenharmony_ciThe sample code is as follows:
57e41f4b71Sopenharmony_ci```ts
58e41f4b71Sopenharmony_ciimport { Image, Shader, MaterialType, Material, ShaderMaterial, Animation, Environment, Container, SceneNodeParameters,
59e41f4b71Sopenharmony_ci  LightType, Light, Camera, SceneResourceParameters, SceneResourceFactory, Scene, Node } from '@kit.ArkGraphics3D';
60e41f4b71Sopenharmony_ci
61e41f4b71Sopenharmony_cifunction callBacks() : void {
62e41f4b71Sopenharmony_ci  let scene: Promise<Scene> = Scene.load($rawfile("gltf/CubeWithFloor/glTF/AnimatedCube.gltf"));
63e41f4b71Sopenharmony_ci  scene.then(async (result: Scene) => {
64e41f4b71Sopenharmony_ci    if (result) {
65e41f4b71Sopenharmony_ci      let anim: Animation = result.animations[0];
66e41f4b71Sopenharmony_ci      // Register the callbacks.
67e41f4b71Sopenharmony_ci      anim.onFinished(()=>{
68e41f4b71Sopenharmony_ci        console.info("onFinished");
69e41f4b71Sopenharmony_ci      });
70e41f4b71Sopenharmony_ci      anim.onStarted(()=>{
71e41f4b71Sopenharmony_ci        console.info("onStarted");
72e41f4b71Sopenharmony_ci      });
73e41f4b71Sopenharmony_ci    }
74e41f4b71Sopenharmony_ci  });
75e41f4b71Sopenharmony_ci}
76e41f4b71Sopenharmony_ci```
77