API / ui/uiscale
ui/uiscale
UI 배율.
UI scale.
UIScale
UI 배율.
- 뷰의 고정된 쪽(세로형은 세로, 가로형은 가로)은 늘 같은 길이라, 작은 화면(포털의 PC iframe 등)에서는 뷰 한 단위가 화면에서 1 CSS 픽셀보다 한참 작아져 글자가 읽히지 않는다. 그때 화면 위의 UI 층만 키울 배율을 셈한다. (게임의 그림은 그대로)
- 한 단위가 minimumCssScale CSS 픽셀보다 작으면 그만큼 키운다. (1 ~ maximumScale)
- 낮은 화면은 키운 UI 층의 높이가 minimumLayerHeight 보다 낮아지게는 키우지 않는다. (폰 가로에서 위아래 UI 가 화면을 덮지 않게, 0 이면 보지 않음)
- 세로 화면(가로가 세로의 portraitRatio 배 이하)은 상한 없이 한 단위가 minimumCssScale CSS 픽셀이 되게 키운다.
- 사용: const uiScale = UIScale.read(viewWidth, viewHeight); layerNode.setLocalScale(Vector2.create(uiScale, uiScale)); UIWindowLayer 가 이 배율로 창을 키운다.
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화면 크기마다 UI 배율 셈하기Compute the UI scale for each screen size
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);UI 배율 읽기.
Reads the UI scale.
- viewWidthnumber
- UI 층이 덮는 영역의 너비. (뷰 단위)Width of the area the UI layer covers. (view units)
- viewHeightnumber
- UI 층이 덮는 영역의 높이. (뷰 단위)Height of the area the UI layer covers. (view units)
- optionsobject
- { minimumCssScale = 0.8, maximumScale = 1.8, minimumLayerHeight = 0, portraitRatio = 1.1, cssPixelScale? (없으면 엔진의 ViewManager.getCssPixelScale) }{ minimumCssScale = 0.8, maximumScale = 1.8, minimumLayerHeight = 0, portraitRatio = 1.1, cssPixelScale? (if absent, the engine's ViewManager.getCssPixelScale) }
반환Returnsnumber UI 층에 곱할 배율. (1 이상, cssPixelScale 이 0 보다 크지 않으면 1)Scale to multiply the UI layer by. (1 or more, 1 if cssPixelScale is not above 0)
짝Counterpart: vanilla-engine UIScale.read
예제ExampleUI 층 노드를 화면에 맞춰 키우기Scale up a UI layer node to fit the screen
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());세로 화면인지. (가로가 세로의 portraitRatio 배 이하)
Whether the screen is portrait. (width at most portraitRatio times the height)
- viewWidthnumber
- 영역의 너비. (뷰 단위)Width of the area. (view units)
- viewHeightnumber
- 영역의 높이. (뷰 단위)Height of the area. (view units)
- portraitRationumber
- 세로 화면으로 볼 가름. (가로가 세로의 이 배 이하면 세로, 기본 1.1)Threshold for a portrait screen. (portrait if the width is at most this many times the height, default 1.1)
반환Returnsboolean 가로가 세로의 portraitRatio 배 이하면 참.True if the width is at most portraitRatio times the height.
짝Counterpart: vanilla-engine UIScale.is_portrait
뷰 한 단위가 화면에서 몇 CSS 픽셀인지. (엔진이 없으면 1)
How many CSS pixels one view unit is on screen. (1 if there is no engine)
반환Returnsnumber 뷰 한 단위의 CSS 픽셀 수. (엔진이 없으면 1)CSS pixels per view unit. (1 if there is no engine)