Screen Orientation API는 장치 화면 방향에 대한 정보를 제공하고 개발자가 프로그래밍 방식으로 화면 방향을 제어할 수 있도록 하는 JavaScript API입니다. API는 두 가지 주요 속성에 대한 액세스를 제공합니다.
1. `screen.orientation.type`: 이 속성은 화면의 현재 방향을 반환합니다. 가능한 값은 "portrait-primary", "portrait-secondary", "landscape-primary" 및 "landscape-secondary"입니다.
2. `screen.orientation.angle`: 이 속성은 화면이 현재 향하고 있는 각도를 도 단위로 반환합니다.
다음은 Screen Orientation API를 사용하여 기기 화면의 현재 방향을 감지하는 방법의 예입니다.
if (screen.orientation.type.startsWith("portrait")) {
console.log("The device is in portrait mode.");
} else {
console.log("The device is in landscape mode.");
}
다음은 API를 사용하여 화면 방향을 특정 모드로 잠그는 방법의 예입니다.
screen.orientation.lock("landscape-primary").then(function() {
console.log("Screen orientation locked to landscape-primary.");
}).catch(function() {
console.error("Failed to lock screen orientation.");
});
화면 방향 잠금은 터치 또는 클릭 이벤트와 같은 사용자 상호 작용에 대한 응답으로만 수행할 수 있습니다. 이는 웹사이트가 이용자의 동의 없이 화면의 방향을 임의로 변경하는 것을 방지하기 위함입니다.
'IT' 카테고리의 다른 글
자바스크립트 "Resize Observer API" (0) | 2023.03.13 |
---|---|
자바스크립트 "Screen Capture API" (0) | 2023.03.13 |
자바스크립트 "Selection API" (0) | 2023.03.13 |
자바스크립트 "Sensor API" (0) | 2023.03.13 |
자바스크립트 "Server Sent Events API" (0) | 2023.03.13 |