# base/platform (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/base/platform/

Platform information.

## Platform extends Object

Platform information.

Example: Detect the platform from a user agent string

```js
import { Platform, PlatformType, BrowserType } from "./build/vanilla.js";

const platform = new Platform();
platform.applyUserAgent("Mozilla/5.0 (Linux; Android 14; Pixel 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Mobile Safari/537.36");
console.log(platform.platformName === PlatformType.android, platform.browserName === BrowserType.chrome, platform.isMobile);
```

Example: Detect the current device

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

const platform = new Platform();
const platformInfo = platform.getPlatformInfo();
console.log(platformInfo.platformName, platformInfo.browserName, platform.isMobile, platform.isDesktopWeb());
```

- `platformName` (field, since 0.0.8): Platform name. (a PlatformType value, filled by getPlatformInfo or applyUserAgent)
  - returns string
- `browserName` (field, since 0.0.8): Browser name. (a BrowserType value, filled by getPlatformInfo or applyUserAgent)
  - returns string
- `isMobile = false` (field, since 0.0.8): Whether it is a mobile device. (true if the user agent contains Mobi, Android, iPhone or iPad)
  - returns boolean
- `constructor()` (constructor, since 0.0.8): Constructor.
- `getOrAddCanvas(canvasId)` (method, since 0.2.0): Creates or returns the canvas.
  - `canvasId` string: id of the element to find.
  - returns HTMLCanvasElement: The element with that id. (if none, a canvas is created with that id and appended to the end of body)
- `getPlatformInfo()` (method, since 0.0.8): Detects and applies platform information.
  - returns { platformName: string, browserName: string }: Platform name and browser name detected from navigator.userAgent. (also fills the platformName, browserName and isMobile fields)
- `applyUserAgent(userAgent)` (method, since 0.4.0): Determines the platform, browser and mobile status from the user agent string. (getPlatformInfo calls it with the browser's string)
  - `userAgent` string: User agent string of the browser. (in the navigator.userAgent form)
- `isDesktopWeb()` (method, since 0.4.0): Returns whether it is desktop web. (Used to decide things shown only on desktop, like mouse wheel or keyboard hints)
  - Not if mobile. Safari on iPad reports itself as Mac like a desktop, so a Mac with multi-touch input is treated as an iPad.
  - Reads platform information anew.
  - returns boolean: True if it is desktop web.
- `isCoarsePointer()` (method, since 0.4.0): Returns whether the primary pointer is a finger. (CSS pointer: coarse, used to decide things shown only on mouse-driven screens, like shortcut hints)
  - returns boolean: True if the (pointer: coarse) media query matches. (false if matchMedia is not available)
- `getResourceUsage()` (method, since 0.4.0): Returns resource usage.
  - returns { totalTransferSize: number, totalDecodedSize: number, loadedFiles: array }: Total transferred size, total decoded size (bytes) and the list of loaded files. (path, name, type, transferSize, decodedSize per file, read from the resource entries of performance)
- `getResouceUsage()` (method, since 0.0.8): Returns resource usage. (Alias that keeps the typo of the old name, same as getResourceUsage)
  - returns { totalTransferSize: number, totalDecodedSize: number, loadedFiles: array }: Result of getResourceUsage.
- `getSafeAreaRect(canvas)` (method, since 0.0.8): Returns the safe area.
  - `canvas` HTMLCanvasElement | null: Canvas to measure. (uses its CSS size clientWidth, clientHeight; if null, the window's innerWidth, innerHeight)
  - returns Rect: Rectangle with the safe area insets removed. (all in CSS pixels; left and top are the left and top insets, the insets are the CSS env(safe-area-inset-*) values, width and height are 0 or more)

  Example: Get the area of a canvas that avoids the notch

  ```js
  import { Platform } from "./build/vanilla.js";
  
  const platform = new Platform();
  const canvas = platform.getOrAddCanvas("gameCanvas");
  canvas.width = 640;
  canvas.height = 360;
  const safeAreaRect = platform.getSafeAreaRect(canvas);
  console.log(safeAreaRect.left, safeAreaRect.top, safeAreaRect.width, safeAreaRect.height);
  ```

## PlatformType (enum)

Platform type.

- `windows` = `Windows`: Windows.
- `macOS` = `macOS`: Mac.
- `android` = `Android`: Android.
- `steamDeck` = `SteamDeck`: Steam Deck added
- `iPadOS` = `iPadOS`: iPadOS added
- `iOS` = `iOS`: iPhone, iPod.
- `linux` = `Linux`: Linux.
- `unknown` = `Unknown`: Unknown.

## BrowserType (enum)

Browser type.

- `chrome` = `Chrome`: Chrome.
- `edge` = `Edge`: Edge.
- `firefox` = `Firefox`: Firefox.
- `internetExplorer` = `Internet Explorer`: Internet Explorer.
- `safari` = `Safari`: Safari.
- `unknown` = `Unknown`: Unknown.
