# misc/imagescroller (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/misc/imagescroller/

Image scroller.
- A port of the original misc/imagescroller.js. Tiles one image inside the visible area (view_rect) and draws it flowing at the set speed. (infinite background)
- Like the original, it is a plain value, not a component. The owner calls tick and draw.
- Differences from the original:
  The image is `Rc<Image>`. The ImageAsset taken by the original setImage is passed with set_image_asset.
  get_scroll_position / get_scroll_speed return values, not mutable references like the original. (tick adds to them directly inside)

## ImageScroller (struct) (Clone, Default)

Image scroller.
- As the scroll position increases, the image flows right (horizontal) and down (vertical).

Example: Position of the first tile of a background flowing left

```rust
use vanilla::{Image, ImageScroller, Rect, Vector2};

let mut image_scroller = ImageScroller::new();
image_scroller.set_image(Some(Image::create_from_pixels(100, 100, vec![255u8; 100 * 100 * 4])));
image_scroller.set_view_rect(Rect::create(10.0, 20.0, 300.0, 200.0));
image_scroller.set_horizontal_scroll_speed(-50.0);
image_scroller.tick(1.0);
assert_eq!(image_scroller.get_scroll_position(), Vector2::create(-50.0, 0.0));
assert_eq!(image_scroller.calculate_tile_start(100.0, 100.0), Vector2::create(-40.0, 20.0));
```

Example: Draw a flowing background in a scene

```rust
use vanilla::{Engine, Graphic, ImageScroller, Rect};

fn create_background(engine: &Engine) -> ImageScroller {
	let mut image_scroller = ImageScroller::new();
	image_scroller.set_image(engine.load_image("examples/assets/atlas/icons.png").ok());
	image_scroller.set_view_rect(Rect::create(0.0, 0.0, 1280.0, 720.0));
	image_scroller.set_vertical_scroll_speed(40.0);
	image_scroller
}

fn draw_background(image_scroller: &mut ImageScroller, graphic: &mut Graphic, time_delta: f64) {
	image_scroller.tick(time_delta);
	image_scroller.draw(graphic);
}
```

- `pub fn new() -> ImageScroller` (constructor, since 0.3.2): Constructor. (no image, area 0, stopped)
  - returns ImageScroller: A scroller with no image and scroll position and speed of 0.
- `pub fn tick(&mut self, time_delta: f64)` (method, since 0.3.2): Update. (moves the scroll position by the speed)
  - `time_delta` f64: Time elapsed since the last frame. (seconds)
- `pub fn calculate_tile_start(&self, image_width: f64, image_height: f64) -> Vector2` (method, since 0.3.2): Start coordinate of the first tile. (pulled back by the remainder of the scroll position divided by the image size. Coordinates are floored to integers to prevent gaps)
  - `image_width` f64: Image width. (pixels, the remainder is NaN if 0)
  - `image_height` f64: Image height. (pixels, the remainder is NaN if 0)
  - returns Vector2: Top left coordinate of the first tile. (equal to the top left of the visible area or up to one image ahead of it)
- `pub fn draw(&self, graphic: &mut Graphic)` (method, since 0.3.2): Draw. (clips to the visible area and overlaps by 1.5 pixels to hide gaps between tiles)
  - Draws nothing if there is no image or its width or height is 0.
  - `graphic` &mut Graphic: Graphic to draw to. (same coordinate system as the visible area)
- `pub fn set_image(&mut self, image: Option<Rc<Image>>)` (method, since 0.3.2): Sets the image. (None draws nothing)
  - `image` Option<Rc<Image>>: Image to tile. None draws nothing.
- `pub fn set_image_asset(&mut self, image_asset: &mut ImageAsset)` (method, since 0.3.2): Sets the image from an image asset. (None if not loaded yet)
  - Takes the image once at call time. It does not change automatically when the asset finishes loading later.
  - `image_asset` &mut ImageAsset: Asset to take the image from.
- `pub fn get_image(&self) -> Option<&Rc<Image>>` (method, since 0.3.2): Returns the image.
  - returns Option<&Rc<Image>>: Reference to the tiled image. None if none.
- `pub fn set_view_rect(&mut self, rect: Rect)` (method, since 0.3.2): Sets the visible area. (caller's coordinate system)
  - `rect` Rect: Area to tile and clip to.
- `pub fn get_view_rect(&self) -> Rect` (method, since 0.3.2): Returns the visible area.
  - returns Rect: Area to tile and clip to.
- `pub fn set_vertical_scroll_speed(&mut self, speed: f64)` (method, since 0.3.2): Sets the vertical scroll speed. (pixels per second)
  - `speed` f64: Vertical speed. (pixels per second, + makes the image flow down)
- `pub fn set_horizontal_scroll_speed(&mut self, speed: f64)` (method, since 0.3.2): Sets the horizontal scroll speed. (pixels per second)
  - `speed` f64: Horizontal speed. (pixels per second, + makes the image flow right)
- `pub fn get_scroll_speed(&self) -> Vector2` (method, since 0.3.2): Returns the scroll speed.
  - returns Vector2: Horizontal (x) and vertical (y) speed. (pixels per second, a copy)
- `pub fn set_scroll_position(&mut self, position: Vector2)` (method, since 0.3.2): Sets the scroll position.
  - `position` Vector2: Scroll position. (pixels, only the remainder divided by the image size shows in the image)
- `pub fn get_scroll_position(&self) -> Vector2` (method, since 0.3.2): Returns the scroll position.
  - returns Vector2: Current scroll position. (pixels, a copy)
