# core/animation (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/core/animation/

애니메이션 처리기.

## Animation extends Object

애니메이션 처리기.

예제: 스프라이트 시트로 한 번 도는 애니메이션

```js
import { ImageAsset, Animation, Rect } from "./build/vanilla.js";

const imageAsset = new ImageAsset();
await imageAsset.load("examples/assets/spritesheet.png");
const image = imageAsset.getImage();
const rects = [];
for (let row = 0; row < 3; ++row) {
	for (let column = 0; column < 3; ++column) {
		rects.push(Rect.create(column * 200, row * 200, 200, 200));
	}
}

const animation = new Animation();
animation.setFramesFromRects(image, rects);
animation.setAnimationSpeed(8);
animation.setLoop(false);
animation.setOnComplete(() => {
	console.log("complete", animation.getCurrentFrameIndex());
});
animation.play();
animation.tick(0.5);
console.log(animation.getCurrentFrameIndex(), animation.getTotalFrameCount());
animation.tick(1.0);
console.log(animation.isPlaying());
```

- `constructor()` (constructor, since 0.0.8): 생성.
- `tick(timeDelta)` (method, since 0.0.8): 갱신.
  - `timeDelta` number: 지난 시간. (초, 재생 중이고 프레임이 있고 속도가 0 보다 클 때만 넘긴다)
- `play()` (method, since 0.0.8): 재생.
- `pause()` (method, since 0.0.8): 일시정지. (지금 프레임과 쌓인 시간을 그대로 둔다)
- `resume()` (method, since 0.0.8): 재개. (멈춘 프레임에서 이어 간다)
- `stop()` (method, since 0.0.8): 정지. (첫 프레임으로 되돌린다. 멈춰 있어도 되돌린다)
- `setFrames(frames)` (method, since 0.0.8): 프레임 설정.
  - `frames` Frame[]: 프레임 목록. (배열이 아니면 무시한다, 넣으면 stop 처럼 첫 프레임으로 되돌리고 멈춘다)
- `setFramesFromImages(images)` (method, since 0.0.8): 프레임 설정. (낱장의 스프라이트 이미지 목록)
  - `images` HTMLImageElement[]: 낱장 이미지 목록. (이미지마다 전체 영역 프레임 하나, 비었거나 배열이 아니면 무시한다)
- `setFramesFromRects(image, rects)` (method, since 0.0.8): 프레임 설정. (스프라이트 시트)
  - `image` HTMLImageElement | HTMLCanvasElement | OffscreenCanvas | ImageBitmap: 스프라이트 시트 그림. (이미지, 캔버스, ImageBitmap 만 받고 그 밖의 값은 무시한다)
  - `rects` Rect[]: 시트 안의 프레임 영역 목록. (픽셀, 비었거나 배열이 아니면 무시한다)
- `setAnimationSpeed(animationSpeed)` (method, since 0.0.8): 초당 프레임 숫 설정.
  - 예) 60으로 지정시 초당 이미지 60회 변경됨.
  - `animationSpeed` number: 초당 프레임 수. (기본 10, 0 이하이면 넘기지 않는다)
- `setLoop(value)` (method, since 0.0.8): 반복 여부 설정.
  - `value` boolean: 반복하면 참. (기본 참, 거짓이면 마지막 프레임에서 멈추고 완료 콜백을 부른다)
- `setOnComplete(callback)` (method, since 0.0.8): 애니메이션 완료 콜백 설정.
  - `callback` Function: 반복하지 않는 애니메이션이 마지막 프레임에 닿으면 부를 함수. (인자 없음, null 이면 부르지 않는다)
- `gotoAndPlay(index)` (method, since 0.0.8): 특정 프레임으로 이동하여 재생합니다.
  - `index` number: 프레임 번호. (0 부터, 범위 밖이면 처음이나 끝으로 자른다)
- `gotoAndStop(index)` (method, since 0.0.8): 특정 프레임으로 이동하여 정지합니다.
  - `index` number: 프레임 번호. (0 부터, 범위 밖이면 처음이나 끝으로 자른다)
- `isPlaying()` (method, since 0.0.8): 재생 중인지 여부 반환.
  - returns boolean: 재생 중이면 참.
- `getCurrentFrame()` (method, since 0.0.8): 현재 프레임 반환.
  - returns Frame: 현재 프레임. (프레임이 없으면 null)
- `getCurrentImage()` (method, since 0.0.8): 현재 이미지 반환.
  - returns HTMLImageElement: 현재 프레임의 이미지. (프레임이 없으면 null)
- `getCurrentRect()` (method, since 0.0.8): 현재 영역 반환.
  - returns Rect | null: 현재 프레임의 이미지 안 영역. (픽셀, 프레임이 없으면 null)
- `getCurrentFrameIndex()` (method, since 0.0.8): 현재 프레임 번호 반환.
  - returns number: 현재 프레임 번호. (0 부터)
- `getTotalFrameCount()` (method, since 0.0.8): 모든 프레임 수 반환.
  - returns number: 프레임 수.
