๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’๊ณต๋ถ€/JS

[JavaScript] window.requestAnimationFrame()

by rindev 2021. 10. 7.

 

์„ธ์ƒ์€ ๋„“๊ณ  ์•„์ง ๊ฐˆ๊ธธ์ด ๋ฉ€~~~~~~๋‹ค๋Š” ๊ฒƒ์„^^,, #ํ•˜์ง€๋งŒ #๊ฐ€๋ณด์ž๊ณ 

 

๐Ÿค” requestAnimationFrame?

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ webAPI์— ์žˆ๋Š” ๋น„๋™๊ธฐ ํ•จ์ˆ˜

๋ธŒ๋ผ์šฐ์ €์—๊ฒŒ ์ˆ˜ํ–‰ํ•˜๊ธฐ๋ฅผ ์›ํ•˜๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์•Œ๋ฆฌ๊ณ , ๋‹ค์Œ ๋ฆฌํŽ˜์ธํŠธ๊ฐ€ ์ง„ํ–‰๋˜๊ธฐ ์ „ ํ•ด๋‹น ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์—…๋ฐ์ดํŠธํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ํ•จ. ๋ฆฌํŽ˜์ธํŠธ ์ด์ „์— ์‹คํ–‰ํ•  ์ฝœ๋ฐฑ์„ ์ธ์ž๋กœ ๋ฐ›์Œ. ๋ฆฌํ„ด๊ฐ’์€ ์ฝœ๋ฐฑ ๋ฆฌ์ŠคํŠธ์˜ ํ•ญ๋ชฉ์„ ์ •์˜ํ•˜๋Š” ๊ณ ์œ ํ•œ ์š”์ฒญ id์ธ long ์ •์ˆ˜ ๊ฐ’. (MDN ์ฐธ๊ณ )

 

์ด๊ฑธ ์™œ ์“ฐ๋Š”๊ฑฐ์ง€?

  • ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์œ„ํ•ด ๋งŒ๋“ค์–ด์ง„ ""๋น„๋™๊ธฐ""ํ•จ์ˆ˜
  • ์„ฑ๋Šฅ ์ตœ์ ํ™” (๋ฐฑ๊ทธ๋ผ์šด๋“œ ๋™์ž‘ ๋ฐ ๋น„ํ™œ์„ฑํ™” ์‹œ ์ค‘์ง€)
  • ์ตœ๋Œ€ 1ms๋กœ ์ œํ•œ๋˜๋ฉฐ, 1์ดˆ์— 60๋ฒˆ ๋™์ž‘
  • ๋‹ค์ˆ˜์˜ ์• ๋‹ˆ๋ฉ”์ด์…˜๋„ ๊ฐ๊ฐ ํƒ€์ด๋จธ๊ฐ’์„ ์ƒ์„ฑ ๋ฐ ์ฐธ์กฐํ•˜์ง€ ์•Š๊ณ , ๋‚ด๋ถ€์˜ ๋™์ผํ•œ ํƒ€์ด๋จธ๋ฅผ ์ฐธ์กฐํ•จ

 

requesetAnimationFrame์„ ์‚ฌ์šฉํ•˜๋ฉด ์ด๋Ÿฐ ๋Š๋‚Œ์ด๋‹ค!  (์šฉ๋Ÿ‰์„ ๋‚ฎ์ถ”๋ ค ํ–ˆ๋”๋‹ˆ ํ™”์งˆ์ด ์ข€ ์•ˆ์ข‹์€ ๊ฒƒ ๊ฐ™์ง€๋งŒ ์• ์จ ๋ฌด์‹œํ•ด๋ณธ๋‹ค...)

 

const startBtn = document.getElementById("startBtn");
const stopBtn = document.getElementById("stopBtn");
const count = document.getElementById("count");

let cnt = 0;
let isCounting;

function countUp() {
  count.innerText = cnt;
  cnt++;

  isCounting = requestAnimationFrame(countUp);
}


function handleStart() {
  countUp();
}

function handleStop() {
  cancelAnimationFrame(isCounting);
}

startBtn.addEventListener("click", handleStart);
stopBtn.addEventListener("click", handleStop);

๋ฐ˜๋ณต ํ˜ธ์ถœํ•  ํ•จ์ˆ˜ countUp์„ requestAnimationFrame( ์—ฌ๊ธฐ!! )์˜ ์ธ์ž๋กœ ๋„˜๊ฒจ์ฃผ์–ด ์žฌ๊ท€์ ์œผ๋กœ ์‹คํ–‰ํ•œ๋‹ค.

ํŠน์ •ํ•œ ๋ถ„๊ธฐ๋ฌธ์ด ์—†๋‹ค๋ฉด ์•„๋งˆ ํ‰์ƒ ๋™์ž‘ํ•  ๊ฒƒ์ด๋‹ค.

๋ฉˆ์ถ”๋Š” ๋ฐฉ๋ฒ•์€ requestAnimationFrame์„ ๋ณ€์ˆ˜๋กœ ์ €์žฅํ•˜์—ฌ cancelAnimationFrame์— ๋„˜๊ฒจ์ค€๋‹ค.

 

 

๐Ÿค” setInterval๊ณผ ๋น„์Šทํ•˜์ง€ ์•Š๋‚˜?

๋ฉ‹์ง€๊ฒŒ ์ •๋ฆฌ๋œ ๋ธ”๋กœ๊ทธ๋ฅผ ๋ฐœ๊ฒฌํ•ด์„œ ์ฐธ๊ณ ํ•˜์—ฌ ๊ธ€์„ ์ž‘์„ฑํ•œ๋‹ค.

 

  • 1์ดˆ์— 60ํ”„๋ ˆ์ž„ === 1ํ”„๋ ˆ์ž„์— 16.6ms ์ •๋„ ์†Œ์š”
  • ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ฝ”๋“œ๊ฐ€ ๋ณต์žกํ•˜๊ณ  ์˜ค๋ž˜๊ฑธ๋ ค์„œ ์‹คํ–‰ํ•˜๋Š”๋ฐ์— 16.6ms๋ณด๋‹ค ๋” ๊ฑธ๋ฆฌ๋ฉด?
    → ์ •ํ•ด์ง„ ์‹œ๊ฐ„ ๋‚ด์— ํ”„๋ ˆ์ž„์„ ์ฐ์–ด๋‚ด์ง€ ๋ชปํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ๋š๋š ๋Š๊ธฐ๋“ฏ์ด ๋ณด์ด๊ฒŒ ๋œ๋‹ค.

์ด๋ฏธ์ง€ ์ถœ์ฒ˜ : https://simsimjae.tistory.com/402

์œ„ ์ด๋ฏธ์ง€๋ฅผ ๋ณด๋ฉด ํ•œ ํ”„๋ ˆ์ž„๋‹น 16ms๊ฐ€ ์ฃผ์–ด์ง€๋Š”๋ฐ, setTimeout์ด ์•ฝ 8ms์ฏค์— ์‹œ์ž‘์ด ๋๋‹ค.

์ €๋ ‡๊ฒŒ ์‹œ๊ฐ„์„ ์ดˆ๊ณผํ•˜๊ฒŒ ๋˜๋ฉด ํ”„๋ ˆ์ž„์ด ์ƒ์„ฑ๋˜์ง€ ๋ชปํ•˜๊ณ  ๋ˆ„๋ฝ๋œ ์ฑ„ ๋„˜์–ด๊ฐ€๊ฒŒ ๋œ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ์‹ฑ๊ธ€์Šค๋ ˆ๋“œ์ด๊ธฐ ๋•Œ๋ฌธ์—, ๋ฌด๊ฑฐ์šด ์ž‘์—…์„ ๊ณ„์† ๋Œ๋ฆฌ๊ฒŒ ๋˜๋ฉด ํ”„๋ ˆ์ž„์ด ๋ˆ„๋ฝ๋  ํ™•๋ฅ ์ด ๋” ๋†’์•„์ง„๋‹ค. (setTimeout, setInterval์€ ํ”„๋ ˆ์ž„์„ ์‹ ๊ฒฝ์“ฐ์ง€ ์•Š๋Š”๋‹ค. ๋‚˜๋Š” ๋‚˜์˜ ๊ธธ์„ ๊ฐ„๋‹ค)

๊ทธ๋ฆฌ๊ณ  CPU๋‚˜ ๊ทธ๋ž˜ํ”ฝ์นด๋“œ๊ฐ€ ๋Š๋ฆฌ๋ฉด ์ธํ„ฐ๋ฒŒ์ด ๋” ๋Š๋ ค์งˆ ์ˆ˜ ๋„ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค!

 

์œ„์—์„œ๋„ ๋‚˜์˜จ ์–˜๊ธฐ์ด์ง€๋งŒ, setTimeout์ด๋‚˜ setInterval์€ ๋ณด์ด์ง€ ์•Š๋Š” ๊ณณ์—์„œ๋„ ์ˆ˜ํ–‰ ๋˜์ง€๋งŒ,

RAF๋Š” ํ˜„์žฌ ์ฐฝ์— ํ‘œ์‹œ๋˜์ง€ ์•Š์œผ๋ฉด ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์ค‘์ง€ํ•ด์„œ ์„ฑ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚จ๋‹ค. (์ตœ์ ํ™”)

 

 

 

 

๐Ÿ‘€์ฐธ๊ณ  (๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค)

๋”๋ณด๊ธฐ

https://simsimjae.tistory.com/402

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์• ๋‹ˆ๋ฉ”์ด์…˜ - requestAnimationFrame ํ™œ์šฉํ•˜๊ธฐ

์‚ฌ๋žŒ์€ 1์ดˆ์— 60๊ฐœ์˜ ํ”„๋ ˆ์ž„์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค. ๊ทธ ์ด์ƒ์˜ ํ”„๋ ˆ์ž„์„ ๋” ์ฐ์–ด๋‚ด๋”๋ผ๋„ ์‚ฌ๋žŒ์ด ๋Š๋ผ๊ธฐ์—” ๊ฑฐ์˜ ์ฐจ์ด๊ฐ€ ์—†๋‹ค๋Š” ๋ง์ด๋‹ค. ๊ทธ๋ž˜์„œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋กœ ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ๊ตฌํ˜„ํ• ๋•Œ๋„ 1์ดˆ์— 60

simsimjae.tistory.com

 

https://corock.tistory.com/466

 

[33-js-concepts] 10. setTimeout, setInterval and requestAnimationFrame

- setTimeout : ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๊ฐœ๋…์—์„œ ์˜จ ๊ฒƒ์ด ์•„๋‹ˆ๊ณ , ๋ธŒ๋ผ์šฐ์ € ๊ทธ๋ฆฌ๊ณ  nodeJS ๊ฐœ๋…์—์„œ ์™”๋‹ค. => ์ผ์ • ์‹œ๊ฐ„์ด ์ง€๋‚œ ํ›„์— ๊ธฐ๋Šฅ์„ ๋ฉ”์‹œ์ง€ํ์— ๋„ฃ์–ด์ฃผ๊ณ  ์Šคํƒ์ด ๋น„๊ฒŒ๋˜๋ฉด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๊ฐ€ ์‹คํ–‰ํ•œ๋‹ค. ์ผ

corock.tistory.com

 

https://blog.eunsatio.io/develop/JavaScript-window.requestAnimationFrame-%ED%8A%9C%ED%86%A0%EB%A6%AC%EC%96%BC

 

Slacking studio x BLOG

Slacking studio blog.

blog.eunsatio.io

 

https://webisfree.com/2020-03-19/[%EC%9E%90%EB%B0%94%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8]-requestanimationframe()%EC%9D%84-%EC%82%AC%EC%9A%A9%ED%95%98%EB%8A%94-%EB%B0%A9%EB%B2%95-%EB%B0%8F-%EC%98%88%EC%A0%9C 

 

[์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] requestAnimationFrame()์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ๋ฐ ์˜ˆ์ œ

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋‚ด์žฅํ•จ์ˆ˜์ธ requestAnimationFrame()์— ๋Œ€ํ•˜์—ฌ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. ์• ๋‹ˆ๋ฉ”์ด์…˜ ๊ตฌํ˜„์‹œ ๊ผญ ํ•„์š”ํ•œ ๋ฐฉ๋ฒ• ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค.

webisfree.com

 

https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/dev-guides/hh920765(v=vs.85)?redirectedfrom=MSDN 

 

Timing control for script-based animations ('requestAnimationFrame') (Windows)

Timing control for script-based animations ("requestAnimationFrame") 11/03/2017 5 minutes to read In this article --> Internet Explorer 10 and Windows apps using JavaScript introduce support for the requestAnimationFrame method, which provides a smoother

docs.microsoft.com

 

https://blog.naver.com/PostView.naver?blogId=tk2rush90&logNo=221076910932&parentCategoryNo=&categoryNo=&viewDate=&isShowPopularPosts=false&from=postView 

 

[์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ] requestAnimationFrame ์•Œ์•„๋ณด๊ธฐ

0. ์„œ๋ฌธ๊ฐœ์ธ์ ์œผ๋กœ, ์ •๋ง ์ง€๊ทนํžˆ ๊ฐœ์ธ์ ์œผ๋กœ ์• ๋‹ˆ๋ฉ”์ด์…˜ ๊ตฌํ˜„์€ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๊ฝƒ์ด๋ผ๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ์ด๋Ÿฌํ•œ...

blog.naver.com

 

๋Œ“๊ธ€