# base/platform (vanilla.js 0.4.0)

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

플랫폼 정보.

## Platform extends Object

플랫폼 정보.

예제: user agent 글로 플랫폼 가리기

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

예제: 지금 기기 알아보기

```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): 플랫폼 이름. (PlatformType 값, getPlatformInfo 나 applyUserAgent 가 채운다)
  - returns string
- `browserName` (field, since 0.0.8): 브라우저 이름. (BrowserType 값, getPlatformInfo 나 applyUserAgent 가 채운다)
  - returns string
- `isMobile = false` (field, since 0.0.8): 모바일 기기인지. (user agent 에 Mobi, Android, iPhone, iPad 가 있으면 참)
  - returns boolean
- `constructor()` (constructor, since 0.0.8): 생성.
- `getOrAddCanvas(canvasId)` (method, since 0.2.0): 캔버스 생성 or 반환.
  - `canvasId` string: 찾을 요소의 id.
  - returns HTMLCanvasElement: 그 id 의 요소. (없으면 캔버스를 만들어 id 를 주고 body 끝에 붙인다)
- `getPlatformInfo()` (method, since 0.0.8): 플랫폼 정보 탐지 및 적용.
  - returns { platformName: string, browserName: string }: navigator.userAgent 로 고른 플랫폼 이름과 브라우저 이름. (platformName, browserName, isMobile 필드도 채운다)
- `applyUserAgent(userAgent)` (method, since 0.4.0): user agent 글로 플랫폼, 브라우저, 모바일 여부 고르기. (getPlatformInfo 가 브라우저의 글로 부른다)
  - `userAgent` string: 브라우저의 user agent 글. (navigator.userAgent 꼴)
- `isDesktopWeb()` (method, since 0.4.0): 데스크탑 웹인지 반환. (마우스 휠, 키보드 안내처럼 데스크탑에서만 보일 것을 가릴 때 쓴다)
  - 모바일이면 아니다. 아이패드의 사파리는 데스크탑처럼 맥이라고 알리므로, 맥인데 손가락 입력이 여럿 되면 아이패드로 본다.
  - 플랫폼 정보를 새로 읽는다.
  - returns boolean: 데스크탑 웹이면 참.
- `isCoarsePointer()` (method, since 0.4.0): 주된 손이 손가락인지 반환. (CSS 의 pointer: coarse, 단축키 안내처럼 마우스로 하는 화면에만 보일 것을 가릴 때 쓴다)
  - returns boolean: (pointer: coarse) 미디어 질의가 맞으면 참. (matchMedia 가 없으면 거짓)
- `getResourceUsage()` (method, since 0.4.0): 리소스 사용량 반환.
  - returns { totalTransferSize: number, totalDecodedSize: number, loadedFiles: array }: 네트워크로 받은 크기의 합, 풀린 크기의 합(바이트)과 불러온 파일 목록. (파일마다 path, name, type, transferSize, decodedSize, performance 의 resource 항목에서 읽는다)
- `getResouceUsage()` (method, since 0.0.8): 리소스 사용량 반환. (옛 이름의 오타를 그대로 둔 별칭, getResourceUsage 와 같다)
  - returns { totalTransferSize: number, totalDecodedSize: number, loadedFiles: array }: getResourceUsage 의 결과.
- `getSafeAreaRect(canvas)` (method, since 0.0.8): 세이프 에어리어 반환.
  - `canvas` HTMLCanvasElement | null: 크기를 잴 캔버스. (CSS 크기 clientWidth, clientHeight 를 쓴다, null 이면 창의 innerWidth, innerHeight 를 쓴다)
  - returns Rect: 세이프 에어리어 여백을 뺀 사각형. (모두 CSS 픽셀, left, top 은 왼쪽, 위 여백, 여백은 CSS 의 env(safe-area-inset-*) 값, 너비와 높이는 0 이상)

  예제: 캔버스에서 노치를 피한 자리 구하기

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

플랫폼 타입.

- `windows` = `Windows`: 윈도우.
- `macOS` = `macOS`: 맥.
- `android` = `Android`: 안드로이드.
- `steamDeck` = `SteamDeck`: 스팀덱 추가
- `iPadOS` = `iPadOS`: iPadOS 추가
- `iOS` = `iOS`: 아이폰, 아이팟.
- `linux` = `Linux`: 리눅스.
- `unknown` = `Unknown`: 알 수 없음.

## BrowserType (enum)

브라우저 타입.

- `chrome` = `Chrome`: 크롬.
- `edge` = `Edge`: 엣지.
- `firefox` = `Firefox`: 파이어폭스.
- `internetExplorer` = `Internet Explorer`: 인터넷 익스플로러.
- `safari` = `Safari`: 사파리.
- `unknown` = `Unknown`: 알 수 없음.
