๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป Frontend/Angular

[Angular] ๋ชจ๋ฐ”์ผ ๋ธŒ๋ผ์šฐ์ € ํ™”๋ฉด ๊ณ ์ •ํ•˜๊ธฐ (Lock Screen orientation in mobile browser)

by Fomagran ๐Ÿ’ป 2022. 5. 5.
728x90
๋ฐ˜์‘ํ˜•

 

1์ฐจ ์‹œ๋„

 

ํ™”๋ฉด๋งˆ๋‹ค ์„ธ๋กœ๋กœ ๊ณ ์ • ๋˜๋Š” ๊ฐ€๋กœ๋กœ ๊ณ ์ •์ด ๋˜์•ผ ํ•œ๋‹ค๋Š” ์š”๊ตฌ์‚ฌํ•ญ์„ ๋ฐ›๊ณ  iOS์—์„  ๊ฑฐ์˜ ์ฝ”๋“œ ํ•œ ์ค„๋งŒ ์ถ”๊ฐ€ํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ธˆ๋ฐฉ ๋๋‚ ๊ฑฐ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

 

๋ฐ”๋กœ ๊ตฌ๊ธ€๋ง ํ•˜๋‹ˆ๊น MDN Web APIs ๋ฌธ์„œ์— ScreenOrientation.lock()์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ๊ฒ€์ƒ‰๋˜์—ˆ๋‹ค.

 

https://developer.mozilla.org/en-US/docs/Web/API/ScreenOrientation/lock

 

ScreenOrientation.lock() - Web APIs | MDN

The lock() property of the ScreenOrientation interface locks the orientation of the containing document to the specified orientation.

developer.mozilla.org

 

์œ„ API๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด window์˜ screen์˜ ๋ฐฉํ–ฅ์„ ์›ํ•˜๋Š” ํƒ€์ž… portrait(์„ธ๋กœ) or landscape(๊ฐ€๋กœ)๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

(์ด๊ฒƒ๋“ค ์™ธ์—๋„ natural,portrait-primary ๋“ฑ ๋งŽ์€ lock type์ด ์กด์žฌํ•œ๋‹ค.)

 

 window.screen.orientation.lock('portrait').then(
      (success) => alert('success!'),
      (fail) => alert(`fail... ${fail}`)
    );

 

์šฐ์„  ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ ์‹คํ–‰์‹œ์ผœ ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ด์œ ๋กœ lock์ด ์‹คํŒจํ•œ๋‹ค.

 

 

๋ฌธ์„œ๋ฅผ ์ฝ์–ด๋ณด๋ฉด ํ™”๋ฉด lock์€ ๋ชจ๋ฐ”์ผ ๋””๋ฐ”์ด์Šค์—์„œ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค๊ณ  ๋ช…์‹œ๋˜์–ด ์žˆ๋‹ค.

 

 

๊ทธ๋ ‡๋‹ค๋ฉด ๋ชจ๋ฐ”์ผ๋กœ ์‹คํ–‰์‹œํ‚ค๋ฉด ์ž˜ ์ž‘๋™๋˜๊ฒ ๊ตฌ๋‚˜... ํ•˜๊ณ  ์‹คํ–‰์‹œ์ผœ ๋ณด๋‹ˆ.. ์„ฑ๊ณต๋„ ์‹คํŒจ๋„ ํ•˜์ง€ ์•Š์•˜๋‹ค.

 

window.screen.orientation์„ ์ถœ๋ ฅํ•ด๋ณด๋‹ˆ undefined๋กœ ๋‚˜์™€ lock ๋ฉ”์„œ๋“œ๊ฐ€ ์•„์˜ˆ ์‹คํ–‰์ด ์•ˆ๋˜๋Š” ๊ฒƒ์ด์—ˆ๋‹ค.

 

์•„๋‹ˆ ๊ณต์‹ ๋ฌธ์„œ์— ๋‚˜์™€์žˆ๋Š”๋Œ€๋กœ ํ–ˆ๋Š”๋ฐ ์™œ ์•ˆ๋˜๋Š”๊ฑฐ์ง€.. ๋ผ๋Š” ์ƒ๊ฐ์œผ๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐฉ์‹์œผ๋กœ ์ ์šฉํ•ด ๋ณด์•˜๋Š”๋ฐ ๊ฒฐ๊ตญ ์•ˆ๋๋‹ค.

 

๋ชจ๋ฐ”์ผ ๋ธŒ๋ผ์šฐ์ €์—์„œ ํ•œ ๋ฒˆ๋„ ๊ฐ€๋กœ๋ชจ๋“œ๊ฐ€ ๊ฐ•์ œ๋กœ ์ ์šฉ๋˜๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ณธ ์ ์ด ์—†์–ด์„œ ์ด๊ฒŒ ๊ฐ€๋Šฅํ•œ ๊ธฐ๋Šฅ์ธ์ง€ ์˜์‹ฌ๊นŒ์ง€ ๋“ค์—ˆ๋‹ค..


ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

 

๋ชจ๋ฐ”์ผ ๋ธŒ๋ผ์šฐ์ €์—์„œ ํ™”๋ฉด์„ ๊ณ ์ •์‹œํ‚ค๋Š” ํ•ต์‹ฌ์€ ๋ฐ”๋กœ "ํ’€์Šคํฌ๋ฆฐ" ์ด์—ˆ๋‹ค.

 

ํ•ด๋‹น ๋˜๋Š” ๋ธŒ๋ผ์šฐ์ €๋ฅผ ํ’€์Šคํฌ๋ฆฐ์œผ๋กœ ๋งŒ๋“  ๋‹ค์Œ ์œ„ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ–ˆ๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด ํ•ด๋‹น ๋˜๋Š” ๋ธŒ๋ผ์šฐ์ € ๋งˆ๋‹ค ํ’€์Šคํฌ๋ฆฐ์„ ์š”์ฒญํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ๊ทธ ๋‹ค์Œ์œผ๋กœ screen.orientation.lock ์„ ์‹คํ–‰์‹œ์ผœ์•ผ lock์ด ์ž‘๋™๋˜์—ˆ๋‹ค.

 

 openFullscreen(type: OrientationLockType) {
    if (this.elem.documentElement.requestFullscreen) {
      this.elem.documentElement.requestFullscreen();
    } else if (this.elem.documentElement.mozRequestFullScreen) {
      /* Firefox */
      this.elem.documentElementmozRequestFullScreen();
    } else if (this.elem.documentElement.webkitRequestFullscreen) {
      /* Chrome, Safari and Opera */
      this.elem.webkitRequestFullscreen();
    } else if (this.elem.documentElement.msRequestFullscreen) {
      /* IE/Edge */
      this.elem.documentElement.msRequestFullscreen();
    }
    screen.orientation.lock(type);
  }

 

ํ’€์Šคํฌ๋ฆฐ์„ ๋„๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

 closeFullscreen() {
    if (this.document.exitFullscreen) {
      this.document.exitFullscreen();
    } else if (this.document.mozCancelFullScreen) {
      /* Firefox */
      this.document.mozCancelFullScreen();
    } else if (this.document.webkitExitFullscreen) {
      /* Chrome, Safari and Opera */
      this.document.webkitExitFullscreen();
    } else if (this.document.msExitFullscreen) {
      /* IE/Edge */
      this.document.msExitFullscreen();
    }
  }

 

์œ„์—์„œ ํ’€์Šคํฌ๋ฆฐ์„ ์ œ๊ณตํ•˜๋Š” ๋ธŒ๋ผ์šฐ์ € ์™ธ์—๋„ ํ™”๋ฉด์„ ๊ณ ์ •ํ•ด์•ผ ํ–ˆ๋Š”๋ฐ 

 

1. Safari

 

๋จผ์ € ์‚ฌํŒŒ๋ฆฌ๋Š” ์œ„์™€ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•ด๋„ ํ’€์Šคํฌ๋ฆฐ์ด ์ž‘๋™๋˜์ง€ ์•Š์•„ ํ™”๋ฉด ๊ณ ์ •์„ ํ•  ์ˆ˜๊ฐ€ ์—†์—ˆ๋‹ค.

 

๊ตฌ๊ธ€๋ง ํ•ด ์ฐพ์•„๋ณด๋‹ˆ ์‚ฌํŒŒ๋ฆฌ๋Š” ํ’€์Šคํฌ๋ฆฐ๋„ ์•ˆ๋˜๊ณ  screen.orientation.lock ๋ฉ”์„œ๋“œ๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ–ˆ๋‹ค.

 

https://stackoverflow.com/questions/52504841/how-do-i-lock-the-screen-orientation-to-portrait-in-safari-using-plain-javascrip

 

 

์šฐ์„  ์•„๋ž˜ ์ฝ”๋“œ๋กœ ์–ด๋–ค ๋ธŒ๋ผ์šฐ์ €์ธ์ง€ ์•Œ์•„๋‚ธ ๋‹ค์Œ ์‚ฌํŒŒ๋ฆฌ๋ผ๋ฉด ์ถ”๊ฐ€์ ์ธ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ์ค˜์•ผ ํ–ˆ๋‹ค.

 

๋ธŒ๋ผ์šฐ์ € ์•Œ์•„๋‚ด๊ธฐ

 

  detectBrowswerName():string {
    let userAgent = navigator.userAgent;
    let browserName;

    if (userAgent.match(/chrome|chromium|crios/i)) {
      browserName = 'chrome';
    } else if (userAgent.match(/firefox|fxios/i)) {
      browserName = 'firefox';
    } else if (userAgent.match(/safari/i)) {
      browserName = 'safari';
    } else if (userAgent.match(/opr\//i)) {
      browserName = 'opera';
    } else if (userAgent.match(/edg/i)) {
      browserName = 'edge';
    } else {
      browserName = 'No browser detection';
    }
    alert(`${browserName}`)
    return browserName
  }

 

ํ˜„์žฌ ๋””๋ฐ”์ด์Šค ๋ฐฉํ–ฅ ์•Œ์•„๋‚ด๊ธฐ

 

window์— addEventListner๋ฅผ ๊ฑธ์–ด ํ™”๋ฉด ๋ฐฉํ–ฅ์ด ๋ฐ”๋€Œ๋Š” ๊ฒƒ์„ ๊ฐ์ง€ํ•˜๊ณ 

 

  detectChangeOrientation() {
    window.addEventListener(
      'resize',() => this.readDeviceOrientation(),
      false
    );
  }

 

ํ•ด๋‹น ๋””๋ฐ”์ด์Šค ๋ฐฉํ–ฅ์„ ์ฝ์–ด๋‚ธ๋‹ค.

 

  readDeviceOrientation() {
    if (Math.abs(window.orientation) === 90 || Math.abs(window.orientation) === 270  ) {
      this.orientation = "Landscape"
    } else {
      this.orientation = "Portrait"
    }
  }

 

CSS

 

๊ทธ ๋‹ค์Œ CSS์— ํ™”๋ฉด์„ ํšŒ์ „ํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ์ƒํ™ฉ์— ๋งž๊ฒŒ ํ™”๋ฉด์„ ๋Œ๋ ค์ฃผ์—ˆ๋‹ค.

 

@media only screen and (orientation: portrait) {
  body.portrait {
    height: 100vw;
    transform: rotate(90deg);
  }

  body.landscape {
    height: 100vw;
    transform: rotate(0deg);
  }
}

 

html ์ „์ฒด๋ฅผ body๋กœ ๊ฐ์‹ผ ๋‹ค์Œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•ด ์ฃผ์—ˆ๋‹ค.

 

  @ViewChild('body') body: ElementRef | undefined;

  constructor(@Inject(DOCUMENT) private document: any) {}

if (matchMedia('only screen and (orientation: portrait)').matches) {
    if (type == 'landscape') {
      this.body?.nativeElement.classList.remove('landscape');
      this.body?.nativeElement.classList.add('portrait');
    } else {
      this.body?.nativeElement.classList.remove('portrait');
      this.body?.nativeElement.classList.add('landscape');
    }
  }

 

2. Zebra Enterprise

 

ํŠน์ • ๊ธฐ์—…์—์„œ๋Š” ์•Œ๋ ค์ง„ ๋ธŒ๋ผ์šฐ์ € ๋ง๊ณ ๋„ ์Šค์บ”ํ•˜๋Š” ์†๋„๋ฅผ ์œ„ํ•ด Zebra Enterprise ๋ธŒ๋ผ์šฐ์ €๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์—ˆ๋‹ค.

 

ํ•ด๋‹น ๋ธŒ๋ผ์šฐ์ €๋Š” ์ด๋ฏธ ํ™”๋ฉด์ด ๋ฝ์ด ๊ฑธ๋ฆฐ ์ƒํƒœ๋กœ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ œ๊ณต๋˜์–ด CSS๋ฅผ ์ด์šฉํ•ด ์ƒํ™ฉ์— ๋งž๊ฒŒ ํ™”๋ฉด์„ ํšŒ์ „ํ•ด ์ฃผ๋ฉด ๋๋‹ค.


Reference

 

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€