requestAnimationFrame ï¼ ãã¼ããã¾ããã¼ãã¢ãã¡ã¼ã·ã§ã³ã«setInterval使ããªãã¼
ããé©åãªãã®ã追å ãããã®ã§ãã£ã¡ã使ãã¾ãããã
setInterval/setTimeout â requestAnimationFrame;
ããã¾ã§ã¯ setInterval
function render(){ // ã¢ãã¡ã¼ã·ã§ã³ 1ãã¬ã¼ã åã®å¦ç } setInterval(render, 1000 / 60);
ããã¯ããå¤ãã
ãããã㯠requestAnimationFrame
function animationLoop(){ render(); requestAnimationFrame(animationLoop); } animationLoop();
ãããã¹ãã
requestAnimationFrameã«ããã¨ä½ãããã®ï¼
- è¤æ°ã®ã¢ãã¡ã¼ã·ã§ã³æåå¦çãä¸ã¤ã«ã¾ã¨ãã¦æé©åãã¦ããã
- ä¸è¦ãªå ´åã«ã¢ãã¡ã¼ã·ã§ã³ã«ã¼ããè¡ããªã
- ãã©ã¦ã¶ã®ã¿ããè£ã«åã£ã¦é表示ã«ãªã£ã¦ãã¨ãã¨ãã¯å¦çããªã
- CPU,GPUã«ãããã
- ããããªã¼ãã¡ãè¯ããªã£ã¦ãã¼ãPCã®äººã¨ããããã
- ãã©ã¦ã¶ã®ã¿ããè£ã«åã£ã¦é表示ã«ãªã£ã¦ãã¨ãã¨ãã¯å¦çããªã
- 軽ã
- ç¹ã«webkitç³»ã®ãã©ã¦ã¶ã§å¹æãé«ãããã
requestAnimationFrameãªãã¦ãªãã¸ã§ã¯ããã¼ãï¼ã ã¾ãããï¼
ã¾ã ãã³ããã¬ãã£ãã¯ã¹ãä»ãã¦ãããã¾ãã
// åãã©ã¦ã¶å¯¾å¿ window.requestAnimationFrame = (function(){ return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback, element){ window.setTimeout(callback, 1000 / 60); }; })();
ãµã³ãã«
requestAnimationFrame - jsdo.it - share JavaScript, HTML5 and CSS
åèï¼http://paulirish.com/2011/requestanimationframe-for-smart-animating/
è¿½è¨ (2011/05/27)
使ãã¾ãããã¨è¨ã£ã¨ãã¦ããã§ãã
requestAnimationFrameã¯ãNon-standardã§ãããå®é¨çãªæ©è½ã§ãããä»æ§ã¯æµåçã§ãã
ãã¨ãã¨ã®@paul_irishã®ã³ã¼ãã§ã¯plyfill*1ã«å¥ã®ååã使ããã¦ãã¾ãã
I am using 'requestAnimFrame` here because since the spec is still in flux, I don't want to make a straight polyfill, yet.
'requestAnimFrame'ã¨ããååã使ã£ã¦ããã©ãä»æ§ãä¾ç¶æµåçã ãããã¾ã ã¹ãã¬ã¼ããªpolyfillã¯ä½ããããªããã ã
å®éãç¹å¾´ã¨ãã¦ãããç¹ã®ä¸é¨ãã¨ã£ã¦ãåãã©ã¦ã¶ã®å®è£ ã¯ã°ãã°ãã§ã
- ã¿ããé表示ã«ãªã£ããæåãæ¢ã¾ã
- Chrome12 æ¢ã¾ãã
ã¨ãããsetTimeoutã§ãæ¢ã¾ãâ ã³ã¡ã³ãã§ææãã¦ãããã¾ãããsetTimeoutãã¿ããããã¦ã«ããã¨ãã¯5msãããã§ãè£ã«ããã¨ãã¯ããªãé·ã(ãã©ã¦ã¶ã«ãã£ã¦ã¯1s)ã«ãªã£ãã¯ããã確ãã«æ¢ã¾ã£ã¦ã¯ããªãã¦ãã¼ã£ããé²ãã§ã¾ããï¼ï¼ - Firefox4 æ¢ã¾ã
- Safari5 æ¢ã¾ããªã
- Chrome12 æ¢ã¾ãã
- 60fpsã«è¿ãåæçãªæå
- Chrome12 â
- Firefox4 Ã
- Safari5 Ã
ãµã³ãã« â http://jsfiddle.net/calpo/H7EEE/
ããã§ã使ããã¨ãããã使ã£ã¦ããã¾ããã
ç¹ã«ã²ã¼ã ç³»ã®ããã°ã©ã ã«ã¯éè¦ãªãã®ã ã¨æãã®ã§ãã£ã±ã使ã£ã¦ãã£ã¼ãããã¯ãããã¨ããã
ã²ã¼ã ã¨requestAnimationFrameã«ã¤ãã¦ã¯ãã¡ãã®è¨äºãé¢ç½ãã£ãã§ãã
JavaScript ã§ã²ã¼ã ã«ã¼ããä½ã (The Game Loop) - t100ã®ããã°ã©ãã³ã°è±åºä½æ¦