나는 이것을 알아 내려고 벽에 부딪쳤다. 나는 OO Javascript에 익숙하지 않고 첫 번째 클래스/객체를 조합하려고합니다. 캔버스 로더를 만들려고하는데 작동하지 않습니다. 내 클럭 클래스에서 내 animate 함수 내부 requestAnimationWindow 부분에 오류를 좁혔습니다. 나는 객체 [객체 전역]에 '애니메이션'오류가 없다는 것을 알았습니다. 여기 내 코드가있다.자바 스크립트 객체 내부에서 requestAnimationFrame 호출하기
HTML :
<div id="loader"><canvas id="showLoader" width="250" height="250"></canvas><div id="showTimer"><p id="elapsedTime">
<script>
var clockTest = new clock(document.getElementById("showLoader"), 0, 100);
clockTest.animate();
</script>
</p></div></div>
자바 스크립트 :
function clock(canvas, curPerc, endPrecent){
var showPerc = document.getElementById("elapsedTime");
this.canvas = document.getElementById("showLoader");
var context = this.canvas.getContext('2d');
var x = this.canvas.width/2;
var y = this.canvas.height/2;
var radius = 75;
this.curPerc = 0;
this.endPercent = 110;
var counterClockwise = false;
var circ = Math.PI * 2;
var quart = Math.PI/2;
context.lineWidth = 10;
context.strokeStyle = '#ed3f36';
this.animate = function(current) {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
this.context.beginPath();
this.context.arc(x, y, radius, -(quart), ((circ) * current) - quart, false);
this.context.stroke();
this.curPerc++;
if(this.curPerc < this.endPercent) {
requestAnimationFrame(function() {
this.animate(curPerc/100);
showPerc.innerHTML = this.curPerc + '%';
});
}
};
}
모든 팁은 감사합니다. 감사!
위의 코드 예제는'this.animate' 위의'this.context = context'가 없습니다. 여기에 사람들을 보여줄 수있는 바이올린이 있습니다 : http://jsfiddle.net/v45K8/ – Jorg