# core/viewmanager (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/core/viewmanager/

View manager.

## ViewManager extends Object

View manager.
- clientNativeSize:	Full web browser resolution size. (0,0 ~ canvasNativeSize.x, )
- canvasNativeSize:	Resolution size of the currently rendered canvas element. (web browser based)
- canvasPixelSize:	Actual rendering pixel resolution size inside the canvas element. (canvas size * device pixel ratio)
- viewNativeRect:	The view area actually used. (sub-area relative to the client area)
- viewSize:			The view size actually used.

Example: Set the view scale mode and convert coordinates

```js
import { Engine, EngineConfiguration, Scene, Vector2, ViewScaleMode } from "./build/vanilla.js";

const engineConfiguration = new EngineConfiguration();
engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
engineConfiguration.autoResizeOnWindowResize = true;
const engine = new Engine(engineConfiguration);
const viewManager = engine.getViewManager();
viewManager.setViewScaleMode(ViewScaleMode.stretchShort);
engine.run(new Scene());
const viewSize = viewManager.getViewSize();
const viewNativeRect = viewManager.getViewNativeRect();
console.log(viewSize.x, viewSize.y, viewNativeRect.size.x, viewNativeRect.size.y);
const viewPosition = viewManager.convertClientToView(100, 100);
console.log(viewPosition.x, viewPosition.y);
```

- `constructor(engine)` (constructor, since 0.0.8): Constructor.
  - `engine` Engine: Engine to read the reference resolution from. (referenceResolutionSize of the engine configuration)
- `calculateViewRect()` (method, since 0.0.8): Calculates the view area.
- `applyTransform(graphic, scaleX, scaleY, skewX, skewY, translateX, translateY)` (method, since 0.0.8): Applies the coordinate system.
  - `graphic` Graphic: Graphic to apply the transform to.
  - `scaleX` number: Horizontal scale. (a)
  - `scaleY` number: Vertical scale. (d)
  - `skewX` number: Horizontal skew. (c)
  - `skewY` number: Vertical skew. (b)
  - `translateX` number: Horizontal translation. (e, canvas pixels)
  - `translateY` number: Vertical translation. (f, canvas pixels)
- `applyCanvasNativeRect(graphic)` (method, since 0.0.8): Applies the full screen area.
  - If not ViewScaleMode.none, initialization to handle that screen resolution before output.
  - getCanvasNativeSize()
  - `graphic` Graphic: Graphic to apply the transform to.
- `applyViewRect(graphic)` (method, since 0.0.8): Applies the view area.
  - If not ViewScaleMode.none, initialization to handle that screen resolution before output.
  - ViewScaleMode.none uses getCanvasNativeSize(), and the other modes use getReferenceResolutionSize().
  - With getViewSize(), you always get the viewport resolution suited to the mode automatically, without distinguishing modes.
  - `graphic` Graphic: Graphic to apply the transform to.
- `applyReferenceResolutionSize(referenceResolutionSize)` (method, since 0.0.11): Resets the reference resolution size.
  - `referenceResolutionSize` Vector2: New reference resolution.
- `setCanvas(canvas)` (method, since 0.0.8): Sets the canvas.
  - `canvas` HTMLCanvasElement: The canvas the engine draws to.
- `getCanvas()` (method, since 0.0.8): Returns the canvas.
  - returns HTMLCanvasElement: The canvas the engine draws to.
- `getDevicePixelRatio()` (method, since 0.0.8): Returns the device's actual pixel ratio.
  - returns number: Device pixel ratio used for rendering. (up to the cap if one is set)
- `setViewScaleMode(viewScaleMode)` (method, since 0.0.8): Sets the view scale mode.
  - `viewScaleMode` ViewScaleMode: A ViewScaleMode value. (does nothing if unchanged; recomputes the view area if changed)
- `getViewScaleMode()` (method, since 0.0.8): Returns the view scale mode.
  - returns ViewScaleMode: A ViewScaleMode value.
- `getClientNativeSize()` (method, since 0.0.8): Returns the full size of the web page.
  - returns Vector2: Inner size of the window. (CSS pixels, innerWidth, innerHeight)
- `getCanvasNativeSize()` (method, since 0.0.8): Returns the canvas element size.
  - returns Vector2: Size of the canvas element. (CSS pixels, rounded)
- `getCanvasPixelSize()` (method, since 0.0.8): Returns the screen size for canvas pixel rendering. (The actual internal canvas resolution. Not needed in engine or content logic)
  - returns Vector2: Render pixel size of the canvas. (CSS size x device pixel ratio)
- `getReferenceResolutionSize()` (method, since 0.0.8): Returns the reference resolution size.
  - returns Vector2: Reference resolution.
- `getViewSize()` (method, since 0.0.11): Returns the resolution size actually in use.
  - returns Vector2: View size. (view coordinates, decided by the view scale mode)
- `getCssPixelScale()` (method, since 0.4.0): Returns how many CSS pixels one view unit is on screen. (canvas CSS height / view height, 1 if it cannot be measured)
  - The fixed side of the view is always the same length, so on small screens (a portal's PC iframe, etc.) one unit becomes much smaller than 1 CSS pixel and text becomes unreadable.
    Used to calculate a scale that enlarges only the UI layer. (e.g. minimum CSS scale / this value)
  - returns number: CSS pixels per view unit. (1 if it cannot be measured)
- `getViewNativeRect()` (method, since 0.0.11): Returns the actual area within the canvas where the view exists. (values relative to canvasNativeSize)
  - returns Rect: View area inside the canvas. (CSS pixels, origin at the canvas top-left)
- `getTargetResolutionScale()` (method, since 0.0.8): Returns the scale.
  - returns number: Scale from the reference resolution to the canvas. (1 for none and referenceResolution)
- `canvasPositionToViewPosition(canvasPosition)` (method, since 0.0.9): Converts canvas coordinates to view coordinates.
  - `canvasPosition` Vector2: Canvas coordinates. (origin at the canvas top-left, CSS pixels)
  - returns Vector2: View coordinates. (rounded integers, (0, 0) if the scale is 0)
- `convertClientToView(clientX, clientY)` (method, since 0.4.0): Screen (client) coordinates to view coordinates. (Subtracts the canvas's current position from a DOM event's clientX, clientY and converts with canvasPositionToViewPosition)
  - `clientX` number: Client X coordinate. (CSS pixels, clientX)
  - `clientY` number: Client Y coordinate. (CSS pixels, clientY)
  - returns Vector2: View coordinates. (rounded integers, (0, 0) if the scale is 0)
- `setMaxRenderPixelRatio(maxRenderPixelRatio)` (method, since 0.3.0): Set the render pixel ratio cap.
  - Keeps the canvas from growing too large on high-DPI devices. 0 means no cap.
  - The engine applies the canvas size at the start of the next frame through the same path as a window resize (Engine.resize). (scenes also receive the screen area change)
    Resizing the canvas in the middle of a frame (tick, draw) clears the drawing buffer while that frame still draws with the earlier viewport, which made one frame black.
  - `maxRenderPixelRatio` number: Render pixel ratio cap. (0 means no limit, negative becomes 0)
- `isViewRectPending()` (method, since 0.4.0): Return whether the view area is recalculated at the start of the next frame. (true after the render pixel ratio cap changed and before it is applied)
  - returns boolean: True when a recalculation is still pending. (false once calculateViewRect applies it)

  Example: Check whether applying a changed render pixel ratio cap is still pending

  ```js
  import { Engine, EngineConfiguration } from "./build/vanilla.js";
  
  const engine = new Engine(new EngineConfiguration());
  const viewManager = engine.getViewManager();
  viewManager.setMaxRenderPixelRatio(1);
  console.log(viewManager.isViewRectPending());
  ```

- `getMaxRenderPixelRatio()` (method, since 0.3.0): Returns the render scale upper limit.
  - returns number: Render pixel ratio cap. (0 means no limit)
- `limitRenderPixelRatio(devicePixelRatio)` (method, since 0.4.0): The ratio with the cap applied.
  - Returns the cap if the cap is greater than 0 and the device ratio exceeds it, otherwise returns the device ratio as is. (the calculation calculateViewRect uses)
  - `devicePixelRatio` number: Device pixel ratio. (in the form of window.devicePixelRatio)
  - returns number: The ratio with the cap applied.

  Example: With a cap of 2, a ratio of 3 becomes 2

  ```js
  import { Engine, EngineConfiguration } from "./build/vanilla.js";
  
  const engine = new Engine(new EngineConfiguration());
  const viewManager = engine.getViewManager();
  viewManager.setMaxRenderPixelRatio(2);
  console.log(viewManager.limitRenderPixelRatio(3), viewManager.limitRenderPixelRatio(1.5));
  ```

- `applyAspectViewScaleMode()` (method, since 0.3.0): Automatically selects the view scale mode matching the aspect ratio.
  - If the screen is taller than the reference resolution, fixes the width and extends the height,
    and if it is wider, fixes the height and extends the width. (Logic several games kept copy-pasting)
- `setSafeAreaOverride(insets)` (method, since 0.3.0): Sets a forced safe area value. (CSS px, for emulating a notch on desktop)
  - `insets` object | null: { top, right, bottom, left }
- `getSafeAreaInsets()` (method, since 0.3.0): Returns the safe area insets. (view coordinates)
  - Measures env(safe-area-inset-*) through the padding of a hidden element and converts it to view coordinates.
  - Priority: setSafeAreaOverride() > URL ?safeArea=top,right,bottom,left > env() measurement.
  - returns object: { top, right, bottom, left }

  Example: Simulate a safe area and read it in view coordinates

  ```js
  import { Engine, EngineConfiguration, Vector2, ViewScaleMode } from "./build/vanilla.js";
  
  const engineConfiguration = new EngineConfiguration();
  engineConfiguration.referenceResolutionSize = Vector2.create(1280, 720);
  const engine = new Engine(engineConfiguration);
  const viewManager = engine.getViewManager();
  viewManager.setViewScaleMode(ViewScaleMode.stretchShortExpandLong);
  viewManager.setSafeAreaOverride({ top: 44, right: 0, bottom: 34, left: 0 });
  viewManager.setIgnoredSafeAreaSides(["bottom"]);
  const insets = viewManager.getSafeAreaInsets();
  console.log(insets.top, insets.right, insets.bottom, insets.left);
  ```

- `setIgnoredSafeAreaSides(sides)` (method, since 0.4.0): Sets the safe area sides to ignore. (getSafeAreaInsets returns 0 for those sides, an empty list uses all)
  - The game decides which sides to ignore and passes them in, as with a webview whose platform app already draws clear of that side.
  - `sides` string[]: One of "top", "right", "bottom", "left".
- `getIgnoredSafeAreaSides()` (method, since 0.4.0): Returns the safe area sides to ignore.
  - returns string[]: List of sides to ignore. (among "top", "right", "bottom", "left")

## ViewScaleMode (enum)

View scale mode.

- `none` = `none`: Not used (the view area changes when the web browser size changes)
- `referenceResolution` = `referenceResolution`: Defines the view area by the reference resolution (both axes may be cropped or have leftover space)
- `stretchWidth` = `stretchWidth`: Defines the view area by the reference resolution + stretches to fit along the horizontal axis while keeping the view's aspect ratio. (The opposite axis may be cropped or have leftover space)
- `stretchHeight` = `stretchHeight`: Defines the view area by the reference resolution + stretches to fit along the vertical axis while keeping the view's aspect ratio. (The opposite axis may be cropped or have leftover space)
- `stretchShort` = `stretchShort`: Defines the view area by the reference resolution + stretches to fit along the shorter of the two axes while keeping the view's aspect ratio. (The opposite axis may have leftover space)
- `stretchWidthExpandHeight` = `stretchWidthExpandHeight`: Stretches the width across the whole screen (reference resolution width = always fixed). The height is computed automatically from the screen ratio. No margins on either side. (Instead, the vertical resolution varies by display)
- `stretchHeightExpandWidth` = `stretchHeightExpandWidth`: Stretches the height across the whole screen (reference resolution height = always fixed). The width is computed automatically from the screen ratio. No margins on either side. (Instead, the horizontal resolution varies by display)
- `stretchShortExpandLong` = `stretchShortExpandLong`: Scale based on the short axis. The long axis is also stretched across the whole screen. No margins on either side. (Instead, the long axis resolution varies by display)
