# resource/animationclip (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/resource/animationclip/

Animation clip.

## AnimationClip extends Object

Animation clip.
- Frames are a list of areas (Rect) within the sprite sheet. (Animator applies that area with the sprite's setImageRect)

Example: Make a walk clip from atlas JSON

```js
import { AnimationClip } from "./build/vanilla.js";

const atlasJson = {
	frames: {
		walk0: { frame: { x: 0, y: 0, w: 200, h: 200 } },
		walk1: { frame: { x: 200, y: 0, w: 200, h: 200 } },
		walk2: { frame: { x: 400, y: 0, w: 200, h: 200 } },
	},
};
const clip = AnimationClip.fromAtlas(atlasJson, ["walk0", "walk1", "walk2"], 12, true);
const firstRect = clip.getFrame(0);
console.log(clip.getFrameCount(), clip.getDuration(), clip.isLoop(), firstRect.width);
```

- `constructor()` (constructor, since 0.0.8): Constructor.
- `static fromAtlas(atlasJson, frameIds, fps = 10, isLoop = true)` (static, since 0.3.0): Creates a clip from atlas JSON. (static)
  - Accepts both the TexturePacker format ({ frames: { name: { frame: {x,y,w,h} } } }) and
    the array format ([{x,y,w,h}, ...]).
  - Picks frames in frameIds order and makes it with duration = frameCount / fps.
  - `atlasJson` object: Atlas JSON. (a TexturePacker style object or a region array, each region has x, y and w, h or width, height)
  - `frameIds` string[] | number[]: Order of frame names or indices to take. (entries missing from the atlas are skipped)
  - `fps` number: Frames per second. (default 10, duration 0 if 0 or less)
  - `isLoop` boolean: Whether to loop. (default true)
  - returns AnimationClip: The created clip. (frames are a Rect list)
- `setFrames(frames)` (method, since 0.0.8): Sets the frame list.
  - `frames` Rect[]: Frame area list. (pixel areas within the sprite sheet, fromAtlas also puts in a Rect list)
- `setLoop(isLoop)` (method, since 0.0.8): Sets whether to loop.
  - `isLoop` boolean: True to loop.
- `setDuration(duration)` (method, since 0.0.8): Sets the animation duration.
  - `duration` number: Duration. (seconds)
- `isLoop()` (method, since 0.0.8): Returns whether it loops.
  - returns boolean: True if it loops. (default false)
- `getFrame(index)` (method, since 0.0.8): Returns the frame for the target index.
  - `index` number: Frame index. (from 0)
  - returns Rect: The frame area at that index. (undefined if out of range)
- `getFrames()` (method, since 0.0.8): Returns the frame list.
  - returns Rect[]: Frame area list.
- `getFrameCount()` (method, since 0.0.8): Returns the total frame count.
  - returns number: Frame count.
- `getDuration()` (method, since 0.0.8): Returns the animation duration.
  - returns number: Duration. (seconds, default 0)
