¿©·¯°³ÀÇ cooldwon ¿ÀºêÁ§Æ®¸¦ ¸¸µé±â À§ÇØ ¿ÀºêÁ§Æ®¸¦ new·Î »ý¼ºÇÑ´Ù. Ưº°È÷ ÁÖÀÇÇÒ Á¡À̶ó¸é requestAnimationFrame ÇÔ¼ö¸¦ ½ÇÇàÇÒ¶§ this¸¦ Àü´ÞÇØ¾ß ÇÑ´Ù. requestAnimationFrame(onFrameCooldown) À̳ª requestAnimationFrame(this.onFrameCooldown) µÑ´Ù Á¤»óÀûÀ¸·Î ½ÇÇàÀÌ µÇÁö ¾ÊÀ» °ÍÀÌ´Ù. this¸¦ Àü´ÞÇÏ´Â ¹æ¹ýÀº ´ÙÀ½°ú °°Àº ¹æ¹ýÀÌ ÀÖ´Ù. requestAnimationFrame¿¡¼ this Àü´Þ Çϱâ1. bind(this) »ç¿ë1requestAnimationFrame(this.onFrameCooldown.bind(this));
2. bind(this) »ç¿ë2 this.onFrameCooldown = function() {
requestAnimationFrame(this.onFrameCooldown); }.bind(this); 3. »õ·Î¿î ÇÔ¼ö »ç¿ë this.onFrameCooldown = function() {
var self = this; if (!timer) return; var now = getTime(); this.updateCooldown((now - lastTime)/1000.0); lastTime = now; requestAnimationFrame(function() { self.onFrameCooldown(); }); } 4. ES6 »ç¿ë requestAnimationFrame(() => { this.onFrameCooldown() } );
5. »ý¼ºÀÚ¿¡¼ ¹ÙÀεù ÇÔ¼ö »ç¿ë var Game = function () {
this.counter = 0; this.loopBound = this.loop.bind(this); this.loopBound(); } Game.prototype.loop = function () { console.log(this.counter++); requestAnimationFrame(this.loopBound); } var gameOne = new Game(); loopBound À̸§ ´ë½Å loop À̸§À» »ç¿ëÇصµ ¹®Á¦°¡ ¾ø´Ù. ÀÌ ¹æ¹ýÀº ²À ÇÁ·ÎÅäŸÀÔÀ¸·Î ÇØ¾ß Çϴ°ÍÀÎÁö ??? ³ªµµ È®½ÇÇÏ°Ô ¸ð¸£°Ú´Ù. ³»ºÎ¿¡¼ ÇÔ¼ö Á¤ÀǸ¦ ÇÏ¸é ¹®¹ý ¿À·ù°¡ ¶á´Ù. 6. Ŭ·¡½º »ý¼ºÀÚ¿¡¼ ¹ÙÀεù ÇÔ¼ö »ç¿ë class Game {
constructor() { this.counter = 0; this.loop = this.loop.bind(this); } loop() { console.log(this.counter++); requestAnimationFrame(this.loop); } } Å×½ºÆ® : test04.html setTimeoutÀ» »ç¿ëÇÏ´Â ¹æ¹ýÀº ¹ö±×°¡ ÀÖ´Ù. setTimeoutµµ this¸¦ ¹ÙÀεù ÇØÁÖ¾î¾ß ÇÑ´Ù. ÀÌ ¹ö±×´Â ³ªÁß¿¡ ¼öÁ¤ÇÒ°ÍÀÌ´Ù. Àüü Cooldown ¼Ò½º´Â ´ÙÀ½°ú °°´Ù. function Cooldown(elementName) {
var canvas = document.getElementById(elementName); var timer; var timerStart; var lastTime; var nowTime; var accumulatedTime = 0; var remainedTime = 0; const frameTime = 0.03333; var radius = 100; this.render = function(degree) { var digits = 6; var radian = degree * Math.pi/180; var ctx = canvas.getContext('2d'); ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.lineWidth = 3; ctx.fillStyle = 'rgba(200,100,100,0.6)'; ctx.strokeStyle = 'rgba(200,100,100,0.3)'; ctx.translate(canvas.width/2, canvas.height/2); ctx.rotate(-Math.PI/2); ctx.beginPath(); ctx.moveTo(0, 0); ctx.lineTo( radius * Math.cos(0).toFixed(digits), radius * Math.sin(0).toFixed(digits)); ctx.moveTo(0, 0); ctx.lineTo( radius * Math.cos(radian).toFixed(digits), radius * Math.sin(radian).toFixed(digits)); ctx.arc(0, 0, radius, degree * Math.PI/180, Math.PI*2, false); ctx.stroke(); ctx.fill(); ctx.closePath(); } var clearCanvas = function() { var ctx = canvas.getContext('2d'); ctx.setTransform(1, 0, 0, 1, 0, 0); ctx.clearRect(0, 0, canvas.width, canvas.height); }; var endCooldown = function() { clearCanvas(); timer = null; } var getTime = function() { return new Date().getTime(); } this.onFrameCooldown = function() { var self = this; if (!timer) return; var now = getTime(); this.updateCooldown((now - lastTime)/1000.0); lastTime = now; requestAnimationFrame(function() { self.onFrameCooldown(); }); } this.updateCooldown = function(deltaTime) { remainedTime = remainedTime + deltaTime; accumulatedTime += deltaTime; if (remainedTime > frameTime) { remainedTime -= frameTime; var degree = accumulatedTime*360; this.render(degree); } } this.initCooldown = function() { if (!timer) { radius = Math.sqrt(Math.pow(canvas.clientWidth, 2) + Math.pow(canvas.clientHeight, 2)); timer = setTimeout(endCooldown, 3000); lastTime = getTime(); timerStart = getTime(); this.render(0); this.onFrameCooldown(); } } this.play = function() { if (!timer) this.initCooldown(); } }; var cooldown = new Cooldown('myCanvas'); //cooldown.render(90); cooldown.play(); var cooldown1 = new Cooldown('myCanvas1'); setTimeout(function(){ cooldown1.play(); } , 300); var cooldown2 = new Cooldown('myCanvas2'); setTimeout(function() { cooldown2.play(); } , 900); Âü°í) this ¹ÙÀεù¾øÀÌ requestAnimationFrame »ç¿ëÇϱâ http://www.html5gamedevs.com/topic/20251-using-a-non-global-function-as-requestanimationframe-callback/ »ý¼ºÀÚ¿¡¼ ¹ÙÀεù ÇÔ¼ö »ç¿ëÇϱâ https://stackoverflow.com/questions/6065169/requestanimationframe-with-this-keyword |