# core/animation (vanilla.js 0.4.0)

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

Animation handler.

## Animation extends Object

Animation handler.

Example: An animation that plays once from a sprite sheet

```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): Constructor.
- `tick(timeDelta)` (method, since 0.0.8): Update.
  - `timeDelta` number: Elapsed time. (seconds, advances only while playing, with frames and a speed above 0)
- `play()` (method, since 0.0.8): Play.
- `pause()` (method, since 0.0.8): Pause. (keeps the current frame and the accumulated time as they are)
- `resume()` (method, since 0.0.8): Resume. (continues from the paused frame)
- `stop()` (method, since 0.0.8): Stop. (returns to the first frame. Returns to it even when already stopped)
- `setFrames(frames)` (method, since 0.0.8): Sets the frames.
  - `frames` Frame[]: Frame list. (ignored if not an array, setting it rewinds to the first frame and stops like stop)
- `setFramesFromImages(images)` (method, since 0.0.8): Sets the frames. (a list of individual sprite images)
  - `images` HTMLImageElement[]: List of single images. (one whole-image frame per image, ignored if empty or not an array)
- `setFramesFromRects(image, rects)` (method, since 0.0.8): Sets the frames. (sprite sheet)
  - `image` HTMLImageElement | HTMLCanvasElement | OffscreenCanvas | ImageBitmap: Sprite sheet picture. (only images, canvases, OffscreenCanvas and ImageBitmap are accepted; other values are ignored)
  - `rects` Rect[]: List of frame regions in the sheet. (pixels, ignored if empty or not an array)
- `setAnimationSpeed(animationSpeed)` (method, since 0.0.8): Sets the frames per second.
  - e.g.) When set to 60, the image changes 60 times per second.
  - `animationSpeed` number: Frames per second. (default 10, does not advance if 0 or less)
- `setLoop(value)` (method, since 0.0.8): Sets whether to loop.
  - `value` boolean: True to loop. (default true, if false it stops at the last frame and calls the complete callback)
- `setOnComplete(callback)` (method, since 0.0.8): Sets the animation completion callback.
  - `callback` Function: Function called when a non-looping animation reaches the last frame. (no arguments, not called if null)
- `gotoAndPlay(index)` (method, since 0.0.8): Moves to a specific frame and plays.
  - `index` number: Frame index. (from 0, clamped to the first or last if out of range)
- `gotoAndStop(index)` (method, since 0.0.8): Moves to a specific frame and stops.
  - `index` number: Frame index. (from 0, clamped to the first or last if out of range)
- `isPlaying()` (method, since 0.0.8): Returns whether playing.
  - returns boolean: True if playing.
- `getCurrentFrame()` (method, since 0.0.8): Returns the current frame.
  - returns Frame: Current frame. (null if there are no frames)
- `getCurrentImage()` (method, since 0.0.8): Returns the current image.
  - returns HTMLImageElement: Image of the current frame. (null if there are no frames)
- `getCurrentRect()` (method, since 0.0.8): Returns the current region.
  - returns Rect | null: Region inside the image of the current frame. (pixels, null if there are no frames)
- `getCurrentFrameIndex()` (method, since 0.0.8): Returns the current frame number.
  - returns number: Current frame index. (from 0)
- `getTotalFrameCount()` (method, since 0.0.8): Returns the total number of frames.
  - returns number: Frame count.
