2016-06-19 1 views
0

저는 HTML5에서 캔버스를 사용하여 일렁 거리는 달 효과를 얻으려고합니다. 맥동 효과가 있지만 requestAnimation 함수가 정의한 순환 경로를 따라 프레임을 업데이트하지 않는 것처럼 보입니다. 여기에 자바 스크립트가 있습니다.원형 경로 애니메이션이 펄싱 동작과 함께 작동하지 않습니다.

window.requestAnimFrame = function() { 
return window.requestAnimationFrame || window.webkitRequestAnimationFrame 
|| 
window.mozRequestAnimationFrame || window.oRequestAnimationFrame 
|| window.msRequestAnimationFrame || 
    function(a) { 
    window.setTimeout(a, 1E3/60) 
    } 
}(); 


var canvas = document.getElementById('canvas');        
var context = canvas.getContext('2d'); 

function Ball(radius, color) { 
    if (radius === undefined) { 
    radius = 40; 
    } 
    if (color === undefined) { 
    color = "#ff0000"; 
    } 

    this.x = 0; 
    this.y = 0; 
    this.radius = radius; 
    this.rotation = 0; 
    this.scaleX = 1; 
    this.scaleY = 1; 
    this.lineWidth = 1; 
    } 

    Ball.prototype.draw = function(context) { 
    context.save(); 
    context.translate(this.x, this.y); 
    context.rotate(this.rotation); 
    context.scale(this.scaleX, this.scaleY); 
    context.lineWidth = this.lineWidth; 
    context.fillStyle = "#e50000"; 
    context.beginPath(); 
    //x, y, radius, start_angle, end_angle, anti-clockwise 
    context.arc(0, 0, this.radius, 0, (Math.PI * 2), true); 
    context.closePath(); 
    context.fill(); 
if (this.lineWidth > 0) { 
    context.stroke(); 
    } 
    context.restore(); 
    }; 

    window.onload = function() {     
    var canvas = document.getElementById('canvas'), 
        context = canvas.getContext('2d'), 
        ball = new Ball(), 
        angle = 0, 
        centerScale = 1, 
        range = 0.5, 
        speed = 0.02, 
  
pathX = canvas.width/2, 
pathY = canvas.height/2, 
pathRadius = 150, 
pathAngle = 0; 
ball.x = Math.cos(pathAngle) * pathRadius + pathX; 
ball.y = Math.sin(pathAngle) * pathRadius + pathY;   

    (function drawFrame() {       
    window.requestAnimationFrame(drawFrame, canvas);       
    context.clearRect(0, 0, canvas.width, canvas.height);       
    ball.scaleX = ball.scaleY = centerScale + Math.sin(angle) * range;   
    angle += speed;     
    pathAngle += speed; 
    ball.draw(context);     
    }());   
}; 
+0

가 CodePen에 대한 링크입니다 : http://codepen.io/WriterState/pen/EyKPpq?editors = 0110 – WriterState

답변

0

원 중심을 중심으로 회전하기 때문에 중심점을 기준으로 궤도를 그리지 않습니다.

// offset the circles rotation by 100px off its centerpoint 
context.arc(100, 0, this.radius, 0, (Math.PI * 2), true); 

예제 코드 광고 데모 :

여기

window.requestAnimFrame = function() { 
 
return window.requestAnimationFrame || window.webkitRequestAnimationFrame 
 
|| 
 
window.mozRequestAnimationFrame || window.oRequestAnimationFrame 
 
|| window.msRequestAnimationFrame || 
 
    function(a) { 
 
    window.setTimeout(a, 1E3/60) 
 
    } 
 
}(); 
 

 

 
var canvas = document.getElementById('canvas');  
 
var context = canvas.getContext('2d'); 
 

 
function Ball(radius, color) { 
 
    if (radius === undefined) { 
 
    radius = 40; 
 
    } 
 
    if (color === undefined) { 
 
    color = "#ff0000"; 
 
    } 
 

 
    this.x = 0; 
 
    this.y = 0; 
 
    this.radius = radius; 
 
    this.rotation = 0; 
 
    this.scaleX = 1; 
 
    this.scaleY = 1; 
 
    this.lineWidth = 1; 
 
    } 
 

 
    Ball.prototype.draw = function(context) { 
 
    context.save(); 
 
    context.translate(this.x, this.y); 
 
    context.rotate(this.rotation); 
 
    context.scale(this.scaleX, this.scaleY); 
 
    context.lineWidth = this.lineWidth; 
 
    context.fillStyle = "#e50000"; 
 
    context.beginPath(); 
 
    //x, y, radius, start_angle, end_angle, anti-clockwise 
 
    context.arc(50, 0, this.radius, 0, (Math.PI * 2), true); 
 
    context.closePath(); 
 
    context.fill(); 
 
if (this.lineWidth > 0) { 
 
    context.stroke(); 
 
    } 
 
    context.restore(); 
 
    }; 
 

 
    window.onload = function() {  
 
    var canvas = document.getElementById('canvas'), 
 
     context = canvas.getContext('2d'), 
 
     ball = new Ball(), 
 
     angle = 0, 
 
     centerScale = 1, 
 
     range = 0.5, 
 
     speed = 0.02, 
 
    
 
     pathX = canvas.width/2, 
 
     pathY = canvas.height/2, 
 
     pathRadius = 50, 
 
     pathAngle = 0; 
 
     ball.x = canvas.width/2; // Math.cos(pathAngle) * pathRadius + pathX; 
 
     ball.y = canvas.height/2; //Math.sin(pathAngle) * pathRadius + pathY; 
 

 
    (function drawFrame() {  
 
    window.requestAnimationFrame(drawFrame, canvas);  
 
    context.clearRect(0, 0, canvas.width, canvas.height);  
 
    ball.scaleX = ball.scaleY = centerScale + Math.sin(angle) * range; 
 
    angle += speed;  
 
    pathAngle += speed; 
 
    ball.rotation+=Math.PI/180; 
 
    ball.draw(context);  
 
    }()); 
 
};
body{ background-color:white; } 
 
#canvas{border:1px solid red; margin:0 auto; }
<canvas id="canvas" width=300 height=300></canvas>

+0

이해할 수 있는지 잘 모르겠습니다. 궤도 경로에 의해 정의된다 pathX = canvas.width/2 = pathY canvas.height/2 pathRadius = 150 pathAngle = 0; – WriterState

+0

원의 중심점으로 번역 중입니다. 그런 다음 그 지점을 중심으로 캔버스를 회전하고 있습니다. 그러나 원점을 중심점에서 바깥쪽으로 이동하지 않습니다 (pathRadius가 적용되지 않음). 그래서 원이 실제로 주변의 레코드처럼 회전합니다. 또한 각 프레임마다 회전을 증가시켜야합니다. 내 대답에 예제 코드와 데모를 추가했습니다. 건배! – markE

+1

친구 감사합니다! – WriterState

관련 문제