# ui/uiimageview (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/ui/uiimageview/

UI image view.

## UIImageView extends UIView

UI image view.
- Display only. Inherits UIView since it takes no input.
- When attached to a node, automatically creates/manages a Sprite component internally.
- Exposes commonly used Sprite properties such as image/color/flip/draw mode/blend.

Example: Draw a tinted piece of an image

```js
import { Engine, EngineConfiguration, UIScene, UIImageView, ImageAsset, WorldNode, Pivot, Vector2, Rect, Color } from "./build/vanilla.js";

class MainScene extends UIScene {
	constructor() {
		super();
		this.setLoadingMinDurationMs(0);
		this.imageAsset = null;
	}

	async loadAssets() {
		await super.loadAssets();
		const imageAsset = new ImageAsset();
		await imageAsset.load("examples/assets/spritesheet.png");
		this.imageAsset = imageAsset;
	}

	initialize(engine) {
		super.initialize(engine);
		const iconNode = new WorldNode();
		iconNode.setPivot(Pivot.topLeft);
		iconNode.setAnchor(Pivot.topLeft);
		iconNode.setLocalPosition(Vector2.create(40, 40));
		iconNode.setContentSize(Vector2.create(120, 120));
		const imageView = iconNode.addComponent(UIImageView);
		imageView.setImage(this.imageAsset);
		imageView.setImageRect(Rect.create(0, 0, 200, 200));
		imageView.setColor(new Color(1, 0.5, 0, 0.4));
		const root = this.getRoot();
		root.addChild(iconNode);
	}
}

const engineConfiguration = new EngineConfiguration();
engineConfiguration.canvasId = "mainCanvas";
engineConfiguration.referenceResolutionSize = Vector2.create(640, 360);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
engine.run(new MainScene());
```

- `constructor()` (constructor, since 0.2.1): Constructor.
- `attach(node)` (method, since 0.2.1): Attached to a node. (attaches the internal Sprite component)
  - `node` ComponentNode: Node to attach to.
- `detach(node)` (method, since 0.2.1): Detached from a node. (removes the internal Sprite component)
  - `node` ComponentNode: Node being detached from.
- `getSprite()` (method, since 0.2.1): Returns the internal Sprite component. (for direct access when fine control is needed)
  - returns Sprite | null: The internal Sprite. (null before attaching or after detaching)
- `setImage(image)` (method, since 0.2.1): Sets the image. (HTMLImageElement / OffscreenCanvas / ImageAsset are all allowed)
  - `image` HTMLImageElement | OffscreenCanvas | ImageAsset | null: Image to draw. (for an ImageAsset its image, clears if null, ignored before being attached to a node)
- `getImage()` (method, since 0.2.1): Returns the image.
  - returns HTMLImageElement | OffscreenCanvas | null: The current image. (null if none or before being attached to a node)
- `setImagePath(imagePath)` (method, since 0.4.0): Sets the image path. (Only records it and does not load the image, UIDocument writes and reads this path instead of the image)
  - `imagePath` string: Image path.
- `getImagePath()` (method, since 0.4.0): Returns the image path. (empty string if not set)
  - returns string: Image path. (empty string if not set)
- `setImageRect(imageRect)` (method, since 0.2.1): Sets the image sub-region (source rectangle).
  - `imageRect` Rect: Area of the image to cut out and draw. (image pixels, the whole image if the rectangle is empty, used in simple and tiled modes)
- `setColor(color)` (method, since 0.2.1): Sets the color. (sprite tint)
  - `color` Color | string: Tint color. (if alpha is above 0, covers the inside of the image silhouette with this color, fills the content area with this color if there is no image, default transparent)
- `getColor()` (method, since 0.4.0): Returns the color. (sprite tint, null if there is no sprite)
  - returns Color | null: Tint color. (null before being attached to a node)
- `setHorizontalFlip(isHorizontalFlip)` (method, since 0.2.1): Sets horizontal flip.
  - `isHorizontalFlip` boolean: If true, draws flipped horizontally.
- `setVerticalFlip(isVerticalFlip)` (method, since 0.2.1): Sets vertical flip.
  - `isVerticalFlip` boolean: If true, draws flipped vertically.
- `setDrawMode(mode)` (method, since 0.2.1): Sets the draw mode. (simple / sliced / tiled)
  - `mode` string: Draw mode. (one of simple, sliced, tiled, default simple)
- `setBlendMode(mode)` (method, since 0.2.1): Sets the blend mode.
  - `mode` string: Blend mode. (a Canvas2D globalCompositeOperation name, default source-over)
- `setNineSlice(nineSlice)` (method, since 0.2.1): Sets the nine-patch region.
  - `nineSlice` Rect: Nine-slice border thickness. (position.x left, position.y top, size.x right, size.y bottom, in image pixels, splits the whole image in sliced mode)

  Example: Draw a panel whose corners stay intact when stretched

  ```js
  import { Engine, EngineConfiguration, UIScene, UIImageView, ImageAsset, WorldNode, Pivot, Vector2, Rect } from "./build/vanilla.js";
  
  class MainScene extends UIScene {
  	constructor() {
  		super();
  		this.setLoadingMinDurationMs(0);
  		this.imageAsset = null;
  	}
  
  	async loadAssets() {
  		await super.loadAssets();
  		const imageAsset = new ImageAsset();
  		await imageAsset.load("assets/icons/icon.png");
  		this.imageAsset = imageAsset;
  	}
  
  	initialize(engine) {
  		super.initialize(engine);
  		const panelNode = new WorldNode();
  		panelNode.setPivot(Pivot.topLeft);
  		panelNode.setAnchor(Pivot.topLeft);
  		panelNode.setLocalPosition(Vector2.create(40, 40));
  		panelNode.setContentSize(Vector2.create(400, 160));
  		const imageView = panelNode.addComponent(UIImageView);
  		imageView.setImage(this.imageAsset);
  		imageView.setDrawMode("sliced");
  		imageView.setNineSlice(Rect.create(64, 64, 64, 64));
  		const root = this.getRoot();
  		root.addChild(panelNode);
  	}
  }
  
  const engineConfiguration = new EngineConfiguration();
  engineConfiguration.canvasId = "mainCanvas";
  engineConfiguration.referenceResolutionSize = Vector2.create(640, 360);
  engineConfiguration.autoResizeOnWindowResize = true;
  const engine = new Engine(engineConfiguration);
  engine.run(new MainScene());
  ```

