# core/frame (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/core/frame/

Image region. (frame)
- A port of the original core/frame.js. Not a frame loop, but one sprite image or one cell in a sheet.
  Animation holds these as a list and steps through them in order.
- The original throws an exception if image is null. The Rust version takes `Rc<Image>` so it cannot be empty.
- Like the original, it does not clip the region to the image size. (the original also has that line commented out)

Example: Splitting one sheet into four cell frames

```rust
use vanilla::{Frame, Image, Rect};

let image = Image::create_from_pixels(64, 16, vec![0u8; 64 * 16 * 4]);
let mut frame_list = Vec::new();
for index in 0..4 {
	let cell_rect = Rect::create(index as f64 * 16.0, 0.0, 16.0, 16.0);
	frame_list.push(Frame::new(image.clone(), Some(cell_rect)));
}
assert_eq!(frame_list.len(), 4);
assert_eq!(frame_list[3].get_image_rect(), Rect::create(48.0, 0.0, 16.0, 16.0));
```

## Frame (struct) (Clone)

Image region.
- Holds one image together with a rectangular region (pixels) in it. When cloned, the image is shared through `Rc`.

Example: A whole-image frame and a one-cell frame

```rust
use std::rc::Rc;
use vanilla::{Frame, Image, Rect};

let image = Image::create_from_pixels(64, 32, vec![0u8; 64 * 32 * 4]);
let whole_frame = Frame::new(image.clone(), None);
assert_eq!(whole_frame.get_image_rect(), Rect::create(0.0, 0.0, 64.0, 32.0));
assert!(Rc::ptr_eq(whole_frame.get_image(), &image));

let cell_frame = Frame::new(image, Some(Rect::create(16.0, 8.0, 16.0, 16.0)));
assert_eq!(cell_frame.get_image_rect(), Rect::create(16.0, 8.0, 16.0, 16.0));
```

- `pub fn new(image: Rc<Image>, rect: Option<Rect>) -> Frame` (constructor, since 0.3.2): Constructor. (uses the whole image as the region if rect is None)
  - `image` Rc<Image>: Image to take the region from.
  - `rect` Option<Rect>: Region in the image. (pixels, from the top left of the image) None means (0, 0, image width, image height). Not clipped even if it goes outside the image.
  - returns Frame: New frame.
- `pub fn get_image(&self) -> &Rc<Image>` (method, since 0.3.2): Returns the image.
  - returns &Rc<Image>: Reference to the image the frame holds.
- `pub fn get_image_rect(&self) -> Rect` (method, since 0.3.2): Returns the image inner region.
  - returns Rect: Region in the image. (pixels, a copy)
