# misc/imagescroller (vanilla.js 0.4.0)

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

Image scroller.

## ImageScroller extends Object

Image scroller.

Example: Scroll a background image tiled endlessly

```js
import { Engine, EngineConfiguration, Scene, ImageScroller, ImageAsset, Rect } from "./build/vanilla.js";

const imageAsset = new ImageAsset();
await imageAsset.load("examples/assets/spritesheet.png");
const image = imageAsset.getImage();
const imageScroller = new ImageScroller();
imageScroller.setImage(image);
imageScroller.setViewRect(Rect.create(0, 0, 640, 360));
imageScroller.setHorizontalScrollSpeed(-60);
imageScroller.setVerticalScrollSpeed(20);

class MainScene extends Scene {
	tick(timeDelta) {
		super.tick(timeDelta);
		imageScroller.tick(timeDelta);
	}

	draw(graphic) {
		super.draw(graphic);
		imageScroller.draw(graphic);
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
```

- `constructor()` (constructor, since 0.0.8): Constructor.
- `tick(timeDelta)` (method, since 0.0.8): Update.
  - `timeDelta` number: Time since the last frame. (Seconds)
- `draw(graphic)` (method, since 0.0.8): Draw.
  - `graphic` Graphic: Graphic to draw with.
- `setImage(image)` (method, since 0.0.8): Sets the image.
  - `image` HTMLImageElement | HTMLCanvasElement | ImageAsset | null: Image to tile. (For an ImageAsset its image element is kept, other images are kept as is, null draws nothing)

  Example: Pass a loaded ImageAsset as is

  ```js
  import { ImageScroller, ImageAsset } from "./build/vanilla.js";
  
  const imageAsset = new ImageAsset();
  await imageAsset.load("examples/assets/spritesheet.png");
  const imageScroller = new ImageScroller();
  imageScroller.setImage(imageAsset);
  const image = imageScroller.getImage();
  console.log(image === imageAsset.getImage(), image.width, image.height);
  ```

- `getImage()` (method, since 0.0.8): Returns the image.
  - returns HTMLImageElement: Image drawn tiled. (null if none)
- `setViewRect(rect)` (method, since 0.0.8): Sets the screen visible area.
  - `rect` Rect: Screen area filled with the image. (Clipped outside it)
- `getViewRect()` (method, since 0.0.8): Returns the screen visible area.
  - returns Rect: Screen area filled with the image.
- `setVerticalScrollSpeed(speed)` (method, since 0.0.8): Sets vertical scrolling.
  - `speed` number: Vertical scroll speed. (pixels / second)
- `setHorizontalScrollSpeed(value)` (method, since 0.0.8): Sets horizontal scrolling.
  - `value` number: Horizontal scroll speed. (pixels / second)
- `getScrollSpeed(speed)` (method, since 0.0.8): Returns the scroll speed.
  - `speed` any: Unused.
  - returns Vector2: Scroll speed. (pixels / second, the internally held vector as is)
- `setScrollPosition(position)` (method, since 0.0.8): Sets the scroll position.
  - `position` Vector2: Scroll position. (pixels, the given vector is held as is and modified by tick)
- `getScrollPosition()` (method, since 0.0.8): Returns the scroll position.
  - returns HTMLImageElement: Scroll position. (pixels, the internally held vector as is)
