# ui/uiimageview (vanilla.js 0.4.0)

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

UI 이미지 뷰.

## UIImageView extends UIView

UI 이미지 뷰.
- 표시 전용. 입력을 받지 않으므로 UIView 를 상속한다.
- 노드에 부착되면 내부적으로 Sprite 컴포넌트를 자동 생성/관리한다.
- Sprite 의 이미지/색/플립/드로우모드/블렌드 등 자주 쓰는 속성을 노출한다.

예제: 그림의 한 조각을 틴트해 그리기

```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): 생성.
- `attach(node)` (method, since 0.2.1): 노드에 붙음. (내부 Sprite 컴포넌트 부착)
  - `node` ComponentNode: 붙을 노드.
- `detach(node)` (method, since 0.2.1): 노드에서 떨어짐. (내부 Sprite 컴포넌트 제거)
  - `node` ComponentNode: 떨어질 노드.
- `getSprite()` (method, since 0.2.1): 내부 Sprite 컴포넌트 반환. (디테일 제어가 필요할 때 직접 접근)
  - returns Sprite | null: 내부 Sprite. (노드에 붙기 전이나 떨어진 뒤면 null)
- `setImage(image)` (method, since 0.2.1): 이미지 설정. (HTMLImageElement / OffscreenCanvas / ImageAsset 모두 허용)
  - `image` HTMLImageElement | OffscreenCanvas | ImageAsset | null: 그릴 그림. (ImageAsset 이면 그 image, null 이면 비운다, 노드에 붙기 전이면 무시한다)
- `getImage()` (method, since 0.2.1): 이미지 반환.
  - returns HTMLImageElement | OffscreenCanvas | null: 지금 그림. (없거나 노드에 붙기 전이면 null)
- `setImagePath(imagePath)` (method, since 0.4.0): 그림 경로 설정. (적어 두기만 하고 그림을 읽지 않는다, UIDocument 가 그림 대신 이 경로를 적고 읽는다)
  - `imagePath` string: 그림 경로.
- `getImagePath()` (method, since 0.4.0): 그림 경로 반환. (정하지 않았으면 빈 글)
  - returns string: 그림 경로. (정하지 않았으면 빈 글)
- `setImageRect(imageRect)` (method, since 0.2.1): 이미지 부분 영역(소스 사각형) 설정.
  - `imageRect` Rect: 그림에서 잘라 그릴 영역. (그림의 픽셀, 빈 사각형이면 그림 전체, simple 과 tiled 모드에서 쓴다)
- `setColor(color)` (method, since 0.2.1): 색상 설정. (스프라이트 틴트)
  - `color` Color | string: 틴트 색. (알파가 0 보다 크면 그림의 실루엣 안을 이 색으로 덮는다, 그림이 없으면 내용 영역을 이 색으로 칠한다, 기본 투명)
- `getColor()` (method, since 0.4.0): 색상 반환. (스프라이트 틴트, 스프라이트가 없으면 null)
  - returns Color | null: 틴트 색. (노드에 붙기 전이면 null)
- `setHorizontalFlip(isHorizontalFlip)` (method, since 0.2.1): 가로 플립 설정.
  - `isHorizontalFlip` boolean: 참이면 좌우로 뒤집어 그린다.
- `setVerticalFlip(isVerticalFlip)` (method, since 0.2.1): 세로 플립 설정.
  - `isVerticalFlip` boolean: 참이면 위아래로 뒤집어 그린다.
- `setDrawMode(mode)` (method, since 0.2.1): 드로우 모드 설정. (simple / sliced / tiled)
  - `mode` string: 그리기 모드. (simple, sliced, tiled 가운데 하나, 기본 simple)
- `setBlendMode(mode)` (method, since 0.2.1): 블렌드 모드 설정.
  - `mode` string: 블렌드 모드. (Canvas2D globalCompositeOperation 이름, 기본 source-over)
- `setNineSlice(nineSlice)` (method, since 0.2.1): 나인패치 영역 설정.
  - `nineSlice` Rect: 나인 슬라이스 테두리 두께. (position.x 왼쪽, position.y 위, size.x 오른쪽, size.y 아래, 그림의 픽셀, sliced 모드에서 그림 전체를 나눈다)

  예제: 늘려도 귀퉁이가 그대로인 판 그리기

  ```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());
  ```

