# ui/uiscale (vanilla.js 0.4.0)

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

UI scale.

## UIScale

UI scale.
- The fixed side of the view (height for portrait, width for landscape) always has the same length, so on a small screen (a portal's PC iframe, etc.)
  one view unit becomes much smaller than 1 CSS pixel on screen and text becomes unreadable. In that case it computes a scale that enlarges only the UI layer on screen. (the game's graphics stay as they are)
- If one unit is smaller than minimumCssScale CSS pixels, it enlarges by that much. (1 ~ maximumScale)
  - On a short screen, it does not enlarge so far that the enlarged UI layer's height falls below minimumLayerHeight. (so the top and bottom UI do not cover the screen in phone landscape, 0 means not checked)
  - On a portrait screen (width at most portraitRatio times the height), it enlarges without an upper limit so that one unit becomes minimumCssScale CSS pixels.
- Usage: const uiScale = UIScale.read(viewWidth, viewHeight); layerNode.setLocalScale(Vector2.create(uiScale, uiScale));
  UIWindowLayer enlarges windows by this scale.

Example: Compute the UI scale for each screen size

```js
import { UIScale } from "./build/vanilla.js";

const smallScreenScale = UIScale.read(1280, 720, { cssPixelScale: 0.5 });
const largeScreenScale = UIScale.read(1280, 720, { cssPixelScale: 1.2 });
const portraitScale = UIScale.read(720, 1280, { cssPixelScale: 0.4 });
const limitedScale = UIScale.read(1280, 720, { cssPixelScale: 0.25, maximumScale: 1.5 });
console.log(smallScreenScale, largeScreenScale, portraitScale, limitedScale);
```

- `static read(viewWidth, viewHeight, options = {})` (static, since 0.4.0): Reads the UI scale.
  - `viewWidth` number: Width of the area the UI layer covers. (view units)
  - `viewHeight` number: Height of the area the UI layer covers. (view units)
  - `options` object: { minimumCssScale = 0.8, maximumScale = 1.8, minimumLayerHeight = 0, portraitRatio = 1.1, cssPixelScale? (if absent, the engine's ViewManager.getCssPixelScale) }
  - returns number: Scale to multiply the UI layer by. (1 or more, 1 if cssPixelScale is not above 0)

  Example: Scale up a UI layer node to fit the screen

  ```js
  import { Engine, EngineConfiguration, UIScene, UIScale, UILabel, WorldNode, Pivot, Vector2 } from "./build/vanilla.js";
  
  class MainScene extends UIScene {
  	constructor() {
  		super();
  		this.setLoadingMinDurationMs(0);
  	}
  
  	initialize(engine) {
  		super.initialize(engine);
  		const layerNode = new WorldNode();
  		layerNode.setPivot(Pivot.topLeft);
  		layerNode.setAnchor(Pivot.topLeft);
  		const labelNode = new WorldNode();
  		labelNode.setPivot(Pivot.topLeft);
  		labelNode.setAnchor(Pivot.topLeft);
  		labelNode.setLocalPosition(Vector2.create(16, 16));
  		labelNode.setContentSize(Vector2.create(240, 32));
  		const label = labelNode.addComponent(UILabel);
  		label.setText("Readable on small screens");
  		label.setFontSize(16);
  		layerNode.addChild(labelNode);
  		const root = this.getRoot();
  		root.addChild(layerNode);
  
  		const viewManager = engine.getViewManager();
  		const viewSize = viewManager.getViewSize();
  		const uiScale = UIScale.read(viewSize.x, viewSize.y, { minimumLayerHeight: 320 });
  		layerNode.setLocalScale(Vector2.create(uiScale, uiScale));
  		console.log("ui scale", uiScale);
  	}
  }
  
  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());
  ```

- `static isPortrait(viewWidth, viewHeight, portraitRatio = DEFAULT_PORTRAIT_RATIO)` (static, since 0.4.0): Whether the screen is portrait. (width at most portraitRatio times the height)
  - `viewWidth` number: Width of the area. (view units)
  - `viewHeight` number: Height of the area. (view units)
  - `portraitRatio` number: Threshold for a portrait screen. (portrait if the width is at most this many times the height, default 1.1)
  - returns boolean: True if the width is at most portraitRatio times the height.
- `static readCssPixelScale()` (static, since 0.4.0): How many CSS pixels one view unit is on screen. (1 if there is no engine)
  - returns number: CSS pixels per view unit. (1 if there is no engine)
